From 206b614e112ab7ca4ac144f1e48389c7a7f5a082 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Thu, 24 Jan 2019 18:11:43 +0100 Subject: [PATCH] AMP: move checks for AMP requests later, inside modules. See https://github.com/Automattic/jetpack/issues/11169#issuecomment-456875877 --- 3rd-party/class.jetpack-amp-support.php | 24 +----------------------- class.jetpack.php | 5 +++++ modules/carousel/jetpack-carousel.php | 6 +++--- modules/lazy-images/lazy-images.php | 12 ++++-------- modules/sharedaddy/sharing-service.php | 4 ++++ 5 files changed, 17 insertions(+), 34 deletions(-) diff --git a/3rd-party/class.jetpack-amp-support.php b/3rd-party/class.jetpack-amp-support.php index 56699395e6b5e..87e021b53963f 100644 --- a/3rd-party/class.jetpack-amp-support.php +++ b/3rd-party/class.jetpack-amp-support.php @@ -14,21 +14,9 @@ static function init() { add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) ); } - // carousel - add_filter( 'jp_carousel_maybe_disable', array( __CLASS__, 'is_amp_request' ) ); - - // sharing - 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' ) ); + // Sharing. add_filter( 'jetpack_sharing_display_markup', array( 'Jetpack_AMP_Support', 'render_sharing_html' ), 10, 2 ); - // disable lazy images - add_filter( 'lazyload_is_enabled', array( __CLASS__, 'is_not_amp_request' ) ); - - // disable imploding CSS - 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' ) ); @@ -64,16 +52,6 @@ static function is_amp_request() { return apply_filters( 'jetpack_is_amp_request', $is_amp_request ); } - /** - * 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' ); diff --git a/class.jetpack.php b/class.jetpack.php index d15f41285a559..08b0629651a3e 100644 --- a/class.jetpack.php +++ b/class.jetpack.php @@ -6873,6 +6873,11 @@ public function implode_frontend_css( $travis_test = false ) { $do_implode = false; } + // Do not implode CSS when the page loads via the AMP plugin. + if ( Jetpack_AMP_Support::is_amp_request() ) { + $do_implode = false; + } + /** * Allow CSS to be concatenated into a single jetpack.css file. * diff --git a/modules/carousel/jetpack-carousel.php b/modules/carousel/jetpack-carousel.php index 8123e7746d493..9ffa1bca40bd2 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( 'wp', array( $this, 'init' ), 99 ); + add_action( 'init', array( $this, 'init' ) ); } function init() { @@ -44,7 +44,7 @@ function init() { if ( is_admin() ) { // Register the Carousel-related related settings - $this->register_settings(); + add_action( 'admin_init', array( $this, 'register_settings' ), 5 ); 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 @@ -217,7 +217,7 @@ function check_content_for_blocks( $content ) { if ( Jetpack_AMP_Support::is_amp_request() ) { return $content; } - + if ( function_exists( 'has_block' ) && ( has_block( 'gallery', $content ) || has_block( 'jetpack/tiled-gallery', $content ) ) diff --git a/modules/lazy-images/lazy-images.php b/modules/lazy-images/lazy-images.php index c0a14853a36ca..befa5ec866f22 100644 --- a/modules/lazy-images/lazy-images.php +++ b/modules/lazy-images/lazy-images.php @@ -40,6 +40,10 @@ private function __construct() { return; } + if ( Jetpack_AMP_Support::is_amp_request() ) { + return; + } + add_action( 'wp_head', array( $this, 'setup_filters' ), 9999 ); // we don't really want to modify anything in since it's mostly all metadata add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); @@ -102,11 +106,6 @@ public function add_image_placeholders( $content ) { return $content; } - // Don't lazyload for amp-wp content - if ( Jetpack_AMP_Support::is_amp_request() ) { - return $content; - } - // This is a pretty simple regex, but it works $content = preg_replace_callback( '#<(img)([^>]+?)(>(.*?)|[\/]?>)#si', array( __CLASS__, 'process_image' ), $content ); @@ -340,9 +339,6 @@ private static function build_attributes_string( $attributes ) { } public function enqueue_assets() { - if ( Jetpack_AMP_Support::is_amp_request() ) { - return; - } wp_enqueue_script( 'jetpack-lazy-images', Jetpack::get_file_url_for_environment( diff --git a/modules/sharedaddy/sharing-service.php b/modules/sharedaddy/sharing-service.php index ec7bb0b64c84f..ddccc8b532e1d 100644 --- a/modules/sharedaddy/sharing-service.php +++ b/modules/sharedaddy/sharing-service.php @@ -577,6 +577,10 @@ function sharing_maybe_enqueue_scripts() { } function sharing_add_footer() { + if ( Jetpack_AMP_Support::is_amp_request() ) { + return; + } + global $jetpack_sharing_counts; /**