Google_Font_Dropdown_Custom_Control

A class to create a dropdown for all google fonts.

Defined (1)

The class is defined in the following location(s).

/lib/customizer/includes/googlefont-control.php  
  1. class Google_Font_Dropdown_Custom_Control extends WP_Customize_Control { 
  2.  
  3. private $fonts = false; 
  4.  
  5. public function __construct( $manager, $id, $args = array(), $options = array() ) { 
  6. $this->fonts = $this->get_fonts( $args['amount'] ); 
  7.  
  8. parent::__construct( $manager, $id, $args ); 
  9.  
  10. /** 
  11. * Render the content on the theme customizer page 
  12. */ 
  13. public function render_content() { 
  14.  
  15. if ( empty( $this->fonts ) ) { 
  16. return false; 
  17. ?> 
  18. <label> 
  19. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span> 
  20. <span class="description customize-control-description"><?php echo esc_attr( $this->description ); ?></span> 
  21. <select <?php $this->link(); ?>> 
  22. <?php 
  23. foreach ( $this->fonts as $k => $v ) { 
  24. printf( '<option value="%s" %s>%s</option>', $k, selected( $this->value(), $k, false ), $v->family ); 
  25. ?> 
  26. </select> 
  27. </label> 
  28. <?php 
  29.  
  30. /** 
  31. * Get the google fonts from the API or in the cache 
  32. * @param integer $amount 
  33. * @return String 
  34. */ 
  35. public function get_fonts( $amount = 500, $api_key = 'AIzaSyBa9fRtVKujSxVn2uoxtqa3DbR-fCym1ig' ) { 
  36.  
  37. global $onetone_google_fonts; 
  38.  
  39. $selectDirectory = ONETONE_DIR . '/includes/'; 
  40. $selectDirectoryInc = ONETONE_DIR . '/includes/'; 
  41.  
  42. $finalselectDirectory = ''; 
  43.  
  44. if ( is_dir( $selectDirectory ) ) { 
  45. $finalselectDirectory = $selectDirectory; 
  46.  
  47. if ( is_dir( $selectDirectoryInc ) ) { 
  48. $finalselectDirectory = $selectDirectoryInc; 
  49.  
  50. $fontFile = $finalselectDirectory . '/google-web-fonts.php'; 
  51.  
  52. //Total time the file will be cached in seconds, set to a week 
  53. $cachetime = 86400 * 7; 
  54.  
  55. if ( file_exists( $fontFile ) && $cachetime < filemtime( $fontFile ) ) { 
  56. require_once $fontFile; 
  57. $content = json_decode( $onetone_google_fonts ); 
  58. } else {  
  59. $googleApi = '//www.googleapis.com/webfonts/v1/webfonts?sort=popularity&key=' . $api_key; 
  60. $fontContent = wp_remote_get( $googleApi, array( 'sslverify' => false ) ); 
  61. $content = json_decode( $fontContent['body'] ); 
  62.  
  63. if ( $amount == 'all' ) { 
  64. return $content->items; 
  65. } else { 
  66. return array_slice( $content->items, 0, $amount );