Completed
Push — add/search-set-woo-result-form... ( 67cdd7...ac37c7 )
by Jeremy
11:32
created

User_Agent_Info::is_opera_desktop()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
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
	 * Detect modern Opera desktop
711
	 *
712
	 * Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 OPR/74.0.3911.203
713
	 *
714
	 * Looking for "OPR/" specifically.
715
	 */
716 View Code Duplication
	public static function is_opera_desktop() {
717
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
718
			return false;
719
		}
720
721
		if ( false === strpos( $_SERVER['HTTP_USER_AGENT'], 'OPR/' ) ) {
722
			return false;
723
		}
724
725
		return true;
726
	}
727
728
	/**
729
	 * Detects if the current browser is Opera Mobile
730
	 *
731
	 * What is the difference between Opera Mobile and Opera Mini?
732
	 * - Opera Mobile is a full Internet browser for mobile devices.
733
	 * - Opera Mini always uses a transcoder to convert the page for a small display.
734
	 * (it uses Opera advanced server compression technology to compress web content before it gets to a device.
735
	 *  The rendering engine is on Opera's server.)
736
	 *
737
	 * Opera/9.80 (Windows NT 6.1; Opera Mobi/14316; U; en) Presto/2.7.81 Version/11.00"
738
	 * Opera/9.50 (Nintendo DSi; Opera/507; U; en-US)
739
	 */
740 View Code Duplication
	public static function is_opera_mobile() {
741
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
742
			return false;
743
		}
744
745
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
746
747
		if ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'mobi' ) !== false ) {
748
			return true;
749
		} elseif ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'nintendo dsi' ) !== false ) {
750
			return true;
751
		} else {
752
			return false;
753
		}
754
	}
755
756
	/**
757
	 * Detects if the current browser is Opera Mini
758
	 *
759
	 * Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)
760
	 * Opera/9.80 (Android;Opera Mini/6.0.24212/24.746 U;en) Presto/2.5.25 Version/10.5454
761
	 * Opera/9.80 (iPhone; Opera Mini/5.0.019802/18.738; U; en) Presto/2.4.15
762
	 * Opera/9.80 (J2ME/iPhone;Opera Mini/5.0.019802/886; U; ja) Presto/2.4.15
763
	 * Opera/9.80 (J2ME/iPhone;Opera Mini/5.0.019802/886; U; ja) Presto/2.4.15
764
	 * Opera/9.80 (Series 60; Opera Mini/5.1.22783/23.334; U; en) Presto/2.5.25 Version/10.54
765
	 * Opera/9.80 (BlackBerry; Opera Mini/5.1.22303/22.387; U; en) Presto/2.5.25 Version/10.54
766
	 */
767
	public static function is_opera_mini() {
768
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
769
			return false;
770
		}
771
772
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
773
774
		if ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'mini' ) !== false ) {
775
			return true;
776
		} else {
777
			return false;
778
		}
779
	}
780
781
	/**
782
	 * Detects if the current browser is Opera Mini, but not on a smart device OS(Android, iOS, etc)
783
	 * Used to send users on dumb devices to m.wor
784
	 */
785
	public static function is_opera_mini_dumb() {
786
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
787
			return false;
788
		}
789
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
790
791
		if ( self::is_opera_mini() ) {
792
			if ( strpos( $ua, 'android' ) !== false || strpos( $ua, 'iphone' ) !== false || strpos( $ua, 'ipod' ) !== false
793
			|| strpos( $ua, 'ipad' ) !== false || strpos( $ua, 'blackberry' ) !== false ) {
794
				return false;
795
			} else {
796
				return true;
797
			}
798
		} else {
799
			return false;
800
		}
801
	}
802
803
	/**
804
	 * Detects if the current browser is Opera Mobile or Mini.
805
	 * DEPRECATED: use is_opera_mobile or is_opera_mini
806
	 *
807
	 * Opera Mini 5 Beta: Opera/9.80 (J2ME/MIDP; Opera Mini/5.0.15650/756; U; en) Presto/2.2.0
808
	 * Opera Mini 8: Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)
809
	 */
810
	public static function is_OperaMobile() {
811
		_deprecated_function( __FUNCTION__, 'always', 'is_opera_mini() or is_opera_mobile()' );
812
813
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
814
			return false;
815
		}
