Skip to content

Instantly share code, notes, and snippets.

@generatepress
Created July 18, 2014 00:44
Show Gist options
  • Save generatepress/1c4b507dbc4080149ae1 to your computer and use it in GitHub Desktop.
Save generatepress/1c4b507dbc4080149ae1 to your computer and use it in GitHub Desktop.
function generate_get_fonts( $amount = 1000 )
{
if ( get_transient('generate_get_fonts') )
return;
$selectDirectory = '';
$selectDirectory = plugin_dir_path( __FILE__ );
$fontFile = $selectDirectory . '/cache/google-web-fonts.txt';
//Total time the file will be cached in seconds, set to a week
$cachetime = 86400 * 7;
if(file_exists($fontFile) && $cachetime < filemtime($fontFile))
{
$content = json_decode(file_get_contents($fontFile));
} else {
$googleApi = 'https://www.googleapis.com/webfonts/v1/webfonts?sort=popularity&key=AIzaSyAfzHmLjE4RBF6kSi3KW8ZD-rzhxSJP9C4';
$fontContent = wp_remote_get( $googleApi, array('sslverify' => false) );
$fp = fopen($fontFile, 'w');
fwrite($fp, $fontContent['body']);
fclose($fp);
$content = json_decode($fontContent['body']);
}
set_transient('generate_get_fonts', $content->items, WEEK_IN_SECONDS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment