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 #10945

Merged
merged 8 commits into from
Jan 3, 2019
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 );
26 changes: 24 additions & 2 deletions modules/carousel/jetpack-carousel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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 ) &&
/**
Expand Down Expand Up @@ -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 ) )
Expand Down Expand Up @@ -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;
}
Expand All @@ -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( '/<img [^>]+>/', $content, $matches ) ) {
return $content;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions modules/comment-likes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ) {
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/lazy-images/lazy-images.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion modules/likes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
14 changes: 12 additions & 2 deletions modules/masterbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
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>";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is made obsolete by #13450.

} 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