816
817
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
818
819
		if ( strpos( $ua, 'opera' ) !== false ) {
820
			if ( ( strpos( $ua, 'mini' ) !== false ) || ( strpos( $ua, 'mobi' ) !== false ) ) {
821
				return true;
822
			} else {
823
				return false;
824
			}
825
		} else {
826
			return false;
827
		}
828
	}
829
830
	/**
831
	 * Detects if the current browser is a Windows Phone 7 device.
832
	 * ex: Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; LG; GW910)
833
	 */
834 View Code Duplication
	public static function is_WindowsPhone7() {
835
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
836
			return false;
837
		}
838
839
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
840
841
		if ( false === strpos( $ua, 'windows phone os 7' ) ) {
842
			return false;
843
		} else {
844
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
845
				return false;
846
			} else {
847
				return true;
848
			}
849
		}
850
	}
851
852
	/**
853
	 * Detects if the current browser is a Windows Phone 8 device.
854
	 * ex: Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; ARM; Touch; IEMobile/10.0; <Manufacturer>; <Device> [;<Operator>])
855
	 */
856 View Code Duplication
	public static function is_windows_phone_8() {
857
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
858
			return false;
859
		}
860
861
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
862
		if ( strpos( $ua, 'windows phone 8' ) === false ) {
863
			return false;
864
		} else {
865
			return true;
866
		}
867
	}
868
869
	/**
870
	 * Detects if the current browser is on a Palm device running the new WebOS. This EXCLUDES TouchPad.
871
	 *
872
	 * 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
873
	 * 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
874
	 */
875 View Code Duplication
	public static function is_PalmWebOS() {
876
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
877
			return false;
878
		}
879
880
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
881
882
		if ( false === strpos( $ua, 'webos' ) ) {
883
			return false;
884
		} else {
885
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
886
				return false;
887
			} else {
888
				return true;
889
			}
890
		}
891
	}
892
893
	/**
894
	 * Detects if the current browser is the HP TouchPad default browser. This excludes phones wt WebOS.
895
	 *
896
	 * 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
897
	 * 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
898
	 */
899
	public static function is_TouchPad() {
900
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
901
			return false;
902
		}
903
904
		$http_user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
905
		if ( false !== strpos( $http_user_agent, 'hp-tablet' ) || false !== strpos( $http_user_agent, 'hpwos' ) || false !== strpos( $http_user_agent, 'touchpad' ) ) {
906
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
907
				return false;
908
			} else {
909
				return true;
910
			}
911
		} else {
912
			return false;
913
		}
914
	}
915
916
	/**
917
	 * Detects if the current browser is the Series 60 Open Source Browser.
918
	 *
919
	 * 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
920
	 *
921
	 * 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
922
	 *
923
	 * 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
924
	 */
925
	public static function is_S60_OSSBrowser() {
926
927
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
928
			return false;
929
		}
930
931
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
932
		if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
933
			return false;
934
		}
935
936
		$pos_webkit = strpos( $agent, 'webkit' );
937
		if ( false !== $pos_webkit ) {
938
			// First, test for WebKit, then make sure it's either Symbian or S60.
939
			if ( strpos( $agent, 'symbian' ) !== false || strpos( $agent, 'series60' ) !== false ) {
940
					return true;
941
			} else {
942
				return false;
943
			}
944 View Code Duplication
		} elseif ( strpos( $agent, 'symbianos' ) !== false && strpos( $agent, 'series60' ) !== false ) {
945
			return true;
946
		} elseif ( strpos( $agent, 'nokia' ) !== false && strpos( $agent, 'series60' ) !== false ) {
947
			return true;
948
		}
949
950
		return false;
951
	}
952
953
	/**
954
	 * Detects if the device platform is the Symbian Series 60.
955
	 */
956
	public static function is_symbian_platform() {
957
958
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
959
			return false;
960
		}
961
962
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
963
964
		$pos_webkit = strpos( $agent, 'webkit' );
