diff --git a/3rd-party/class.jetpack-amp-support.php b/3rd-party/class.jetpack-amp-support.php index 780bc10063290..56699395e6b5e 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,21 +64,14 @@ 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' ) ); - } - - return $widgets; - } - - 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() { @@ -299,11 +264,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; @@ -349,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/carousel/jetpack-carousel.php b/modules/carousel/jetpack-carousel.php index c61b181b2bd69..8123e7746d493 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 @@ -159,6 +159,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 ) && /** @@ -210,6 +214,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() ) { + return $content; + } + if ( function_exists( 'has_block' ) && ( has_block( 'gallery', $content ) || has_block( 'jetpack/tiled-gallery', $content ) ) @@ -359,6 +367,9 @@ function enqueue_assets() { } function set_in_gallery( $output ) { + if ( Jetpack_AMP_Support::is_amp_request() ) { + return $output; + } $this->in_gallery = true; return $output; } @@ -374,6 +385,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; } @@ -423,6 +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() ) { + return $attr; + } + $attachment_id = intval( $attachment->ID ); if ( ! wp_attachment_is_image( $attachment_id ) ) { return $attr; @@ -491,6 +510,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(); 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; } 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..93ff9275daf07 100644 --- a/modules/likes.php +++ b/modules/likes.php @@ -37,7 +37,10 @@ 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' ) ); + // 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 ); + add_action( 'admin_init', array( $this, 'admin_init' ) ); if ( $this->in_jetpack ) { 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(); + } +} 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 ); } 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;