Skip to content

Instantly share code, notes, and snippets.

@Misplon
Last active May 4, 2022 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Misplon/ae1360916989e282830e3f45242055d4 to your computer and use it in GitHub Desktop.
Save Misplon/ae1360916989e282830e3f45242055d4 to your computer and use it in GitHub Desktop.
SiteOrigin Vantage Update Google Fonts Array
#!/usr/bin/php -q
<?php
/* To update the SiteOrigin Vantage Google Fonts array, insert this file fetch_google_fonts.php
into the inc/customizer directory along with a file named google-key.php containing your Google Fonts API key.
From the command line, navigate to inc/customizer and run `php ./fetch_google_fonts.php`.
Once done, remove the fetch_google_fonts.php and google-key.php file from the inc/customizer directory. */
function get_data( $url ) {
$ch = curl_init();
$timeout = 5;
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
$data = curl_exec( $ch );
curl_close( $ch );
return $data;
}
// Add your Google webfont key here <https://developers.google.com/fonts/docs/developer_api?hl=en#APIKey>
$key = trim( file_get_contents( __DIR__ . '/google-key.php' ) );
echo 'KEY: ' . $key . "\n";
echo 'Fetching fonts from: ' . 'https://www.googleapis.com/webfonts/v1/webfonts?key=' . urlencode( $key ) . "\n";
$response = get_data( 'https://www.googleapis.com/webfonts/v1/webfonts?key=' . urlencode( $key ) );
$fonts = json_decode( $response, true )['items'];
$return = array();
foreach( $fonts as $font ) {
$return[ $font['family'] ] = $font['variants'];
}
// Sort the keys, just in case.
ksort( $return );
echo 'Writing fonts to: ' . realpath( dirname(__FILE__) . '/google-fonts.php' ) . "\n";
$contents = "<?php\n\n";
$contents .= 'return ' . str_replace(' ', "\t", var_export( $return, true ) ) . ';';
file_put_contents( realpath( dirname(__FILE__). '/google-fonts.php' ), $contents );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment