true, ); public static $firstLoad = true; /** * Constructor * * @since 1.4 */ function __construct( $settings, $owner ) { if ( defined( 'WP_DEBUG' ) ) { if ( WP_DEBUG == true ) { // Warn about deprecation, refer to `font` option TitanFramework::displayFrameworkError( sprintf( __( '%s has been deprecated and will be removed in version %s! Please use %s instead to avoid errors in the future.', TF_I18NDOMAIN ), 'select-googlefont', '1.5', 'font' ) ); } } parent::__construct( $settings, $owner ); add_filter( 'tf_generate_css_select-googlefont_' . $this->getOptionNamespace(), array( $this, 'generateCSS' ), 10, 2 ); } /** * Generates CSS for the font, this is used in TitanFrameworkCSS * * @param string $css The CSS generated * @param TitanFrameworkOption $option The current option being processed * @return string The CSS generated * @since 1.4 */ public function generateCSS( $css, $option ) { if ( $this->settings['id'] != $option->settings['id'] ) { return $css; } $value = $this->getFramework()->getOption( $option->settings['id'] ); if ( ! empty( $value['fontFamily'] ) ) { $css .= "\$" . $option->settings['id'] . "-name: " . $value['fontFamily'] . ";"; $css .= "\$" . $option->settings['id'] . ": " . $value['fontFamily'] . ";"; } if ( ! empty( $option->settings['css'] ) ) { $css .= str_replace( 'value', '$' . $option->settings['id'], $option->settings['css'] ); } return $css; } public static function formScript( $value ) { if ( empty( $value ) ) { return ''; } if ( is_serialized( $value ) ) { $value = unserialize( $value ); } return sprintf( "http://fonts.googleapis.com/css?family=%s:%s&subset=%s", str_replace( ' ', '+', $value['name'] ), implode( ',', $value['variants'] ), implode( ',', $value['subsets'] ) ); } public static function formCSS( $value ) { if ( is_serialized( $value ) ) { $value = unserialize( $value ); } return sprintf( "font-family: '%s', sans-serif;", $value['name'] ); } public static function formFormFamily( $value ) { if ( is_serialized( $value ) ) { $value = unserialize( $value ); } return sprintf( "'%s', sans-serif", $value['name'] ); } public static function getVariantName( $variant ) { $variantName = ''; if (preg_match('#^\d+#', $variant, $matches)) { if (count($matches)) { $fontWeight = $matches[0]; switch ($fontWeight) { case '100': $variantName = __("Ultra-light", TF_I18NDOMAIN); break; case '200': $variantName = __("Light", TF_I18NDOMAIN); break; case '300': $variantName = __("Book", TF_I18NDOMAIN); break; case '500': $variantName = __("Medium", TF_I18NDOMAIN); break; case '600': $variantName = __("Semi-Bold", TF_I18NDOMAIN); break; case '700': $variantName = __("Bold", TF_I18NDOMAIN); break; case '800': $variantName = __("Extra-Bold", TF_I18NDOMAIN); break; case '900': $variantName = __("Ultra-Bold", TF_I18NDOMAIN); break; default: $variantName = __("Regular", TF_I18NDOMAIN); } } } if ( stripos( $variant, 'italic' ) !== false ) { $variantName .= str_replace( 'italic', ' Italic', $variant ); } $variantName = trim( $variantName ); return $variantName; } public static function createScript() { ?> echoOptionHeader( true ); // set a default value $value = $this->getValue(); if ( is_serialized( $value ) ) { $value = unserialize( $value ); } if ( $value == array() || empty( $value['name'] ) || empty( $value['variants'] ) || empty( $value['subsets'] ) ) { $value = array( 'name' => 'Open Sans', 'variants' => array( '400' ), 'subsets' => array( 'latin' ) ); } if ( self::$firstLoad ) { self::$firstLoad = false; self::createScript(); } $allFonts = titan_get_googlefonts(); ?>", TitanFramework::getURL( 'iframe-googlefont-preview.php?f=' . $value['name'], __FILE__ ) ); // select variants echo "

Choose the styles to include:

"; echo "
"; $allVariants = array( '100', '100italic', '200', '200italic', '300', '300italic', '400', 'italic', '500', '500italic', '600', '600italic', '700', '700italic', '800', '800italic', '900', '900italic' ); foreach ( $allVariants as $key => $variant ) { printf( "", esc_attr( $variant ), self::getVariantName( $variant ) ); } echo "
"; // select charsets echo "

Choose the subsets to include:

"; echo "
"; $allSubsets = array( "latin", "latin-ext", "greek", "vietnamese", "cyrillic", "cyrillic-ext", "khmer", "greek-ext" ); foreach ( $allSubsets as $key => $subset ) { printf( "", esc_attr( $subset ), $subset ); } echo "
"; if ( ! is_serialized( $value ) ) { $value = serialize( $value ); } printf( "", esc_attr( $value ), esc_attr( $this->getID() ) ); $this->echoOptionFooter( false ); } public function cleanValueForSaving( $value ) { if ( is_serialized( $value ) ) { return $value; } if ( is_string( $value ) ) { // return serialize $value = explode( ',', $value ); } return serialize( $value ); } public function cleanValueForGetting( $value ) { if ( empty( $value ) ) { return array(); } if ( is_array( $value ) ) { return $value; } if ( is_serialized( stripslashes( $value ) ) ) { $value = unserialize( stripslashes( $value ) ); $value['css'] = self::formCSS( $value ); $value['fontFamily'] = self::formFormFamily( $value ); return $value; } if ( is_string( $value ) && stripos( $value, ',' ) !== false ) { $value = explode( ',', $value ); $value['css'] = self::formCSS( $value ); $value['fontFamily'] = self::formFormFamily( $value ); return $value; } return $this->settings['default']; } /* * Display for theme customizer */ public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) { $wp_customize->add_control( new TitanFrameworkOptionSelectGooglefontControl( $wp_customize, $this->getID(), array( 'label' => $this->settings['name'], 'section' => $section->settings['id'], 'settings' => $this->getID(), 'description' => $this->settings['desc'], 'priority' => $priority, ) ) ); } } /* * WP_Customize_Control with description */ add_action( 'customize_register', 'registerTitanFrameworkOptionSelectGooglefontControl', 1 ); function registerTitanFrameworkOptionSelectGooglefontControl() { class TitanFrameworkOptionSelectGooglefontControl extends WP_Customize_Control { public $description; private static $firstLoad = true; public function render_content() { if ( self::$firstLoad ) { self::$firstLoad = false; TitanFrameworkOptionSelectGooglefont::createScript(); } // set a default value $value = $this->value(); if ( $value == array() || empty( $value ) ) { $value = serialize( array( 'name' => 'Open Sans', 'variants' => array( '400' ), 'subsets' => array( 'latin' ) ) ); } $allFonts = titan_get_googlefonts(); ?>