Skip to content

Commit 7244902

Browse files
committed
Eliminate emoji handling altogether since most browsers now support natively
1 parent 92453d4 commit 7244902

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

includes/class-amp-theme-support.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -849,19 +849,14 @@ public static function add_hooks() {
849849
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.
850850
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>.
851851

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

866861
// @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.
867862
// @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.
@@ -916,6 +911,28 @@ static function() {
916911
);
917912
}
918913

914+
/**
915+
* Filter resource hints to remove the emoji CDN (s.w.org).
916+
*
917+
* @since 2.2
918+
* @see wp_resource_hints()
919+
*
920+
* @param string[] $urls URLs.
921+
* @param string $type Resource hint relation.
922+
* @return string[] Filtered URLs.
923+
*/
924+
public static function filter_resource_hints_to_remove_emoji_dns_prefetch( $urls, $type ) {
925+
if ( 'dns-prefetch' === $type ) {
926+
$urls = array_filter(
927+
$urls,
928+
static function ( $url ) {
929+
return 's.w.org' !== wp_parse_url( $url, PHP_URL_HOST );
930+
}
931+
);
932+
}
933+
return $urls;
934+
}
935+
919936
/**
920937
* Register/override widgets.
921938
*
@@ -2246,10 +2263,13 @@ public static function enqueue_assets() {
22462263
/**
22472264
* Print the important emoji-related styles.
22482265
*
2266+
* @deprecated No longer used since platforms/browsers now support emoji natively. See <https://core.trac.wordpress.org/ticket/35498#comment:7>.
2267+
* @codeCoverageIgnore
22492268
* @see print_emoji_styles()
22502269
* @staticvar bool $printed
22512270
*/
22522271
public static function print_emoji_styles() {
2272+
_deprecated_function( __FUNCTION__, '2.2.0' );
22532273
static $printed = false;
22542274

22552275
if ( $printed ) {

tests/php/test-class-amp-theme-support.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -775,14 +775,9 @@ public function test_add_hooks() {
775775
$this->assertFalse( has_action( 'wp_head', 'wp_post_preview_js' ) );
776776
$this->assertFalse( has_action( 'wp_head', 'wp_oembed_add_host_js' ) );
777777

778+
$this->assertEquals( 10, has_filter( 'wp_resource_hints', [ self::TESTED_CLASS, 'filter_resource_hints_to_remove_emoji_dns_prefetch' ] ) );
778779
$this->assertFalse( has_action( 'wp_head', 'print_emoji_detection_script' ) );
779780
$this->assertFalse( has_action( 'wp_print_styles', 'print_emoji_styles' ) );
780-
$this->assertEquals( 10, has_action( 'wp_print_styles', [ AMP_Theme_Support::class, 'print_emoji_styles' ] ) );
781-
$this->assertEquals( 10, has_filter( 'the_title', 'wp_staticize_emoji' ) );
782-
$this->assertEquals( 10, has_filter( 'the_excerpt', 'wp_staticize_emoji' ) );
783-
$this->assertEquals( 10, has_filter( 'the_content', 'wp_staticize_emoji' ) );
784-
$this->assertEquals( 10, has_filter( 'comment_text', 'wp_staticize_emoji' ) );
785-
$this->assertEquals( 10, has_filter( 'widget_text', 'wp_staticize_emoji' ) );
786781

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

795+
/** @covers AMP_Theme_Support::filter_resource_hints_to_remove_emoji_dns_prefetch() */
796+
public function test_filter_resource_hints_to_remove_emoji_dns_prefetch() {
797+
$hints = [
798+
'preconnect' => [
799+
'https://example.com/',
800+
],
801+
'dns-prefetch' => [
802+
'https://example.prg/',
803+
apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/13.0.0/svg/' ),
804+
],
805+
];
806+
807+
$filtered_hints = [];
808+
foreach ( $hints as $rel => $urls ) {
809+
$filtered_hints[ $rel ] = AMP_Theme_Support::filter_resource_hints_to_remove_emoji_dns_prefetch( $urls, $rel );
810+
}
811+
812+
$this->assertEquals( $hints['preconnect'], $filtered_hints['preconnect'] );
813+
$this->assertNotEquals( $hints['dns-prefetch'], $filtered_hints['dns-prefetch'] );
814+
$this->assertEquals( [ 'https://example.prg/' ], $filtered_hints['dns-prefetch'] );
815+
}
816+
800817
/**
801818
* Test register_content_embed_handlers.
802819
*

0 commit comments

Comments
 (0)