|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Device detection for Jetpack. |
| 4 | + * |
| 5 | + * Since WPSC doesn't use an autoloader or composer, this is a simplified version of the package |
| 6 | + * as of November 11, 2025. |
| 7 | + * |
| 8 | + * @package automattic/jetpack-device-detection |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Automattic\WPSC; |
| 12 | + |
| 13 | +require_once __DIR__ . '/functions.php'; |
| 14 | +require_once __DIR__ . '/class-user-agent-info.php'; |
| 15 | + |
| 16 | +use Automattic\WPSC\Device_Detection\User_Agent_Info; |
| 17 | +use function Automattic\WPSC\Device_Detection\wp_unslash; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class Automattic\WPSC\Device_Detection |
| 21 | + * |
| 22 | + * Determine if the current User Agent matches the passed $kind. |
| 23 | + * |
| 24 | + * Note: str_contains() and other PHP8+ functions that have a polyfill in core are not used here, |
| 25 | + * as wp-includes/compat.php may not be loaded yet. |
| 26 | + */ |
| 27 | +class Device_Detection { |
| 28 | + |
| 29 | + /** |
| 30 | + * Detects phone devices. |
| 31 | + * |
| 32 | + * @return bool |
| 33 | + */ |
| 34 | + public static function is_phone() { |
| 35 | + if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + $ua_info = new User_Agent_Info( $_SERVER['HTTP_USER_AGENT'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Handled in User_Agent_Info |
| 40 | + |
| 41 | + $agent = strtolower( filter_var( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ); |
| 42 | + if ( strpos( $agent, 'ipad' ) ) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + // Remove Samsung Galaxy tablets (SCH-I800) from being mobile devices. |
| 47 | + if ( strpos( $agent, 'sch-i800' ) ) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + if ( $ua_info->is_android_tablet() && false === $ua_info->is_kindle_touch() ) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + if ( $ua_info->is_blackberry_tablet() ) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + // checks for iPhoneTier devices & RichCSS devices. |
| 60 | + if ( $ua_info->isTierIphone() || $ua_info->isTierRichCSS() ) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + $dumb_agents = $ua_info->dumb_agents; |
| 65 | + |
| 66 | + foreach ( $dumb_agents as $dumb_agent ) { |
| 67 | + if ( false !== strpos( $agent, $dumb_agent ) ) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if ( isset( $_SERVER['HTTP_X_WAP_PROFILE'] ) ) { |
| 73 | + return true; |
| 74 | + } 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. |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + return false; |
| 79 | + } |
| 80 | +} |
0 commit comments