Skip to content

Commit

Permalink
Remove S3 Uploads bucket URL from DNS prefetching.
Browse files Browse the repository at this point in the history
Rename `disable_emojis_remove_dns_prefetch` to `remove_domains_from_dns_prefetch` and add a filter to allow for additional domains to be removed.
  • Loading branch information
mikelittle committed Apr 10, 2024
1 parent 56ba867 commit cef0544
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand All @@ -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.
*/
Expand Down

0 comments on commit cef0544

Please sign in to comment.