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
|
|
|
|
10
|
|
|
static function init() { |
11
|
|
|
if ( ! self::is_amp_request() ) { |
12
|
|
|
return; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
// enable stats |
16
|
|
|
if ( Jetpack::is_module_active( 'stats' ) ) { |
17
|
|
|
add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) ); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
// carousel |
21
|
|
|
add_filter( 'jp_carousel_maybe_disable', '__return_true' ); |
22
|
|
|
|
23
|
|
|
// sharing |
24
|
|
|
add_filter( 'sharing_enqueue_scripts', '__return_false' ); |
25
|
|
|
add_filter( 'jetpack_sharing_counts', '__return_false' ); |
26
|
|
|
add_filter( 'sharing_js', '__return_false' ); |
27
|
|
|
add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 ); |
28
|
|
|
|
29
|
|
|
// disable lazy images |
30
|
|
|
add_filter( 'lazyload_is_enabled', '__return_false' ); |
31
|
|
|
|
32
|
|
|
// disable imploding CSS |
33
|
|
|
add_filter( 'jetpack_implode_frontend_css', '__return_false' ); |
34
|
|
|
|
35
|
|
|
// enforce freedom mode for videopress |
36
|
|
|
add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) ); |
37
|
|
|
|
38
|
|
|
// include Jetpack og tags when rendering native AMP head |
39
|
|
|
add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) ); |
40
|
|
|
|
41
|
|
|
// Post rendering changes for legacy AMP |
42
|
|
|
add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) ); |
43
|
|
|
|
44
|
|
|
// Add post template metadata for legacy AMP |
45
|
|
|
add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
static function admin_init() { |
49
|
|
|
// disable Likes metabox for post editor if AMP canonical disabled |
50
|
|
|
add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
static function init_filter_jetpack_widgets() { |
54
|
|
|
if ( ! self::is_amp_request() ) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// widgets |
59
|
|
|
add_filter( 'jetpack_widgets_to_include', array( 'Jetpack_AMP_Support', 'filter_available_widgets' ) ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
static function is_amp_canonical() { |
63
|
|
|
return function_exists( 'amp_is_canonical' ) && amp_is_canonical(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
static function is_amp_request() { |
67
|
|
|
// can't use is_amp_endpoint() since it's not ready early enough in init. |
68
|
|
|
// is_amp_endpoint() implementation calls is_feed, which bails with a notice if plugins_loaded isn't finished |
69
|
|
|
// "Conditional query tags do not work before the query is run" |
70
|
|
|
$is_amp_request = |
71
|
|
|
defined( 'AMP__VERSION' ) |
72
|
|
|
&& |
73
|
|
|
! is_admin() // this is necessary so that modules can still be enabled/disabled/configured as per normal via Jetpack admin |
74
|
|
|
&& |
75
|
|
|
function_exists( 'amp_is_canonical' ) // this is really just testing if the plugin exists |
76
|
|
|
&& |
77
|
|
|
( |
78
|
|
|
amp_is_canonical() |
79
|
|
|
|| |
80
|
|
|
isset( $_GET[ amp_get_slug() ] ) |
81
|
|
|
|| |
82
|
|
|
( version_compare( AMP__VERSION, '1.0', '<' ) && self::has_amp_suffix() ) // after AMP 1.0, the amp suffix will no longer be supported |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns true if the current request should return valid AMP content. |
87
|
|
|
* |
88
|
|
|
* @since 6.2.0 |
89
|
|
|
* |
90
|
|
|
* @param boolean $is_amp_request Is this request supposed to return valid AMP content? |
91
|
|
|
*/ |
92
|
|
|
return apply_filters( 'jetpack_is_amp_request', $is_amp_request ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
static function has_amp_suffix() { |
96
|
|
|
$request_path = wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); |
97
|
|
|
return $request_path && preg_match( '#/amp/?$#i', $request_path ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
static function filter_available_widgets( $widgets ) { |
101
|
|
|
if ( self::is_amp_request() ) { |
102
|
|
|
$widgets = array_filter( $widgets, array( 'Jetpack_AMP_Support', 'is_supported_widget' ) ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $widgets; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
static function is_supported_widget( $widget_path ) { |
109
|
|
|
return substr( $widget_path, -14 ) !== '/milestone.php'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
static function amp_disable_the_content_filters() { |
113
|
|
|
if ( defined( 'WPCOM') && WPCOM ) { |
114
|
|
|
add_filter( 'videopress_show_2015_player', '__return_true' ); |
115
|
|
|
add_filter( 'protected_embeds_use_form_post', '__return_false' ); |
116
|
|
|
remove_filter( 'the_title', 'widont' ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 ); |
120
|
|
|
remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Add Jetpack stats pixel. |
125
|
|
|
* |
126
|
|
|
* @since 6.2.1 |
127
|
|
|
*/ |
128
|
|
|
static function add_stats_pixel() { |
129
|
|
|
if ( ! has_action( 'wp_footer', 'stats_footer' ) ) { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
stats_render_amp_footer( stats_build_view_data() ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Add publisher and image metadata to legacy AMP post. |
137
|
|
|
* |
138
|
|
|
* @since 6.2.0 |
139
|
|
|
* |
140
|
|
|
* @param array $metadata Metadata array. |
141
|
|
|
* @param WP_Post $post Post. |
142
|
|
|
* @return array Modified metadata array. |
143
|
|
|
*/ |
144
|
|
|
static function amp_post_template_metadata( $metadata, $post ) { |
145
|
|
|
if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) { |
146
|
|
|
$metadata = self::add_site_icon_to_metadata( $metadata ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ( ! isset( $metadata['image'] ) ) { |
150
|
|
|
$metadata = self::add_image_to_metadata( $metadata, $post ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $metadata; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Add blavatar to legacy AMP post metadata. |
158
|
|
|
* |
159
|
|
|
* @since 6.2.0 |
160
|
|
|
* |
161
|
|
|
* @param array $metadata Metadata. |
162
|
|
|
* @return array Metadata. |
163
|
|
|
*/ |
164
|
|
|
static function add_site_icon_to_metadata( $metadata ) { |
165
|
|
|
$size = 60; |
166
|
|
|
|
167
|
|
|
if ( function_exists( 'blavatar_domain' ) ) { |
168
|
|
|
$metadata['publisher']['logo'] = array( |
169
|
|
|
'@type' => 'ImageObject', |
170
|
|
|
'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ), |
171
|
|
|
'width' => $size, |
172
|
|
|
'height' => $size, |
173
|
|
|
); |
174
|
|
|
} else if ( $site_icon_url = Jetpack_Sync_Functions::site_icon_url( $size ) ) { |
175
|
|
|
$metadata['publisher']['logo'] = array( |
176
|
|
|
'@type' => 'ImageObject', |
177
|
|
|
'url' => $site_icon_url, |
178
|
|
|
'width' => $size, |
179
|
|
|
'height' => $size, |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $metadata; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Add image to legacy AMP post metadata. |
188
|
|
|
* |
189
|
|
|
* @since 6.2.0 |
190
|
|
|
* |
191
|
|
|
* @param array $metadata Metadata. |
192
|
|
|
* @param WP_Post $post Post. |
193
|
|
|
* @return array Metadata. |
194
|
|
|
*/ |
195
|
|
|
static function add_image_to_metadata( $metadata, $post ) { |
196
|
|
|
$image = Jetpack_PostImages::get_image( $post->ID, array( |
197
|
|
|
'fallback_to_avatars' => true, |
198
|
|
|
'avatar_size' => 200, |
199
|
|
|
// AMP already attempts these. |
200
|
|
|
'from_thumbnail' => false, |
201
|
|
|
'from_attachment' => false, |
202
|
|
|
) ); |
203
|
|
|
|
204
|
|
|
if ( empty( $image ) ) { |
205
|
|
|
return self::add_fallback_image_to_metadata( $metadata ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if ( ! isset( $image['src_width'] ) ) { |
209
|
|
|
$dimensions = self::extract_image_dimensions_from_getimagesize( array( |
210
|
|
|
$image['src'] => false, |
211
|
|
|
) ); |
212
|
|
|
|
213
|
|
View Code Duplication |
if ( false !== $dimensions[ $image['src'] ] ) { |
214
|
|
|
$image['src_width'] = $dimensions['width']; |
215
|
|
|
$image['src_height'] = $dimensions['height']; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$metadata['image'] = array( |
220
|
|
|
'@type' => 'ImageObject', |
221
|
|
|
'url' => $image['src'], |
222
|
|
|
'width' => $image['src_width'], |
223
|
|
|
'height' => $image['src_height'], |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
return $metadata; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Add fallback image to legacy AMP post metadata. |
231
|
|
|
* |
232
|
|
|
* @since 6.2.0 |
233
|
|
|
* |
234
|
|
|
* @param array $metadata Metadata. |
235
|
|
|
* @return array Metadata. |
236
|
|
|
*/ |
237
|
|
|
static function add_fallback_image_to_metadata( $metadata ) { |
238
|
|
|
/** This filter is documented in functions.opengraph.php */ |
239
|
|
|
$default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' ); |
240
|
|
|
|
241
|
|
|
$metadata['image'] = array( |
242
|
|
|
'@type' => 'ImageObject', |
243
|
|
|
'url' => self::staticize_subdomain( $default_image ), |
244
|
|
|
'width' => 200, |
245
|
|
|
'height' => 200, |
246
|
|
|
); |
247
|
|
|
|
248
|
|
|
return $metadata; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
static function staticize_subdomain( $domain ) { |
252
|
|
|
// deal with WPCOM vs Jetpack |
253
|
|
|
if ( function_exists( 'staticize_subdomain' ) ) { |
254
|
|
|
return staticize_subdomain( $domain ); |
255
|
|
|
} else { |
256
|
|
|
return Jetpack::staticize_subdomain( $domain ); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Extract image dimensions via wpcom/imagesize, only on WPCOM |
262
|
|
|
* |
263
|
|
|
* @since 6.2.0 |
264
|
|
|
* |
265
|
|
|
* @param array $dimensions Dimensions. |
266
|
|
|
* @return array Dimensions. |
267
|
|
|
*/ |
268
|
|
|
static function extract_image_dimensions_from_getimagesize( $dimensions ) { |
269
|
|
|
if ( ! ( defined('WPCOM') && WPCOM && function_exists( 'require_lib' ) ) ) { |
270
|
|
|
return $dimensions; |
271
|
|
|
} |
272
|
|
|
require_lib( 'wpcom/imagesize' ); |
273
|
|
|
|
274
|
|
|
foreach ( $dimensions as $url => $value ) { |
275
|
|
|
if ( is_array( $value ) ) { |
276
|
|
|
continue; |
277
|
|
|
} |
278
|
|
|
$result = wpcom_getimagesize( $url ); |
279
|
|
|
if ( is_array( $result ) ) { |
280
|
|
|
$dimensions[ $url ] = array( |
281
|
|
|
'width' => $result[0], |
282
|
|
|
'height' => $result[1], |
283
|
|
|
); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $dimensions; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
static function amp_post_jetpack_og_tags() { |
291
|
|
|
Jetpack::init()->check_open_graph(); |
292
|
|
|
if ( function_exists( 'jetpack_og_tags' ) ) { |
293
|
|
|
jetpack_og_tags(); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
static function videopress_enable_freedom_mode( $options ) { |
298
|
|
|
$options['freedom'] = true; |
299
|
|
|
return $options; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
static function render_sharing_html( $markup, $sharing_enabled ) { |
303
|
|
|
remove_action( 'wp_footer', 'sharing_add_footer' ); |
304
|
|
|
if ( empty( $sharing_enabled ) ) { |
305
|
|
|
return $markup; |
306
|
|
|
} |
307
|
|
|
$supported_services = array( |
308
|
|
|
'facebook' => array( |
309
|
|
|
/** This filter is documented in modules/sharedaddy/sharing-sources.php */ |
310
|
|
|
'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ), |
311
|
|
|
), |
312
|
|
|
'twitter' => array(), |
313
|
|
|
'pinterest' => array(), |
314
|
|
|
'whatsapp' => array(), |
315
|
|
|
'google-plus-1' => array( |
316
|
|
|
'type' => 'gplus', |
317
|
|
|
), |
318
|
|
|
'tumblr' => array(), |
319
|
|
|
'linkedin' => array(), |
320
|
|
|
); |
321
|
|
|
$sharing_links = array(); |
322
|
|
|
foreach ( $sharing_enabled['visible'] as $id => $service ) { |
323
|
|
|
if ( ! isset( $supported_services[ $id ] ) ) { |
324
|
|
|
$sharing_links[] = "<!-- not supported: $id -->"; |
325
|
|
|
continue; |
326
|
|
|
} |
327
|
|
|
$args = array_merge( |
328
|
|
|
array( |
329
|
|
|
'type' => $id, |
330
|
|
|
), |
331
|
|
|
$supported_services[ $id ] |
332
|
|
|
); |
333
|
|
|
$sharing_link = '<amp-social-share'; |
334
|
|
|
foreach ( $args as $key => $value ) { |
335
|
|
|
$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) ); |
336
|
|
|
} |
337
|
|
|
$sharing_link .= '></amp-social-share>'; |
338
|
|
|
$sharing_links[] = $sharing_link; |
339
|
|
|
} |
340
|
|
|
return preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup ); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 ); |
345
|
|
|
|
346
|
|
|
add_action( 'admin_init', array( 'Jetpack_AMP_Support', 'admin_init' ), 1 ); |
347
|
|
|
|
348
|
|
|
// this is necessary since for better or worse Jetpack modules and widget files are loaded during plugins_loaded, which means we must |
349
|
|
|
// take the opportunity to intercept initialisation before that point, either by adding explicit detection into the module, |
350
|
|
|
// or preventing it from loading in the first place (better for performance) |
351
|
|
|
add_action( 'plugins_loaded', array( 'Jetpack_AMP_Support', 'init_filter_jetpack_widgets' ), 1 ); |
352
|
|
|
|