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

Eliminate static emoji handling since most browsers now support natively #6717

Merged
merged 1 commit into from
Nov 15, 2021
Merged
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
46 changes: 33 additions & 13 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -849,19 +849,14 @@ public static function add_hooks() {
remove_action( 'wp_head', 'wp_post_preview_js', 1 ); // @todo Instead of function, the script output by wp_post_preview_js() should get data-ampdevmode.
remove_action( 'wp_head', 'wp_oembed_add_host_js' ); // This is not needed when post embeds are embedded via <amp-wordpress-embed>. See <https://github.com/ampproject/amp-wp/issues/809>.

// Replace JS-based emoji with PHP-based, if the JS-based emoji replacement was not already removed.
if ( has_action( 'wp_head', 'print_emoji_detection_script' ) ) {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'wp_footer', 'gutenberg_the_skip_link' ); // Temporary workaround for <https://github.com/ampproject/amp-wp/issues/6115>.
remove_action( 'wp_footer', 'the_block_template_skip_link' ); // Temporary workaround for <https://github.com/ampproject/amp-wp/issues/6115>.
add_action( 'wp_print_styles', [ __CLASS__, 'print_emoji_styles' ] );
add_filter( 'the_title', 'wp_staticize_emoji' );
add_filter( 'the_excerpt', 'wp_staticize_emoji' );
add_filter( 'the_content', 'wp_staticize_emoji' );
add_filter( 'comment_text', 'wp_staticize_emoji' );
add_filter( 'widget_text', 'wp_staticize_emoji' );
}
// Prevent emoji detection and emoji loading since platforms/browsers now support emoji natively (and Twemoji is not AMP-compatible).
add_filter( 'wp_resource_hints', [ __CLASS__, 'filter_resource_hints_to_remove_emoji_dns_prefetch' ], 10, 2 );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );

// Temporary workarounds for <https://github.com/ampproject/amp-wp/issues/6115>.
remove_action( 'wp_footer', 'gutenberg_the_skip_link' );
remove_action( 'wp_footer', 'the_block_template_skip_link' );

// @todo The wp_mediaelement_fallback() should still run to be injected inside of the audio/video generated by wp_audio_shortcode()/wp_video_shortcode() respectively.
// @todo When custom scripts appear on the page, this logic should be skipped. So the removal of MediaElement.js script & styles should perhaps be done by the script sanitizer instead.
Expand Down Expand Up @@ -916,6 +911,28 @@ static function() {
);
}

/**
* Filter resource hints to remove the emoji CDN (s.w.org).
*
* @since 2.2
* @see wp_resource_hints()
*
* @param string[] $urls URLs.
* @param string $type Resource hint relation.
* @return string[] Filtered URLs.
*/
public static function filter_resource_hints_to_remove_emoji_dns_prefetch( $urls, $type ) {
if ( 'dns-prefetch' === $type ) {
$urls = array_filter(
$urls,
static function ( $url ) {
return 's.w.org' !== wp_parse_url( $url, PHP_URL_HOST );
}
);
}
return $urls;
}

/**
* Register/override widgets.
*
Expand Down Expand Up @@ -2246,10 +2263,13 @@ public static function enqueue_assets() {
/**
* Print the important emoji-related styles.
*
* @deprecated No longer used since platforms/browsers now support emoji natively. See <https://core.trac.wordpress.org/ticket/35498#comment:7>.
* @codeCoverageIgnore
* @see print_emoji_styles()
* @staticvar bool $printed
*/
public static function print_emoji_styles() {
_deprecated_function( __FUNCTION__, '2.2.0' );
static $printed = false;

if ( $printed ) {
Expand Down
29 changes: 23 additions & 6 deletions tests/php/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,14 +775,9 @@ public function test_add_hooks() {
$this->assertFalse( has_action( 'wp_head', 'wp_post_preview_js' ) );
$this->assertFalse( has_action( 'wp_head', 'wp_oembed_add_host_js' ) );

$this->assertEquals( 10, has_filter( 'wp_resource_hints', [ self::TESTED_CLASS, 'filter_resource_hints_to_remove_emoji_dns_prefetch' ] ) );
$this->assertFalse( has_action( 'wp_head', 'print_emoji_detection_script' ) );
$this->assertFalse( has_action( 'wp_print_styles', 'print_emoji_styles' ) );
$this->assertEquals( 10, has_action( 'wp_print_styles', [ AMP_Theme_Support::class, 'print_emoji_styles' ] ) );
$this->assertEquals( 10, has_filter( 'the_title', 'wp_staticize_emoji' ) );
$this->assertEquals( 10, has_filter( 'the_excerpt', 'wp_staticize_emoji' ) );
$this->assertEquals( 10, has_filter( 'the_content', 'wp_staticize_emoji' ) );
$this->assertEquals( 10, has_filter( 'comment_text', 'wp_staticize_emoji' ) );
$this->assertEquals( 10, has_filter( 'widget_text', 'wp_staticize_emoji' ) );

$this->assertEquals( 20, has_action( 'wp_head', 'amp_add_generator_metadata' ) );
$this->assertEquals( 0, has_action( 'wp_enqueue_scripts', [ self::TESTED_CLASS, 'enqueue_assets' ] ) );
Expand All @@ -797,6 +792,28 @@ public function test_add_hooks() {
$this->assertEquals( PHP_INT_MAX, has_filter( 'get_header_image_tag', [ self::TESTED_CLASS, 'amend_header_image_with_video_header' ] ) );
}

/** @covers AMP_Theme_Support::filter_resource_hints_to_remove_emoji_dns_prefetch() */
public function test_filter_resource_hints_to_remove_emoji_dns_prefetch() {
$hints = [
'preconnect' => [
'https://example.com/',
],
'dns-prefetch' => [
'https://example.prg/',
apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/13.0.0/svg/' ),
],
];

$filtered_hints = [];
foreach ( $hints as $rel => $urls ) {
$filtered_hints[ $rel ] = AMP_Theme_Support::filter_resource_hints_to_remove_emoji_dns_prefetch( $urls, $rel );
}

$this->assertEquals( $hints['preconnect'], $filtered_hints['preconnect'] );
$this->assertNotEquals( $hints['dns-prefetch'], $filtered_hints['dns-prefetch'] );
$this->assertEquals( [ 'https://example.prg/' ], $filtered_hints['dns-prefetch'] );
}

/**
* Test register_content_embed_handlers.
*
Expand Down