Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix checks for serving AMP response and improve AMP compatibility #10918

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 22 additions & 55 deletions 3rd-party/class.jetpack-amp-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,26 @@
class Jetpack_AMP_Support {

static function init() {
if ( ! self::is_amp_request() ) {
return;
}

// enable stats
if ( Jetpack::is_module_active( 'stats' ) ) {
add_action( 'amp_post_template_footer', array( 'Jetpack_AMP_Support', 'add_stats_pixel' ) );
}

// 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' ) );
Expand All @@ -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.
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
21 changes: 21 additions & 0 deletions modules/carousel/jetpack-carousel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) &&
/**
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
}
Expand All @@ -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( '/<img [^>]+>/', $content, $matches ) ) {
return $content;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 10 additions & 2 deletions modules/stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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' => "<div><script type='text/javascript'>var src;if(typeof(window.devicePixelRatio)=='undefined'||window.devicePixelRatio<2){src='$img_src';}else{src='$img_src_2x';}document.write('<img src=\''+src+'\' alt=\'$alt\' title=\'$title\' />');</script></div>", 'href' => $url );
$menu = array(
'id' => 'stats',
'href' => $url,
);
if ( Jetpack_AMP_Support::is_amp_request() ) {
$menu['title'] = "<amp-img src='$img_src_2x' width=112 height=24 layout=fixed alt='$alt' title='$title'></amp-img>";
} else {
$menu['title'] = "<div><script type='text/javascript'>var src;if(typeof(window.devicePixelRatio)=='undefined'||window.devicePixelRatio<2){src='$img_src';}else{src='$img_src_2x';}document.write('<img src=\''+src+'\' alt=\'$alt\' title=\'$title\' />');</script></div>";
}

$wp_admin_bar->add_menu( $menu );
}
Expand Down
8 changes: 8 additions & 0 deletions modules/widgets/milestone/milestone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down