| 1 |
<?php |
| 2 |
|
| 3 |
namespace OPTN\Includes\Utils; |
| 4 |
|
| 5 |
use OPTN\Includes\Utils\Device; |
| 6 |
|
| 7 |
class Os { |
| 8 |
|
| 9 |
private static $desktop_oses = |
| 10 |
array( |
| 11 |
'/windows nt 10/i' => 'Windows', |
| 12 |
'/windows nt 6.3/i' => 'Windows', |
| 13 |
'/windows nt 6.2/i' => 'Windows', |
| 14 |
'/windows nt 6.1/i' => 'Windows', |
| 15 |
'/windows nt 6.0/i' => 'Windows', |
| 16 |
'/windows nt 5.2/i' => 'Windows', |
| 17 |
'/windows nt 5.1/i' => 'Windows', |
| 18 |
'/windows xp/i' => 'Windows', |
| 19 |
'/windows nt 5.0/i' => 'Windows', |
| 20 |
'/windows me/i' => 'Windows', |
| 21 |
'/win98/i' => 'Windows', |
| 22 |
'/win95/i' => 'Windows', |
| 23 |
'/win16/i' => 'Windows', |
| 24 |
'/macintosh|mac os x/i' => 'Mac', |
| 25 |
'/mac_powerpc/i' => 'Mac', |
| 26 |
'/linux/i' => 'Linux', |
| 27 |
'/ubuntu/i' => 'Linux', |
| 28 |
); |
| 29 |
|
| 30 |
private static $mobile_oses = |
| 31 |
array( |
| 32 |
'/android/i' => 'Android', |
| 33 |
'/blackberry/i' => 'Android', |
| 34 |
'/webos/i' => 'Android', |
| 35 |
'/iphone/i' => 'iOS', |
| 36 |
'/ipod/i' => 'iOS', |
| 37 |
'/ipad/i' => 'iOS', |
| 38 |
); |
| 39 |
|
| 40 |
public static function getOS() { |
| 41 |
|
| 42 |
if ( ! isset( $_SERVER['HTTP_USER_AGENT'] ) ) { |
| 43 |
return 'Windows'; |
| 44 |
} |
| 45 |
|
| 46 |
$user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ); // phpcs:ignore |
| 47 |
|
| 48 |
$device = Device::get_device( $user_agent ); |
| 49 |
|
| 50 |
$os_list = $device === 'lg' ? self::$desktop_oses : self::$mobile_oses; |
| 51 |
|
| 52 |
foreach ( $os_list as $regex => $value ) { |
| 53 |
if ( preg_match( $regex, $user_agent ) ) { |
| 54 |
return $value; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return $device === 'lg' ? 'Windows' : 'Android'; |
| 59 |
} |
| 60 |
} |
| 61 |
|