965
		if ( false !== $pos_webkit ) {
966
			// First, test for WebKit, then make sure it's either Symbian or S60.
967
			if ( strpos( $agent, 'symbian' ) !== false || strpos( $agent, 'series60' ) !== false ) {
968
					return true;
969
			} else {
970
				return false;
971
			}
972 View Code Duplication
		} elseif ( strpos( $agent, 'symbianos' ) !== false && strpos( $agent, 'series60' ) !== false ) {
973
			return true;
974
		} elseif ( strpos( $agent, 'nokia' ) !== false && strpos( $agent, 'series60' ) !== false ) {
975
			return true;
976
		} elseif ( strpos( $agent, 'opera mini' ) !== false ) {
977
			if ( strpos( $agent, 'symbianos' ) !== false || strpos( $agent, 'symbos' ) !== false || strpos( $agent, 'series 60' ) !== false ) {
978
				return true;
979
			}
980
		}
981
982
		return false;
983
	}
984
985
	/**
986
	 * Detects if the device platform is the Symbian Series 40.
987
	 * Nokia Browser for Series 40 is a proxy based browser, previously known as Ovi Browser.
988
	 * This browser will report 'NokiaBrowser' in the header, however some older version will also report 'OviBrowser'.
989
	 */
990 View Code Duplication
	public static function is_symbian_s40_platform() {
991
992
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
993
			return false;
994
		}
995
996
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
997
998
		if ( strpos( $agent, 'series40' ) !== false ) {
999
			if ( strpos( $agent, 'nokia' ) !== false || strpos( $agent, 'ovibrowser' ) !== false || strpos( $agent, 'nokiabrowser' ) !== false ) {
1000
				return true;
1001
			}
1002
		}
1003
1004
		return false;
1005
	}
1006
1007
	/**
1008
	 * Returns if the device belongs to J2ME capable family.
1009
	 *
1010
	 * @return bool
1011
	 */
1012 View Code Duplication
	public static function is_J2ME_platform() {
1013
1014
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1015
			return false;
1016
		}
1017
1018
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1019
1020
		if ( strpos( $agent, 'j2me/midp' ) !== false ) {
1021
			return true;
1022
		} elseif ( strpos( $agent, 'midp' ) !== false && strpos( $agent, 'cldc' ) ) {
1023
			return true;
1024
		}
1025
		return false;
1026
	}
1027
1028
	/**
1029
	 * Detects if the current UA is on one of the Maemo-based Nokia Internet Tablets.
1030
	 */
1031
	public static function is_MaemoTablet() {
1032
1033
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1034
			return false;
1035
		}
1036
1037
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1038
1039
		$pos_maemo = strpos( $agent, 'maemo' );
1040
		if ( false === $pos_maemo ) {
1041
			return false;
1042
		}
1043
1044
		// Must be Linux + Tablet, or else it could be something else.
1045 View Code Duplication
		if ( strpos( $agent, 'tablet' ) !== false && strpos( $agent, 'linux' ) !== false ) {
1046
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1047
				return false;
1048
			} else {
1049
				return true;
1050
			}
1051
		} else {
1052
			return false;
1053
		}
1054
	}
1055
1056
	/**
1057
	 * Detects if the current UA is a MeeGo device (Nokia Smartphone).
1058
	 */
1059 View Code Duplication
	public static function is_MeeGo() {
1060
1061
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1062
			return false;
1063
		}
1064
1065
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1066
1067
		if ( false === strpos( $ua, 'meego' ) ) {
1068
			return false;
1069
		} else {
1070
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1071
				return false;
1072
			} else {
1073
				return true;
1074
			}
1075
		}
1076
	}
1077
1078
	/**
1079
	 * The is_webkit() method can be used to check the User Agent for an webkit generic browser.
1080
	 */
1081 View Code Duplication
	public static function is_webkit() {
1082
1083
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1084
			return false;
1085
		}
1086
1087
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1088
1089
		$pos_webkit = strpos( $agent, 'webkit' );
1090
1091
		if ( false !== $pos_webkit ) {
1092
			return true;
1093
		} else {
1094
			return false;
1095
		}
1096
	}
1097
1098
	/**
1099
	 * Detects if the current browser is the Native Android browser.
1100
	 *
1101
	 * @return boolean true if the browser is Android otherwise false
1102
	 */
