true, 'show_color' => true, 'show_font_size' => true, 'show_font_weight' => true, 'show_font_style' => true, 'show_line_height' => true, 'show_letter_spacing' => true, 'show_text_transform' => true, 'show_font_variant' => true, 'show_text_shadow' => true, 'show_preview' => true, 'enqueue' => true, 'preview_text' => '', 'include_fonts' => '', // A regex string or array of regex strings to match font names to include. 'show_websafe_fonts' => true, 'show_google_fonts' => true, ); // Default style options public static $defaultStyling = array( 'font-family' => 'inherit', 'color' => '#333333', 'font-size' => 'inherit', 'font-weight' => 'inherit', 'font-style' => 'normal', 'line-height' => '1.5em', 'letter-spacing' => 'normal', 'text-transform' => 'none', 'font-variant' => 'normal', 'text-shadow-location' => 'none', 'text-shadow-distance' => '0px', 'text-shadow-blur' => '0px', 'text-shadow-color' => '#333333', 'text-shadow-opacity' => '1', 'font-type' => 'google', // Only used internally to determine if the font is a 'dark' => '', // only used to toggle the preview background ); // The list of web safe fonts public static $webSafeFonts = array( 'Arial, Helvetica, sans-serif' => 'Arial', '"Arial Black", Gadget, sans-serif' => 'Arial Black', '"Comic Sans MS", cursive, sans-serif' => 'Comic Sans', '"Courier New", Courier, monospace' => 'Courier New', 'Georgia, serif' => 'Geogia', 'Impact, Charcoal, sans-serif' => 'Impact', '"Lucida Console", Monaco, monospace' => 'Lucida Console', '"Lucida Sans Unicode", "Lucida Grande", sans-serif' => 'Lucida Sans', '"Palatino Linotype", "Book Antiqua", Palatino, serif' => 'Palatino', 'Tahoma, Geneva, sans-serif' => 'Tahoma', '"Times New Roman", Times, serif' => 'Times New Roman', '"Trebuchet MS", Helvetica, sans-serif' => 'Trebuchet', 'Verdana, Geneva, sans-serif' => 'Verdana', ); // Holds all the options with Google Fonts for enqueuing. // We need to do this since we want to gather all the fonts first then enqueue only the unique fonts private static $optionsToEnqueue = array(); /** * Constructor * * @return void * @since 1.4 */ function __construct( $settings, $owner ) { parent::__construct( $settings, $owner ); tf_add_action_once( 'admin_enqueue_scripts', array( $this, 'loadAdminScripts' ) ); tf_add_action_once( 'customize_controls_enqueue_scripts', array( $this, 'loadAdminScripts' ) ); tf_add_action_once( 'admin_head', array( __CLASS__, 'createFontScript' ) ); tf_add_action_once( 'wp_enqueue_scripts', array( $this, 'enqueueGooglefonts' ) ); add_filter( 'tf_generate_css_font_' . $this->getOptionNamespace(), array( $this, 'generateCSS' ), 10, 2 ); // Customizer preview handling tf_add_action_once( 'tf_generate_customizer_preview_js', array( $this, 'generateCustomizerPreviewJS' ) ); tf_add_filter_once( 'tf_generate_customizer_preview_css_' . $this->getOptionNamespace(), array( $this, 'generateCustomizerPreviewCSS' ) ); if ( $this->settings['enqueue'] ) { self::$optionsToEnqueue[] = $this; } } /** * Adds the Javascript code that adds Google fonts straight into the customizer preview. * * @since 1.9.2 * * @return void * * @see TitanFrameworkCustomizer->livePreviewMainScript() */ public function generateCustomizerPreviewJS() { ?> for ( var fontName in data.google_fonts ) { if ( document.querySelector( '#tf-preview-' + fontName ) ) { continue; } var link = document.createElement('LINK'); link.setAttribute( 'rel', 'stylesheet' ); link.setAttribute( 'type', 'text/css' ); link.setAttribute( 'media', 'all' ); link.setAttribute( 'id', 'tf-preview' + fontName ); link.setAttribute( 'href', data.google_fonts[ fontName ] ); document.head.appendChild( link ); } getGoogleFontURLs() ); return $generated; } /** * Gets all the Google font URLs for enqueuing. This was previously inside $this->enqueueGooglefonts() * but was split off so it can be used by other functions. * * @since 1.9.2 * * @return array An array containing the font names as keys and the font URLs as values. */ public function getGoogleFontURLs() { $urls = array(); // Gather all the fonts that we need to load, some may be repeated so we need to // load them once after gathering them $fontsToLoad = array(); foreach ( self::$optionsToEnqueue as $option ) { $fontValue = $option->getValue(); if ( empty( $fontValue['font-family'] ) ) { continue; } if ( $fontValue['font-family'] == 'inherit' ) { continue; } if ( $fontValue['font-type'] != 'google' ) { continue; } // Get all the fonts that we need to load if ( empty( $fontsToLoad[ $fontValue['font-family'] ] ) ) { $fontsToLoad[ $fontValue['font-family'] ] = array(); } // Get the weight $variant = $fontValue['font-weight']; if ( $variant == 'normal' ) { $variant = '400'; } else if ( $variant == 'bold' ) { $variant = '500'; } else if ( $variant == 'bolder' ) { $variant = '800'; } else if ( $variant == 'lighter' ) { $variant = '100'; } if ( $fontValue['font-style'] == 'italic' ) { $variant .= 'italic'; } $fontsToLoad[ $fontValue['font-family'] ][] = $variant; } // Font subsets, allow others to change this $subsets = apply_filters( 'tf_google_font_subsets_' . $this->getOptionNamespace(), array( 'latin', 'latin-ext' ) ); // Enqueue the Google Font foreach ( $fontsToLoad as $fontName => $variants ) { // Always include the normal weight so that we don't error out $variants[] = '400'; $variants = array_unique( $variants ); $fontUrl = sprintf( '//fonts.googleapis.com/css?family=%s:%s&subset=%s', str_replace( ' ', '+', $fontName ), implode( ',', $variants ), implode( ',', $subsets ) ); $fontUrl = apply_filters( 'tf_enqueue_google_webfont_' . $this->getOptionNamespace(), $fontUrl, $fontName ); if ( $fontUrl != false ) { $urls[ $fontName ] = $fontUrl; } } return $urls; } /** * Enqueues all the Google fonts, used in wp_enqueue_scripts * * @since 1.4 * * @return void */ public function enqueueGooglefonts() { $urls = $this->getGoogleFontURLs(); foreach ( $urls as $fontName => $url ) { wp_enqueue_style( 'tf-google-webfont-' . strtolower( str_replace( ' ', '-', $fontName ) ), $url ); } } /** * 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; } $skip = array( 'dark', 'font-type', 'text-shadow-distance', 'text-shadow-blur', 'text-shadow-color', 'text-shadow-opacity' ); // If the value is blank, use the defaults $value = $this->getValue(); $value = array_merge( self::$defaultStyling, $value ); foreach ( $value as $key => $val ) { // Force skip other keys, those are processed under another key, e.g. text-shadow-distance is // used by text-shadow-location if ( in_array( $key, $skip ) ) { continue; } // Don't include keys which are not in the default styles if ( ! in_array( $key, array_keys( self::$defaultStyling ) ) ) { continue; } if ( $key == 'font-family' ) { if ( $value[ $key ] == 'inherit' ) { $css .= '$' . $option->settings['id'] . '-' . $key . ': ' . $value[ $key ] . ';'; continue; } if ( ! empty( $value['font-type'] ) ) { if ( $value['font-type'] == 'google' ) { $css .= '$' . $option->settings['id'] . '-' . $key . ': "' . $value[ $key ] . '";'; continue; } } $css .= '$' . $option->settings['id'] . '-' . $key . ': ' . $value[ $key ] . ';'; continue; } if ( $key == 'text-shadow-location' ) { $textShadow = ''; if ( $value[ $key ] != 'none' ) { if ( stripos( $value[ $key ], 'left' ) !== false ) { $textShadow .= '-' . $value['text-shadow-distance']; } else if ( stripos( $value[ $key ], 'right' ) !== false ) { $textShadow .= $value['text-shadow-distance']; } else { $textShadow .= '0'; } $textShadow .= ' '; if ( stripos( $value[ $key ], 'top' ) !== false ) { $textShadow .= '-' . $value['text-shadow-distance']; } else if ( stripos( $value[ $key ], 'bottom' ) !== false ) { $textShadow .= $value['text-shadow-distance']; } else { $textShadow .= '0'; } $textShadow .= ' '; $textShadow .= $value['text-shadow-blur']; $textShadow .= ' '; $rgb = tf_hex2rgb( $value['text-shadow-color'] ); $rgb[] = $value['text-shadow-opacity']; $textShadow .= 'rgba(' . implode( ',', $rgb ) . ')'; } else { $textShadow .= $value[ $key ]; } $css .= '$' . $option->settings['id'] . '-text-shadow: ' . $textShadow . ';'; continue; } $css .= '$' . $option->settings['id'] . '-' . $key . ': ' . $value[ $key ] . ';'; } /* * There are 2 ways to include the values for the CSS. The normal `value-arraykey`, or just `value` * Using `value` will print out the entire font CSS. */ // Create the entire CSS for the font, this should just be used to replace the `value` variable. $cssVariables = ''; $cssChecking = array( 'font_family', 'color', 'font_size', 'font_weight', 'font_style', 'line_height', 'letter_spacing', 'text_transform', 'font_variant', 'text_shadow' ); // Enter values that are not marked as false. foreach ( $cssChecking as $subject ) { if ( $option->settings[ 'show_'.$subject ] ) { $cssVariableArray[] = str_replace( '_', '-', $subject ); } } // Now, integrate these values with their corresponding keys. foreach ( $cssVariableArray as $param ) { $cssVariables .= $param . ': $' . $option->settings['id'] . '-' . $param . ";\n"; } // Replace the `value` parameters in the given css. $modifiedCss = ''; if ( ! empty( $option->settings['css'] ) ) { $modifiedCss = $option->settings['css']; // If `value` is given, replace it with the entire css we created above in $cssVariables. $modifiedCss = preg_replace( '/value[^-]/', $cssVariables, $modifiedCss ); // Normal `value-arraykey` values. $modifiedCss = str_replace( 'value-', '$' . $option->settings['id'] . '-', $modifiedCss ); } $css .= $modifiedCss; return $css; } /** * Enqueues the needed scripts for the admin * * @return void * @since 1.4 */ public function loadAdminScripts() { wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_style( 'wp-color-picker' ); } /** * Creates the Javascript for running the font option * * @return void * @since 1.4 */ public static function createFontScript() { ?> echoOptionHeader( true ); // Get the current value and merge with defaults $value = $this->getValue(); $value = array_merge( self::$defaultStyling, $value ); /* * Create all the fields */ $visibilityAttrs = ''; if ( ! $this->settings['show_font_family'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?>