Skip to content

Commit

Permalink
Extract browser and platform from user agent (#36568)
Browse files Browse the repository at this point in the history
* Add get_browser capability

* changelog

* Remove union types for backwards compatibility

* Add get desktop platform method

* Update Add get_browser capability for device stats

* Browser and platform details to methods

---------

Co-authored-by: Michalis Despotopoulos <[email protected]>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8537589675
  • Loading branch information
lovemeblender authored and matticbot committed Apr 3, 2024
1 parent a91601f commit 6be89be
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "wordpress-plugin",
"license": "GPL-2.0-or-later",
"require": {
"automattic/jetpack-device-detection": "^2.1.2"
"automattic/jetpack-device-detection": "^2.1.3-alpha"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.1.0",
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/automattic/jetpack-device-detection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.1.3-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

### Added
- Added functionality for extracting the browser and desktop platform from a user agent

## [2.1.2] - 2024-03-18
### Changed
- Internal updates.
Expand Down Expand Up @@ -189,6 +196,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Moving jetpack_is_mobile into a package

[2.1.3-alpha]: https://github.com/Automattic/jetpack-device-detection/compare/v2.1.2...v2.1.3-alpha
[2.1.2]: https://github.com/Automattic/jetpack-device-detection/compare/v2.1.1...v2.1.2
[2.1.1]: https://github.com/Automattic/jetpack-device-detection/compare/v2.1.0...v2.1.1
[2.1.0]: https://github.com/Automattic/jetpack-device-detection/compare/v2.0.1...v2.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public static function get_info( $ua = '' ) {
'is_smartphone' => self::is_mobile( 'smart', false, $ua_info ),
'is_tablet' => $ua_info->is_tablet(),
'platform' => $ua_info->get_platform(),
'desktop_platform' => $ua_info->get_desktop_platform(),
'browser' => $ua_info->get_browser(),
);

$info['is_handheld'] = $info['is_phone'] || $info['is_tablet'];
Expand Down
188 changes: 175 additions & 13 deletions vendor/automattic/jetpack-device-detection/src/class-user-agent-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,30 @@ class User_Agent_Info {
*
* @var null|string
*/
private $platform = null;
const PLATFORM_WINDOWS = 'windows';
const PLATFORM_IPHONE = 'iphone';
const PLATFORM_IPOD = 'ipod';
const PLATFORM_IPAD = 'ipad';
const PLATFORM_BLACKBERRY = 'blackberry';
const PLATFORM_BLACKBERRY_10 = 'blackberry_10';
const PLATFORM_SYMBIAN = 'symbian_series60';
const PLATFORM_SYMBIAN_S40 = 'symbian_series40';
const PLATFORM_J2ME_MIDP = 'j2me_midp';
const PLATFORM_ANDROID = 'android';
const PLATFORM_ANDROID_TABLET = 'android_tablet';
const PLATFORM_FIREFOX_OS = 'firefoxOS';
private $platform = null;
const PLATFORM_WINDOWS = 'windows';
const PLATFORM_IPHONE = 'iphone';
const PLATFORM_IPOD = 'ipod';
const PLATFORM_IPAD = 'ipad';
const PLATFORM_BLACKBERRY = 'blackberry';
const PLATFORM_BLACKBERRY_10 = 'blackberry_10';
const PLATFORM_SYMBIAN = 'symbian_series60';
const PLATFORM_SYMBIAN_S40 = 'symbian_series40';
const PLATFORM_J2ME_MIDP = 'j2me_midp';
const PLATFORM_ANDROID = 'android';
const PLATFORM_ANDROID_TABLET = 'android_tablet';
const PLATFORM_FIREFOX_OS = 'firefoxOS';
const PLATFORM_DESKTOP_LINUX = 'linux';
const PLATFORM_DESKTOP_MAC = 'mac';
const PLATFORM_DESKTOP_WINDOWS = 'windows';
const PLATFORM_DESKTOP_CHROME = 'chrome';
const BROWSER_CHROME = 'chrome';
const BROWSER_FIREFOX = 'firefox';
const BROWSER_SAFARI = 'safari';
const BROWSER_EDGE = 'edge';
const BROWSER_OPERA = 'opera';
const BROWSER_IE = 'ie';
const OTHER = 'other';

/**
* A list of dumb-phone user agent parts.
Expand Down Expand Up @@ -277,6 +288,57 @@ public function get_platform() {
return $this->platform;
}

/**
* Returns the platform for desktops
*
* @return string
*/
public function get_desktop_platform() {
$ua = $this->useragent;
if ( empty( $ua ) ) {
return false;
}
$platform = self::OTHER;

if ( static::is_linux_desktop() ) {
$platform = self::PLATFORM_DESKTOP_LINUX;
} elseif ( static::is_mac_desktop() ) {
$platform = self::PLATFORM_DESKTOP_MAC;
} elseif ( static::is_windows_desktop() ) {
$platform = self::PLATFORM_DESKTOP_WINDOWS;
} elseif ( static::is_chrome_desktop() ) {
$platform = self::PLATFORM_DESKTOP_CHROME;
}
return $platform;
}

/**
* A simple pattern matching method for extracting the browser from the user agent.
*
* @return string
*/
public function get_browser() {
$ua = $this->useragent;
if ( empty( $ua ) ) {
return self::OTHER;
}

if ( static::is_opera_mini() || static::is_opera_mobile() || static::is_opera_desktop() || static::is_opera_mini_dumb() ) {
return self::BROWSER_OPERA;
} elseif ( static::is_edge_browser() ) {
return self::BROWSER_EDGE;
} elseif ( static::is_chrome_desktop() || self::is_chrome_for_iOS() ) {
return self::BROWSER_CHROME;
} elseif ( static::is_safari_browser() ) {
return self::BROWSER_SAFARI;
} elseif ( static::is_firefox_mobile() || static::is_firefox_desktop() ) {
return self::BROWSER_FIREFOX;
} elseif ( static::is_ie_browser() ) {
return self::BROWSER_IE;
}
return self::OTHER;
}

/**
* This method detects for UA which can display iPhone-optimized web content.
* Includes iPhone, iPod Touch, Android, WebOS, Fennec (Firefox mobile), etc.
Expand Down Expand Up @@ -714,6 +776,46 @@ public static function is_firefox_os() {
}
}

/**
* Detect Safari browser
*/
public static function is_safari_browser() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
return false;
}
if ( false === strpos( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ), 'Safari' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- This is validating.
return false;
}
return true;
}