1103 View Code Duplication
	public static function is_android() {
1104
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1105
			return false;
1106
		}
1107
1108
		$agent       = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1109
		$pos_android = strpos( $agent, 'android' );
1110
		if ( false !== $pos_android ) {
1111
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1112
				return false;
1113
			} else {
1114
				return true;
1115
			}
1116
		} else {
1117
			return false;
1118
		}
1119
	}
1120
1121
	/**
1122
	 * Detects if the current browser is the Native Android Tablet browser.
1123
	 * Assumes 'Android' should be in the user agent, but not 'mobile'
1124
	 *
1125
	 * @return boolean true if the browser is Android and not 'mobile' otherwise false
1126
	 */
1127
	public static function is_android_tablet() {
1128
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1129
			return false;
1130
		}
1131
1132
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1133
1134
		$pos_android      = strpos( $agent, 'android' );
1135
		$pos_mobile       = strpos( $agent, 'mobile' );
1136
		$post_android_app = strpos( $agent, 'wp-android' );
1137
1138
		if ( false !== $pos_android && false === $pos_mobile && false === $post_android_app ) {
1139
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1140
				return false;
1141
			} else {
1142
				return true;
1143
			}
1144
		} else {
1145
			return false;
1146
		}
1147
	}
1148
1149
	/**
1150
	 * Detects if the current browser is the Kindle Fire Native browser.
1151
	 *
1152
	 * 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
1153
	 * 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
1154
	 *
1155
	 * @return boolean true if the browser is Kindle Fire Native browser otherwise false
1156
	 */
1157
	public static function is_kindle_fire() {
1158
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1159
			return false;
1160
		}
1161
1162
		$agent        = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1163
		$pos_silk     = strpos( $agent, 'silk/' );
1164
		$pos_silk_acc = strpos( $agent, 'silk-accelerated=' );
1165
		if ( false !== $pos_silk && false !== $pos_silk_acc ) {
1166
			return true;
1167
		} else {
1168
			return false;
1169
		}
1170
	}
1171
1172
	/**
1173
	 * Detects if the current browser is the Kindle Touch Native browser
1174
	 *
1175
	 * 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+
1176
	 *
1177
	 * @return boolean true if the browser is Kindle monochrome Native browser otherwise false
1178
	 */
1179 View Code Duplication
	public static function is_kindle_touch() {
1180
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1181
			return false;
1182
		}
1183
		$agent            = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1184
		$pos_kindle_touch = strpos( $agent, 'kindle/3.0+' );
1185
		if ( false !== $pos_kindle_touch && false === self::is_kindle_fire() ) {
1186
			return true;
1187
		} else {
1188
			return false;
1189
		}
1190
	}
1191
1192
	/**
1193
	 * Detect if user agent is the WordPress.com Windows 8 app (used ONLY on the custom oauth stylesheet)
1194
	 */
1195 View Code Duplication
	public static function is_windows8_auth() {
1196
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1197
			return false;
1198
		}
1199
1200
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1201
		$pos   = strpos( $agent, 'msauthhost' );
1202
		if ( false !== $pos ) {
1203
			return true;
1204
		} else {
1205
			return false;
1206
		}
1207
	}
1208
1209
	/**
1210
	 * Detect if user agent is the WordPress.com Windows 8 app.
1211
	 */
1212 View Code Duplication
	public static function is_wordpress_for_win8() {
1213
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1214
			return false;
1215
		}
1216
1217
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1218
		$pos   = strpos( $agent, 'wp-windows8' );
1219
		if ( false !== $pos ) {
1220
			return true;
1221
		} else {
1222
			return false;
1223
		}
1224
	}
1225
1226
	/**
1227
	 * Detect if user agent is the WordPress.com Desktop app.
1228
	 */
1229 View Code Duplication
	public static function is_wordpress_desktop_app() {
1230
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1231
			return false;
1232
		}
1233
1234
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1235
		$pos   = strpos( $agent, 'WordPressDesktop' );
1236
		if ( false !== $pos ) {
1237
			return true;
1238
		} else {
1239
			return false;
1240
		}
1241
	}
