Sin descripción

GoogleFonts.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. class PUM_Integration_GoogleFonts {
  6. /**
  7. * @var string
  8. */
  9. private static $default_api_key = 'AIzaSyA1Q0uFOhEh3zv_Pk31FqlACArFquyBeQU';
  10. /**
  11. * @var string
  12. */
  13. public static $api_key;
  14. /**
  15. *
  16. */
  17. public static function init() {
  18. // Set the API key based on options first then default second.
  19. self::$api_key = pum_get_option( 'google_fonts_api_key', self::$default_api_key );
  20. add_filter( 'pum_theme_font_family_options', array( __CLASS__, 'font_family_options' ), 20 );
  21. }
  22. /**
  23. * Loads a static backup list of Google Fonts in case the API is not responding.
  24. *
  25. * @return array|mixed|object
  26. */
  27. public static function load_backup_fonts() {
  28. $json_data = file_get_contents( Popup_Maker::$DIR . 'includes/google-fonts.json' );
  29. return json_decode( $json_data, true );
  30. }
  31. /**
  32. * Fetch list of Google Fonts or fallback to local list.
  33. *
  34. * @param string $sort
  35. *
  36. * @return array|mixed
  37. */
  38. public static function fetch_fonts( $sort = 'alpha' ) {
  39. if ( false !== $font_list = get_site_transient( 'pum_google_fonts_list' ) ) {
  40. return $font_list;
  41. }
  42. $google_api_url = 'https://www.googleapis.com/webfonts/v1/webfonts?key=' . self::$api_key . '&sort=' . $sort;
  43. $response = wp_remote_retrieve_body( wp_remote_get( $google_api_url, array( 'sslverify' => false ) ) );
  44. if ( ! is_wp_error( $response ) ) {
  45. $data = json_decode( $response, true );
  46. }
  47. // Store transient for a long time after fetching from Google to save API key hits.
  48. $transient_time = self::$api_key == self::$default_api_key ? 8 * WEEK_IN_SECONDS : 1 * WEEK_IN_SECONDS;
  49. if ( ! empty( $data['errors'] ) || empty( $data['items'] ) ) {
  50. $data = self::load_backup_fonts();
  51. // Store transient for short period.
  52. $transient_time = 1 * DAY_IN_SECONDS;
  53. }
  54. $items = $data['items'];
  55. $font_list = array();
  56. if ( count( $items ) ) {
  57. foreach ( $items as $item ) {
  58. $font_list[ $item['family'] ] = $item;
  59. }
  60. }
  61. set_site_transient( 'pum_google_fonts_list', $font_list, $transient_time );
  62. return $font_list;
  63. }
  64. /**
  65. * Adds options to the font family dropdowns.
  66. *
  67. * @param $options
  68. *
  69. * @return array
  70. */
  71. public static function font_family_options( $options ) {
  72. $font_list = self::fetch_fonts();
  73. if ( empty( $font_list ) ) {
  74. return $options;
  75. }
  76. $new_options = array(
  77. );
  78. // $options = array_merge( $options, array(
  79. // '' => __( 'Google Web Fonts', 'popup-maker' ) . ' &#10549;',
  80. // ) );
  81. foreach ( $font_list as $font_family => $font ) {
  82. $new_options[ $font_family ] = $font_family;
  83. }
  84. $options[__( 'Google Web Fonts', 'popup-maker' )] = $new_options;
  85. return $options;
  86. }
  87. }