From a07508ab055851176810c448e88f9b85ad6f7170 Mon Sep 17 00:00:00 2001 From: Mike Little Date: Wed, 10 Apr 2024 16:03:18 +0000 Subject: [PATCH 1/2] Remove S3 Uploads bucket URL from DNS prefetching. Rename `disable_emojis_remove_dns_prefetch` to `remove_domains_from_dns_prefetch` and add a filter to allow for additional domains to be removed. --- inc/namespace.php | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index 4bbe04c..7c1b70f 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -221,23 +221,23 @@ function remove_emoji() { remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); - add_filter( 'wp_resource_hints', __NAMESPACE__ . '\\disable_emojis_remove_dns_prefetch', 10, 2 ); + add_filter( 'wp_resource_hints', __NAMESPACE__ . '\\remove_domains_from_dns_prefetch', 10, 2 ); } /** - * Remove emoji DNS prefetching. + * Remove domains from DNS prefetching. * * @param array $urls URLs for resources. * @param string $relation_type Relation type. * @return array New array with resources. */ -function disable_emojis_remove_dns_prefetch( array $urls, string $relation_type ) : array { +function remove_domains_from_dns_prefetch( array $urls, string $relation_type ) : array { if ( 'dns-prefetch' !== $relation_type ) { return $urls; } - // Strip out any URLs referencing the WordPress.org emoji location. - $emoji_svg_url_bit = 'https://s.w.org/images/core/emoji/'; + // Strip out any URLs we don't want. + $domains_to_remove = get_prefetch_domains_to_remove(); foreach ( $urls as $key => $url ) { if ( is_array( $url ) ) { if ( ! isset( $url['href'] ) ) { @@ -246,13 +246,32 @@ function disable_emojis_remove_dns_prefetch( array $urls, string $relation_type $url = $url['href']; } - if ( strpos( $url, $emoji_svg_url_bit ) !== false ) { - unset( $urls[ $key ] ); - } + array_map( function ( $domain ) use ( $key, $url, $urls ) { + if ( str_contains( $url, $domain ) ) { + unset( $urls[ $key ] ); + } + }, $domains_to_remove ); + } return $urls; } +/** + * Get the domains to remove from DNS prefetching. + * + * @return array + */ +function get_prefetch_domains_to_remove() : array { + $domains = [ + 'https://s.w.org/images/core/emoji/', + ] ; + if ( defined('S3_UPLOADS_BUCKET_URL') ) { + $domains[] = S3_UPLOADS_BUCKET_URL; + } + + return apply_filters( 'altis.cms.prefetch_domains.remove', $domains ); +} + /** * Add the custom login logo to the login page. */ From f537448543e6f575bf3733eb9097e27763783d46 Mon Sep 17 00:00:00 2001 From: Mike Little Date: Wed, 10 Apr 2024 16:32:36 +0000 Subject: [PATCH 2/2] Use the returned modified copy. Remove the empty entries. --- inc/namespace.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index 7c1b70f..d61877e 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -227,8 +227,9 @@ function remove_emoji() { /** * Remove domains from DNS prefetching. * - * @param array $urls URLs for resources. + * @param array $urls URLs for resources. * @param string $relation_type Relation type. + * * @return array New array with resources. */ function remove_domains_from_dns_prefetch( array $urls, string $relation_type ) : array { @@ -246,14 +247,14 @@ function remove_domains_from_dns_prefetch( array $urls, string $relation_type ) $url = $url['href']; } - array_map( function ( $domain ) use ( $key, $url, $urls ) { + $urls = array_map( function ( $domain ) use ( $key, $url, $urls ) { if ( str_contains( $url, $domain ) ) { unset( $urls[ $key ] ); } }, $domains_to_remove ); - } - return $urls; + + return array_filter( $urls ); } /** @@ -264,8 +265,8 @@ function remove_domains_from_dns_prefetch( array $urls, string $relation_type ) function get_prefetch_domains_to_remove() : array { $domains = [ 'https://s.w.org/images/core/emoji/', - ] ; - if ( defined('S3_UPLOADS_BUCKET_URL') ) { + ]; + if ( defined( 'S3_UPLOADS_BUCKET_URL' ) ) { $domains[] = S3_UPLOADS_BUCKET_URL; }