1242
1243
	/**
1244
	 * The is_blackberry_tablet() method can be used to check the User Agent for a RIM blackberry tablet.
1245
	 * The user agent of the BlackBerry® Tablet OS follows a format similar to the following:
1246
	 * 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+
1247
	 */
1248
	public static function is_blackberry_tablet() {
1249
1250
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1251
			return false;
1252
		}
1253
1254
		$agent          = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1255
		$pos_playbook   = stripos( $agent, 'PlayBook' );
1256
		$pos_rim_tablet = stripos( $agent, 'RIM Tablet' );
1257
1258
		if ( ( false === $pos_playbook ) || ( false === $pos_rim_tablet ) ) {
1259
			return false;
1260
		} else {
1261
			return true;
1262
		}
1263
	}
1264
1265
	/**
1266
	 * The is_blackbeberry() method can be used to check the User Agent for a blackberry device.
1267
	 * Note that opera mini on BB matches this rule.
1268
	 */
1269 View Code Duplication
	public static function is_blackbeberry() {
1270
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1271
			return false;
1272
		}
1273
1274
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1275
1276
		$pos_blackberry = strpos( $agent, 'blackberry' );
1277
		if ( false !== $pos_blackberry ) {
1278
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1279
				return false;
1280
			} else {
1281
				return true;
1282
			}
1283
		} else {
1284
			return false;
1285
		}
1286
	}
1287
1288
	/**
1289
	 * The is_blackberry_10() method can be used to check the User Agent for a BlackBerry 10 device.
1290
	 */
1291
	public static function is_blackberry_10() {
1292
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1293
		return ( strpos( $agent, 'bb10' ) !== false ) && ( strpos( $agent, 'mobile' ) !== false );
1294
	}
1295
1296
	/**
1297
	 * Retrieve the blackberry OS version.
1298
	 *
1299
	 * Return strings are from the following list:
1300
	 * - blackberry-10
1301
	 * - blackberry-7
1302
	 * - blackberry-6
1303
	 * - blackberry-torch //only the first edition. The 2nd edition has the OS7 onboard and doesn't need any special rule.
1304
	 * - blackberry-5
1305
	 * - blackberry-4.7
1306
	 * - blackberry-4.6
1307
	 * - blackberry-4.5
1308
	 *
1309
	 * @return string Version of the BB OS.
1310
	 * If version is not found, get_blackbeberry_OS_version will return boolean false.
1311
	 */
1312
	public static function get_blackbeberry_OS_version() {
1313
1314
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1315
			return false;
1316
		}
1317
1318
		if ( self::is_blackberry_10() ) {
1319
			return 'blackberry-10';
1320
		}
1321
1322
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1323
1324
		$pos_blackberry = stripos( $agent, 'blackberry' );
1325
		if ( false === $pos_blackberry ) {
1326
			// Not a blackberry device.
1327
			return false;
1328
		}
1329
1330
		// Blackberry devices OS 6.0 or higher.
1331
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9670; en) AppleWebKit/534.3+ (KHTML, like Gecko) Version/6.0.0.286 Mobile Safari/534.3+.
1332
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+.
1333
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0 Mobile Safari/534.11+.
1334
		$pos_webkit = stripos( $agent, 'webkit' );
1335
		if ( false !== $pos_webkit ) {
1336
			// Detected blackberry webkit browser.
1337
			$pos_torch = stripos( $agent, 'BlackBerry 9800' );
1338
			if ( false !== $pos_torch ) {
1339
				return 'blackberry-torch'; // Match the torch first edition. the 2nd edition should use the OS7 and doesn't need any special rule.
1340
			} else {
1341
				// Detecting the BB OS version for devices running OS 6.0 or higher.
1342
				if ( preg_match( '#Version\/([\d\.]+)#i', $agent, $matches ) ) {
1343
					$version     = $matches[1];
1344
					$version_num = explode( '.', $version );
1345
					if ( false === is_array( $version_num ) || count( $version_num ) <= 1 ) {
1346
						return 'blackberry-6'; // not a BB device that match our rule.
1347
					} else {
1348
						return 'blackberry-' . $version_num[0];
1349
					}
1350
				} else {
1351
					// if doesn't match returns the minimun version with a webkit browser. we should never fall here.
1352
					return 'blackberry-6'; // not a BB device that match our rule.
1353
				}
1354
			}
1355
		}
