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.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / 3rd-party / class.jetpack-amp-support.php
jetpack / 3rd-party Last commit date
debug-bar 2 months ago 3rd-party.php 1 week ago activitypub.php 1 week ago amp.php 6 months ago atomic.php 6 months ago bbpress.php 6 months ago beaverbuilder.php 6 months ago bitly.php 6 months ago buddypress.php 6 months ago class-domain-mapping.php 6 months ago class-jetpack-bbpress-rest-api.php 2 years ago class.jetpack-amp-support.php 3 months ago creative-mail.php 6 months ago debug-bar.php 6 months ago jetpack-backup.php 6 months ago jetpack-boost.php 6 months ago qtranslate-x.php 6 months ago vaultpress.php 6 months ago web-stories.php 6 months ago woocommerce-services.php 6 months ago woocommerce.php 6 months ago wpcom-reader.php 6 months ago wpml.php 6 months ago
class.jetpack-amp-support.php
541 lines
1 <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 use Automattic\Jetpack\Assets;
4 use Automattic\Jetpack\Post_Media\Images;
5 use Automattic\Jetpack\Stats\Tracking_Pixel as Stats_Tracking_Pixel;
6 use Automattic\Jetpack\Sync\Functions;
7
8 /**
9 * Manages compatibility with the amp-wp plugin
10 *
11 * @see https://github.com/Automattic/amp-wp
12 */
13 class Jetpack_AMP_Support {
14
15 /**
16 * Apply custom AMP changes on the front-end.
17 */
18 public static function init() {
19
20 // Add Stats tracking pixel on Jetpack sites when the Stats module is active.
21 if (
22 Jetpack::is_module_active( 'stats' )
23 && ! ( defined( 'IS_WPCOM' ) && IS_WPCOM )
24 ) {
25 add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) );
26 }
27
28 // Sharing.
29 add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 );
30 add_filter( 'sharing_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_disable_sharedaddy_css' ) );
31 add_action( 'wp_enqueue_scripts', array( 'Jetpack_AMP_Support', 'amp_enqueue_sharing_css' ) );
32
33 // Sharing for Reader mode.
34 if ( function_exists( 'jetpack_social_menu_include_svg_icons' ) ) {
35 add_action( 'amp_post_template_footer', 'jetpack_social_menu_include_svg_icons' );
36 }
37 add_action( 'amp_post_template_css', array( 'Jetpack_AMP_Support', 'amp_reader_sharing_css' ), 10, 0 );
38
39 // enforce freedom mode for videopress.
40 add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );
41
42 // include Jetpack og tags when rendering native AMP head.
43 add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );
44
45 // Post rendering changes for legacy AMP.
46 add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );
47
48 // Disable Comment Likes.
49 add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) );
50
51 // Transitional mode AMP should not have comment likes.
52 add_filter( 'the_content', array( 'Jetpack_AMP_Support', 'disable_comment_likes_before_the_content' ) );
53
54 // Add post template metadata for legacy AMP.
55 add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );
56
57 // Filter photon image args for AMP Stories.
58 add_filter( 'jetpack_photon_post_image_args', array( 'Jetpack_AMP_Support', 'filter_photon_post_image_args_for_stories' ), 10, 2 );
59
60 // Sync the amp-options.
61 add_filter( 'jetpack_options_whitelist', array( 'Jetpack_AMP_Support', 'filter_jetpack_options_safelist' ) );
62 }
63
64 /**
65 * Disable the Comment Likes feature on AMP views.
66 *
67 * @param bool $enabled Should comment likes be enabled.
68 */
69 public static function comment_likes_enabled( $enabled ) {
70 return $enabled && ! self::is_amp_request();
71 }
72
73 /**
74 * Apply custom AMP changes in wp-admin.
75 */
76 public static function admin_init() {
77 // disable Likes metabox for post editor if AMP canonical disabled.
78 add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );
79 }
80
81 /**
82 * Is the page in AMP 'canonical mode'.
83 * Used when themes register support for AMP with `add_theme_support( 'amp' )`.
84 *
85 * @return bool is_amp_canonical
86 */
87 public static function is_amp_canonical() {
88 return function_exists( 'amp_is_canonical' ) && amp_is_canonical();
89 }
90
91 /**
92 * Is AMP available for this request
93 * This returns false for admin, CLI requests etc.
94 *
95 * @return bool is_amp_available
96 */
97 public static function is_amp_available() {
98 return ( function_exists( 'amp_is_available' ) && amp_is_available() );
99 }
100
101 /**
102 * Does the page return AMP content.
103 *
104 * @return bool $is_amp_request Are we on am AMP view.
105 */
106 public static function is_amp_request() {
107 $is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() );
108
109 /**
110 * Returns true if the current request should return valid AMP content.
111 *
112 * @since 6.2.0
113 *
114 * @param boolean $is_amp_request Is this request supposed to return valid AMP content?
115 */
116 return apply_filters( 'jetpack_is_amp_request', $is_amp_request );
117 }
118
119 /**
120 * Determines whether the legacy AMP post templates are being used.
121 *
122 * @since 10.6.0
123 *
124 * @return bool
125 */
126 public static function is_amp_legacy() {
127 return ( function_exists( 'amp_is_legacy' ) && amp_is_legacy() );
128 }
129
130 /**
131 * Remove content filters added by Jetpack.
132 */
133 public static function amp_disable_the_content_filters() {
134 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
135 add_filter( 'protected_embeds_use_form_post', '__return_false' );
136 remove_filter( 'the_title', 'widont' );
137 }
138
139 remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'filter' ), 11 );
140 remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
141 }
142
143 /**
144 * Do not add comment likes on AMP requests.
145 *
146 * @param string $content Post content.
147 */
148 public static function disable_comment_likes_before_the_content( $content ) {
149 if ( self::is_amp_request() ) {
150 remove_filter( 'comment_text', 'comment_like_button', 12 );
151 }
152 return $content;
153 }
154
155 /**
156 * Add Jetpack stats pixel.
157 *
158 * @since 6.2.1
159 */
160 public static function add_stats_pixel() {
161 if ( ! has_action( 'wp_footer', array( Stats_Tracking_Pixel::class, 'add_amp_pixel' ) ) ) {
162 return;
163 }
164
165 $stats_data = Stats_Tracking_Pixel::build_view_data();
166 Stats_Tracking_Pixel::render_amp_footer( $stats_data );
167 }
168
169 /**
170 * Add publisher and image metadata to legacy AMP post.
171 *
172 * @since 6.2.0
173 *
174 * @param array $metadata Metadata array.
175 * @param WP_Post $post Post.
176 * @return array Modified metadata array.
177 */
178 public static function amp_post_template_metadata( $metadata, $post ) {
179 if ( isset( $metadata['publisher'] ) && ! isset( $metadata['publisher']['logo'] ) ) {
180 $metadata = self::add_site_icon_to_metadata( $metadata );
181 }
182
183 if ( ! isset( $metadata['image'] ) && ! empty( $post ) ) {
184 $metadata = self::add_image_to_metadata( $metadata, $post );
185 }
186
187 return $metadata;
188 }
189
190 /**
191 * Add blavatar to legacy AMP post metadata.
192 *
193 * @since 6.2.0
194 *
195 * @param array $metadata Metadata.
196 *
197 * @return array Metadata.
198 */
199 private static function add_site_icon_to_metadata( $metadata ) {
200 $size = 60;
201 $site_icon_url = class_exists( 'Automattic\\Jetpack\\Sync\\Functions' ) ? Functions::site_icon_url( $size ) : '';
202
203 if ( function_exists( 'blavatar_domain' ) ) {
204 $metadata['publisher']['logo'] = array(
205 '@type' => 'ImageObject',
206 'url' => blavatar_url( blavatar_domain( site_url() ), 'img', $size, self::staticize_subdomain( 'https://wordpress.com/i/favicons/apple-touch-icon-60x60.png' ) ),
207 'width' => $size,
208 'height' => $size,
209 );
210 } elseif ( $site_icon_url ) {
211 $metadata['publisher']['logo'] = array(
212 '@type' => 'ImageObject',
213 'url' => $site_icon_url,
214 'width' => $size,
215 'height' => $size,
216 );
217 }
218
219 return $metadata;
220 }
221
222 /**
223 * Add image to legacy AMP post metadata.
224 *
225 * @since 6.2.0
226 *
227 * @param array $metadata Metadata.
228 * @param WP_Post $post Post.
229 * @return array Metadata.
230 */
231 private static function add_image_to_metadata( $metadata, $post ) {
232 $image = Images::get_image(
233 $post->ID,
234 array(
235 'fallback_to_avatars' => true,
236 'avatar_size' => 200,
237 // AMP already attempts these.
238 'from_thumbnail' => false,
239 'from_attachment' => false,
240 )
241 );
242
243 if ( empty( $image ) ) {
244 return self::add_fallback_image_to_metadata( $metadata );
245 }
246
247 if ( ! isset( $image['src_width'] ) ) {
248 $dimensions = self::extract_image_dimensions_from_getimagesize(
249 array(
250 $image['src'] => false,
251 )
252 );
253
254 if ( false !== $dimensions[ $image['src'] ] ) {
255 $image['src_width'] = $dimensions['width'];
256 $image['src_height'] = $dimensions['height'];
257 }
258 }
259
260 $metadata['image'] = array(
261 '@type' => 'ImageObject',
262 'url' => $image['src'],
263 );
264 if ( isset( $image['src_width'] ) ) {
265 $metadata['image']['width'] = $image['src_width'];
266 }
267 if ( isset( $image['src_width'] ) ) {
268 $metadata['image']['height'] = $image['src_height'];
269 }
270
271 return $metadata;
272 }
273
274 /**
275 * Add fallback image to legacy AMP post metadata.
276 *
277 * @since 6.2.0
278 *
279 * @param array $metadata Metadata.
280 * @return array Metadata.
281 */
282 private static function add_fallback_image_to_metadata( $metadata ) {
283 /** This filter is documented in functions.opengraph.php */
284 $default_image = apply_filters( 'jetpack_open_graph_image_default', 'https://wordpress.com/i/blank.jpg' );
285
286 $metadata['image'] = array(
287 '@type' => 'ImageObject',
288 'url' => self::staticize_subdomain( $default_image ),
289 'width' => 200,
290 'height' => 200,
291 );
292
293 return $metadata;
294 }
295
296 /**
297 * Return static WordPress.com domain to use to load resources from WordPress.com.
298 *
299 * @param string $domain Asset URL.
300 */
301 private static function staticize_subdomain( $domain ) {
302 // deal with WPCOM vs Jetpack.
303 if ( function_exists( 'staticize_subdomain' ) ) {
304 return staticize_subdomain( $domain );
305 } else {
306 return Assets::staticize_subdomain( $domain );
307 }
308 }
309
310 /**
311 * Extract image dimensions via wpcom/imagesize, only on WPCOM
312 *
313 * @since 6.2.0
314 *
315 * @param array $dimensions Dimensions.
316 * @return array Dimensions.
317 */
318 private static function extract_image_dimensions_from_getimagesize( $dimensions ) {
319 if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'require_lib' ) ) ) {
320 return $dimensions;
321 }
322 require_lib( 'wpcom/imagesize' );
323
324 foreach ( $dimensions as $url => $value ) {
325 if ( is_array( $value ) ) {
326 continue;
327 }
328 $result = wpcom_getimagesize( $url );
329 if ( is_array( $result ) ) {
330 $dimensions[ $url ] = array(
331 'width' => $result[0],
332 'height' => $result[1],
333 );
334 }
335 }
336
337 return $dimensions;
338 }
339
340 /**
341 * Display Open Graph Meta tags in AMP views.
342 */
343 public static function amp_post_jetpack_og_tags() {
344 if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
345 Jetpack::init()->check_open_graph();
346 }
347
348 if ( function_exists( 'jetpack_og_tags' ) ) {
349 jetpack_og_tags();
350 }
351 }
352
353 /**
354 * Force Freedom mode in VideoPress.
355 *
356 * @param array $options Array of VideoPress shortcode options.
357 */
358 public static function videopress_enable_freedom_mode( $options ) {
359 if ( self::is_amp_request() ) {
360 $options['freedom'] = true;
361 }
362 return $options;
363 }
364
365 /**
366 * Display custom markup for the sharing buttons when in an AMP view.
367 *
368 * @param string $markup Content markup of the Jetpack sharing links.
369 * @param array $sharing_enabled Array of Sharing Services currently enabled.
370 */
371 public static function render_sharing_html( $markup, $sharing_enabled ) {
372 global $post;
373
374 if ( empty( $post ) ) {
375 return '';
376 }
377
378 if ( ! self::is_amp_request() ) {
379 return $markup;
380 }
381
382 remove_action( 'wp_footer', 'sharing_add_footer' );
383 if ( empty( $sharing_enabled ) ) {
384 return $markup;
385 }
386
387 $sharing_links = array();
388 foreach ( $sharing_enabled['visible'] as $service ) {
389 $sharing_link = $service->get_amp_display( $post );
390 if ( ! empty( $sharing_link ) ) {
391 $sharing_links[] = $sharing_link;
392 }
393 }
394
395 // Replace the existing unordered list with AMP sharing buttons.
396 $markup = preg_replace( '#<ul>(.+)</ul>#', implode( '', $sharing_links ), $markup );
397
398 // Remove any lingering share-end list items.
399 $markup = str_replace( '<li class="share-end"></li>', '', $markup );
400
401 return $markup;
402 }
403
404 /**
405 * Tells Jetpack not to enqueue CSS for share buttons.
406 *
407 * @param bool $enqueue Whether or not to enqueue.
408 * @return bool Whether or not to enqueue.
409 */
410 public static function amp_disable_sharedaddy_css( $enqueue ) {
411 if ( self::is_amp_request() ) {
412 $enqueue = false;
413 }
414
415 return $enqueue;
416 }
417
418 /**
419 * Enqueues the AMP specific sharing styles for the sharing icons.
420 */
421 public static function amp_enqueue_sharing_css() {
422 if (
423 Jetpack::is_module_active( 'sharedaddy' )
424 && self::is_amp_request()
425 && ! self::is_amp_legacy()
426 ) {
427 wp_enqueue_style( 'sharedaddy-amp', plugin_dir_url( __DIR__ ) . 'modules/sharedaddy/amp-sharing.css', array( 'social-logos' ), JETPACK__VERSION );
428 }
429 }
430
431 /**
432 * For the AMP Reader mode template, include styles that we need.
433 */
434 public static function amp_reader_sharing_css() {
435 // If sharing is not enabled, we should not proceed to render the CSS.
436 if ( ! defined( 'JETPACK_SOCIAL_LOGOS_DIR' ) || ! defined( 'JETPACK_SOCIAL_LOGOS_URL' ) || ! defined( 'WP_SHARING_PLUGIN_DIR' ) ) {
437 return;
438 }
439
440 /*
441 * We'll need to output the full contents of the 2 files
442 * in the head on AMP views. We can't rely on regular enqueues here.
443 * @todo As of AMP plugin v1.5, you can actually rely on regular enqueues thanks to https://github.com/ampproject/amp-wp/pull/4299. Once WPCOM upgrades AMP, then this method can be eliminated.
444 *
445 * phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
446 * phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
447 */
448 $css = file_get_contents( JETPACK_SOCIAL_LOGOS_DIR . 'social-logos.css' );
449 $css = preg_replace( '#(?<=url\(")(?=social-logos\.)#', JETPACK_SOCIAL_LOGOS_URL, $css ); // Make sure font files get their absolute paths.
450 echo $css;
451 echo file_get_contents( WP_SHARING_PLUGIN_DIR . 'amp-sharing.css' );
452
453 /*
454 * phpcs:enable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
455 * phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
456 */
457 }
458
459 /**
460 * Ensure proper Photon image dimensions for AMP Stories.
461 *
462 * @param array $args Array of Photon Arguments.
463 * @param array $details {
464 * Array of image details.
465 *
466 * @type string $tag Image tag (Image HTML output).
467 * @type string $src Image URL.
468 * @type string $src_orig Original Image URL.
469 * @type int|false $width Image width.
470 * @type int|false $height Image height.
471 * @type int|false $width_orig Original image width before constrained by content_width.
472 * @type int|false $height_orig Original Image height before constrained by content_width.
473 * @type string $transform_orig Original transform before constrained by content_width.
474 * }
475 * @return array Args.
476 */
477 public static function filter_photon_post_image_args_for_stories( $args, $details ) {
478 if ( ! is_singular( 'amp_story' ) ) {
479 return $args;
480 }
481
482 // Percentage-based dimensions are not allowed in AMP, so this shouldn't happen, but short-circuit just in case.
483 if ( str_contains( $details['width_orig'], '%' ) || str_contains( $details['height_orig'], '%' ) ) {
484 return $args;
485 }
486
487 $max_height = 1280; // See image size with the slug \AMP_Story_Post_Type::MAX_IMAGE_SIZE_SLUG.
488 $transform = $details['transform_orig'];
489 $width = $details['width_orig'];
490 $height = $details['height_orig'];
491
492 // If height is available, constrain to $max_height.
493 if ( false !== $height ) {
494 if ( $height > $max_height && false !== $height ) {
495 $width = ( $max_height * $width ) / $height;
496 $height = $max_height;
497 } elseif ( $height > $max_height ) {
498 $height = $max_height;
499 }
500 }
501
502 /*
503 * Set a height if none is found.
504 * If height is set in this manner and height is available, use `fit` instead of `resize` to prevent skewing.
505 */
506 if ( false === $height ) {
507 $height = $max_height;
508 if ( false !== $width ) {
509 $transform = 'fit';
510 }
511 }
512
513 // Build array of Photon args and expose to filter before passing to Photon URL function.
514 $args = array();
515
516 if ( false !== $width && false !== $height ) {
517 $args[ $transform ] = $width . ',' . $height;
518 } elseif ( false !== $width ) {
519 $args['w'] = $width;
520 } elseif ( false !== $height ) {
521 $args['h'] = $height;
522 }
523
524 return $args;
525 }
526
527 /**
528 * Adds amp-options to the list of options to sync, if AMP is available
529 *
530 * @param array $options_safelist Safelist of options to sync.
531 *
532 * @return array Updated options safelist
533 */
534 public static function filter_jetpack_options_safelist( $options_safelist ) {
535 if ( function_exists( 'is_amp_endpoint' ) ) {
536 $options_safelist[] = 'amp-options';
537 }
538 return $options_safelist;
539 }
540 }
541