Completed
Push — renovate/react-monorepo ( 545e89...65a81e )
by
unknown
268:48 queued 253:47
created

User_Agent_Info   F

Complexity

Total Complexity 341

Size/Duplication

Total Lines 1562
Duplicated Lines 21.06 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 329
loc 1562
rs 0.8
c 0
b 0
f 0
wmc 341
lcom 1
cbo 0

49 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
D get_mobile_user_agent_name() 0 71 32
C get_platform() 0 43 17
D isTierIphone() 48 89 19
B isTierRichCss() 6 37 9
B is_tablet() 0 10 7
B is_iphoneOrIpod() 9 16 7
B is_iphone_or_ipod() 0 17 7
A is_chrome_for_iOS() 0 17 4
A is_twitter_for_iphone() 0 17 4
A is_twitter_for_ipad() 0 15 5
B is_facebook_for_iphone() 0 21 9
A is_facebook_for_ipad() 17 17 6
A is_wordpress_for_ios() 0 12 3
A is_ipad() 0 19 6
A is_firefox_mobile() 0 14 3
A is_firefox_desktop() 14 14 5
A is_firefox_os() 14 14 6
A is_opera_mobile() 15 15 6
A is_opera_mini() 0 13 4
B is_opera_mini_dumb() 0 17 8
A is_OperaMobile() 0 19 5
A is_WindowsPhone7() 17 17 6
A is_windows_phone_8() 0 12 3
A is_PalmWebOS() 17 17 6
B is_TouchPad() 0 16 8
C is_S60_OSSBrowser() 3 27 12
C is_symbian_platform() 3 28 13
A is_symbian_s40_platform() 16 16 6
A is_J2ME_platform() 15 15 5
B is_MaemoTablet() 9 24 8
A is_MeeGo() 18 18 6
A is_webkit() 16 16 3
A is_android() 17 17 6
B is_android_tablet() 0 21 8
A is_kindle_fire() 0 14 4
A is_kindle_touch() 12 12 4
A is_windows8_auth() 13 13 3
A is_wordpress_for_win8() 13 13 3
A is_wordpress_desktop_app() 13 13 3
A is_blackberry_tablet() 0 16 4
A is_blackbeberry() 18 18 6
A is_blackberry_10() 0 4 2
D get_blackbeberry_OS_version() 3 75 19
C detect_blackberry_browser_version() 3 49 13
B is_mobile_app() 0 26 6
A is_Nintendo_3DS() 0 11 3
A is_bot() 0 9 2
B is_bot_user_agent() 0 75 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like User_Agent_Info often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use User_Agent_Info, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * User agent detection for Jetpack.
4
 *
5
 * @package automattic/jetpack-device-detection
6
 *
7
 * We don't want to rename public members.
8
 * @phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
9
 * @phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
10
 * @phpcs:disable WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
11
 * @phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
12
 */
13
14
namespace Automattic\Jetpack\Device_Detection;
15
16
/**
17
 * A class providing device properties detection.
18
 */
19
class User_Agent_Info {
20
21
	/**
22
	 * Provided or fetched User-Agent string.
23
	 *
24
	 * @var string
25
	 */
26
	public $useragent;
27
28
	/**
29
	 * A device group matched user agent, e.g. 'iphone'.
30
	 *
31
	 * @var string
32
	 */
33
	public $matched_agent;
34
35
	/**
36
	 * Stores whether is the iPhone tier of devices.
37
	 *
38
	 * @var bool
39
	 */
40
	public $isTierIphone;
41
42
	/**
43
	 * Stores whether the device can probably support Rich CSS, but JavaScript (jQuery) support is not assumed.
44
	 *
45
	 * @var bool
46
	 */
47
	public $isTierRichCss;
48
49
	/**
50
	 * Stores whether it is another mobile device, which cannot be assumed to support CSS or JS (eg, older BlackBerry, RAZR)
51
	 *
52
	 * @var bool
53
	 */
54
	public $isTierGenericMobile;
55
56
	/**
57
	 * Stores the device platform name
58
	 *
59
	 * @var null|string
60
	 */
61
	private $platform             = null;
62
	const PLATFORM_WINDOWS        = 'windows';
63
	const PLATFORM_IPHONE         = 'iphone';
64
	const PLATFORM_IPOD           = 'ipod';
65
	const PLATFORM_IPAD           = 'ipad';
66
	const PLATFORM_BLACKBERRY     = 'blackberry';
67
	const PLATFORM_BLACKBERRY_10  = 'blackberry_10';
68
	const PLATFORM_SYMBIAN        = 'symbian_series60';
69
	const PLATFORM_SYMBIAN_S40    = 'symbian_series40';
70
	const PLATFORM_J2ME_MIDP      = 'j2me_midp';
71
	const PLATFORM_ANDROID        = 'android';
72
	const PLATFORM_ANDROID_TABLET = 'android_tablet';
73
	const PLATFORM_FIREFOX_OS     = 'firefoxOS';
74
75
	/**
76
	 * A list of dumb-phone user agent parts.
77
	 *
78
	 * @var array
79
	 */
80
	public $dumb_agents = array(
81
		'nokia',
82
		'blackberry',
83
		'philips',
84
		'samsung',
85
		'sanyo',
86
		'sony',
87
		'panasonic',
88
		'webos',
89
		'ericsson',
90
		'alcatel',
91
		'palm',
92
		'windows ce',
93
		'opera mini',
94
		'series60',
95
		'series40',
96
		'au-mic,',
97
		'audiovox',
98
		'avantgo',
99
		'blazer',
100
		'danger',
101
		'docomo',
102
		'epoc',
103
		'ericy',
104
		'i-mode',
105
		'ipaq',
106
		'midp-',
107
		'mot-',
108
		'netfront',
109
		'nitro',
110
		'palmsource',
111
		'pocketpc',
112
		'portalmmm',
113
		'rover',
114
		'sie-',
115
		'symbian',
116
		'cldc-',
117
		'j2me',
118
		'smartphone',
119
		'up.browser',
120
		'up.link',
121
		'up.link',
122
		'vodafone/',
123
		'wap1.',
124
		'wap2.',
125
		'mobile',
126
		'googlebot-mobile',
127
	);
128
129
	/**
130
	 * The constructor.
131
	 *
132
	 * @param string $ua (Optional) User agent.
133
	 */
134
	public function __construct( $ua = '' ) {
135
		if ( $ua ) {
136
			$this->useragent = $ua;
137
		} else {
138
			if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
139
				$this->useragent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
140
			}
141
		}
142
	}
143
144
	/**
145
	 * This method detects the mobile User Agent name.
146
	 *
147
	 * @return string The matched User Agent name, false otherwise.
148
	 */