1356
1357
		// Blackberry devices <= 5.XX.
1358
		// BlackBerry9000/5.0.0.93 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/179.
1359
		if ( preg_match( '#BlackBerry\w+\/([\d\.]+)#i', $agent, $matches ) ) {
1360
			$version = $matches[1];
1361
		} else {
1362
			return false; // not a BB device that match our rule.
1363
		}
1364
1365
		$version_num = explode( '.', $version );
1366
1367 View Code Duplication
		if ( is_array( $version_num ) === false || count( $version_num ) <= 1 ) {
1368
			return false;
1369
		}
1370
1371
		$version_num_major = (int) $version_num[0];
1372
		$version_num_minor = (int) $version_num[1];
1373
1374
		if ( 5 === $version_num_major ) {
1375
			return 'blackberry-5';
1376
		} elseif ( 4 === $version_num_major && 7 === $version_num_minor ) {
1377
			return 'blackberry-4.7';
1378
		} elseif ( 4 === $version_num_major && 6 === $version_num_minor ) {
1379
			return 'blackberry-4.6';
1380
		} elseif ( 4 === $version_num_major && 5 === $version_num_minor ) {
1381
			return 'blackberry-4.5';
1382
		} else {
1383
			return false;
1384
		}
1385
1386
	}
1387
1388
	/**
1389
	 * Retrieve the blackberry browser version.
1390
	 *
1391
	 * Return string are from the following list:
1392
	 * - blackberry-10
1393
	 * - blackberry-webkit
1394
	 * - blackberry-5
1395
	 * - blackberry-4.7
1396
	 * - blackberry-4.6
1397
	 *
1398
	 * @return string Type of the BB browser.
1399
	 * If browser's version is not found, detect_blackbeberry_browser_version will return boolean false.
1400
	 */
1401
	public static function detect_blackberry_browser_version() {
1402
1403
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1404
			return false;
1405
		}
1406
1407
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1408
1409
		if ( self::is_blackberry_10() ) {
1410
			return 'blackberry-10';
1411
		}
1412
1413
		$pos_blackberry = strpos( $agent, 'blackberry' );
1414
		if ( false === $pos_blackberry ) {
1415
			// Not a blackberry device.
1416
			return false;
1417
		}
1418
1419
		$pos_webkit = strpos( $agent, 'webkit' );
1420
1421
		if ( ! ( false === $pos_webkit ) ) {
1422
			return 'blackberry-webkit';
1423
		} else {
1424
			if ( ! preg_match( '#BlackBerry\w+\/([\d\.]+)#i', $agent, $matches ) ) {
1425
				return false; // not a BB device that match our rule.
1426
			}
1427
1428
			$version_num = explode( '.', $matches[1] );
1429
1430 View Code Duplication
			if ( false === is_array( $version_num ) || count( $version_num ) <= 1 ) {
1431
				return false;
1432
			}
1433
1434
			$version_num_major = (int) $version_num[0];
1435
			$version_num_minor = (int) $version_num[1];
1436
1437
			if ( 5 === $version_num_major ) {
1438
				return 'blackberry-5';
1439
			} elseif ( 4 === $version_num_major && 7 === $version_num_minor ) {
1440
				return 'blackberry-4.7';
1441
			} elseif ( 4 === $version_num_major && 6 === $version_num_minor ) {
1442
				return 'blackberry-4.6';
1443
			} else {
1444
				// A very old BB device is found or this is a BB device that doesn't match our rules.
1445
				return false;
1446
			}
1447
		}
1448
1449
	}
1450
1451
	/**
1452
	 * Checks if a visitor is coming from one of the WordPress mobile apps.
1453
	 *
1454
	 * @return bool
1455
	 */
1456
	public static function is_mobile_app() {
1457
1458
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1459
			return false;
1460
		}
1461
1462
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1463
1464
		if ( isset( $_SERVER['X_USER_AGENT'] ) && preg_match( '|wp-webos|', $_SERVER['X_USER_AGENT'] ) ) {
1465
			return true; // Wp4webos 1.1 or higher.
1466
		}