/**
* Detect Edge browser
*/
public static function is_edge_browser() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
return false;
}
if ( false === strpos( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ), 'Edge' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- This is validating.
return false;
}
return true;
}

/**
* Detect Edge browser
*/
public static function is_ie_browser() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
return false;
}
$ua = wp_unslash( $_SERVER['HTTP_USER_AGENT'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- This is validating.
if ( false === ( strpos( $ua, 'MSIE' ) || strpos( $ua, 'Trident/7' ) ) ) {
return false;
}
return true;
}

/**
* Detect modern Opera desktop
*
Expand Down Expand Up @@ -1271,6 +1373,66 @@ public static function is_blackberry_10() {
return ( strpos( $agent, 'bb10' ) !== false ) && ( strpos( $agent, 'mobile' ) !== false );
}

/**
* Determines whether a desktop platform is Linux OS
*
* @return bool
*/
public static function is_linux_desktop() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
return false;
}
if ( ! preg_match( '/linux/i', wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- This is validating.
return false;
}
return true;
}

/**
* Determines whether a desktop platform is Mac OS
*
* @return bool
*/
public static function is_mac_desktop() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
return false;
}
if ( ! preg_match( '/macintosh|mac os x/i', wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- This is validating.
return false;
}
return true;
}

/**
* Determines whether a desktop platform is Windows OS
*
* @return bool
*/
public static function is_windows_desktop() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
return false;
}
if ( ! preg_match( '/windows|win32/i', wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- This is validating.
return false;
}
return true;
}

/**
* Determines whether a desktop platform is Chrome OS
*
* @return bool
*/
public static function is_chrome_desktop() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
return false;
}
if ( ! preg_match( '/chrome/i', wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- This is validating.
return false;
}
return true;
}

/**
* Retrieve the blackberry OS version.
*
Expand Down
6 changes: 3 additions & 3 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"packages": [
{
"name": "automattic/jetpack-device-detection",
"version": "2.1.2",
"version_normalized": "2.1.2.0",
"version": "2.1.3-alpha.1712142201",
"version_normalized": "2.1.3.0-alpha1712142201",
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-device-detection",
"reference": "529d8b7668a3fb4fbcc95a1774414968851a83a8"
"reference": "65b2344927c035d1d1c012117b9e5c801ee11c3b"
},
"require": {
"php": ">=7.0"
Expand Down
6 changes: 3 additions & 3 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
),
'versions' => array(
'automattic/jetpack-device-detection' => array(
'pretty_version' => '2.1.2',
'version' => '2.1.2.0',
'reference' => '529d8b7668a3fb4fbcc95a1774414968851a83a8',
'pretty_version' => '2.1.3-alpha.1712142201',
'version' => '2.1.3.0-alpha1712142201',
'reference' => '65b2344927c035d1d1c012117b9e5c801ee11c3b',
'type' => 'jetpack-library',
'install_path' => __DIR__ . '/../automattic/jetpack-device-detection',
'aliases' => array(),
Expand Down

0 comments on commit 6be89be

Please sign in to comment.