Automattic\Jetpack

Device_Detection::is_mobile()private staticWPSCache 1.0

Determine if the current User Agent matches the passed $kind.

Method of the class: Device_Detection{}

No Hooks.

Return

true|false|String. Boolean indicating if current UA matches $kind. If $return_matched_agent is true, returns the UA string.

Usage

$result = Device_Detection::is_mobile( $kind, $return_matched_agent, $ua_info );
$kind(string) (required)
Category of mobile device to check for. Either: any, dumb, smart.
$return_matched_agent(true|false) (required)
Boolean indicating if the UA should be returned.
$ua_info(User_Agent_Info) (required)
Boolean indicating if the UA should be returned.

Device_Detection::is_mobile() code WPSCache 1.12.0

private static function is_mobile( $kind, $return_matched_agent, $ua_info ) {
	$kinds         = array(
		'smart' => false,
		'dumb'  => false,
		'any'   => false,
	);
	$first_run     = true;
	$matched_agent = '';

	// If an invalid kind is passed in, reset it to default.
	if ( ! isset( $kinds[ $kind ] ) ) {
			$kind = 'any';
	}

	if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
		return false;
	}

	$agent = strtolower( filter_var( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) );
	if ( strpos( $agent, 'ipad' ) ) {
		return false;
	}

	// Remove Samsung Galaxy tablets (SCH-I800) from being mobile devices.
	if ( strpos( $agent, 'sch-i800' ) ) {
		return false;
	}

	if ( $ua_info->is_android_tablet() && false === $ua_info->is_kindle_touch() ) {
		return false;
	}

	if ( $ua_info->is_blackberry_tablet() ) {
		return false;
	}

	if ( $first_run ) {
		$first_run = false;

		// checks for iPhoneTier devices & RichCSS devices.
		if ( $ua_info->isTierIphone() || $ua_info->isTierRichCSS() ) {
			$kinds['smart'] = true;
			$matched_agent  = $ua_info->matched_agent;
		}

		if ( ! $kinds['smart'] ) {
			// if smart, we are not dumb so no need to check.
			$dumb_agents = $ua_info->dumb_agents;

			foreach ( $dumb_agents as $dumb_agent ) {
				if ( false !== strpos( $agent, $dumb_agent ) ) {
					$kinds['dumb'] = true;
					$matched_agent = $dumb_agent;

					break;
				}
			}

			if ( ! $kinds['dumb'] ) {
				if ( isset( $_SERVER['HTTP_X_WAP_PROFILE'] ) ) {
					$kinds['dumb'] = true;
					$matched_agent = 'http_x_wap_profile';
				} elseif ( isset( $_SERVER['HTTP_ACCEPT'] ) && ( preg_match( '/wap\.|\.wap/i', $_SERVER['HTTP_ACCEPT'] ) || false !== strpos( strtolower( $_SERVER['HTTP_ACCEPT'] ), 'application/vnd.wap.xhtml+xml' ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- This is doing the validating.
					$kinds['dumb'] = true;
					$matched_agent = 'vnd.wap.xhtml+xml';
				}
			}
		}

		if ( $kinds['dumb'] || $kinds['smart'] ) {
			$kinds['any'] = true;
		}
	}

	$value = $kinds[ $kind ];

	if ( $return_matched_agent ) {
		$value = $matched_agent;
	}
	return $value;
}