Page MenuHomePhabricator
Paste P16237

Adopt Google Font proxy for Wikimedia.it WordPress theme
ActivePublic

Authored by valerio.bozzolan on May 29 2021, 10:04 PM.
<?php
/*
* Enable a Google Font proxy
*
* Author: Valerio Bozzolan
* License: GNU General Public License v2 or later
* Date: 2021
*
* See https://phabricator.wikimedia.org/T282613
* See https://gitpull.it/T776
* See https://gitpull.it/R26:f8b39ca15e8ba48a92fb0d58c2772e9b02691059
*/
/**
* Local Google Font cache
*/
define( 'GOOGLE_FONT_PROXY', 'fonts.wikimedia.it' );
/**
* Convert a Google Font domain to your local proxy cache
*
* See https://phabricator.wikimedia.org/T266998
*/
function convert_google_font_to_local_proxy( $url ) {
return str_replace( 'fonts.googleapis.com', GOOGLE_FONT_PROXY, $url );
}
/**
* Avoid Google Fonts in your stylesheets
*
* https://phabricator.wikimedia.org/T282613
*/
add_filter( 'style_loader_src', 'convert_google_font_to_local_proxy' );
/**
* Avoid Google Fonts in your DNS prefetch
*
* https://phabricator.wikimedia.org/T282613
*/
add_filter( 'wp_resource_hints', function( $urls ) {
foreach( $urls as & $url ) {
$url = convert_google_font_to_local_proxy( $url );
}
return $urls;
} );