public function render_content()
    {
        $family_val = '';
        $variant_val = '';
        $subsets_val = array();
        $variant_options = array();
        $subsets_options = array();
        $google_fonts = Youxi_Google_Font::fetch();
        $value = Youxi_Google_Font::parse_str($this->value());
        if (isset($value['family'])) {
            $family_val = $value['family'];
            $variant_options = Youxi_Google_Font::get_variants($family_val);
            $subsets_options = Youxi_Google_Font::get_subsets($family_val);
            if (isset($value['variant'])) {
                $variant_val = $value['variant'];
            }
            if (isset($value['subsets']) && is_array($value['subsets'])) {
                $subsets_val = $value['subsets'];
            }
        }
        ?>
<label>

			<?php 
        if (!empty($this->label)) {
            ?>
				<span class="customize-control-title"><?php 
            echo esc_html($this->label);
            ?>
</span>
			<?php 
        }
        if (!empty($this->description)) {
            ?>
				<span class="description customize-control-description"><?php 
            echo $this->description;
            ?>
</span>
			<?php 
        }
        ?>

			<input type="hidden" class="youxi-google-font-value" <?php 
        $this->link();
        ?>
>

			<select class="youxi-google-font-control youxi-google-font-family" style="width: 100%">
				<option value=""<?php 
        selected('', $family_val);
        ?>
><?php 
        esc_html_e('Inherit', 'youxi');
        ?>
</option>
				<?php 
        foreach ($google_fonts as $font) {
            $font_family = preg_replace('/\\s+/', '+', $font['family']);
            ?>
				<option value="<?php 
            echo esc_attr($font_family);
            ?>
"<?php 
            selected($font_family, $family_val);
            ?>
><?php 
            echo esc_html($font['family']);
            ?>
</option>
				<?php 
        }
        ?>
			</select>

			<select class="youxi-google-font-control youxi-google-font-variant" style="width: 100%;<?php 
        if (!$family_val) {
            echo ' display: none;';
        }
        ?>
"<?php 
        disabled('', $family_val);
        ?>
>
				<option value=""><?php 
        esc_html_e('Select a Font Variant', 'youxi');
        ?>
</option>
				<?php 
        foreach ($variant_options as $font_variant) {
            ?>
				<option value="<?php 
            echo esc_attr($font_variant);
            ?>
"<?php 
            selected($variant_val, $font_variant);
            ?>
><?php 
            echo esc_html($font_variant);
            ?>
</option>
				<?php 
        }
        ?>
			</select>

			<div class="youxi-google-font-control youxi-google-font-subsets">
				<?php 
        foreach ($subsets_options as $subset) {
            ?>
				<label>
					<input type="checkbox" value="<?php 
            echo esc_attr($subset);
            ?>
"<?php 
            checked(true, in_array($subset, $subsets_val));
            ?>
>
					<?php 
            echo esc_html($subset);
            ?>
					<br>
				</label>
				<?php 
        }
        ?>
			</div>

		</label>
		<?php 
    }
Esempio n. 2
0
 /**
  * Fetch Google Fonts and cache it for a week
  */
 public static function fetch()
 {
     /* See if we have the fonts in internal cache */
     if (!is_null(self::$cached_fonts) && is_array(self::$cached_fonts) && !empty(self::$cached_fonts)) {
         return self::$cached_fonts;
     }
     /* Try to get Google Fonts first in case some external code provides the fonts */
     $google_fonts = apply_filters('youxi_google_fonts_cache', array());
     /* If no fonts are fetched, get it from cache */
     if (empty($google_fonts)) {
         /* Google Fonts cache key */
         $google_fonts_cache_key = apply_filters('youxi_google_fonts_cache_key', 'youxi_google_fonts_cache');
         $google_fonts = get_transient($google_fonts_cache_key);
     }
     /* If we still don't have the fonts, let's get it directly from Google */
     if (!is_array($google_fonts) || empty($google_fonts)) {
         $google_fonts = array();
         /* API url and key */
         $google_fonts_api_url = apply_filters('youxi_google_fonts_api_url', 'https://www.googleapis.com/webfonts/v1/webfonts');
         $google_fonts_api_key = apply_filters('youxi_google_fonts_api_key', 'AIzaSyC-0ipgZdTRp2jeOct8w9GuPqjBX5LDDHE');
         /* API arguments */
         $google_fonts_fields = apply_filters('youxi_google_fonts_fields', array('family', 'variants', 'subsets'));
         $google_fonts_sort = apply_filters('youxi_google_fonts_sort', 'alpha');
         /* Initiate API request */
         $google_fonts_query_args = array('key' => $google_fonts_api_key, 'fields' => 'items(' . implode(',', $google_fonts_fields) . ')', 'sort' => $google_fonts_sort);
         /* Build and make the request */
         $google_fonts_query = esc_url_raw(add_query_arg($google_fonts_query_args, $google_fonts_api_url));
         $google_fonts_response = wp_safe_remote_get($google_fonts_query, array('sslverify' => false, 'timeout' => 15));
         /* continue if we got a valid response */
         if (200 == wp_remote_retrieve_response_code($google_fonts_response)) {
             if ($response_body = wp_remote_retrieve_body($google_fonts_response)) {
                 /* JSON decode the response body and cache the result */
                 $google_fonts_data = json_decode(trim($response_body), true);
                 if (is_array($google_fonts_data) && isset($google_fonts_data['items'])) {
                     $google_fonts = $google_fonts_data['items'];
                     $google_font_keys = wp_list_pluck($google_fonts, 'family');
                     $google_font_keys = implode('|', $google_font_keys);
                     $google_font_keys = preg_replace('/\\s+/', '+', $google_font_keys);
                     $google_font_keys = explode('|', $google_font_keys);
                     $google_fonts = array_combine($google_font_keys, $google_fonts);
                     set_transient($google_fonts_cache_key, $google_fonts, WEEK_IN_SECONDS);
                 }
             }
         }
     }
     return self::$cached_fonts = is_array($google_fonts) ? $google_fonts : array();
 }
 public function js_vars()
 {
     return Youxi_Google_Font::fetch();
 }
Esempio n. 4
0
 public static function is_valid_font($type, $value)
 {
     switch ($type) {
         case 'typekit':
             $result = Youxi_Typekit::to_css($value);
             break;
         case 'google':
             $result = Youxi_Google_Font::to_css($value);
             break;
         case 'websafe':
             $result = Youxi_Websafe::to_css($value);
             break;
     }
     return isset($result);
 }