Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.00% covered (danger)
8.00%
14 / 175
4.17% covered (danger)
4.17%
1 / 24
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_AMP_Support
8.00% covered (danger)
8.00%
14 / 175
4.17% covered (danger)
4.17%
1 / 24
5845.18
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
 comment_likes_enabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 admin_init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_amp_canonical
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 is_amp_available
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 is_amp_request
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 is_amp_legacy
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 amp_disable_the_content_filters
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 disable_comment_likes_before_the_content
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 add_stats_pixel
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 amp_post_template_metadata
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
30
 add_site_icon_to_metadata
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 add_image_to_metadata
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
42
 add_fallback_image_to_metadata
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 staticize_subdomain
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 extract_image_dimensions_from_getimagesize
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
56
 amp_post_jetpack_og_tags
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 videopress_enable_freedom_mode
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 render_sharing_html
80.00% covered (warning)
80.00%
12 / 15
0.00% covered (danger)
0.00%
0 / 1
6.29
 amp_disable_sharedaddy_css
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 amp_enqueue_sharing_css
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 amp_reader_sharing_css
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 filter_photon_post_image_args_for_stories
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
210
 filter_jetpack_options_safelist
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3use Automattic\Jetpack\Assets;
4use Automattic\Jetpack\Post_Media\Images;
5use Automattic\Jetpack\Stats\Tracking_Pixel as Stats_Tracking_Pixel;
6use Automattic\Jetpack\Sync\Functions;
7
8/**
9 * Manages compatibility with the amp-wp plugin
10 *
11 * @see https://github.com/Automattic/amp-wp
12 */
13class 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}