149
	public function get_mobile_user_agent_name() {
150
		if ( $this->is_chrome_for_iOS() ) { // Keep this check before the safari rule.
151
			return 'chrome-for-ios';
152
		} elseif ( $this->is_iphone_or_ipod( 'iphone-safari' ) ) {
153
			return 'iphone';
154
		} elseif ( $this->is_ipad( 'ipad-safari' ) ) {
155
			return 'ipad';
156
		} elseif ( $this->is_android_tablet() ) { // Keep this check before the android rule.
157
			return 'android_tablet';
158
		} elseif ( $this->is_android() ) {
159
			return 'android';
160
		} elseif ( $this->is_blackberry_10() ) {
161
			return 'blackberry_10';
162
		} elseif ( $this->is_blackbeberry() ) {
163
			return 'blackberry';
164
		} elseif ( $this->is_WindowsPhone7() ) {
165
			return 'win7';
166
		} elseif ( $this->is_windows_phone_8() ) {
167
			return 'winphone8';
168
		} elseif ( $this->is_opera_mini() ) {
169
			return 'opera-mini';
170
		} elseif ( $this->is_opera_mini_dumb() ) {
171
			return 'opera-mini-dumb';
172
		} elseif ( $this->is_opera_mobile() ) {
173
			return 'opera-mobi';
174
		} elseif ( $this->is_blackberry_tablet() ) {
175
			return 'blackberry_tablet';
176
		} elseif ( $this->is_kindle_fire() ) {
177
			return 'kindle-fire';
178
		} elseif ( $this->is_PalmWebOS() ) {
179
			return 'webos';
180
		} elseif ( $this->is_S60_OSSBrowser() ) {
181
			return 'series60';
182
		} elseif ( $this->is_firefox_os() ) {
183
			return 'firefoxOS';
184
		} elseif ( $this->is_firefox_mobile() ) {
185
			return 'firefox_mobile';
186
		} elseif ( $this->is_MaemoTablet() ) {
187
			return 'maemo';
188
		} elseif ( $this->is_MeeGo() ) {
189
			return 'meego';
190
		} elseif ( $this->is_TouchPad() ) {
191
			return 'hp_tablet';
192
		} elseif ( $this->is_facebook_for_iphone() ) {
193
			return 'facebook-for-iphone';
194
		} elseif ( $this->is_facebook_for_ipad() ) {
195
			return 'facebook-for-ipad';
196
		} elseif ( $this->is_twitter_for_iphone() ) {
197
			return 'twitter-for-iphone';
198
		} elseif ( $this->is_twitter_for_ipad() ) {
199
			return 'twitter-for-ipad';
200
		} elseif ( $this->is_wordpress_for_ios() ) {
201
			return 'ios-app';
202
		} elseif ( $this->is_iphone_or_ipod( 'iphone-not-safari' ) ) {
203
			return 'iphone-unknown';
204
		} elseif ( $this->is_ipad( 'ipad-not-safari' ) ) {
205
			return 'ipad-unknown';
206
		} elseif ( $this->is_Nintendo_3DS() ) {
207
			return 'nintendo-3ds';
208
		} else {
209
			$agent       = strtolower( $_SERVER['HTTP_USER_AGENT'] );
210
			$dumb_agents = $this->dumb_agents;
211
			foreach ( $dumb_agents as $dumb_agent ) {
212
				if ( false !== strpos( $agent, $dumb_agent ) ) {
213
					return $dumb_agent;
214
				}
215
			}
216
		}
217
218
		return false;
219
	}
220
221
	/**
222
	 * This method detects the mobile device's platform. All return strings are from the class constants.
223
	 * Note that this function returns the platform name, not the UA name/type. You should use a different function
224
	 * if you need to test the UA capabilites.
225
	 *
226
	 * @return string Name of the platform, false otherwise.
227
	 */
228
	public function get_platform() {
229
		if ( isset( $this->platform ) ) {
230
				return $this->platform;
231
		}
232
233
		if ( strpos( $this->useragent, 'windows phone' ) !== false ) {
234
				$this->platform = self::PLATFORM_WINDOWS;
235
		} elseif ( strpos( $this->useragent, 'windows ce' ) !== false ) {
236
			$this->platform = self::PLATFORM_WINDOWS;
237
		} elseif ( strpos( $this->useragent, 'ipad' ) !== false ) {
238
			$this->platform = self::PLATFORM_IPAD;
239
		} elseif ( strpos( $this->useragent, 'ipod' ) !== false ) {
240
			$this->platform = self::PLATFORM_IPOD;
241
		} elseif ( strpos( $this->useragent, 'iphone' ) !== false ) {
242
			$this->platform = self::PLATFORM_IPHONE;
243
		} elseif ( strpos( $this->useragent, 'android' ) !== false ) {
244
			if ( $this->is_android_tablet() ) {
245
				$this->platform = self::PLATFORM_ANDROID_TABLET;
246
			} else {
247
				$this->platform = self::PLATFORM_ANDROID;
248
			}
249
		} elseif ( $this->is_kindle_fire() ) {
250
			$this->platform = self::PLATFORM_ANDROID_TABLET;
251
		} elseif ( $this->is_blackberry_10() ) {
252
			$this->platform = self::PLATFORM_BLACKBERRY_10;
253
		} elseif ( strpos( $this->useragent, 'blackberry' ) !== false ) {
254
			$this->platform = self::PLATFORM_BLACKBERRY;
255
		} elseif ( $this->is_blackberry_tablet() ) {
256
			$this->platform = self::PLATFORM_BLACKBERRY;
257
		} elseif ( $this->is_symbian_platform() ) {
258
			$this->platform = self::PLATFORM_SYMBIAN;
259
		} elseif ( $this->is_symbian_s40_platform() ) {
260
			$this->platform = self::PLATFORM_SYMBIAN_S40;
261
		} elseif ( $this->is_J2ME_platform() ) {
262
			$this->platform = self::PLATFORM_J2ME_MIDP;
263
		} elseif ( $this->is_firefox_os() ) {
264
			$this->platform = self::PLATFORM_FIREFOX_OS;
265
		} else {
266
			$this->platform = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type null|string of property $platform.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
267
		}
268
269
		return $this->platform;
270
	}
271
272
	/**
273
	 * This method detects for UA which can display iPhone-optimized web content.
274
	 * Includes iPhone, iPod Touch, Android, WebOS, Fennec (Firefox mobile), etc.
275
	 */
276
	public function isTierIphone() {
277
		if ( isset( $this->isTierIphone ) ) {
278
			return $this->isTierIphone;
279
		}
280
		if ( $this->is_iphoneOrIpod() ) {
281
			$this->matched_agent       = 'iphone';
282
			$this->isTierIphone        = true;
283
			$this->isTierRichCss       = false;
284
			$this->isTierGenericMobile = false;
285 View Code Duplication
		} elseif ( $this->is_android() ) {
286
			$this->matched_agent       = 'android';
287
			$this->isTierIphone        = true;
288
			$this->isTierRichCss       = false;
289
			$this->isTierGenericMobile = false;
290
		} elseif ( $this->is_windows_phone_8() ) {
291
			$this->matched_agent       = 'winphone8';
292
			$this->isTierIphone        = true;
293
			$this->isTierRichCss       = false;
294
			$this->isTierGenericMobile = false;
295 View Code Duplication
		} elseif ( $this->is_WindowsPhone7() ) {
296
			$this->matched_agent       = 'win7';
297
			$this->isTierIphone        = true;
298
			$this->isTierRichCss       = false;
299
			$this->isTierGenericMobile = false;
300
		} elseif ( $this->is_blackberry_10() ) {
301
			$this->matched_agent       = 'blackberry-10';
302
			$this->isTierIphone        = true;
303
			$this->isTierRichCss       = false;
304
			$this->isTierGenericMobile = false;
305 View Code Duplication
		} elseif ( $this->is_blackbeberry() && 'blackberry-webkit' === $this->detect_blackberry_browser_version() ) {
306
			$this->matched_agent       = 'blackberry-webkit';
307
			$this->isTierIphone        = true;
308
			$this->isTierRichCss       = false;
309
			$this->isTierGenericMobile = false;
310
		} elseif ( $this->is_blackberry_tablet() ) {
311
			$this->matched_agent       = 'blackberry_tablet';
312
			$this->isTierIphone        = true;
313
			$this->isTierRichCss       = false;
314
			$this->isTierGenericMobile = false;
315 View Code Duplication
		} elseif ( $this->is_PalmWebOS() ) {
316
			$this->matched_agent       = 'webos';
317
			$this->isTierIphone        = true;
318
			$this->isTierRichCss       = false;
319
			$this->isTierGenericMobile = false;
320
		} elseif ( $this->is_TouchPad() ) {
321
			$this->matched_agent       = 'hp_tablet';
322
			$this->isTierIphone        = true;
323
			$this->isTierRichCss       = false;
324
			$this->isTierGenericMobile = false;
325 View Code Duplication
		} elseif ( $this->is_firefox_os() ) {
326
			$this->matched_agent       = 'firefoxOS';
327
			$this->isTierIphone        = true;
328
			$this->isTierRichCss       = false;
329
			$this->isTierGenericMobile = false;
330
		} elseif ( $this->is_firefox_mobile() ) {
331
			$this->matched_agent       = 'fennec';
332
			$this->isTierIphone        = true;
333
			$this->isTierRichCss       = false;
334
			$this->isTierGenericMobile = false;
335 View Code Duplication
		} elseif ( $this->is_opera_mobile() ) {
336
			$this->matched_agent       = 'opera-mobi';
337
			$this->isTierIphone        = true;
338
			$this->isTierRichCss       = false;
339
			$this->isTierGenericMobile = false;
340
		} elseif ( $this->is_MaemoTablet() ) {
341
			$this->matched_agent       = 'maemo';
342
			$this->isTierIphone        = true;
343
			$this->isTierRichCss       = false;
344
			$this->isTierGenericMobile = false;
345 View Code Duplication
		} elseif ( $this->is_MeeGo() ) {
346
			$this->matched_agent       = 'meego';
347
			$this->isTierIphone        = true;
348
			$this->isTierRichCss       = false;
349
			$this->isTierGenericMobile = false;
350
		} elseif ( $this->is_kindle_touch() ) {
351
			$this->matched_agent       = 'kindle-touch';
352
			$this->isTierIphone        = true;
353
			$this->isTierRichCss       = false;
354
			$this->isTierGenericMobile = false;
355 View Code Duplication
		} elseif ( $this->is_Nintendo_3DS() ) {
356
			$this->matched_agent       = 'nintendo-3ds';
357
			$this->isTierIphone        = true;
358
			$this->isTierRichCss       = false;
359
			$this->isTierGenericMobile = false;
360
		} else {
361
			$this->isTierIphone = false;
362
		}
363
		return $this->isTierIphone;
364
	}
365
366
	/**
367
	 * This method detects for UA which are likely to be capable
368
	 * but may not necessarily support JavaScript.
369
	 * Excludes all iPhone Tier UA.
370
	 */
371
	public function isTierRichCss() {
372
		if ( isset( $this->isTierRichCss ) ) {
373
			return $this->isTierRichCss;
374
		}
375
		if ( $this->isTierIphone() ) {
376
			return false;
377
		}
378
379
		// The following devices are explicitly ok.
380
		if ( $this->is_S60_OSSBrowser() ) {
381
			$this->matched_agent       = 'series60';
382
			$this->isTierIphone        = false;
383
			$this->isTierRichCss       = true;
384
			$this->isTierGenericMobile = false;
385 View Code Duplication
		} elseif ( $this->is_opera_mini() ) {
386
			$this->matched_agent       = 'opera-mini';
387
			$this->isTierIphone        = false;
388
			$this->isTierRichCss       = true;
389
			$this->isTierGenericMobile = false;
390
		} elseif ( $this->is_blackbeberry() ) {
391
			$detectedDevice = $this->detect_blackberry_browser_version();
392
			if (
393
				'blackberry-5' === $detectedDevice
394
				|| 'blackberry-4.7' === $detectedDevice
395
				|| 'blackberry-4.6' === $detectedDevice
396
			) {
397
				$this->matched_agent       = $detectedDevice;
398
				$this->isTierIphone        = false;
399
				$this->isTierRichCss       = true;
400
				$this->isTierGenericMobile = false;
401
			}
402
		} else {
403
			$this->isTierRichCss = false;
404
		}
405
406
		return $this->isTierRichCss;
407
	}
408
409
	/**
410
	 * Detects if the user is using a tablet.
411
	 * props Corey Gilmore, BGR.com
412
	 *
413
	 * @return bool
414
	 */
415
	public function is_tablet() {
416
		return ( 0 // Never true, but makes it easier to manage our list of tablet conditions.
417
				|| self::is_ipad()
418
				|| self::is_android_tablet()
419
				|| self::is_blackberry_tablet()
420
				|| self::is_kindle_fire()
421
				|| self::is_MaemoTablet()
422
				|| self::is_TouchPad()
423
		);
424
	}
425
426
	/**
427
	 *  Detects if the current UA is the default iPhone or iPod Touch Browser.
428
	 *
429
	 *  DEPRECATED: use is_iphone_or_ipod
430
	 */
431
	public function is_iphoneOrIpod() {
432
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
433
			return false;
434
		}
435
436
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
437 View Code Duplication
		if ( ( strpos( $ua, 'iphone' ) !== false ) || ( strpos( $ua, 'ipod' ) !== false ) ) {
438
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
439
				return false;
440
			} else {
441
				return true;
442
			}
443
		} else {
444
			return false;
445
		}
446
	}
447
448
	/**
449
	 *  Detects if the current UA is iPhone Mobile Safari or another iPhone or iPod Touch Browser.
450
	 *
451
	 *  They type can check for any iPhone, an iPhone using Safari, or an iPhone using something other than Safari.
452
	 *
453
	 *  Note: If you want to check for Opera mini, Opera mobile or Firefox mobile (or any 3rd party iPhone browser),
454
	 *  you should put the check condition before the check for 'iphone-any' or 'iphone-not-safari'.
455
	 *  Otherwise those browsers will be 'catched' by the iphone string.
456
	 *
457
	 * @param string $type Type of iPhone detection.
458
	 */
459
	public static function is_iphone_or_ipod( $type = 'iphone-any' ) {
460
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
461
			return false;
462
		}
463
464
		$ua        = strtolower( $_SERVER['HTTP_USER_AGENT'] );
465
		$is_iphone = ( strpos( $ua, 'iphone' ) !== false ) || ( strpos( $ua, 'ipod' ) !== false );
466
		$is_safari = ( false !== strpos( $ua, 'safari' ) );
467
468
		if ( 'iphone-safari' === $type ) {
469
			return $is_iphone && $is_safari;
470
		} elseif ( 'iphone-not-safari' === $type ) {
471
			return $is_iphone && ! $is_safari;
472
		} else {
473
			return $is_iphone;
474
		}
475
	}
476
477
	/**
478
	 *  Detects if the current UA is Chrome for iOS
479
	 *
480
	 *  The User-Agent string in Chrome for iOS is the same as the Mobile Safari User-Agent, with CriOS/<ChromeRevision> instead of Version/<VersionNum>.
481
	 *  - Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3
482
	 */
483
	public static function is_chrome_for_iOS() {
484
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
485
			return false;
486
		}
487
488
		if ( self::is_iphone_or_ipod( 'iphone-safari' ) === false ) {
489
			return false;
490
		}
