/** * Render the control. * * @return void */ public function render_content() { $thb_theme = thb_theme(); $fonts = thb_get_fonts(); ?> <label> <span class="customize-control-title"><?php echo esc_html($this->label); ?> </span> <select <?php $this->link(); ?> class="font-family"> <?php foreach ($fonts as $style => $families) { ?> <optgroup label="<?php echo $style; ?> "> <?php foreach ($families as $family => $font) { ?> <?php if (is_array($font)) { ?> <option value="<?php echo $family; ?> "><?php echo $font['family']; ?> </option> <?php } ?> <?php } ?> </optgroup> <?php } ?> </select> </label> <?php }
/** * Include the fonts in the generated stylesheet. * * @return void */ private function importFonts() { $imported_fonts = array('google' => array(), 'custom' => array()); $google_fonts = array(); $fonts = thb_get_fonts(); $current_font = ''; $current_font_type = ''; foreach ($this->_sections as $section) { foreach ($section['settings'] as $setting) { if ($setting['type'] === 'THB_FontFamily_Control') { $value = get_theme_mod($setting['key']); if (empty($value)) { break; } foreach ($fonts as $style => $families) { foreach ($families as $css => $data) { if ($css == $value) { $current_font = $value; $current_font_type = $data['type']; $current_font_folder = isset($data['folder']) ? $data['folder'] : ''; $imported_fonts[$data['type']][$value]['folder'] = $current_font_folder; if (!isset($imported_fonts[$data['type']][$value]['variants'])) { $imported_fonts[$data['type']][$value]['variants'] = array(); } } } } } else { if ($setting['type'] === 'THB_TextVariant_Control') { $value = get_theme_mod($setting['key']); if (empty($value)) { break; } if (!in_array($value, $imported_fonts[$current_font_type][$current_font]['variants']) && !empty($value)) { $imported_fonts[$current_font_type][$current_font]['variants'][] = $value; } } } } } $upload_dir = wp_upload_dir(); global $wp_customize; foreach ($imported_fonts as $type => $type_fonts) { foreach ($type_fonts as $font => $data) { if ($type == 'google') { if (isset($wp_customize)) { foreach ($fonts as $cl => $fn) { foreach ($fn as $f => $d) { if ($f == $font) { $google_fonts[] = $font . ':' . $d['variants']; } } } } else { $google_fonts[] = $font . ':' . implode(',', $data['variants']); } } else { if ($type == 'custom') { echo "@import url(" . $upload_dir['baseurl'] . "/fonts/" . $data['folder'] . "/stylesheet.css); "; } } } } if (!empty($google_fonts)) { $google_fonts = implode('|', $google_fonts); echo "@import url(http://fonts.googleapis.com/css?family={$google_fonts}); "; } }