-
Notifications
You must be signed in to change notification settings - Fork 225
/
class-acceleratedmobilepagesfix.php
75 lines (67 loc) · 2.09 KB
/
class-acceleratedmobilepagesfix.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Accelerated Mobile Pages Fix
*
* @link https://docs.pantheon.io/plugins-known-issues#amp-for-wp--accelerated-mobile-pages
* @package Pantheon\Compatibility\Fixes
*/
namespace Pantheon\Compatibility\Fixes;
use function defined;
use function extension_loaded;
/**
* Accelerated Mobile Pages Fix
*
* @package Pantheon\Compatibility\Fixes
*/
class AcceleratedMobilePagesFix {
/**
* @return void
*/
public static function apply() {
SelfUpdatingThemesFix::apply();
global $redux_builder_amp;
// Force disabling AMP mobile redirection.
$redux_builder_amp['amp-mobile-redirection'] = 0;
if ( ! self::globals_exist() || ! self::is_mobile() ) {
return;
}
http_response_code( 301 );
$protocol = ( 0 === stripos( $_SERVER['SERVER_PROTOCOL'], 'https' ) ) ? 'https://' : 'http://';
$redirect_header_value = $protocol . PANTHEON_HOSTNAME . $_SERVER['REQUEST_URI'] . '/amp';
header( 'Location: ' . $redirect_header_value );
// Name transaction "redirect" in New Relic for improved reporting.
if ( extension_loaded( 'newrelic' ) ) {
newrelic_name_transaction( 'redirect' );
}
exit();
}
/**
* Check if the required globals exist
*
* @return bool
*/
private static function globals_exist() {
return isset( $_SERVER['REQUEST_URI'], $_SERVER['HTTP_USER_AGENT'], $_SERVER['SERVER_PROTOCOL'] ) && defined( 'PANTHEON_HOSTNAME' );
}
/**
* Check if the request is from a mobile device
*
* @see https://github.com/serbanghita/Mobile-Detect for a more robust solution
* @return bool
*/
private static function is_mobile() {
if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
return false;
}
if ( str_contains( $_SERVER['HTTP_USER_AGENT'], 'Mobile' )
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Android' )
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Silk/' )
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Kindle' )
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'BlackBerry' )
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' )
|| str_contains( $_SERVER['HTTP_USER_AGENT'], 'Opera Mobi' ) ) {
return true;
}
return false;
}
}