491
492
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
493
494
		if ( strpos( $ua, 'crios/' ) !== false ) {
495
			return true;
496
		} else {
497
			return false;
498
		}
499
	}
500
501
	/**
502
	 *  Detects if the current UA is Twitter for iPhone
503
	 *
504
	 * Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_5 like Mac OS X; nb-no) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8L1 Twitter for iPhone
505
	 * Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B206 Twitter for iPhone
506
	 */
507
	public static function is_twitter_for_iphone() {
508
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
509
			return false;
510
		}
511
512
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
513
514
		if ( strpos( $ua, 'ipad' ) !== false ) {
515
			return false;
516
		}
517
518
		if ( strpos( $ua, 'twitter for iphone' ) !== false ) {
519
			return true;
520
		} else {
521
			return false;
522
		}
523
	}
524
525
	/**
526
	 * Detects if the current UA is Twitter for iPad
527
	 *
528
	 * Old version 4.X - Mozilla/5.0 (iPad; U; CPU OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8L1 Twitter for iPad
529
	 * Ver 5.0 or Higher - Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B206 Twitter for iPhone
530
	 */
531
	public static function is_twitter_for_ipad() {
532
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
533
			return false;
534
		}
535
536
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
537
538
		if ( strpos( $ua, 'twitter for ipad' ) !== false ) {
539
			return true;
540
		} elseif ( strpos( $ua, 'ipad' ) !== false && strpos( $ua, 'twitter for iphone' ) !== false ) {
541
			return true;
542
		} else {
543
			return false;
544
		}
545
	}
546
547
	/**
548
	 * Detects if the current UA is Facebook for iPhone
549
	 * - Facebook 4020.0 (iPhone; iPhone OS 5.0.1; fr_FR)
550
	 * - Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_0 like Mac OS X; en_US) AppleWebKit (KHTML, like Gecko) Mobile [FBAN/FBForIPhone;FBAV/4.0.2;FBBV/4020.0;FBDV/iPhone3,1;FBMD/iPhone;FBSN/iPhone OS;FBSV/5.0;FBSS/2; FBCR/O2;FBID/phone;FBLC/en_US;FBSF/2.0]
551
	 * - Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B206 [FBAN/FBIOS;FBAV/5.0;FBBV/47423;FBDV/iPhone3,1;FBMD/iPhone;FBSN/iPhone OS;FBSV/5.1.1;FBSS/2; FBCR/3ITA;FBID/phone;FBLC/en_US]
552
	 */
553
	public static function is_facebook_for_iphone() {
554
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
555
			return false;
556
		}
557
558
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
559
560
		if ( false === strpos( $ua, 'iphone' ) ) {
561
			return false;
562
		}
563
564
		if ( false !== strpos( $ua, 'facebook' ) && false === strpos( $ua, 'ipad' ) ) {
565
			return true;
566
		} elseif ( false !== strpos( $ua, 'fbforiphone' ) && false === strpos( $ua, 'tablet' ) ) {
567
			return true;
568
		} elseif ( false !== strpos( $ua, 'fban/fbios;' ) && false === strpos( $ua, 'tablet' ) ) { // FB app v5.0 or higher.
569
			return true;
570
		} else {
571
			return false;
572
		}
573
	}
574
575
	/**
576
	 * Detects if the current UA is Facebook for iPad
577
	 * - Facebook 4020.0 (iPad; iPhone OS 5.0.1; en_US)
578
	 * - Mozilla/5.0 (iPad; U; CPU iPhone OS 5_0 like Mac OS X; en_US) AppleWebKit (KHTML, like Gecko) Mobile [FBAN/FBForIPhone;FBAV/4.0.2;FBBV/4020.0;FBDV/iPad2,1;FBMD/iPad;FBSN/iPhone OS;FBSV/5.0;FBSS/1; FBCR/;FBID/tablet;FBLC/en_US;FBSF/1.0]
579
	 * - Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10A403 [FBAN/FBIOS;FBAV/5.0;FBBV/47423;FBDV/iPad2,1;FBMD/iPad;FBSN/iPhone OS;FBSV/6.0;FBSS/1; FBCR/;FBID/tablet;FBLC/en_US]
580
	 */
581 View Code Duplication
	public static function is_facebook_for_ipad() {
582
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
583
			return false;
584
		}
585
586
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
587
588
		if ( false === strpos( $ua, 'ipad' ) ) {
589
			return false;
590
		}
591
592
		if ( false !== strpos( $ua, 'facebook' ) || false !== strpos( $ua, 'fbforiphone' ) || false !== strpos( $ua, 'fban/fbios;' ) ) {
593
			return true;
594
		} else {
595
			return false;
596
		}
597
	}
598
599
	/**
600
	 *  Detects if the current UA is WordPress for iOS
601
	 */
602
	public static function is_wordpress_for_ios() {
603
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
604
			return false;
605
		}
606
607
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
608
		if ( false !== strpos( $ua, 'wp-iphone' ) ) {
609
			return true;
610
		} else {
611
			return false;
612
		}
613
	}
614
615
	/**
616
	 * Detects if the current device is an iPad.
617
	 * They type can check for any iPad, an iPad using Safari, or an iPad using something other than Safari.
618
	 *
619
	 * Note: If you want to check for Opera mini, Opera mobile or Firefox mobile (or any 3rd party iPad browser),
620
	 * you should put the check condition before the check for 'iphone-any' or 'iphone-not-safari'.
621
	 * Otherwise those browsers will be 'catched' by the ipad string.
622
	 *
623
	 * @param string $type iPad type.
624
	 */
625
	public static function is_ipad( $type = 'ipad-any' ) {
626
627
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
628
			return false;
629
		}
630
631
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
632
633
		$is_ipad   = ( false !== strpos( $ua, 'ipad' ) );
634
		$is_safari = ( false !== strpos( $ua, 'safari' ) );
635
636
		if ( 'ipad-safari' === $type ) {
637
			return $is_ipad && $is_safari;
638
		} elseif ( 'ipad-not-safari' === $type ) {
639
			return $is_ipad && ! $is_safari;
640
		} else {
641
			return $is_ipad;
642
		}
643
	}
644
645
	/**
646
	 * Detects if the current browser is Firefox Mobile (Fennec)
647
	 *
648
	 * See http://www.useragentstring.com/pages/Fennec/
649
	 * Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.1.1) Gecko/20110415 Firefox/4.0.2pre Fennec/4.0.1
650
	 * Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1b2pre) Gecko/20081015 Fennec/1.0a1
651
	 */
652
	public static function is_firefox_mobile() {
653
654
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
655
			return false;
656
		}
657
658
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
659
660
		if ( strpos( $ua, 'fennec' ) !== false ) {
661
			return true;
662
		} else {
663
			return false;
664
		}
665
	}
666
667
	/**
668
	 * Detects if the current browser is Firefox for desktop
669
	 *
670
	 * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox
671
	 * Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion
672
	 * The platform section will include 'Mobile' for phones and 'Tablet' for tablets.
673
	 */
674 View Code Duplication
	public static function is_firefox_desktop() {
675
676
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
677
			return false;
678
		}
679
680
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
681
682
		if ( false !== strpos( $ua, 'firefox' ) && false === strpos( $ua, 'mobile' ) && false === strpos( $ua, 'tablet' ) ) {
683
			return true;
684
		} else {
685
			return false;
686
		}
687
	}
