From f9f04611d1ee1838aef233d3c5e7ac4b90eac2a2 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 10 Dec 2018 18:15:25 +0000 Subject: [PATCH 1/7] Defer AMP response detection until after parse_query --- 3rd-party/class.jetpack-amp-support.php | 65 +++++++++---------------- modules/carousel/jetpack-carousel.php | 21 ++++++++ 2 files changed, 45 insertions(+), 41 deletions(-) diff --git a/3rd-party/class.jetpack-amp-support.php b/3rd-party/class.jetpack-amp-support.php index 780bc10063290..6d8d732bf348c 100644 --- a/3rd-party/class.jetpack-amp-support.php +++ b/3rd-party/class.jetpack-amp-support.php @@ -8,9 +8,6 @@ class Jetpack_AMP_Support { static function init() { - if ( ! self::is_amp_request() ) { - return; - } // enable stats if ( Jetpack::is_module_active( 'stats' ) ) { @@ -18,19 +15,19 @@ static function init() { } // carousel - add_filter( 'jp_carousel_maybe_disable', '__return_true' ); + add_filter( 'jp_carousel_maybe_disable', array( __CLASS__, 'is_amp_request' ) ); // sharing - add_filter( 'sharing_enqueue_scripts', '__return_false' ); - add_filter( 'jetpack_sharing_counts', '__return_false' ); - add_filter( 'sharing_js', '__return_false' ); + add_filter( 'sharing_enqueue_scripts', array( __CLASS__, 'is_not_amp_request' ) ); + add_filter( 'jetpack_sharing_counts', array( __CLASS__, 'is_not_amp_request' ) ); + add_filter( 'sharing_js', array( __CLASS__, 'is_not_amp_request' ) ); add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 ); // disable lazy images - add_filter( 'lazyload_is_enabled', '__return_false' ); + add_filter( 'lazyload_is_enabled', array( __CLASS__, 'is_not_amp_request' ) ); // disable imploding CSS - add_filter( 'jetpack_implode_frontend_css', '__return_false' ); + add_filter( 'jetpack_implode_frontend_css', array( __CLASS__, 'is_not_amp_request' ) ); // enforce freedom mode for videopress add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) ); @@ -50,37 +47,12 @@ static function admin_init() { add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 ); } - static function init_filter_jetpack_widgets() { - if ( ! self::is_amp_request() ) { - return; - } - - // widgets - add_filter( 'jetpack_widgets_to_include', array( 'Jetpack_AMP_Support', 'filter_available_widgets' ) ); - } - static function is_amp_canonical() { return function_exists( 'amp_is_canonical' ) && amp_is_canonical(); } static function is_amp_request() { - // can't use is_amp_endpoint() since it's not ready early enough in init. - // is_amp_endpoint() implementation calls is_feed, which bails with a notice if plugins_loaded isn't finished - // "Conditional query tags do not work before the query is run" - $is_amp_request = - defined( 'AMP__VERSION' ) - && - ! is_admin() // this is necessary so that modules can still be enabled/disabled/configured as per normal via Jetpack admin - && - function_exists( 'amp_is_canonical' ) // this is really just testing if the plugin exists - && - ( - amp_is_canonical() - || - isset( $_GET[ amp_get_slug() ] ) - || - ( version_compare( AMP__VERSION, '1.0', '<' ) && self::has_amp_suffix() ) // after AMP 1.0, the amp suffix will no longer be supported - ); + $is_amp_request = ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ); /** * Returns true if the current request should return valid AMP content. @@ -92,11 +64,6 @@ function_exists( 'amp_is_canonical' ) // this is really just testing if the plug return apply_filters( 'jetpack_is_amp_request', $is_amp_request ); } - static function has_amp_suffix() { - $request_path = wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); - return $request_path && preg_match( '#/amp/?$#i', $request_path ); - } - static function filter_available_widgets( $widgets ) { if ( self::is_amp_request() ) { $widgets = array_filter( $widgets, array( 'Jetpack_AMP_Support', 'is_supported_widget' ) ); @@ -109,6 +76,16 @@ static function is_supported_widget( $widget_path ) { return substr( $widget_path, -14 ) !== '/milestone.php'; } + /** + * Returns whether the request is not AMP. + * + * @see Jetpack_AMP_Support::is_amp_request() + * @return bool Whether not AMP. + */ + static function is_not_amp_request() { + return ! self::is_amp_request(); + } + static function amp_disable_the_content_filters() { if ( defined( 'WPCOM') && WPCOM ) { add_filter( 'videopress_show_2015_player', '__return_true' ); @@ -299,11 +276,17 @@ static function amp_post_jetpack_og_tags() { } static function videopress_enable_freedom_mode( $options ) { - $options['freedom'] = true; + if ( self::is_amp_request() ) { + $options['freedom'] = true; + } return $options; } static function render_sharing_html( $markup, $sharing_enabled ) { + if ( ! self::is_amp_request() ) { + return $markup; + } + remove_action( 'wp_footer', 'sharing_add_footer' ); if ( empty( $sharing_enabled ) ) { return $markup; diff --git a/modules/carousel/jetpack-carousel.php b/modules/carousel/jetpack-carousel.php index 4032fe76ac903..798e4d4d2ba0c 100644 --- a/modules/carousel/jetpack-carousel.php +++ b/modules/carousel/jetpack-carousel.php @@ -158,6 +158,10 @@ function display_bail_message( $output = '' ) { } function check_if_shortcode_processed_and_enqueue_assets( $output ) { + if ( Jetpack_AMP_Support::is_amp_request() ) { + return $output; + } + if ( ! empty( $output ) && /** @@ -200,6 +204,9 @@ function check_if_shortcode_processed_and_enqueue_assets( $output ) { } function check_content_for_blocks( $content ) { + if ( Jetpack_AMP_Support::is_amp_request() ) { + return $content; + } if ( function_exists( 'has_block' ) && has_block( 'gallery', $content ) ) { $this->enqueue_assets(); $content = $this->add_data_to_container( $content ); @@ -346,6 +353,9 @@ function enqueue_assets() { } function set_in_gallery( $output ) { + if ( Jetpack_AMP_Support::is_amp_request() ) { + return $output; + } $this->in_gallery = true; return $output; } @@ -361,6 +371,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() ) { + return $content; + } + if ( ! preg_match_all( '/]+>/', $content, $matches ) ) { return $content; } @@ -411,6 +425,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() ) { + return $attr; + } + $attachment_id = intval( $attachment->ID ); if ( ! wp_attachment_is_image( $attachment_id ) ) { return $attr; @@ -479,6 +497,9 @@ function add_data_to_images( $attr, $attachment = null ) { function add_data_to_container( $html ) { global $post; + if ( Jetpack_AMP_Support::is_amp_request() ) { + return $html; + } if ( isset( $post ) ) { $blog_id = (int) get_current_blog_id(); From 79ea9b74e86f8709ad42c1b9b362283e37de2cb0 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 10 Dec 2018 18:16:15 +0000 Subject: [PATCH 2/7] Add AMP support for Milestone widget instead of omitting --- 3rd-party/class.jetpack-amp-support.php | 16 ---------------- modules/widgets/milestone/milestone.php | 8 ++++++++ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/3rd-party/class.jetpack-amp-support.php b/3rd-party/class.jetpack-amp-support.php index 6d8d732bf348c..56699395e6b5e 100644 --- a/3rd-party/class.jetpack-amp-support.php +++ b/3rd-party/class.jetpack-amp-support.php @@ -64,18 +64,6 @@ static function is_amp_request() { return apply_filters( 'jetpack_is_amp_request', $is_amp_request ); } - static function filter_available_widgets( $widgets ) { - if ( self::is_amp_request() ) { - $widgets = array_filter( $widgets, array( 'Jetpack_AMP_Support', 'is_supported_widget' ) ); - } - - return $widgets; - } - - static function is_supported_widget( $widget_path ) { - return substr( $widget_path, -14 ) !== '/milestone.php'; - } - /** * Returns whether the request is not AMP. * @@ -332,7 +320,3 @@ static function render_sharing_html( $markup, $sharing_enabled ) { add_action( 'admin_init', array( 'Jetpack_AMP_Support', 'admin_init' ), 1 ); -// this is necessary since for better or worse Jetpack modules and widget files are loaded during plugins_loaded, which means we must -// take the opportunity to intercept initialisation before that point, either by adding explicit detection into the module, -// or preventing it from loading in the first place (better for performance) -add_action( 'plugins_loaded', array( 'Jetpack_AMP_Support', 'init_filter_jetpack_widgets' ), 1 ); diff --git a/modules/widgets/milestone/milestone.php b/modules/widgets/milestone/milestone.php index 2dd38361b9fd5..8490a8eca9929 100644 --- a/modules/widgets/milestone/milestone.php +++ b/modules/widgets/milestone/milestone.php @@ -74,6 +74,10 @@ public static function enqueue_admin( $hook_suffix ) { } public static function enqueue_template() { + if ( Jetpack_AMP_Support::is_amp_request() ) { + return; + } + wp_enqueue_script( 'milestone', Jetpack::get_file_url_for_environment( @@ -175,6 +179,10 @@ public static function sanitize_color_hex( $hex, $prefix = '#' ) { * Hooks into the "wp_footer" action. */ function localize_script() { + if ( Jetpack_AMP_Support::is_amp_request() ) { + return; + } + if ( empty( self::$config_js['instances'] ) ) { wp_dequeue_script( 'milestone' ); return; From 04d477ae582e55392f37e0b06b684a17e0f64b89 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 10 Dec 2018 18:16:33 +0000 Subject: [PATCH 3/7] Add AMP support for admin bar stats --- modules/stats.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/stats.php b/modules/stats.php index b7b263eaf66a2..cb2208359793e 100644 --- a/modules/stats.php +++ b/modules/stats.php @@ -958,7 +958,7 @@ function stats_admin_bar_head() { } #wpadminbar .quicklinks li#wp-admin-bar-stats a img { height: 24px; - padding: 4px 0; + margin: 4px 0; max-width: none; border: none; } @@ -983,7 +983,15 @@ function stats_admin_bar_menu( &$wp_admin_bar ) { $title = esc_attr( __( 'Views over 48 hours. Click for more Site Stats.', 'jetpack' ) ); - $menu = array( 'id' => 'stats', 'title' => "
", 'href' => $url ); + $menu = array( + 'id' => 'stats', + 'href' => $url, + ); + if ( Jetpack_AMP_Support::is_amp_request() ) { + $menu['title'] = ""; + } else { + $menu['title'] = "
"; + } $wp_admin_bar->add_menu( $menu ); } From 1ff39ce3f58a06e1415f1f51fa8bab96c60d3723 Mon Sep 17 00:00:00 2001 From: Daniel Walmsley Date: Tue, 11 Dec 2018 10:25:11 -0800 Subject: [PATCH 4/7] Ensure AMP checks are called after parse_query --- modules/carousel/jetpack-carousel.php | 4 ++-- modules/lazy-images/lazy-images.php | 2 +- modules/likes.php | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/modules/carousel/jetpack-carousel.php b/modules/carousel/jetpack-carousel.php index 798e4d4d2ba0c..06e566b218ac0 100644 --- a/modules/carousel/jetpack-carousel.php +++ b/modules/carousel/jetpack-carousel.php @@ -29,7 +29,7 @@ class Jetpack_Carousel { public $single_image_gallery_enabled_media_file = false; function __construct() { - add_action( 'init', array( $this, 'init' ) ); + add_action( 'wp', array( $this, 'init' ), 99 ); } function init() { @@ -44,7 +44,7 @@ function init() { if ( is_admin() ) { // Register the Carousel-related related settings - add_action( 'admin_init', array( $this, 'register_settings' ), 5 ); + $this->register_settings(); if ( ! $this->in_jetpack ) { if ( 0 == $this->test_1or0_option( get_option( 'carousel_enable_it' ), true ) ) { return; // Carousel disabled, abort early, but still register setting so user can switch it back on diff --git a/modules/lazy-images/lazy-images.php b/modules/lazy-images/lazy-images.php index e77d2e100c08b..e9cfc7234e3eb 100644 --- a/modules/lazy-images/lazy-images.php +++ b/modules/lazy-images/lazy-images.php @@ -103,7 +103,7 @@ public function add_image_placeholders( $content ) { } // Don't lazyload for amp-wp content - if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { + if ( Jetpack_AMP_Support::is_amp_request() ) { return $content; } diff --git a/modules/likes.php b/modules/likes.php index 45ec890cd2dd2..ea4d24b0bb748 100644 --- a/modules/likes.php +++ b/modules/likes.php @@ -37,8 +37,9 @@ function __construct() { $this->in_jetpack = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? false : true; $this->settings = new Jetpack_Likes_Settings(); - add_action( 'init', array( &$this, 'action_init' ) ); - add_action( 'admin_init', array( $this, 'admin_init' ) ); + // We need to run on wp hook rather than init because we check is_amp_endpoint() + // when bootstrapping hooks + add_action( 'wp', array( &$this, 'action_init' ), 99 ); if ( $this->in_jetpack ) { add_action( 'jetpack_activate_module_likes', array( $this, 'set_social_notifications_like' ) ); From 269b2e407dedaba1bb2a52ffe81c4ea306e7fe48 Mon Sep 17 00:00:00 2001 From: Igor Zinovyev Date: Thu, 13 Dec 2018 22:25:02 +0300 Subject: [PATCH 5/7] Moved Masterbar initialization to a later point and added a condition. --- modules/masterbar.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/modules/masterbar.php b/modules/masterbar.php index 6672a992099e0..900082930bd38 100644 --- a/modules/masterbar.php +++ b/modules/masterbar.php @@ -11,6 +11,16 @@ * Additional Search Queries: adminbar, masterbar */ -include dirname( __FILE__ ) . '/masterbar/masterbar.php'; +require dirname( __FILE__ ) . '/masterbar/masterbar.php'; -new A8C_WPCOM_Masterbar; +// In order to be able to tell if it's an AMP request or not we have to hook into parse_query at a later priority. +add_action( 'parse_query', 'jetpack_initialize_masterbar', 99 ); + +/** + * Initializes the Masterbar in case the request is not AMP. + */ +function jetpack_initialize_masterbar() { + if ( ! Jetpack_AMP_Support::is_amp_request() ) { + new A8C_WPCOM_Masterbar(); + } +} From 4aaa151bf959fdbd7826dd756b887b38e1d14348 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Wed, 2 Jan 2019 16:35:59 +0100 Subject: [PATCH 6/7] Add Likes' admin tools back. --- modules/likes.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/likes.php b/modules/likes.php index ea4d24b0bb748..93ff9275daf07 100644 --- a/modules/likes.php +++ b/modules/likes.php @@ -41,6 +41,8 @@ function __construct() { // when bootstrapping hooks add_action( 'wp', array( &$this, 'action_init' ), 99 ); + add_action( 'admin_init', array( $this, 'admin_init' ) ); + if ( $this->in_jetpack ) { add_action( 'jetpack_activate_module_likes', array( $this, 'set_social_notifications_like' ) ); add_action( 'jetpack_deactivate_module_likes', array( $this, 'delete_social_notifications_like' ) ); From b4565dfceeb3a9ba84d2fea44d03b8ec65d7058b Mon Sep 17 00:00:00 2001 From: Derek Smart Date: Thu, 3 Jan 2019 17:01:35 -0500 Subject: [PATCH 7/7] AMP support: No need to run Comment Likes frontend so early. --- modules/comment-likes.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/modules/comment-likes.php b/modules/comment-likes.php index c884b8e9c75f9..4fd73751adc8d 100644 --- a/modules/comment-likes.php +++ b/modules/comment-likes.php @@ -38,7 +38,7 @@ private function __construct() { $this->url_parts = parse_url( $this->url ); $this->domain = $this->url_parts['host']; - add_action( 'init', array( $this, 'frontend_init' ) ); + add_action( 'template_redirect', array( $this, 'frontend_init' ) ); add_action( 'admin_init', array( $this, 'admin_init' ) ); if ( ! Jetpack::is_module_active( 'likes' ) ) { @@ -117,10 +117,6 @@ public function add_like_count_column( $columns ) { } public function frontend_init() { - if ( is_admin() ) { - return; - } - if ( Jetpack_AMP_Support::is_amp_request() ) { return; }