|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Manages compatibility with the amp-wp plugin |
|
5
|
|
|
* |
|
6
|
|
|
* @see https://github.com/Automattic/amp-wp |
|
7
|
|
|
*/ |
|
8
|
|
|
class Jetpack_AMP_Support { |
|
9
|
|
|
// static $modules_to_disable = array( 'likes', 'comment-likes', 'related-posts', 'carousel', 'photon', 'lazy-images', 'notes' ); |
|
10
|
|
|
|
|
11
|
|
|
static function init() { |
|
12
|
|
|
if ( ! self::is_amp_request() ) { |
|
13
|
|
|
return; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
// carousel |
|
17
|
|
|
add_filter( 'jp_carousel_maybe_disable', '__return_true' ); |
|
18
|
|
|
|
|
19
|
|
|
// sharing |
|
20
|
|
|
add_filter( 'sharing_enqueue_scripts', '__return_false' ); |
|
21
|
|
|
add_filter( 'jetpack_sharing_counts', '__return_false' ); |
|
22
|
|
|
add_filter( 'sharing_js', '__return_false' ); |
|
23
|
|
|
add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 ); |
|
24
|
|
|
|
|
25
|
|
|
// disable imploding CSS |
|
26
|
|
|
add_filter( 'jetpack_implode_frontend_css', array( 'Jetpack_AMP_Support', 'should_implode_css' ) ); |
|
27
|
|
|
|
|
28
|
|
|
// DONE |
|
29
|
|
|
// disable likes |
|
30
|
|
|
// disable comment likes |
|
31
|
|
|
// disable related posts |
|
32
|
|
|
// disable carousel |
|
33
|
|
|
// disable photon |
|
34
|
|
|
// disable notifications |
|
35
|
|
|
// disable devicepx |
|
36
|
|
|
// modify social sharing |
|
37
|
|
|
// disable milestone widget |
|
38
|
|
|
// force using separate stylesheets to avoid unnecessary tree shaking |
|
39
|
|
|
|
|
40
|
|
|
// TODO |
|
|
|
|
|
|
41
|
|
|
// import functions from jetpack-helper.php in amp-wp |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
static function init_filter_jetpack_modules() { |
|
47
|
|
|
if ( ! self::is_amp_request() ) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// widgets |
|
52
|
|
|
add_filter( 'jetpack_widgets_to_include', array( 'Jetpack_AMP_Support', 'filter_available_widgets' ) ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
static function is_amp_request() { |
|
56
|
|
|
// can't use is_amp_endpoint() since it's not ready early enough in init. |
|
57
|
|
|
// is_amp_endpoint() implementation calls is_feed, which bails with a notice if plugins_loaded isn't finished |
|
58
|
|
|
// "Conditional query tags do not work before the query is run" |
|
59
|
|
|
return ! is_admin() // this is necessary so that modules can still be enabled/disabled/configured as per normal via Jetpack admin |
|
60
|
|
|
&& |
|
61
|
|
|
function_exists( 'amp_is_canonical' ) // this is really just testing if the plugin exists |
|
62
|
|
|
&& |
|
63
|
|
|
( amp_is_canonical() || isset( $_GET[ amp_get_slug() ] ) ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
static function filter_available_widgets( $widgets ) { |
|
67
|
|
|
if ( self::is_amp_request() ) { |
|
68
|
|
|
$widgets = array_filter( $widgets, array( 'Jetpack_AMP_Support', 'is_supported_widget' ) ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $widgets; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
static function is_supported_widget( $widget_path ) { |
|
75
|
|
|
return substr($widget_path, -14) !== '/milestone.php'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
static function should_implode_css( $implode ) { |
|
79
|
|
|
if ( self::is_amp_request() ) { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $implode; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
static function render_sharing_html( $markup, $sharing_enabled ) { |
|
87
|
|
|
remove_action( 'wp_footer', 'sharing_add_footer' ); |
|
88
|
|
|
if ( empty( $sharing_enabled ) ) { |
|
89
|
|
|
return $markup; |
|
90
|
|
|
} |
|
91
|
|
|
$supported_services = array( |
|
92
|
|
|
'facebook' => array( |
|
93
|
|
|
/** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
|
94
|
|
|
'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ), |
|
95
|
|
|
), |
|
96
|
|
|
'twitter' => array(), |
|
97
|
|
|
'pinterest' => array(), |
|
98
|
|
|
'whatsapp' => array(), |
|
99
|
|
|
'google-plus-1' => array( |
|
100
|
|
|
'type' => 'gplus', |
|
101
|
|
|
), |
|
102
|
|
|
'tumblr' => array(), |
|
103
|
|
|
'linkedin' => array(), |
|
104
|
|
|
); |
|
105
|
|
|
$sharing_links = array(); |
|
106
|
|
|
foreach ( $sharing_enabled['visible'] as $id => $service ) { |
|
107
|
|
|
if ( ! isset( $supported_services[ $id ] ) ) { |
|
108
|
|
|
$sharing_links[] = "<!-- not supported: $id -->"; |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
$args = array_merge( |
|
112
|
|
|
array( |
|
113
|
|
|
'type' => $id, |
|
114
|
|
|
), |
|
115
|
|
|
$supported_services[ $id ] |
|
116
|
|
|
); |
|
117
|
|
|
$sharing_link = '<amp-social-share'; |
|
118
|
|
|
foreach ( $args as $key => $value ) { |
|
119
|
|
|
$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) ); |
|
120
|
|
|
} |
|
121
|
|
|
$sharing_link .= '></amp-social-share>'; |
|
122
|
|
|
$sharing_links[] = $sharing_link; |
|
123
|
|
|
} |
|
124
|
|
|
return preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup ); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 ); |
|
129
|
|
|
|
|
130
|
|
|
// this is necessary since for better or worse Jetpack modules are loaded during plugins_loaded, which means we must |
|
131
|
|
|
// take the opportunity to intercept initialisation before that point, either by adding explicit detection into the module, |
|
132
|
|
|
// or preventing it from loading in the first place (better for performance) |
|
133
|
|
|
add_action( 'plugins_loaded', array( 'Jetpack_AMP_Support', 'init_filter_jetpack_modules' ), 1 ); |
This check looks
TODOcomments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.