1467
1468
		$app_agents = array( 'wp-android', 'wp-blackberry', 'wp-iphone', 'wp-nokia', 'wp-webos', 'wp-windowsphone' );
1469
		// the mobile reader on iOS has an incorrect UA when loading the reader
1470
		// currently it is the default one provided by the iOS framework which
1471
		// causes problems with 2-step-auth
1472
		// User-Agent	WordPress/3.1.4 CFNetwork/609 Darwin/13.0.0.
1473
		$app_agents[] = 'wordpress/3.1';
1474
1475
		foreach ( $app_agents as $app_agent ) {
1476
			if ( false !== strpos( $agent, $app_agent ) ) {
1477
				return true;
1478
			}
1479
		}
1480
		return false;
1481
	}
1482
1483
	/**
1484
	 * Detects if the current browser is Nintendo 3DS handheld.
1485
	 *
1486
	 * Example: Mozilla/5.0 (Nintendo 3DS; U; ; en) Version/1.7498.US
1487
	 * can differ in language, version and region
1488
	 */
1489 View Code Duplication
	public static function is_Nintendo_3DS() {
1490
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1491
			return false;
1492
		}
1493
1494
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1495
		if ( strpos( $ua, 'nintendo 3ds' ) !== false ) {
1496
			return true;
1497
		}
1498
		return false;
1499
	}
1500
1501
	/**
1502
	 * Was the current request made by a known bot?
1503
	 *
1504
	 * @return boolean
1505
	 */
1506
	public static function is_bot() {
1507
		static $is_bot = null;
1508
1509
		if ( is_null( $is_bot ) ) {
1510
			$is_bot = self::is_bot_user_agent( $_SERVER['HTTP_USER_AGENT'] );
1511
		}
1512
1513
		return $is_bot;
1514
	}
1515
1516
	/**
1517
	 * Is the given user-agent a known bot?
1518
	 * 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.
1519
	 *
1520
	 * @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...
1521
	 *
1522
	 * @return boolean
1523
	 */
1524
	public static function is_bot_user_agent( $ua = null ) {
1525
1526
		if ( empty( $ua ) ) {
1527
			return false;
1528
		}
1529
1530
		$bot_agents = array(
1531
			'alexa',
1532
			'altavista',
1533
			'ask jeeves',
1534
			'attentio',
1535
			'baiduspider',
1536
			'bingbot',
1537
			'chtml generic',
1538
			'crawler',
1539
			'fastmobilecrawl',
1540
			'feedfetcher-google',
1541
			'firefly',
1542
			'froogle',
1543
			'gigabot',
1544
			'googlebot',
1545
			'googlebot-mobile',
1546
			'heritrix',
1547
			'httrack',
1548
			'ia_archiver',
1549
			'irlbot',
1550
			'iescholar',
1551
			'infoseek',
1552
			'jumpbot',
1553
			'linkcheck',
1554
			'lycos',
1555
			'mediapartners',
1556
			'mediobot',
1557
			'motionbot',
1558
			'msnbot',
1559
			'mshots',
1560
			'openbot',
1561
			'pss-webkit-request',
1562
			'pythumbnail',
1563
			'scooter',
1564
			'slurp',
1565
			'snapbot',
1566
			'spider',
1567
			'taptubot',
1568
			'technoratisnoop',
1569
			'teoma',
1570
			'twiceler',
1571
			'yahooseeker',
1572
			'yahooysmcm',
1573
			'yammybot',
1574
			'ahrefsbot',
1575
			'pingdom.com_bot',
1576
			'kraken',
1577
			'yandexbot',
1578
			'twitterbot',
1579
			'tweetmemebot',
1580
			'openhosebot',
1581
			'queryseekerspider',
1582
			'linkdexbot',
1583
			'grokkit-crawler',
1584
			'livelapbot',
1585
			'germcrawler',
1586
			'domaintunocrawler',
1587
			'grapeshotcrawler',
1588
			'cloudflare-alwaysonline',
1589
		);
1590
1591
		foreach ( $bot_agents as $bot_agent ) {
1592
			if ( false !== stripos( $ua, $bot_agent ) ) {
1593
				return true;
1594
			}
1595
		}
1596
1597
		return false;
1598
	}
1599
}
1600