Last active
April 4, 2020 21:57
-
-
Save westonruter/034e15dd7bfd011a9c73c96df86aa291 to your computer and use it in GitHub Desktop.
[OBSOLETE] Hacks to turn off or adapt Jetpack features in AMP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Jetpack Hacks for AMP | |
* Description: Temporary workarounds to get Jetpack features to play nice with AMP theme support. | |
* Plugin URI: https://gist.github.com/westonruter/034e15dd7bfd011a9c73c96df86aa291 | |
* Author: Weston Ruter, XWP | |
* Author URI: https://make.xwp.co | |
* | |
* @package AMP_Jetpack_Hacks | |
*/ | |
namespace AMP_Jetpack_Hacks; | |
/** | |
* Returns whether Jetpack is active. | |
* | |
* @param string $module Module to check active. | |
* @return bool Whether active. | |
*/ | |
function is_jetpack_active( $module = null ) { | |
if ( ! class_exists( 'Jetpack' ) ) { | |
return false; | |
} | |
if ( ! \Jetpack::is_active() ) { | |
return false; | |
} | |
if ( $module ) { | |
return \Jetpack::is_module_active( $module ); | |
} | |
return true; | |
} | |
/** | |
* Whether is AMP in theme support. | |
* | |
* @return bool Is Amplified. | |
*/ | |
function is_amp() { | |
if ( ! function_exists( 'is_amp_endpoint' ) ) { | |
return false; | |
} | |
return current_theme_supports( 'amp' ) && \is_amp_endpoint(); | |
} | |
// Disable Comment Likes since not available in AMP. | |
add_action( 'template_redirect', function() { | |
if ( ! is_jetpack_active( 'comment-likes' ) || ! is_amp() ) { | |
return; | |
} | |
$module = \Jetpack_Comment_Likes::init(); | |
remove_filter( 'sharing_show', '__return_false', 100 ); // See <https://github.com/Automattic/amp-wp/pull/1125>. | |
// Undo \Jetpack_Comment_Likes::frontend_init(). | |
remove_action( 'wp_enqueue_scripts', array( $module, 'load_styles_register_scripts' ) ); | |
remove_filter( 'comment_text', array( $module, 'comment_likes' ) ); | |
} ); | |
// Disable Likes since not available in AMP. | |
add_action( 'template_redirect', function() { | |
if ( ! is_jetpack_active( 'likes' ) || ! is_amp() ) { | |
return; | |
} | |
$module = \Jetpack_Likes::init(); | |
// Undo \Jetpack_Likes::action_init(). | |
remove_filter( 'the_content', array( $module, 'post_likes' ), 30 ); | |
remove_filter( 'the_excerpt', array( $module, 'post_likes' ), 30 ); | |
remove_filter( 'post_flair', array( $module, 'post_likes' ), 30 ); | |
remove_filter( 'post_flair_block_css', array( $module, 'post_flair_service_enabled_like' ) ); | |
wp_dequeue_script( 'postmessage' ); | |
wp_dequeue_script( 'jetpack_resize' ); | |
wp_dequeue_script( 'jetpack_likes_queuehandler' ); | |
wp_dequeue_style( 'jetpack_likes' ); | |
} ); | |
// Disable Related Posts since not available in AMP. | |
add_action( 'wp', function() { | |
if ( ! is_amp() ) { | |
return; | |
} | |
add_filter( 'jetpack_relatedposts_filter_enabled_for_request', '__return_false' ); | |
} ); | |
// Override social sharing for AMP. | |
add_action( 'template_redirect', function() { | |
if ( ! is_amp() ) { | |
return; | |
} | |
remove_action( 'wp_head', 'sharing_add_header', 1 ); | |
if ( ! is_jetpack_active( 'sharedaddy' ) ) { | |
return; | |
} | |
add_filter( 'sharing_maybe_enqueue_scripts', '__return_false' ); | |
add_filter( 'jetpack_sharing_counts', '__return_false' ); | |
add_filter( 'sharing_js', '__return_false' ); | |
$sharing_enabled = null; | |
add_filter( 'sharing_enabled', function( $enabled ) use ( &$sharing_enabled ) { | |
$sharing_enabled = $enabled; | |
return $enabled; | |
} ); | |
add_filter( 'jetpack_sharing_display_markup', function( $markup ) use ( &$sharing_enabled ) { | |
remove_action( 'wp_footer', 'sharing_add_footer' ); | |
if ( empty( $sharing_enabled ) ) { | |
return $markup; | |
} | |
$supported_services = array( | |
'facebook' => array( | |
/** This filter is documented in modules/sharedaddy/sharing-sources.php */ | |
'data-param-app_id' => apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' ), | |
), | |
'twitter' => array(), | |
'pinterest' => array(), | |
'whatsapp' => array(), | |
'google-plus-1' => array( | |
'type' => 'gplus', | |
), | |
'tumblr' => array(), | |
'linkedin' => array(), | |
); | |
$sharing_links = array(); | |
foreach ( $sharing_enabled['visible'] as $id => $service ) { | |
if ( ! isset( $supported_services[ $id ] ) ) { | |
$sharing_links[] = "<!-- not supported: $id -->"; | |
continue; | |
} | |
$args = array_merge( | |
array( | |
'type' => $id, | |
), | |
$supported_services[ $id ] | |
); | |
$sharing_link = '<amp-social-share'; | |
foreach ( $args as $key => $value ) { | |
$sharing_link .= sprintf( ' %s="%s"', sanitize_key( $key ), esc_attr( $value ) ); | |
} | |
$sharing_link .= '></amp-social-share>'; | |
$sharing_links[] = $sharing_link; | |
} | |
return preg_replace( '#(?<=<div class="sd-content">).+?(?=</div>)#s', implode( '', $sharing_links ), $markup ); | |
} ); | |
} ); | |
// Disable Jetpack Carousel in AMP. | |
add_action( 'template_redirect', function() { | |
if ( ! is_jetpack_active( 'carousel' ) || ! is_amp() ) { | |
return; | |
} | |
// This needed because the instantiated Jetpack_Carousel is not stored in a variable. | |
global $wp_filter; | |
$carousel_object = null; | |
if ( ! isset( $wp_filter['post_gallery'] ) || ! isset( $wp_filter['post_gallery']->callbacks[-1000] ) ) { | |
return; | |
} | |
$callback = current( $wp_filter['post_gallery']->callbacks[-1000] ); | |
if ( ! isset( $callback['function'] ) || ! is_array( $callback['function'] ) || get_class( $callback['function'][0] ) !== 'Jetpack_Carousel' ) { | |
return; | |
} | |
$carousel_object = $callback['function'][0]; | |
$hooks_to_remove = array( | |
'post_gallery' => array( | |
'check_if_shortcode_processed_and_enqueue_assets' => 1000, | |
'set_in_gallery' => -1000, | |
), | |
'gallery_style' => array( | |
'add_data_to_container' => 10, | |
), | |
'wp_get_attachment_image_attributes' => array( | |
'add_data_to_images' => 10, | |
), | |
'the_content' => array( | |
'add_data_img_tags_and_enqueue_assets' => 10, | |
), | |
); | |
foreach ( $hooks_to_remove as $hook => $callbacks ) { | |
foreach ( $callbacks as $method => $priority ) { | |
remove_filter( $hook, array( $carousel_object, $method ), $priority ); | |
} | |
} | |
} ); | |
// Disable devicepx. | |
add_action( 'template_redirect', function() { | |
if ( is_amp() && class_exists( 'Jetpack' ) ) { | |
remove_action( 'wp_enqueue_scripts', array( \Jetpack::init(), 'devicepx' ) ); | |
} | |
}, 9 ); | |
// Disable photon. | |
add_action( 'template_redirect', function() { | |
if ( is_amp() && class_exists( 'Jetpack_Photon' ) ) { | |
remove_action( 'wp_enqueue_scripts', array( \Jetpack_Photon::instance(), 'action_wp_enqueue_scripts' ), 9 ); | |
} | |
}, 9 ); | |
// Disable notifications. | |
add_action( 'template_redirect', function() { | |
if ( is_amp() && is_jetpack_active( 'notes' ) ) { | |
remove_action( 'wp_head', array( \Jetpack_Notifications::init(), 'styles_and_scripts'), 120 ); | |
} | |
}, 9 ); | |
// Disable milestone widget. | |
add_action( 'template_redirect', function() { | |
if ( ! is_amp() ) { | |
return; | |
} | |
remove_action( 'wp_enqueue_scripts', array( 'Milestone_Widget', 'enqueue_template' ) ); | |
remove_action( 'wp_head', array( 'Milestone_Widget', 'styles_template' ) ); | |
}, 9 ); | |
// Force using separate stylesheets to avoid unnecessary tree shaking. | |
add_filter( 'jetpack_implode_frontend_css', function( $implode ) { | |
if ( is_amp() ) { | |
$implode = false; | |
} | |
return $implode; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will be superceded by Automattic/jetpack#9458