688
689
	/**
690
	 * Detects if the current browser is FirefoxOS Native browser
691
	 *
692
	 * Mozilla/5.0 (Mobile; rv:14.0) Gecko/14.0 Firefox/14.0
693
	 */
694 View Code Duplication
	public static function is_firefox_os() {
695
696
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
697
			return false;
698
		}
699
700
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
701
702
		if ( strpos( $ua, 'mozilla' ) !== false && strpos( $ua, 'mobile' ) !== false && strpos( $ua, 'gecko' ) !== false && strpos( $ua, 'firefox' ) !== false ) {
703
			return true;
704
		} else {
705
			return false;
706
		}
707
	}
708
709
	/**
710
	 * Detects if the current browser is Opera Mobile
711
	 *
712
	 * What is the difference between Opera Mobile and Opera Mini?
713
	 * - Opera Mobile is a full Internet browser for mobile devices.
714
	 * - Opera Mini always uses a transcoder to convert the page for a small display.
715
	 * (it uses Opera advanced server compression technology to compress web content before it gets to a device.
716
	 *  The rendering engine is on Opera's server.)
717
	 *
718
	 * Opera/9.80 (Windows NT 6.1; Opera Mobi/14316; U; en) Presto/2.7.81 Version/11.00"
719
	 * Opera/9.50 (Nintendo DSi; Opera/507; U; en-US)
720
	 */
721 View Code Duplication
	public static function is_opera_mobile() {
722
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
723
			return false;
724
		}
725
726
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
727
728
		if ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'mobi' ) !== false ) {
729
			return true;
730
		} elseif ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'nintendo dsi' ) !== false ) {
731
			return true;
732
		} else {
733
			return false;
734
		}
735
	}
736
737
	/**
738
	 * Detects if the current browser is Opera Mini
739
	 *
740
	 * Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)
741
	 * Opera/9.80 (Android;Opera Mini/6.0.24212/24.746 U;en) Presto/2.5.25 Version/10.5454
742
	 * Opera/9.80 (iPhone; Opera Mini/5.0.019802/18.738; U; en) Presto/2.4.15
743
	 * Opera/9.80 (J2ME/iPhone;Opera Mini/5.0.019802/886; U; ja) Presto/2.4.15
744
	 * Opera/9.80 (J2ME/iPhone;Opera Mini/5.0.019802/886; U; ja) Presto/2.4.15
745
	 * Opera/9.80 (Series 60; Opera Mini/5.1.22783/23.334; U; en) Presto/2.5.25 Version/10.54
746
	 * Opera/9.80 (BlackBerry; Opera Mini/5.1.22303/22.387; U; en) Presto/2.5.25 Version/10.54
747
	 */
748
	public static function is_opera_mini() {
749
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
750
			return false;
751
		}
752
753
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
754
755
		if ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'mini' ) !== false ) {
756
			return true;
757
		} else {
758
			return false;
759
		}
760
	}
761
762
	/**
763
	 * Detects if the current browser is Opera Mini, but not on a smart device OS(Android, iOS, etc)
764
	 * Used to send users on dumb devices to m.wor
765
	 */
766
	public static function is_opera_mini_dumb() {
767
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
768
			return false;
769
		}
770
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
771
772
		if ( self::is_opera_mini() ) {
773
			if ( strpos( $ua, 'android' ) !== false || strpos( $ua, 'iphone' ) !== false || strpos( $ua, 'ipod' ) !== false
774
			|| strpos( $ua, 'ipad' ) !== false || strpos( $ua, 'blackberry' ) !== false ) {
775
				return false;
776
			} else {
777
				return true;
778
			}
779
		} else {
780
			return false;
781
		}
782
	}
783
784
	/**
785
	 * Detects if the current browser is Opera Mobile or Mini.
786
	 * DEPRECATED: use is_opera_mobile or is_opera_mini
787
	 *
788
	 * Opera Mini 5 Beta: Opera/9.80 (J2ME/MIDP; Opera Mini/5.0.15650/756; U; en) Presto/2.2.0
789
	 * Opera Mini 8: Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)
790
	 */
791
	public static function is_OperaMobile() {
792
		_deprecated_function( __FUNCTION__, 'always', 'is_opera_mini() or is_opera_mobile()' );
793
794
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
795
			return false;
796
		}
797
798
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
799
800
		if ( strpos( $ua, 'opera' ) !== false ) {
801
			if ( ( strpos( $ua, 'mini' ) !== false ) || ( strpos( $ua, 'mobi' ) !== false ) ) {
802
				return true;
803
			} else {
804
				return false;
805
			}
806
		} else {
807
			return false;
808
		}
809
	}
810
811
	/**
812
	 * Detects if the current browser is a Windows Phone 7 device.
813
	 * ex: Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; LG; GW910)
814
	 */
815 View Code Duplication
	public static function is_WindowsPhone7() {
816
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
817
			return false;
818
		}
819
820
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
821
822
		if ( false === strpos( $ua, 'windows phone os 7' ) ) {
823
			return false;
824
		} else {
825
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
826
				return false;
827
			} else {
828
				return true;
829
			}
830
		}
831
	}
832
833
	/**
834
	 * Detects if the current browser is a Windows Phone 8 device.
835
	 * ex: Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; ARM; Touch; IEMobile/10.0; <Manufacturer>; <Device> [;<Operator>])
836
	 */
837
	public static function is_windows_phone_8() {
838
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
839
			return false;
840
		}
841
842
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
843
		if ( strpos( $ua, 'windows phone 8' ) === false ) {
844
			return false;
845
		} else {
846
			return true;
847
		}
848
	}
849
850
	/**
851
	 * Detects if the current browser is on a Palm device running the new WebOS. This EXCLUDES TouchPad.
852
	 *
853
	 * Ex1: Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pre/1.1
854
	 * Ex2: Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pixi/1.1
855
	 */
856 View Code Duplication
	public static function is_PalmWebOS() {
857
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
858
			return false;
859
		}
860
861
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
862
863
		if ( false === strpos( $ua, 'webos' ) ) {
864
			return false;
865
		} else {
866
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
867
				return false;
868
			} else {
869
				return true;
870
			}
871
		}
872
	}
873
874
	/**
875
	 * Detects if the current browser is the HP TouchPad default browser. This excludes phones wt WebOS.
876
	 *
877
	 * TouchPad Emulator: Mozilla/5.0 (hp-desktop; Linux; hpwOS/2.0; U; it-IT) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 Desktop/1.0
878
	 * TouchPad: Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0
879
	 */
880
	public static function is_TouchPad() {
881
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
882
			return false;
883
		}
884
885
		$http_user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
886
		if ( false !== strpos( $http_user_agent, 'hp-tablet' ) || false !== strpos( $http_user_agent, 'hpwos' ) || false !== strpos( $http_user_agent, 'touchpad' ) ) {
887
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
888
				return false;
889
			} else {
890
				return true;
891
			}
892
		} else {
893
			return false;
894
		}
895
	}
896
897
	/**
898
	 * Detects if the current browser is the Series 60 Open Source Browser.
899
	 *
900
	 * OSS Browser 3.2 on E75: Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2 NokiaE75-1/110.48.125 Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413
901
	 *
902
	 * 7.0 Browser (Nokia 5800 XpressMusic (v21.0.025)) : Mozilla/5.0 (SymbianOS/9.4; U; Series60/5.0 Nokia5800d-1/21.0.025; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413
903
	 *
904
	 * Browser 7.1 (Nokia N97 (v12.0.024)) : Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/12.0.024; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.12344
905
	 */
