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 b024d005b6361..18c5dc0cd2bec 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(); diff --git a/modules/stats.php b/modules/stats.php index bcd9deea7d550..4a4b11474c0be 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;