Skip to content
This repository was archived by the owner on Sep 18, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 65 additions & 93 deletions select/google-font-dropdown-custom-control.php
Original file line number Diff line number Diff line change
@@ -1,97 +1,69 @@
<?php

if ( ! class_exists( 'WP_Customize_Control' ) )
return NULL;

/**
* A class to create a dropdown for all google fonts
* A class to create a dropdown for all google fonts.
* Forked from https://github.com/paulund/wordpress-theme-customizer-custom-controls/
*/
class Google_Font_Dropdown_Custom_Control extends WP_Customize_Control
{
private $fonts = false;

public function __construct($manager, $id, $args = array(), $options = array())
{
$this->fonts = $this->get_fonts();
parent::__construct( $manager, $id, $args );
}

/**
* Render the content of the category dropdown
*
* @return HTML
*/
public function render_content()
{
if(!empty($this->fonts))
{
?>
<label>
<span class="customize-category-select-control"><?php echo esc_html( $this->label ); ?></span>
<select <?php $this->link(); ?>>
<?php
foreach ( $this->fonts as $k => $v )
{
printf('<option value="%s" %s>%s</option>', $k, selected($this->value(), $k, false), $v->family);
}
?>
</select>
</label>
<?php
}
}

/**
* Get the google fonts from the API or in the cache
*
* @param integer $amount
*
* @return String
*/
public function get_fonts( $amount = 30 )
{
$selectDirectory = get_stylesheet_directory().'/wordpress-theme-customizer-custom-controls/select/';
$selectDirectoryInc = get_stylesheet_directory().'/inc/wordpress-theme-customizer-custom-controls/select/';

$finalselectDirectory = '';

if(is_dir($selectDirectory))
{
$finalselectDirectory = $selectDirectory;
}

if(is_dir($selectDirectoryInc))
{
$finalselectDirectory = $selectDirectoryInc;
}

$fontFile = $finalselectDirectory . '/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={API_KEY}';

$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);
}
}
}
?>
class Google_Font_Dropdown_Custom_Control extends WP_Customize_Control {
private $fonts = false;

/**
* Fire the constructor up :)
*
* @param [type] $manager [description]
* @param [type] $id [description]
* @param array $args [description]
* @param array $options [description]
*/
public function __construct( $manager, $id, $args = array(), $options = array() ) {
$this->fonts = $this->get_fonts();
parent::__construct( $manager, $id, $args );
}

/**
* Render the content of the category dropdown
*/
public function render_content() {
if ( ! empty( $this->fonts ) ) {
?>
<label>
<span class="customize-category-select-control"><?php echo esc_html( $this->label ); ?></span>
<select <?php $this->link(); ?>>
<?php
foreach ( $this->fonts as $k => $v ) {
printf( '<option value="%s" %s>%s</option>', esc_attr( $k ), selected( $this->value(), esc_html( $k ), false ), $v->family );
}
?>
</select>
</label>
<?php
}
}

/**
* Get the google fonts from the API or in the cache.
*
* @return string
*/
public function get_fonts() {

// If the Google Fonts API is set, then pull data from Google, otherwise default to stored font data
if ( defined( 'GOOGLE_FONTS_API_KEY' ) ) {

// We cache the data from Google
if ( false === ( $fonts = get_transient( $transient_key ) ) ) {
$google_api = 'https://www.googleapis.com/webfonts/v1/webfonts?sort=popularity&key=' . GOOGLE_FONTS_API_KEY;
$fonts = wp_remote_get( $google_api, array( 'sslverify' => false ) );
set_transient( $transient_key, $fonts, HOUR_IN_SECONDS );
}

} else {
$fonts['body'] = file_get_contents( 'google-web-fonts-request.txt' );
}

$content = json_decode( $fonts['body'] );

return $content->items;
}

}
Loading