Automattic\Jetpack
Device_Detection::is_mobile
Determine if the current User Agent matches the passed $kind.
Метод класса: Device_Detection{}
Хуков нет.
Возвращает
true|false|Строку. Boolean indicating if current UA matches $kind. If $return_matched_agent is true, returns the UA string.
Использование
$result = Device_Detection::is_mobile( $kind, $return_matched_agent, $ua_info );
- $kind(строка) (обязательный)
- Category of mobile device to check for. Either: any, dumb, smart.
- $return_matched_agent(true|false) (обязательный)
- Boolean indicating if the UA should be returned.
- $ua_info(User_Agent_Info) (обязательный)
- Boolean indicating if the UA should be returned.
Код Device_Detection::is_mobile() Device Detection::is mobile WPSCache 3.0.3
private static function is_mobile( $kind, $return_matched_agent, $ua_info ) {
$kinds = array(
'smart' => false,
'dumb' => false,
'any' => false,
);
$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;
}
// 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;
}