906
	public static function is_S60_OSSBrowser() {
907
908
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
909
			return false;
910
		}
911
912
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
913
		if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
914
			return false;
915
		}
916
917
		$pos_webkit = strpos( $agent, 'webkit' );
918
		if ( false !== $pos_webkit ) {
919
			// First, test for WebKit, then make sure it's either Symbian or S60.
920
			if ( strpos( $agent, 'symbian' ) !== false || strpos( $agent, 'series60' ) !== false ) {
921
					return true;
922
			} else {
923
				return false;
924
			}
925 View Code Duplication
		} elseif ( strpos( $agent, 'symbianos' ) !== false && strpos( $agent, 'series60' ) !== false ) {
926
			return true;
927
		} elseif ( strpos( $agent, 'nokia' ) !== false && strpos( $agent, 'series60' ) !== false ) {
928
			return true;
929
		}
930
931
		return false;
932
	}
933
934
	/**
935
	 * Detects if the device platform is the Symbian Series 60.
936
	 */
937
	public static function is_symbian_platform() {
938
939
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
940
			return false;
941
		}
942
943
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
944
945
		$pos_webkit = strpos( $agent, 'webkit' );
946
		if ( false !== $pos_webkit ) {
947
			// First, test for WebKit, then make sure it's either Symbian or S60.
948
			if ( strpos( $agent, 'symbian' ) !== false || strpos( $agent, 'series60' ) !== false ) {
949
					return true;
950
			} else {
951
				return false;
952
			}
953 View Code Duplication
		} elseif ( strpos( $agent, 'symbianos' ) !== false && strpos( $agent, 'series60' ) !== false ) {
954
			return true;
955
		} elseif ( strpos( $agent, 'nokia' ) !== false && strpos( $agent, 'series60' ) !== false ) {
956
			return true;
957
		} elseif ( strpos( $agent, 'opera mini' ) !== false ) {
958
			if ( strpos( $agent, 'symbianos' ) !== false || strpos( $agent, 'symbos' ) !== false || strpos( $agent, 'series 60' ) !== false ) {
959
				return true;
960
			}
961
		}
962
963
		return false;
964
	}
965
966
	/**
967
	 * Detects if the device platform is the Symbian Series 40.
968
	 * Nokia Browser for Series 40 is a proxy based browser, previously known as Ovi Browser.
969
	 * This browser will report 'NokiaBrowser' in the header, however some older version will also report 'OviBrowser'.
970
	 */
971 View Code Duplication
	public static function is_symbian_s40_platform() {
972
973
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
974
			return false;
975
		}
976
977
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
978
979
		if ( strpos( $agent, 'series40' ) !== false ) {
980
			if ( strpos( $agent, 'nokia' ) !== false || strpos( $agent, 'ovibrowser' ) !== false || strpos( $agent, 'nokiabrowser' ) !== false ) {
981
				return true;
982
			}
983
		}
984
985
		return false;
986
	}
987
988
	/**
989
	 * Returns if the device belongs to J2ME capable family.
990
	 *
991
	 * @return bool
992
	 */
993 View Code Duplication
	public static function is_J2ME_platform() {
994
995
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
996
			return false;
997
		}
998
999
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1000
1001
		if ( strpos( $agent, 'j2me/midp' ) !== false ) {
1002
			return true;
1003
		} elseif ( strpos( $agent, 'midp' ) !== false && strpos( $agent, 'cldc' ) ) {
1004
			return true;
1005
		}
1006
		return false;
1007
	}
1008
1009
	/**
1010
	 * Detects if the current UA is on one of the Maemo-based Nokia Internet Tablets.
1011
	 */
1012
	public static function is_MaemoTablet() {
1013
1014
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1015
			return false;
1016
		}
1017
1018
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1019
1020
		$pos_maemo = strpos( $agent, 'maemo' );
1021
		if ( false === $pos_maemo ) {
1022
			return false;
1023
		}
1024
1025
		// Must be Linux + Tablet, or else it could be something else.
1026 View Code Duplication
		if ( strpos( $agent, 'tablet' ) !== false && strpos( $agent, 'linux' ) !== false ) {
1027
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1028
				return false;
1029
			} else {
1030
				return true;
1031
			}
1032
		} else {
1033
			return false;
1034
		}
1035
	}
1036
1037
	/**
1038
	 * Detects if the current UA is a MeeGo device (Nokia Smartphone).
1039
	 */
1040 View Code Duplication
	public static function is_MeeGo() {
1041
1042
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1043
			return false;
1044
		}
1045
1046
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1047
1048
		if ( false === strpos( $ua, 'meego' ) ) {
1049
			return false;
1050
		} else {
1051
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1052
				return false;
1053
			} else {
1054
				return true;
1055
			}
1056
		}
1057
	}
1058
1059
	/**
1060
	 * The is_webkit() method can be used to check the User Agent for an webkit generic browser.
1061
	 */
1062 View Code Duplication
	public static function is_webkit() {
1063
1064
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1065
			return false;
1066
		}
1067
1068
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1069
1070
		$pos_webkit = strpos( $agent, 'webkit' );
1071
1072
		if ( false !== $pos_webkit ) {
1073
			return true;
1074
		} else {
1075
			return false;
1076
		}
1077
	}
1078
1079
	/**
1080
	 * Detects if the current browser is the Native Android browser.
1081
	 *
1082
	 * @return boolean true if the browser is Android otherwise false
1083
	 */
1084 View Code Duplication
	public static function is_android() {
1085
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1086
			return false;
1087
		}
1088
1089
		$agent       = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1090
		$pos_android = strpos( $agent, 'android' );
1091
		if ( false !== $pos_android ) {
1092
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1093
				return false;
1094
			} else {
1095
				return true;
1096
			}
1097
		} else {
1098
			return false;
1099
		}
1100
	}
1101
1102
	/**
1103
	 * Detects if the current browser is the Native Android Tablet browser.
1104
	 * Assumes 'Android' should be in the user agent, but not 'mobile'
1105
	 *
1106
	 * @return boolean true if the browser is Android and not 'mobile' otherwise false
1107
	 */
1108
	public static function is_android_tablet() {
1109
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1110
			return false;
1111
		}
1112
1113
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1114
1115
		$pos_android      = strpos( $agent, 'android' );
1116
		$pos_mobile       = strpos( $agent, 'mobile' );
1117
		$post_android_app = strpos( $agent, 'wp-android' );
1118
1119
		if ( false !== $pos_android && false === $pos_mobile && false === $post_android_app ) {
1120
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1121
				return false;
1122
			} else {
1123
				return true;
1124
			}
1125
		} else {
1126
			return false;
1127
		}
1128
	}
1129
1130
	/**
1131
	 * Detects if the current browser is the Kindle Fire Native browser.
1132
	 *
1133
	 * Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-84) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true
1134
	 * Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-84) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=false
1135
	 *
1136
	 * @return boolean true if the browser is Kindle Fire Native browser otherwise false
1137
	 */
1138
	public static function is_kindle_fire() {
1139
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1140
			return false;
1141
		}
1142
1143
		$agent        = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1144
		$pos_silk     = strpos( $agent, 'silk/' );
1145
		$pos_silk_acc = strpos( $agent, 'silk-accelerated=' );
1146
		if ( false !== $pos_silk && false !== $pos_silk_acc ) {
1147
			return true;
1148
		} else {
1149
			return false;
1150
		}
1151
	}
