choices['fonts'] ) && ( isset( $this->choices['fonts']['google'] ) || isset( $this->choices['fonts']['standard'] ) ) && ( ! empty( $this->choices['fonts']['google'] ) || ! empty( $this->choices['fonts']['standard'] ) ) ); $localize_script_var = ( $custom_fonts_array ) ? 'hooFonts' . $this->id : 'hooAllFonts'; wp_localize_script( $script_to_localize, $localize_script_var, array( 'standard' => $this->get_standard_fonts(), 'google' => $this->get_google_fonts(), ) ); } /** * Refresh the parameters passed to the JavaScript via JSON. * * @see WP_Customize_Control::to_json() */ public function to_json() { parent::to_json(); $this->json['default'] = $this->setting->default; if ( isset( $this->default ) ) { $this->json['default'] = $this->default; } $this->json['output'] = $this->output; $this->json['value'] = Hoo_Field_Typography::sanitize( $this->value() ); $this->json['choices'] = $this->choices; $this->json['link'] = $this->get_link(); $this->json['id'] = $this->id; $this->json['inputAttrs'] = ''; foreach ( $this->input_attrs as $attr => $value ) { $this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" '; } $defaults = array( 'font-family' => false, 'font-size' => false, 'variant' => false, 'line-height' => false, 'letter-spacing' => false, 'word-spacing' => false, 'color' => false, 'text-align' => false, ); $this->json['default'] = wp_parse_args( $this->json['default'], $defaults ); foreach ( $this->json['value'] as $key => $val ) { if ( isset( $this->json['default'][ $key ] ) && false === $this->json['default'][ $key ] ) { unset( $this->json['value'][ $key ] ); } } $this->json['show_variants'] = ( true === Hoo_Fonts_Google::$force_load_all_variants ) ? false : true; $this->json['show_subsets'] = ( true === Hoo_Fonts_Google::$force_load_all_subsets ) ? false : true; $this->json['languages'] = Hoo_Fonts::get_google_font_subsets(); } /** * An Underscore (JS) template for this control's content (but not its container). * * Class variables for this control class are available in the `data` JS object; * export custom variables by overriding {@see WP_Customize_Control::to_json()}. * * @see WP_Customize_Control::print_template() * * @access protected */ protected function content_template() { ?>
<# if ( data.default['font-family'] ) { #> <# if ( '' == data.value['font-family'] ) { data.value['font-family'] = data.default['font-family']; } #> <# if ( data.choices['fonts'] ) { data.fonts = data.choices['fonts']; } #>
<# if ( ! _.isUndefined( data.choices['font-backup'] ) && true === data.choices['font-backup'] ) { #>
<# } #> <# if ( true === data.show_variants || false !== data.default.variant ) { #>
<# } #> <# if ( true === data.show_subsets ) { #>
<# } #> <# } #> <# if ( data.default['font-size'] ) { #>
<# } #> <# if ( data.default['line-height'] ) { #>
<# } #> <# if ( data.default['letter-spacing'] ) { #>
<# } #> <# if ( data.default['word-spacing'] ) { #>
<# } #> <# if ( data.default['text-align'] ) { #>
checked="checked"<# } #>> checked="checked"<# } #>> checked="checked"<# } #>> checked="checked"<# } #>> checked="checked"<# } #>>
<# } #> <# if ( data.default['text-transform'] ) { #>
<# } #> <# if ( false !== data.default['color'] && data.default['color'] ) { #>
<# } #> <# if ( data.default['margin-top'] ) { #>
<# } #> <# if ( data.default['margin-bottom'] ) { #>
<# } #>
<# if ( ! _.isUndefined( data.value['font-family'] ) ) { data.value['font-family'] = data.value['font-family'].replace( /"/g, ''' ); } valueJSON = JSON.stringify( data.value ).replace( /'/g, ''' ); #> $variant, 'label' => isset( $all_variants[ $variant ] ) ? $all_variants[ $variant ] : $variant, ); } elseif ( is_array( $variant ) && isset( $variant['id'] ) && isset( $variant['label'] ) ) { $final_variants[] = $variant; } } return $final_variants; } /** * Gets standard fonts properly formatted for our control. * * @access protected * @return array */ protected function get_standard_fonts() { // Add fonts to our JS objects. $standard_fonts = Hoo_Fonts::get_standard_fonts(); $std_user_keys = $this->choices['fonts']['standard']; $standard_fonts_final = array(); $default_variants = $this->format_variants_array( array( 'regular', 'italic', '700', '700italic', ) ); foreach ( $standard_fonts as $key => $font ) { if ( ! empty( $std_user_keys ) && ! in_array( $key, $std_user_keys, true ) ) { continue; } $standard_fonts_final[] = array( 'family' => $font['stack'], 'label' => $font['label'], 'subsets' => array(), 'is_standard' => true, 'variants' => ( isset( $font['variants'] ) ) ? $this->format_variants_array( $font['variants'] ) : $default_variants, ); } return $standard_fonts_final; } /** * Gets google fonts properly formatted for our control. * * @access protected * @return array */ protected function get_google_fonts() { // Add fonts to our JS objects. $google_fonts = Hoo_Fonts::get_google_fonts(); $all_variants = Hoo_Fonts::get_all_variants(); $all_subsets = Hoo_Fonts::get_google_font_subsets(); $gf_user_keys = $this->choices['fonts']['google']; $google_fonts_final = array(); foreach ( $google_fonts as $family => $args ) { if ( ! empty( $gf_user_keys ) && ! in_array( $family, $gf_user_keys, true ) ) { continue; } $label = ( isset( $args['label'] ) ) ? $args['label'] : $family; $variants = ( isset( $args['variants'] ) ) ? $args['variants'] : array( 'regular', '700' ); $subsets = ( isset( $args['subsets'] ) ) ? $args['subsets'] : array(); $available_variants = array(); if ( is_array( $variants ) ) { foreach ( $variants as $variant ) { if ( array_key_exists( $variant, $all_variants ) ) { $available_variants[] = array( 'id' => $variant, 'label' => $all_variants[ $variant ], ); } } } $available_subsets = array(); if ( is_array( $subsets ) ) { foreach ( $subsets as $subset ) { if ( array_key_exists( $subset, $all_subsets ) ) { $available_subsets[] = array( 'id' => $subset, 'label' => $all_subsets[ $subset ], ); } } } $google_fonts_final[] = array( 'family' => $family, 'label' => $label, 'variants' => $available_variants, 'subsets' => $available_subsets, ); } // End foreach(). return $google_fonts_final; } /** * Render the control's content. * * @see WP_Customize_Control::render_content() */ protected function render_content() {} }