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' => 'Open Sans', 'color' => '#333333', 'font-size' => '13px', 'font-weight' => 'normal', '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 Google Fonts for enqueuing private static $googleFontsOptions = array(); private static $firstLoad = true; /** * Constructor * * @return void * @since 1.4 */ function __construct( $settings, $owner ) { parent::__construct( $settings, $owner ); add_action( 'admin_enqueue_scripts', array( $this, "loadAdminScripts" ) ); add_action( 'customize_controls_enqueue_scripts', array( $this, 'loadAdminScripts' ) ); add_action( 'admin_head', array( __CLASS__, 'createFontScript' ) ); add_action( 'tf_create_option_' . $this->getOptionNamespace(), array( $this, "rememberGoogleFonts" ) ); add_action( 'wp_enqueue_scripts', array( $this, "enqueueGooglefonts" ) ); add_filter( 'tf_generate_css_font_' . $this->getOptionNamespace(), array( $this, 'generateCSS' ), 10, 2 ); } /** * Keeps track of all the Google Fonts used for enqueueing later in wp_enqueue_scripts * * @param TitanFramework $option The option being enqueued * @return void * @since 1.4 */ public function rememberGoogleFonts( $option ) { if ( is_a( $option, __CLASS__ ) ) { if ( $option->settings['enqueue'] ) { self::$googleFontsOptions[] = $option; } } } /** * Enqueues all the Google fonts, used in wp_enqueue_scripts * * @return void * @since 1.4 */ public function enqueueGooglefonts() { if ( ! count( self::$googleFontsOptions ) ) { return; } // 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::$googleFontsOptions as $option ) { $fontValue = $option->getFramework()->getOption( $option->settings['id'] ); if ( empty( $fontValue['font-family'] ) ) { 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( "http://fonts.googleapis.com/css?family=%s:%s&subset=%s", str_replace( ' ', '+', $fontName ), implode( ',', $variants ), implode( ',', $subsets ) ); wp_enqueue_style( 'tf-google-webfont-' . strtolower( str_replace( ' ', '-', $fontName ) ), $fontUrl ); } // Don't repeat self::$googleFontsOptions = array(); } /** * 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'] ); $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 if ( empty( $value ) ) { $value = $this->getValue(); if ( is_serialized( $value ) ) { $value = unserialize( $value ); } if ( ! is_array( $value ) ) { $value = array(); } $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 ( ! 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() { if ( ! self::$firstLoad ) { return; } self::$firstLoad = false; ?> echoOptionHeader( true ); // Get the current value and merge with defaults $value = $this->getValue(); if ( is_serialized( $value ) ) { $value = unserialize( $value ); } if ( ! is_array( $value ) ) { $value = array(); } $value = array_merge( self::$defaultStyling, $value ); /* * Create all the fields */ $visibilityAttrs = ''; if ( ! $this->settings['show_font_family'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?>
settings['show_color'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> settings['show_font_size'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> settings['show_font_weight'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> settings['show_font_style'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> settings['show_line_height'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> settings['show_letter_spacing'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> settings['show_text_transform'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> settings['show_font_variant'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> settings['show_text_shadow'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?>
settings['show_preview'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?>
> '/>
", $this->getID(), $this->getID(), esc_attr( $value ) ); $this->echoOptionFooter( false ); } /** * Cleans up the serialized value before saving * * @param string $value The serialized value * @return string The cleaned value * @since 1.4 */ public function cleanValueForSaving( $value ) { if ( is_array( $value ) ) { $value = serialize( $value ); } return stripslashes( $value ); } /** * Cleans the raw value for getting * * @param string $value The raw value * @return string The cleaned value * @since 1.4 */ public function cleanValueForGetting( $value ) { if ( is_string( $value ) ) { if ( is_serialized( stripslashes( $value ) ) ) { $value = unserialize( $value ); } } if ( is_array( $value ) ) { $value = array_merge( self::$defaultStyling, $value ); } if ( ! empty( $value['font-family'] ) ) { $value['font-type'] = in_array( $value['font-family'], array_keys( self::$webSafeFonts ) ) ? 'websafe' : 'google'; } return $value; } /** * Registers the theme customizer control, for displaying the option * * @param WP_Customize $wp_enqueue_script The customize object * @param TitanFrameworkCustomizerSection $section The section where this option will be placed * @param int $priority The order of this control in the section * @return void * @since 1.4 */ public function registerCustomizerControl( $wp_customize, $section, $priority = 1 ) { $wp_customize->add_control( new TitanFrameworkOptionFontControl( $wp_customize, $this->getID(), array( 'label' => $this->settings['name'], 'section' => $section->settings['id'], 'settings' => $this->getID(), 'description' => $this->settings['desc'], 'priority' => $priority, 'params' => $this->settings, ) ) ); } } /* * We create a new control for the theme customizer */ add_action( 'customize_register', 'registerTitanFrameworkOptionFontControl', 1 ); /** * Creates the option for the theme customizer * * @return void * @since 1.4 */ function registerTitanFrameworkOptionFontControl() { class TitanFrameworkOptionFontControl extends WP_Customize_Control { public $description; public $params; public function render_content() { $this->params['show_preview'] = false; TitanFrameworkOptionFont::createFontScript(); ?>
label ); ?> value(); if ( is_serialized( $value ) ) { $value = unserialize( $value ); } if ( ! is_array( $value ) ) { $value = array(); } $value = array_merge( TitanFrameworkOptionFont::$defaultStyling, $value ); /* * Create all the fields */ $visibilityAttrs = ''; if ( ! $this->params['show_font_family'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?>
params['show_color'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> params['show_font_size'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> params['show_font_weight'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> params['show_font_style'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> params['show_line_height'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> params['show_letter_spacing'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> params['show_text_transform'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> params['show_font_variant'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?> params['show_text_shadow'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?>
params['show_preview'] ) { $visibilityAttrs = "data-visible='false' style='display: none'"; } ?>
> '/>
link() ?> value=''/>
{$this->description}

"; } } }