1152
1153
	/**
1154
	 * Detects if the current browser is the Kindle Touch Native browser
1155
	 *
1156
	 * Mozilla/5.0 (X11; U; Linux armv7l like Android; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/5.0 Safari/533.2+ Kindle/3.0+
1157
	 *
1158
	 * @return boolean true if the browser is Kindle monochrome Native browser otherwise false
1159
	 */
1160 View Code Duplication
	public static function is_kindle_touch() {
1161
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1162
			return false;
1163
		}
1164
		$agent            = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1165
		$pos_kindle_touch = strpos( $agent, 'kindle/3.0+' );
1166
		if ( false !== $pos_kindle_touch && false === self::is_kindle_fire() ) {
1167
			return true;
1168
		} else {
1169
			return false;
1170
		}
1171
	}
1172
1173
	/**
1174
	 * Detect if user agent is the WordPress.com Windows 8 app (used ONLY on the custom oauth stylesheet)
1175
	 */
1176 View Code Duplication
	public static function is_windows8_auth() {
1177
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1178
			return false;
1179
		}
1180
1181
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1182
		$pos   = strpos( $agent, 'msauthhost' );
1183
		if ( false !== $pos ) {
1184
			return true;
1185
		} else {
1186
			return false;
1187
		}
1188
	}
1189
1190
	/**
1191
	 * Detect if user agent is the WordPress.com Windows 8 app.
1192
	 */
1193 View Code Duplication
	public static function is_wordpress_for_win8() {
1194
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1195
			return false;
1196
		}
1197
1198
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1199
		$pos   = strpos( $agent, 'wp-windows8' );
1200
		if ( false !== $pos ) {
1201
			return true;
1202
		} else {
1203
			return false;
1204
		}
1205
	}
1206
1207
	/**
1208
	 * Detect if user agent is the WordPress.com Desktop app.
1209
	 */
1210 View Code Duplication
	public static function is_wordpress_desktop_app() {
1211
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1212
			return false;
1213
		}
1214
1215
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1216
		$pos   = strpos( $agent, 'WordPressDesktop' );
1217
		if ( false !== $pos ) {
1218
			return true;
1219
		} else {
1220
			return false;
1221
		}
1222
	}
1223
1224
	/**
1225
	 * The is_blackberry_tablet() method can be used to check the User Agent for a RIM blackberry tablet.
1226
	 * The user agent of the BlackBerry® Tablet OS follows a format similar to the following:
1227
	 * Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.8+ (KHTML, like Gecko) Version/0.0.1 Safari/534.8+
1228
	 */
1229
	public static function is_blackberry_tablet() {
1230
1231
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1232
			return false;
1233
		}
1234
1235
		$agent          = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1236
		$pos_playbook   = stripos( $agent, 'PlayBook' );
1237
		$pos_rim_tablet = stripos( $agent, 'RIM Tablet' );
1238
1239
		if ( ( false === $pos_playbook ) || ( false === $pos_rim_tablet ) ) {
1240
			return false;
1241
		} else {
1242
			return true;
1243
		}
1244
	}
1245
1246
	/**
1247
	 * The is_blackbeberry() method can be used to check the User Agent for a blackberry device.
1248
	 * Note that opera mini on BB matches this rule.
1249
	 */
1250 View Code Duplication
	public static function is_blackbeberry() {
1251
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1252
			return false;
1253
		}
1254
1255
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1256
1257
		$pos_blackberry = strpos( $agent, 'blackberry' );
1258
		if ( false !== $pos_blackberry ) {
1259
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1260
				return false;
1261
			} else {
1262
				return true;
1263
			}
1264
		} else {
1265
			return false;
1266
		}
1267
	}
1268
1269
	/**
1270
	 * The is_blackberry_10() method can be used to check the User Agent for a BlackBerry 10 device.
1271
	 */
1272
	public static function is_blackberry_10() {
1273
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1274
		return ( strpos( $agent, 'bb10' ) !== false ) && ( strpos( $agent, 'mobile' ) !== false );
1275
	}
1276
1277
	/**
1278
	 * Retrieve the blackberry OS version.
1279
	 *
1280
	 * Return strings are from the following list:
1281
	 * - blackberry-10
1282
	 * - blackberry-7
1283
	 * - blackberry-6
1284
	 * - blackberry-torch //only the first edition. The 2nd edition has the OS7 onboard and doesn't need any special rule.
1285
	 * - blackberry-5
1286
	 * - blackberry-4.7
1287
	 * - blackberry-4.6
1288
	 * - blackberry-4.5
1289
	 *
1290
	 * @return string Version of the BB OS.
1291
	 * If version is not found, get_blackbeberry_OS_version will return boolean false.
1292
	 */
1293
	public static function get_blackbeberry_OS_version() {
1294
1295
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1296
			return false;
1297
		}
1298
1299
		if ( self::is_blackberry_10() ) {
1300
			return 'blackberry-10';
1301
		}
1302
1303
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1304
1305
		$pos_blackberry = stripos( $agent, 'blackberry' );
1306
		if ( false === $pos_blackberry ) {
1307
			// Not a blackberry device.
1308
			return false;
1309
		}
1310
1311
		// Blackberry devices OS 6.0 or higher.
1312
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9670; en) AppleWebKit/534.3+ (KHTML, like Gecko) Version/6.0.0.286 Mobile Safari/534.3+.
1313
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+.
1314
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0 Mobile Safari/534.11+.
1315
		$pos_webkit = stripos( $agent, 'webkit' );
1316
		if ( false !== $pos_webkit ) {
1317
			// Detected blackberry webkit browser.
1318
			$pos_torch = stripos( $agent, 'BlackBerry 9800' );
1319
			if ( false !== $pos_torch ) {
1320
				return 'blackberry-torch'; // Match the torch first edition. the 2nd edition should use the OS7 and doesn't need any special rule.
1321
			} else {
1322
				// Detecting the BB OS version for devices running OS 6.0 or higher.
1323
				if ( preg_match( '#Version\/([\d\.]+)#i', $agent, $matches ) ) {
1324
					$version     = $matches[1];
1325
					$version_num = explode( '.', $version );
1326
					if ( false === is_array( $version_num ) || count( $version_num ) <= 1 ) {
1327
						return 'blackberry-6'; // not a BB device that match our rule.
1328
					} else {
1329
						return 'blackberry-' . $version_num[0];
1330
					}
1331
				} else {
1332
					// if doesn't match returns the minimun version with a webkit browser. we should never fall here.
1333
					return 'blackberry-6'; // not a BB device that match our rule.
1334
				}
1335
			}
1336
		}
1337
1338
		// Blackberry devices <= 5.XX.
1339
		// BlackBerry9000/5.0.0.93 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/179.
1340
		if ( preg_match( '#BlackBerry\w+\/([\d\.]+)#i', $agent, $matches ) ) {
1341
			$version = $matches[1];
1342
		} else {
1343
			return false; // not a BB device that match our rule.
1344
		}
1345
1346
		$version_num = explode( '.', $version );
1347
1348 View Code Duplication
		if ( is_array( $version_num ) === false || count( $version_num ) <= 1 ) {
1349
			return false;
1350
		}
1351
1352
		$version_num_major = (int) $version_num[0];
1353
		$version_num_minor = (int) $version_num[1];
