Completed
Push — renovate/url-polyfill-1.x ( 01368d...f2aaf6 )
by
unknown
114:05 queued 106:27
created

User_Agent_Info   F

Complexity

Total Complexity 341

Size/Duplication

Total Lines 1575
Duplicated Lines 20.89 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 329
loc 1575
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_iphone_or_ipod() 0 17 7
A is_chrome_for_iOS() 0 17 4
A is_twitter_for_iphone() 0 17 4
B is_facebook_for_iphone() 0 21 9
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
B is_opera_mini_dumb() 0 17 8
A is_windows_phone_8() 0 12 3
B is_TouchPad() 0 16 8
C is_S60_OSSBrowser() 3 27 12
C is_symbian_platform() 3 28 13
A is_J2ME_platform() 15 15 5
A is_webkit() 16 16 3
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_blackberry_tablet() 0 16 4
A is_blackberry_10() 0 4 2
D get_blackbeberry_OS_version() 3 75 19
C detect_blackberry_browser_version() 3 51 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
B is_iphoneOrIpod() 9 16 7
A is_twitter_for_ipad() 0 15 5
A is_facebook_for_ipad() 17 17 6
A is_firefox_os() 14 14 6
A is_opera_mobile() 15 15 6
A is_opera_mini() 0 13 4
A is_OperaMobile() 0 19 5
A is_WindowsPhone7() 17 17 6
A is_PalmWebOS() 17 17 6
A is_symbian_s40_platform() 16 16 6
B is_MaemoTablet() 9 24 8
A is_MeeGo() 18 18 6
A is_android() 17 17 6
A is_wordpress_desktop_app() 13 13 3
A is_blackbeberry() 18 18 6

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