From 930dea325a31cf4aa081185b51a0f6cd184fbde3 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Wed, 24 Apr 2019 18:48:25 +0200 Subject: [PATCH] AMP: check if the class exists before to use it (#12139) Summary: After adding the AMP compat file that ships with Jetpack (this was done in D22313, D26814, D26819, D26921, and D26928), let's start using this class. This replaces D22154 and D23501, and allows us to start syncing any future changes that would rely on the Jetpack_AMP_Support class. Related Jetpack PRs: - https://github.com/Automattic/jetpack/pull/10945 - https://github.com/Automattic/jetpack/pull/11195, which reverted some of the changes in the PR above. - https://github.com/Automattic/jetpack/pull/12054 - https://github.com/Automattic/jetpack/pull/12053 - https://github.com/Automattic/jetpack/pull/12026 Discussion: - https://[private link] We'll need to test for issues like the ones that popped up on Jetpack at the time: https://github.com/Automattic/jetpack/issues/11169 We will also need to account for the fact that WordPress.com does not run the latest version of the AMP plugin. It runs an old version (0.6.2 vs. the current version on W.org 1.0.2), with its own version of some of the compatibility fixes that come with the class we've added (see [[ https://[private link].php | here ]]) and needs to be updated as per the discussions here: - https://[private link] (D14521) - https://[private link] Test Plan: * Create a post with a gallery * Add a Facebook sharing button to that post. * Try Liking that post. * Comment on one of the images in the gallery. * Load the post in an AMP view, and repeat the steps below. In all cases: - You should not see any js errors in the browser console. - You should not get any PHP notices in your debug log. Reviewers: zinigor Tags: #touches_jetpack_files Differential Revision: D26821-code This commit syncs r190790-wpcom. --- modules/carousel/jetpack-carousel.php | 30 +++++++++++++++---- .../related-posts/jetpack-related-posts.php | 15 ++++++---- modules/sharedaddy/sharing-service.php | 5 +++- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/modules/carousel/jetpack-carousel.php b/modules/carousel/jetpack-carousel.php index ea62aa17ec568..1542a2972d1d7 100644 --- a/modules/carousel/jetpack-carousel.php +++ b/modules/carousel/jetpack-carousel.php @@ -153,7 +153,10 @@ function display_bail_message( $output = '' ) { } function check_if_shortcode_processed_and_enqueue_assets( $output ) { - if ( Jetpack_AMP_Support::is_amp_request() ) { + if ( + class_exists( 'Jetpack_AMP_Support' ) + && Jetpack_AMP_Support::is_amp_request() + ) { return $output; } @@ -208,7 +211,10 @@ function check_if_shortcode_processed_and_enqueue_assets( $output ) { * @return string $content Post content. */ function check_content_for_blocks( $content ) { - if ( Jetpack_AMP_Support::is_amp_request() ) { + if ( + class_exists( 'Jetpack_AMP_Support' ) + && Jetpack_AMP_Support::is_amp_request() + ) { return $content; } @@ -354,7 +360,10 @@ function enqueue_assets() { } function set_in_gallery( $output ) { - if ( Jetpack_AMP_Support::is_amp_request() ) { + if ( + class_exists( 'Jetpack_AMP_Support' ) + && Jetpack_AMP_Support::is_amp_request() + ) { return $output; } $this->in_gallery = true; @@ -372,7 +381,10 @@ function set_in_gallery( $output ) { * @return string Modified HTML content of the post */ function add_data_img_tags_and_enqueue_assets( $content ) { - if ( Jetpack_AMP_Support::is_amp_request() ) { + if ( + class_exists( 'Jetpack_AMP_Support' ) + && Jetpack_AMP_Support::is_amp_request() + ) { return $content; } @@ -426,7 +438,10 @@ function add_data_img_tags_and_enqueue_assets( $content ) { } function add_data_to_images( $attr, $attachment = null ) { - if ( Jetpack_AMP_Support::is_amp_request() ) { + if ( + class_exists( 'Jetpack_AMP_Support' ) + && Jetpack_AMP_Support::is_amp_request() + ) { return $attr; } @@ -498,7 +513,10 @@ function add_data_to_images( $attr, $attachment = null ) { function add_data_to_container( $html ) { global $post; - if ( Jetpack_AMP_Support::is_amp_request() ) { + if ( + class_exists( 'Jetpack_AMP_Support' ) + && Jetpack_AMP_Support::is_amp_request() + ) { return $html; } diff --git a/modules/related-posts/jetpack-related-posts.php b/modules/related-posts/jetpack-related-posts.php index 842b6e636d34b..d1d4855f61bbe 100644 --- a/modules/related-posts/jetpack-related-posts.php +++ b/modules/related-posts/jetpack-related-posts.php @@ -1604,12 +1604,15 @@ protected function _log_click( $post_id, $to_post_id, $link_position ) { */ protected function _enabled_for_request() { $enabled = is_single() - && - ! is_admin() - && - ( !$this->_allow_feature_toggle() || $this->get_option( 'enabled' ) ) - && - ! Jetpack_AMP_Support::is_amp_request(); + && ! is_admin() + && ( ! $this->_allow_feature_toggle() || $this->get_option( 'enabled' ) ); + + if ( + class_exists( 'Jetpack_AMP_Support' ) + && Jetpack_AMP_Support::is_amp_request() + ) { + $enabled = false; + } /** * Filter the Enabled value to allow related posts to be shown on pages as well. diff --git a/modules/sharedaddy/sharing-service.php b/modules/sharedaddy/sharing-service.php index 756fa1d069f5e..7e453262338e3 100644 --- a/modules/sharedaddy/sharing-service.php +++ b/modules/sharedaddy/sharing-service.php @@ -575,7 +575,10 @@ function sharing_maybe_enqueue_scripts() { } function sharing_add_footer() { - if ( Jetpack_AMP_Support::is_amp_request() ) { + if ( + class_exists( 'Jetpack_AMP_Support' ) + && Jetpack_AMP_Support::is_amp_request() + ) { return; }