Skip to content

Commit

Permalink
Merge pull request #805 from humanmade/backport-802-to-v16-branch
Browse files Browse the repository at this point in the history
[Backport v16-branch] 1521 remove altis.cloud from dns prefetch
  • Loading branch information
mikelittle authored Apr 11, 2024
2 parents 56ba867 + f286550 commit d13bd06
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,24 @@ 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 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,11 +247,30 @@ 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 ] );
}
$urls = array_map( function ( $domain ) use ( $key, $url, $urls ) {
if ( str_contains( $url, $domain ) ) {
unset( $urls[ $key ] );
}
}, $domains_to_remove );
}

return array_filter( $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 $urls;

return apply_filters( 'altis.cms.prefetch_domains.remove', $domains );
}

/**
Expand Down

0 comments on commit d13bd06

Please sign in to comment.