Skip to content

Commit

Permalink
Comment Likes: do not load on AMP views (#14840)
Browse files Browse the repository at this point in the history
* Prevent noticons from being enqueued in AMP mode

Summary:
This patch disables enqueuing of assets for some non-functional features
when in AMP mode.

Noticons is a very large CSS asset, and so it prevents a lot of other CSS from
being enqueued. A review of the places we use it showed that it was not actually
being used by any functionality that works in AMP mode.

Affected features:
- Comment Likes (non-functional in AMP)
- Geo Location (not used on most pages - might be useful to port `wpcom_vip_load_geolocation_styles_only_when_needed()` to all sites)
- Notifications UI (non-functional in AMP)
- admin-bar - (non-functional in AMP)

Test Plan:
Load a page in Transitional mode

Ensure that noticons-css is not enqueued, and that little or no CSS is blocked by AMP due to being over the 50kb max

[x] - ensure that features whose CSS is not being enqueued are not actually used

Reviewers: josephscott, #devops_team, davidbinovec, goldsounds

Subscribers: davidbinovec

Tags: #touches_jetpack_files

Differential Revision: D38340-code

This commit syncs r203534-wpcom.

* AMP: fix linting issues introduced in D38340

Summary:
The file is synced with Jetpack, and D38340 cannot be merged into Jetpack as is as it introduces changes that will not pass our pre-commit hook.
This diff fixes all PHPCS warnings.
Test Plan: * Not much to test here, this only impacts documentation / comments.
Reviewers: goldsounds
Tags: #touches_jetpack_files

Differential Revision: D39590

This commit syncs r r203555-wpcom.

Co-authored-by: Daniel Walmsley <goldsounds@gmail.com>
  • Loading branch information
jeherve and gravityrail authored Mar 4, 2020
1 parent 0365248 commit 025161a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions 3rd-party/class.jetpack-amp-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public static function init() {
// Post rendering changes for legacy AMP.
add_action( 'pre_amp_render_post', array( 'Jetpack_AMP_Support', 'amp_disable_the_content_filters' ) );

// Transitional mode AMP should not have comment likes.
add_action( 'the_content', array( 'Jetpack_AMP_Support', 'disable_comment_likes_before_the_content' ) );

// Add post template metadata for legacy AMP.
add_filter( 'amp_post_template_metadata', array( 'Jetpack_AMP_Support', 'amp_post_template_metadata' ), 10, 2 );

Expand All @@ -49,6 +52,28 @@ public static function init() {

// Sync the amp-options.
add_filter( 'jetpack_options_whitelist', array( 'Jetpack_AMP_Support', 'filter_jetpack_options_whitelist' ) );

// Show admin bar.
add_filter( 'show_admin_bar', array( 'Jetpack_AMP_Support', 'show_admin_bar' ) );
add_filter( 'jetpack_comment_likes_enabled', array( 'Jetpack_AMP_Support', 'comment_likes_enabled' ) );
}

/**
* Disable the admin bar on AMP views.
*
* @param Whether bool $show the admin bar should be shown. Default false.
*/
public static function show_admin_bar( $show ) {
return $show && ! self::is_amp_request();
}

/**
* Disable the Comment Likes feature on AMP views.
*
* @param bool $enabled Should comment likes be enabled.
*/
public static function comment_likes_enabled( $enabled ) {
return $enabled && ! self::is_amp_request();
}

/**
Expand Down Expand Up @@ -101,6 +126,18 @@ public static function amp_disable_the_content_filters() {
remove_filter( 'pre_kses', array( 'Filter_Embedded_HTML_Objects', 'maybe_create_links' ), 100 );
}

/**
* Do not add comment likes on AMP requests.
*
* @param string $content Post content.
*/
public static function disable_comment_likes_before_the_content( $content ) {
if ( self::is_amp_request() ) {
remove_filter( 'comment_text', 'comment_like_button', 12, 2 );
}
return $content;
}

/**
* Add Jetpack stats pixel.
*
Expand Down

0 comments on commit 025161a

Please sign in to comment.