Skip to content

Instantly share code, notes, and snippets.

@generatepress
Created July 14, 2014 20:04
Show Gist options
  • Save generatepress/444fdda2c1bc473bc7cd to your computer and use it in GitHub Desktop.
Save generatepress/444fdda2c1bc473bc7cd to your computer and use it in GitHub Desktop.
public function get_fonts( $amount = 5000 )
{
$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']);
}
if($amount == 'all')
{
return $content->items;
} else {
return array_slice($content->items, 0, $amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment