| 1 |
<?php |
| 2 |
/** |
| 3 |
* Device helper. |
| 4 |
* |
| 5 |
* @package WP_Defender\Traits |
| 6 |
* @since 4.2.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WP_Defender\Traits; |
| 10 |
|
| 11 |
trait Device { |
| 12 |
|
| 13 |
/** |
| 14 |
* Detect if the device is a tablet. |
| 15 |
* |
| 16 |
* @return bool |
| 17 |
*/ |
| 18 |
private function is_tablet() { |
| 19 |
$user_agent = defender_get_data_from_request( 'HTTP_USER_AGENT', 's' ); |
| 20 |
if ( '' === $user_agent ) { |
| 21 |
return false; |
| 22 |
} |
| 23 |
/** |
| 24 |
* It doesn't work with IpadOS due to of this: |
| 25 |
* https://stackoverflow.com/questions/62323230/how-can-i-detect-with-php-that-the-user-uses-an-ipad-when-my-user-agent-doesnt-c |
| 26 |
*/ |
| 27 |
$tablet_pattern = '/(tablet|ipad|playbook|kindle|silk)/i'; |
| 28 |
|
| 29 |
return preg_match( $tablet_pattern, $user_agent ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Detect if the device is a mobile. |
| 34 |
* |
| 35 |
* @return bool|string |
| 36 |
*/ |
| 37 |
private function is_mobile() { |
| 38 |
$user_agent = defender_get_data_from_request( 'HTTP_USER_AGENT', 's' ); |
| 39 |
if ( '' === $user_agent ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
// Do not use wp_is_mobile() since it doesn't detect ipad/tablet. |
| 43 |
$mobile_patten = '/Mobile|iP(hone|od|ad)|Android|BlackBerry|tablet|IEMobile|Kindle|NetFront|Silk|(hpw|web)OS|Fennec|Minimo|Opera M(obi|ini)|Blazer|Dolfin|Dolphin|Skyfire|Zune|playbook/i'; |
| 44 |
|
| 45 |
return preg_match( $mobile_patten, $user_agent ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get current device type. |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
protected function get_device() { |
| 54 |
if ( $this->is_mobile() ) { |
| 55 |
return 'mobile'; |
| 56 |
} |
| 57 |
|
| 58 |
if ( $this->is_tablet() ) { |
| 59 |
return 'tablet'; |
| 60 |
} |
| 61 |
|
| 62 |
return 'desktop'; |
| 63 |
} |
| 64 |
} |
| 65 |
|