customs
5 months ago
holders
5 months ago
checkbox.php
5 months ago
color-and-opacity.php
5 months ago
color.php
5 months ago
datepicker-range.php
5 months ago
dropdown-and-colors.php
5 months ago
dropdown.php
5 months ago
font.php
5 months ago
google-font.php
5 months ago
gradient.php
5 months ago
hidden.php
5 months ago
index.php
6 months ago
integer.php
5 months ago
list.php
5 months ago
multiple-textbox.php
5 months ago
paddings-editor.php
5 months ago
pattern.php
5 months ago
radio-colors.php
5 months ago
radio.php
5 months ago
textarea.php
5 months ago
textbox.php
5 months ago
url.php
5 months ago
wp-editor.php
5 months ago
google-font.php
146 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Dropdown List Control |
| 5 | * |
| 6 | * Main options: |
| 7 | * name => a name of the control |
| 8 | * value => a value to show in the control |
| 9 | * default => a default value of the control if the "value" option is not specified |
| 10 | * items => a callback to return items or an array of items to select |
| 11 | * |
| 12 | * @package core |
| 13 | * @since 1.0.0 |
| 14 | */ |
| 15 | class Wbcr_FactoryForms600_GoogleFontControl extends Wbcr_FactoryForms600_FontControl { |
| 16 | |
| 17 | public $type = 'google-font'; |
| 18 | const APIKEY = 'AIzaSyB-3vazYv7Q-5QZA04bmSKFrWcw_VhC40w'; |
| 19 | |
| 20 | public function __construct( $options, $form, $provider = null ) { |
| 21 | parent::__construct( $options, $form, $provider ); |
| 22 | $this->addCssClass( 'factory-font' ); |
| 23 | |
| 24 | $option_google_font_data = [ |
| 25 | 'name' => $this->options['name'] . '__google_font_data', |
| 26 | 'cssClass' => 'factory-google-font-data', |
| 27 | ]; |
| 28 | |
| 29 | $this->google_font_data = new Wbcr_FactoryForms600_HiddenControl( $option_google_font_data, $form, $provider ); |
| 30 | $this->inner_controls[] = $this->google_font_data; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @return array|mixed |
| 35 | */ |
| 36 | public function getDefaultFonts() { |
| 37 | |
| 38 | $cache_fonts = get_transient( 'wbcr_factory_google_fonts' ); |
| 39 | |
| 40 | if ( ! empty( $cache_fonts ) ) { |
| 41 | return $cache_fonts; |
| 42 | } |
| 43 | |
| 44 | $google_fonts = $this->getGoogleFonts(); |
| 45 | |
| 46 | $fonts = [ |
| 47 | [ 'inherit', '(' . __( 'Use default website font', 'robin-image-optimizer' ) . ')' ], |
| 48 | ]; |
| 49 | |
| 50 | $fontsCommon = [ |
| 51 | 'group', |
| 52 | __( 'Standard:', 'robin-image-optimizer' ), |
| 53 | [ |
| 54 | |
| 55 | [ 'Arial, "Helvetica Neue", Helvetica, sans-serif', 'Arial' ], |
| 56 | [ '"Helvetica Neue", Helvetica, Arial, sans-serif', 'Helvetica' ], |
| 57 | [ 'Tahoma, Verdana, Segoe, sans-serif', 'Tahoma' ], |
| 58 | [ 'Verdana, Geneva, sans-serif', 'Verdana' ], |
| 59 | |
| 60 | ], |
| 61 | ]; |
| 62 | |
| 63 | $fontsGoogleFonts = [ 'group', __( 'Google Fonts:', 'robin-image-optimizer' ), [] ]; |
| 64 | |
| 65 | foreach ( $google_fonts->items as $item ) { |
| 66 | |
| 67 | $alt_font = $item->category; |
| 68 | if ( in_array( $alt_font, [ 'handwriting', 'display' ] ) ) { |
| 69 | $alt_font = 'serif'; |
| 70 | } |
| 71 | |
| 72 | $listItem = [ |
| 73 | 'title' => $item->family, |
| 74 | 'value' => $item->family . ', ' . $item->category, |
| 75 | 'hint' => '<em>Google Font</em>', |
| 76 | 'data' => [ |
| 77 | 'google-font' => true, |
| 78 | 'family' => $item->family, |
| 79 | 'variants' => $item->variants, |
| 80 | 'subsets' => $item->subsets, |
| 81 | ], |
| 82 | ]; |
| 83 | |
| 84 | $fontsGoogleFonts[2][] = $listItem; |
| 85 | } |
| 86 | |
| 87 | $fonts[] = $fontsCommon; |
| 88 | $fonts[] = $fontsGoogleFonts; |
| 89 | |
| 90 | set_transient( 'wbcr_factory_google_fonts', $fonts, 60 * 60 * 6 ); |
| 91 | |
| 92 | return $fonts; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @return array|mixed|object |
| 97 | */ |
| 98 | protected function getGoogleFonts() { |
| 99 | |
| 100 | $body = get_transient( 'wbcr_factory_google_fonts_raw' ); |
| 101 | if ( ! empty( $body ) ) { |
| 102 | return $body; |
| 103 | } |
| 104 | |
| 105 | $response = wp_remote_get( sprintf( 'https://www.googleapis.com/webfonts/v1/webfonts?key=%s', self::APIKEY ) ); |
| 106 | |
| 107 | $this->error = false; |
| 108 | $this->defailed_error = false; |
| 109 | |
| 110 | if ( is_wp_error( $response ) ) { |
| 111 | |
| 112 | $this->error = __( 'Unable to retrieve the list of Google Fonts.', 'robin-image-optimizer' ); |
| 113 | $this->defailed_error = $response->get_error_message(); |
| 114 | |
| 115 | return $body; |
| 116 | } |
| 117 | |
| 118 | if ( ! isset( $response['body'] ) ) { |
| 119 | |
| 120 | $this->error = __( 'Invalid response from the Google Fonts API.', 'robin-image-optimizer' ); |
| 121 | $this->defailed_error = $response['body']; |
| 122 | |
| 123 | return $body; |
| 124 | } |
| 125 | |
| 126 | $body = json_decode( $response['body'] ); |
| 127 | |
| 128 | if ( empty( $body->items ) ) { |
| 129 | |
| 130 | $this->error = __( 'Unexpected error. The list of Google Fonts is empty.', 'robin-image-optimizer' ); |
| 131 | |
| 132 | return $body; |
| 133 | } |
| 134 | |
| 135 | set_transient( 'wbcr_factory_google_fonts_raw', $body, 60 * 60 * 6 ); |
| 136 | |
| 137 | return $body; |
| 138 | } |
| 139 | |
| 140 | public function afterControlsHtml() { |
| 141 | ?> |
| 142 | <?php $this->google_font_data->html(); ?> |
| 143 | <?php |
| 144 | } |
| 145 | } |
| 146 |