Automattic\Jetpack

Device_Detection::get_info()public staticWPSCache 1.0

Returns information about the current device accessing the page.

Method of the class: Device_Detection{}

Hooks from the method

Returns

Array. Device information.

array( 'is_phone' => (bool) Whether the current device is a mobile phone. 'is_smartphone' => (bool) Whether the current device is a smartphone. 'is_tablet' => (bool) Whether the current device is a tablet device. 'is_handheld' => (bool) Whether the current device is a handheld device. 'is_desktop' => (bool) Whether the current device is a laptop / desktop device. 'platform' => (string) Detected platform. 'is_phone_matched_ua' => (string) Matched UA. );

Usage

$result = Device_Detection::get_info( $ua );
$ua(string)
(Optional) User-Agent string.
Default: ''

Device_Detection::get_info() code WPSCache 2.0.1

public static function get_info( $ua = '' ) {
	// Return memoized result if available.
	// phpcs:disable WordPress.Security.ValidatedSanitizedInput
	$memo_key = ! empty( $ua ) ? $ua : ( $_SERVER['HTTP_USER_AGENT'] ?? '' );
	// Note: UA string used raw for compatibility reasons.
	// No sanitization is needed as the value is never output or persisted, and is only used for memoization.
	// phpcs:enable WordPress.Security.ValidatedSanitizedInput
	if ( isset( self::$get_info_memo[ $memo_key ] ) ) {
		return self::$get_info_memo[ $memo_key ];
	}

	$ua_info = new User_Agent_Info( $ua );

	$info = array(
		'is_phone'            => self::is_mobile( 'any', false, $ua_info ),
		'is_phone_matched_ua' => self::is_mobile( 'any', true, $ua_info ),
		'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'];
	$info['is_desktop']  = ! $info['is_handheld'];

	if ( function_exists( 'apply_filters' ) ) {
		/**
		 * Filter the value of Device_Detection::get_info.
		 *
		 * @since 1.0.0
		 *
		 * @param array           $info    Array of device information.
		 * @param string          $ua      User agent string passed to Device_Detection::get_info.
		 * @param User_Agent_Info $ua_info Instance of Automattic\Jetpack\Device_Detection\User_Agent_Info.
		 */
		$info = apply_filters( 'jetpack_device_detection_get_info', $info, $ua, $ua_info );
	}

	// Memoize the result.
	self::$get_info_memo[ $memo_key ] = $info;
	if ( count( self::$get_info_memo ) > self::$max_memo_size ) {
		array_shift( self::$get_info_memo );
	}

	return $info;
}