1354
1355
		if ( 5 === $version_num_major ) {
1356
			return 'blackberry-5';
1357
		} elseif ( 4 === $version_num_major && 7 === $version_num_minor ) {
1358
			return 'blackberry-4.7';
1359
		} elseif ( 4 === $version_num_major && 6 === $version_num_minor ) {
1360
			return 'blackberry-4.6';
1361
		} elseif ( 4 === $version_num_major && 5 === $version_num_minor ) {
1362
			return 'blackberry-4.5';
1363
		} else {
1364
			return false;
1365
		}
1366
1367
	}
1368
1369
	/**
1370
	 * Retrieve the blackberry browser version.
1371
	 *
1372
	 * Return string are from the following list:
1373
	 * - blackberry-10
1374
	 * - blackberry-webkit
1375
	 * - blackberry-5
1376
	 * - blackberry-4.7
1377
	 * - blackberry-4.6
1378
	 *
1379
	 * @return string Type of the BB browser.
1380
	 * If browser's version is not found, detect_blackbeberry_browser_version will return boolean false.
1381
	 */
1382
	public static function detect_blackberry_browser_version() {
1383
1384
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1385
			return false;
1386
		}
1387
1388
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1389
1390
		if ( self::is_blackberry_10() ) {
1391
			return 'blackberry-10';
1392
		}
1393
1394
		$pos_blackberry = strpos( $agent, 'blackberry' );
1395
		if ( false === $pos_blackberry ) {
1396
			// Not a blackberry device.
1397
			return false;
1398
		}
1399
1400
		$pos_webkit = strpos( $agent, 'webkit' );
1401
1402
		if ( ! ( false === $pos_webkit ) ) {
1403
			return 'blackberry-webkit';
1404
		} else {
1405
			if ( ! preg_match( '#BlackBerry\w+\/([\d\.]+)#i', $agent, $matches ) ) {
1406
				return false; // not a BB device that match our rule.
1407
			}
1408
1409
			$version_num = explode( '.', $matches[1] );
1410
1411 View Code Duplication
			if ( false === is_array( $version_num ) || count( $version_num ) <= 1 ) {
1412
				return false;
1413
			}
1414
1415
			$version_num_major = (int) $version_num[0];
1416
			$version_num_minor = (int) $version_num[1];
1417
1418
			if ( 5 === $version_num_major ) {
1419
				return 'blackberry-5';
1420
			} elseif ( 4 === $version_num_major && 7 === $version_num_minor ) {
1421
				return 'blackberry-4.7';
1422
			} elseif ( 4 === $version_num_major && 6 === $version_num_minor ) {
1423
				return 'blackberry-4.6';
1424
			} else {
1425
				// A very old BB device is found or this is a BB device that doesn't match our rules.
1426
				return false;
1427
			}
1428
		}
1429
1430
	}
1431
1432
	/**
1433
	 * Checks if a visitor is coming from one of the WordPress mobile apps.
1434
	 *
1435
	 * @return bool
1436
	 */
1437
	public static function is_mobile_app() {
1438
1439
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1440
			return false;
1441
		}
1442
1443
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1444
1445
		if ( isset( $_SERVER['X_USER_AGENT'] ) && preg_match( '|wp-webos|', $_SERVER['X_USER_AGENT'] ) ) {
1446
			return true; // Wp4webos 1.1 or higher.
1447
		}
1448
1449
		$app_agents = array( 'wp-android', 'wp-blackberry', 'wp-iphone', 'wp-nokia', 'wp-webos', 'wp-windowsphone' );
1450
		// the mobile reader on iOS has an incorrect UA when loading the reader
1451
		// currently it is the default one provided by the iOS framework which
1452
		// causes problems with 2-step-auth
1453
		// User-Agent	WordPress/3.1.4 CFNetwork/609 Darwin/13.0.0.
1454
		$app_agents[] = 'wordpress/3.1';
1455
1456
		foreach ( $app_agents as $app_agent ) {
1457
			if ( false !== strpos( $agent, $app_agent ) ) {
1458
				return true;
1459
			}
1460
		}
1461
		return false;
1462
	}
1463
1464
	/**
1465
	 * Detects if the current browser is Nintendo 3DS handheld.
1466
	 *
1467
	 * Example: Mozilla/5.0 (Nintendo 3DS; U; ; en) Version/1.7498.US
1468
	 * can differ in language, version and region
1469
	 */
1470
	public static function is_Nintendo_3DS() {
1471
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1472
			return false;
1473
		}
1474
1475
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1476
		if ( strpos( $ua, 'nintendo 3ds' ) !== false ) {
1477
			return true;
1478
		}
1479
		return false;
1480
	}
1481
1482
	/**
1483
	 * Was the current request made by a known bot?
1484
	 *
1485
	 * @return boolean
1486
	 */
1487
	public static function is_bot() {
1488
		static $is_bot = null;
1489
1490
		if ( is_null( $is_bot ) ) {
1491
			$is_bot = self::is_bot_user_agent( $_SERVER['HTTP_USER_AGENT'] );
1492
		}
1493
1494
		return $is_bot;
1495
	}
1496
1497
	/**
1498
	 * Is the given user-agent a known bot?
1499
	 * If you want an is_bot check for the current request's UA, use is_bot() instead of passing a user-agent to this method.
1500
	 *
1501
	 * @param string $ua A user-agent string.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $ua not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1502
	 *
1503
	 * @return boolean
1504
	 */
1505
	public static function is_bot_user_agent( $ua = null ) {
1506
1507
		if ( empty( $ua ) ) {
1508
			return false;
1509
		}
1510
1511
		$bot_agents = array(
1512
			'alexa',
1513
			'altavista',
1514
			'ask jeeves',
1515
			'attentio',
1516
			'baiduspider',
1517
			'bingbot',
1518
			'chtml generic',
1519
			'crawler',
1520
			'fastmobilecrawl',
1521
			'feedfetcher-google',
1522
			'firefly',
1523
			'froogle',
1524
			'gigabot',
1525
			'googlebot',
1526
			'googlebot-mobile',
1527
			'heritrix',
1528
			'httrack',
1529
			'ia_archiver',
1530
			'irlbot',
1531
			'iescholar',
1532
			'infoseek',
1533
			'jumpbot',
1534
			'linkcheck',
1535
			'lycos',
1536
			'mediapartners',
1537
			'mediobot',
1538
			'motionbot',
1539
			'msnbot',
1540
			'mshots',
1541
			'openbot',
1542
			'pss-webkit-request',
1543
			'pythumbnail',
1544
			'scooter',
1545
			'slurp',
1546
			'snapbot',
1547
			'spider',
1548
			'taptubot',
1549
			'technoratisnoop',
1550
			'teoma',
1551
			'twiceler',
1552
			'yahooseeker',
1553
			'yahooysmcm',
1554
			'yammybot',
1555
			'ahrefsbot',
1556
			'pingdom.com_bot',
1557
			'kraken',
1558
			'yandexbot',
1559
			'twitterbot',
1560
			'tweetmemebot',
1561
			'openhosebot',
1562
			'queryseekerspider',
1563
			'linkdexbot',
1564
			'grokkit-crawler',
1565
			'livelapbot',
1566
			'germcrawler',
1567
			'domaintunocrawler',
1568
			'grapeshotcrawler',
1569
			'cloudflare-alwaysonline',
1570
		);
1571
1572
		foreach ( $bot_agents as $bot_agent ) {
1573
			if ( false !== stripos( $ua, $bot_agent ) ) {
1574
				return true;
1575
			}
1576
		}
1577
1578
		return false;
1579
	}
1580
}
1581