| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -class Customify_Font_Selector extends PixCustomifyPlugin { | |
| 3 | +class Customify_Font_Selector { | |
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | 6 | * Instance of this class. |
| 7 | 7 | * @since 1.0.0 |
| @@ -6,40 +6,111 @@ | ||
| 6 | 6 | * Instance of this class. |
| 7 | 7 | * @since 1.0.0 |
| 8 | 8 | * @var object |
| 9 | 9 | */ |
| 10 | - protected static $instance = null; | |
| 11 | - protected $parent = null; | |
| 10 | + protected static $_instance = null; | |
| 11 | + | |
| 12 | 12 | protected static $typo_settings = null; |
| 13 | 13 | protected static $options_list = null; |
| 14 | - static $theme_fonts = null; | |
| 14 | + protected $theme_fonts = null; | |
| 15 | 15 | protected $customify_CSS_output = array(); |
| 16 | 16 | |
| 17 | 17 | |
| 18 | - function __construct( $parent ) { | |
| 19 | - global $pixcustomify_plugin; | |
| 18 | + function __construct() { | |
| 19 | + $this->theme_fonts = apply_filters( 'customify_theme_fonts', array() ); | |
| 20 | 20 | |
| 21 | - $load_location = PixCustomifyPlugin::get_plugin_option( 'style_resources_location', 'wp_head' ); | |
| 21 | + $load_location = PixCustomifyPlugin()->settings->get_plugin_setting( 'style_resources_location', 'wp_head' ); | |
| 22 | + add_action( $load_location, array( $this, 'output_webfont_script' ), 99 ); | |
| 23 | + add_action( $load_location, array( $this, 'output_fonts_dynamic_style' ), 100 ); | |
| 24 | + add_action( 'customify_font_family_before_options', array( $this, 'add_customify_theme_fonts' ), 11, 1 ); | |
| 22 | 25 | |
| 23 | - add_action( $load_location, array( $this, 'output_font_dynamic_style' ), 999999999 ); | |
| 26 | + if ( PixCustomifyPlugin()->settings->get_plugin_setting( 'enable_editor_style', true ) ) { | |
| 27 | + add_action( 'admin_head', array( $this, 'script_to_add_customizer_settings_into_wp_editor' ) ); | |
| 28 | + } | |
| 24 | 29 | |
| 25 | - $this->parent = $parent; | |
| 26 | - self::$theme_fonts = apply_filters( 'customify_theme_fonts', array() ); | |
| 30 | + } | |
| 27 | 31 | |
| 28 | - add_action( 'customify_font_family_before_options', array( $this, 'add_customify_theme_fonts' ), 11, 2 ); | |
| 32 | + function script_to_add_customizer_settings_into_wp_editor() { | |
| 33 | + | |
| 34 | + ob_start(); | |
| 35 | + $this->output_fonts_dynamic_style(); | |
| 36 | + | |
| 37 | + $custom_css = ob_get_clean(); | |
| 38 | + | |
| 39 | + ob_start(); ?> | |
| 40 | + (function ($) { | |
| 41 | + $(window).load(function () { | |
| 42 | + /** | |
| 43 | + * @param iframe_id the id of the frame you want to append the style | |
| 44 | + * @param style_element the style element you want to append | |
| 45 | + */ | |
| 46 | + var append_script_to_iframe = function (ifrm_id, scriptEl) { | |
| 47 | + var myIframe = document.getElementById(ifrm_id); | |
| 48 | + | |
| 49 | + var script = myIframe.contentWindow.document.createElement("script"); | |
| 50 | + script.type = "text/javascript"; | |
| 51 | + script.innerHTML = scriptEl.innerHTML; | |
| 52 | + | |
| 53 | + myIframe.contentWindow.document.head.appendChild(script); | |
| 54 | + }; | |
| 55 | + | |
| 56 | + var append_style_to_iframe = function (ifrm_id, styleElment) { | |
| 57 | + var ifrm = window.frames[ifrm_id]; | |
| 58 | + if ( typeof ifrm === "undefined" ) { | |
| 59 | + return; | |
| 60 | + } | |
| 61 | + ifrm = ( ifrm.contentDocument || ifrm.document ); | |
| 62 | + var head = ifrm.getElementsByTagName('head')[0]; | |
| 63 | + | |
| 64 | + if (typeof styleElment !== "undefined") { | |
| 65 | + head.appendChild(styleElment); | |
| 66 | + } | |
| 67 | + }; | |
| 68 | + | |
| 69 | + var xmlString = <?php echo json_encode( str_replace( "\n", "", $custom_css ) ); ?>, | |
| 70 | + parser = new DOMParser(), | |
| 71 | + doc = parser.parseFromString(xmlString, "text/html"); | |
| 72 | + | |
| 73 | + if (typeof window.frames['content_ifr'] !== 'undefined') { | |
| 74 | + | |
| 75 | + $.each(doc.head.childNodes, function (key, el) { | |
| 76 | + if (typeof el !== "undefined" && typeof el.tagName !== "undefined") { | |
| 77 | + | |
| 78 | + switch (el.tagName) { | |
| 79 | + case 'STYLE' : | |
| 80 | + append_style_to_iframe('content_ifr', el); | |
| 81 | + break; | |
| 82 | + case 'SCRIPT' : | |
| 83 | + append_script_to_iframe('content_ifr', el); | |
| 84 | + break; | |
| 85 | + default: | |
| 86 | + break; | |
| 87 | + } | |
| 88 | + } | |
| 89 | + }); | |
| 90 | + } | |
| 91 | + }); | |
| 92 | + })(jQuery); | |
| 93 | + <?php | |
| 94 | + $script = ob_get_clean(); | |
| 95 | + wp_add_inline_script( 'editor', $script ); | |
| 29 | 96 | } |
| 30 | 97 | |
| 31 | - function add_customify_theme_fonts( $active_font_family, $val ) { | |
| 98 | + public function get_theme_fonts() { | |
| 99 | + return $this->theme_fonts; | |
| 100 | + } | |
| 101 | + | |
| 102 | + function add_customify_theme_fonts( $active_font_family ) { | |
| 32 | 103 | //first get all the published custom fonts |
| 33 | - if ( empty( self::$theme_fonts ) ) { | |
| 104 | + if ( empty( $this->theme_fonts ) ) { | |
| 34 | 105 | return; |
| 35 | 106 | } |
| 36 | 107 | |
| 37 | 108 | echo '<optgroup label="' . esc_html__( 'Theme Fonts', 'customify' ) . '">'; |
| 38 | - foreach ( self::$theme_fonts as $font ) { | |
| 109 | + foreach ( $this->theme_fonts as $font ) { | |
| 39 | 110 | if ( ! empty( $font ) ) { |
| 40 | 111 | //display the select option's HTML |
| 41 | - Pix_Customize_Font_Control::output_font_option( $font['family'], $active_font_family, $font, 'theme_font' ); | |
| 112 | + Pix_Customize_Font_Control::output_font_option( $font, $active_font_family, 'theme_font' ); | |
| 42 | 113 | } |
| 43 | 114 | } |
| 44 | 115 | echo "</optgroup>"; |
| 45 | 116 | } |
| @@ -53,26 +124,27 @@ | ||
| 53 | 124 | |
| 54 | 125 | return $to_return; |
| 55 | 126 | } |
| 56 | 127 | |
| 57 | - function output_font_dynamic_style() { | |
| 128 | + protected function get_fonts_args() { | |
| 58 | 129 | |
| 59 | - self::$options_list = $this->get_options(); | |
| 130 | + $args = array( | |
| 131 | + 'google_families' => array(), | |
| 132 | + 'local_families' => array(), | |
| 133 | + 'local_srcs' => array(), | |
| 134 | + ); | |
| 60 | 135 | |
| 61 | - self::get_typography_fields( self::$options_list, 'type', 'font', self::$typo_settings ); | |
| 136 | + /** @var PixCustomifyPlugin $local_plugin */ | |
| 137 | + $local_plugin = PixCustomifyPlugin(); | |
| 62 | 138 | |
| 139 | + self::$options_list = $local_plugin->get_options_details(); | |
| 140 | + | |
| 141 | + $local_plugin->customizer->get_typography_fields( self::$options_list, 'type', 'font', self::$typo_settings ); | |
| 142 | + | |
| 63 | 143 | if ( empty( self::$typo_settings ) ) { |
| 64 | - return; | |
| 144 | + return $args; | |
| 65 | 145 | } |
| 66 | 146 | |
| 67 | - $google_families = $local_families = ''; | |
| 68 | - | |
| 69 | - $args = array( | |
| 70 | - 'google_families' => '', | |
| 71 | - 'local_families' => '', | |
| 72 | - 'local_srcs' => '', | |
| 73 | - ); | |
| 74 | - | |
| 75 | 147 | foreach ( self::$typo_settings as $id => $font ) { |
| 76 | 148 | if ( isset ( $font['value'] ) ) { |
| 77 | 149 | |
| 78 | 150 | $load_all_weights = false; |
| @@ -83,11 +155,11 @@ | ||
| 83 | 155 | $value = $this->maybe_decode_value( $font['value'] ); |
| 84 | 156 | |
| 85 | 157 | $value = $this->validate_font_values( $value ); |
| 86 | 158 | |
| 87 | - // in case the value is still null, try default value(mostly for google fonts) | |
| 159 | + // in case the value is still null, try default value (mostly for google fonts) | |
| 88 | 160 | if ( ! is_array( $value ) || $value === null ) { |
| 89 | - $value = $this->get_font_defaults_value( str_replace( '"', '', $font['value'] ) ); | |
| 161 | + $value = $local_plugin->customizer->get_font_defaults_value( str_replace( '"', '', $font['value'] ) ); | |
| 90 | 162 | } |
| 91 | 163 | |
| 92 | 164 | //bail if by this time we don't have a value of some sort |
| 93 | 165 | if ( empty( $value ) ) { |
| @@ -93,137 +165,177 @@ | ||
| 93 | 165 | if ( empty( $value ) ) { |
| 94 | 166 | continue; |
| 95 | 167 | } |
| 96 | 168 | |
| 97 | - //Handle special logic for when the $value array is not an associative array | |
| 98 | - if ( ! self::is_assoc( $value ) ) { | |
| 99 | - $value = $this->process_a_not_associative_font_default( $value ); | |
| 169 | + // Handle special logic for when the $value array is not an associative array | |
| 170 | + if ( ! $local_plugin->is_assoc( $value ) ) { | |
| 171 | + $value = $local_plugin->customizer->standardize_non_associative_font_default( $value ); | |
| 100 | 172 | } |
| 101 | 173 | |
| 102 | - if ( isset( $value['font_family'] ) && isset( $value['type'] ) && $value['type'] == 'google' ) { | |
| 103 | - $args['google_families'] .= "'" . $value['font_family']; | |
| 174 | + // If we have reached this far and we don't have a type, we will assume it's a google font. | |
| 175 | + if ( ! isset( $value['type'] ) ) { | |
| 176 | + $value['type'] = 'google'; | |
| 177 | + } | |
| 104 | 178 | |
| 105 | - if ( $load_all_weights && is_array( $value['variants'] ) ) { | |
| 106 | - $args['google_families'] .= ":" . implode( ',', $value['variants'] ); | |
| 107 | - } elseif ( isset( $value['selected_variants'] ) && ! empty( $value['selected_variants'] ) ) { | |
| 179 | + if ( isset( $this->theme_fonts[ $value['font_family'] ] ) ) { | |
| 180 | + | |
| 181 | +// $value['type'] = 'theme_font'; | |
| 182 | +// $args['local_srcs'] .= $this->theme_fonts[ $value['font_family'] ]['src'] . ','; | |
| 183 | +// $value['variants'] = $this->theme_fonts[ $value['font_family'] ]['variants']; | |
| 184 | + | |
| 185 | + if ( false === array_search( $value['font_family'], $args['local_families'] ) ) { | |
| 186 | + $args['local_families'][] = "'" . $value['font_family'] . "'"; | |
| 187 | + } | |
| 188 | + | |
| 189 | + if ( false === array_search( $this->theme_fonts[ $value['font_family'] ]['src'], $args['local_srcs'] ) ) { | |
| 190 | + $args['local_srcs'][] = "'" . $this->theme_fonts[ $value['font_family'] ]['src'] . "'"; | |
| 191 | + } | |
| 192 | + } elseif ( isset( $value['font_family'] ) && isset( $value['type'] ) && $value['type'] === 'google' ) { | |
| 193 | + $family = "'" . $value['font_family']; | |
| 194 | + | |
| 195 | + if ( $load_all_weights && ! empty( $value['variants'] ) && is_array( $value['variants'] ) ) { | |
| 196 | + $family .= ":" . implode( ',', $value['variants'] ); | |
| 197 | + } elseif ( ! empty( $value['selected_variants'] ) ) { | |
| 108 | 198 | if ( is_array( $value['selected_variants'] ) ) { |
| 109 | - $args['google_families'] .= ":" . implode( ',', $value['selected_variants'] ); | |
| 199 | + $family .= ":" . implode( ',', $value['selected_variants'] ); | |
| 110 | 200 | } elseif ( is_string( $value['selected_variants'] ) || is_numeric( $value['selected_variants'] ) ) { |
| 111 | - $args['google_families'] .= ":" . $value['selected_variants']; | |
| 201 | + $family .= ":" . $value['selected_variants']; | |
| 112 | 202 | } |
| 113 | - } elseif ( isset( $value['variants'] ) && ! empty( $value['variants'] ) ) { | |
| 203 | + } elseif ( ! empty( $value['variants'] ) ) { | |
| 114 | 204 | if ( is_array( $value['variants'] ) ) { |
| 115 | - $args['google_families'] .= ":" . implode( ',', $value['variants'] ); | |
| 205 | + $family .= ":" . implode( ',', $value['variants'] ); | |
| 116 | 206 | } else { |
| 117 | - $args['google_families'] .= ":" . $value['variants']; | |
| 207 | + $family .= ":" . $value['variants']; | |
| 118 | 208 | } |
| 119 | 209 | } |
| 120 | 210 | |
| 121 | - if ( isset( $value['selected_subsets'] ) && ! empty( $value['selected_subsets'] ) ) { | |
| 211 | + if ( ! empty( $value['selected_subsets'] ) ) { | |
| 122 | 212 | if ( is_array( $value['selected_subsets'] ) ) { |
| 123 | - $args['google_families'] .= ":" . implode( ',', $value['selected_subsets'] ); | |
| 213 | + $family .= ":" . implode( ',', $value['selected_subsets'] ); | |
| 124 | 214 | } else { |
| 125 | - $args['google_families'] .= ":" . $value['selected_subsets']; | |
| 215 | + $family .= ":" . $value['selected_subsets']; | |
| 126 | 216 | } |
| 127 | - } elseif ( isset( $value['subsets'] ) && ! empty( $value['subsets'] ) ) { | |
| 217 | + } elseif ( ! empty( $value['subsets'] ) ) { | |
| 128 | 218 | if ( is_array( $value['subsets'] ) ) { |
| 129 | - $args['google_families'] .= ":" . implode( ',', $value['subsets'] ); | |
| 219 | + $family .= ":" . implode( ',', $value['subsets'] ); | |
| 130 | 220 | } else { |
| 131 | - $args['google_families'] .= ":" . $value['subsets']; | |
| 221 | + $family .= ":" . $value['subsets']; | |
| 132 | 222 | } |
| 133 | 223 | } |
| 134 | 224 | |
| 135 | - $args['google_families'] .= '\','; | |
| 136 | - } elseif ( isset( self::$theme_fonts[ $value['font_family'] ] ) ) { | |
| 225 | + $family .= "'"; | |
| 137 | 226 | |
| 138 | -// $value['type'] = 'theme_font'; | |
| 139 | -// $args['local_srcs'] .= self::$theme_fonts[ $value['font_family'] ]['src'] . ','; | |
| 140 | -// $value['variants'] = self::$theme_fonts[ $value['font_family'] ]['variants']; | |
| 141 | - | |
| 142 | - if ( false === strpos( $args['local_families'], $value['font_family'] ) ) { | |
| 143 | - $args['local_families'] .= "'" . $value['font_family'] . "',"; | |
| 144 | - } | |
| 145 | - | |
| 146 | - if ( false === strpos( $args['local_srcs'], self::$theme_fonts[ $value['font_family'] ]['src'] ) ) { | |
| 147 | - $args['local_srcs'] .= "'" . self::$theme_fonts[ $value['font_family'] ]['src'] . "',"; | |
| 148 | - } | |
| 227 | + $args['google_families'][] = $family; | |
| 149 | 228 | } |
| 150 | 229 | } |
| 151 | 230 | } |
| 152 | 231 | |
| 153 | - if ( ( ! empty ( $args['local_families'] ) || ! empty ( $args['google_families'] ) ) && self::get_plugin_option( 'typography', '1' ) && self::get_plugin_option( 'typography_google_fonts', 1 ) ) { | |
| 154 | - $this->display_webfont_script( $args ); | |
| 232 | + $args = array( | |
| 233 | + 'google_families' => array_unique( $args['google_families'] ), | |
| 234 | + 'local_families' => array_unique( $args['local_families'] ), | |
| 235 | + 'local_srcs' => array_unique( $args['local_srcs'] ), | |
| 236 | + ); | |
| 237 | + | |
| 238 | + return $args; | |
| 239 | + } | |
| 240 | + | |
| 241 | + function output_fonts_dynamic_style() { | |
| 242 | + | |
| 243 | + /** @var PixCustomifyPlugin $local_plugin */ | |
| 244 | + $local_plugin = PixCustomifyPlugin(); | |
| 245 | + | |
| 246 | + self::$options_list = $local_plugin->get_options_details(); | |
| 247 | + $local_plugin->customizer->get_typography_fields( self::$options_list, 'type', 'font', self::$typo_settings ); | |
| 248 | + | |
| 249 | + if ( empty( self::$typo_settings ) ) { | |
| 250 | + return; | |
| 155 | 251 | } |
| 156 | 252 | |
| 253 | + $output = ''; | |
| 254 | + | |
| 157 | 255 | foreach ( self::$typo_settings as $key => $font ) { |
| 158 | - if ( empty( $font['selector'] ) || empty( $font['value'] ) ) { | |
| 256 | + $font_output = $this->get_font_style( $font ); | |
| 257 | + if ( empty( $font_output ) ) { | |
| 159 | 258 | continue; |
| 160 | 259 | } |
| 161 | - $value = $this->maybe_decode_value( $font['value'] ); | |
| 162 | 260 | |
| 163 | - if ( $value === null ) { | |
| 164 | - $value = $this->get_font_defaults_value( $font['value'] ); | |
| 165 | - } | |
| 261 | + $output .= $font_output . PHP_EOL; | |
| 166 | 262 | |
| 167 | - // shim the old case when the default was only the font name | |
| 168 | - if ( is_string( $value ) && ! empty( $value ) ) { | |
| 169 | - $value = array( 'font_family' => $value ); | |
| 263 | + // If we are in a Customizer context we will output CSS rules grouped so we can target them. | |
| 264 | + // In the frontend we want a whole bulk. | |
| 265 | + if ( isset( $GLOBALS['wp_customize'] ) ) { ?> | |
| 266 | + <style id="customify_font_output_for_<?php echo sanitize_html_class( $key ); ?>"> | |
| 267 | + <?php echo $font_output; ?> | |
| 268 | + </style><?php | |
| 170 | 269 | } |
| 171 | - | |
| 172 | - //Handle special logic for when the $value array is not an associative array | |
| 173 | - if ( ! self::is_assoc( $value ) ) { | |
| 174 | - $value = $this->process_a_not_associative_font_default( $value ); | |
| 175 | - } | |
| 176 | - | |
| 177 | - $this->output_font_style( $key, $font, $value ); | |
| 178 | 270 | } |
| 179 | 271 | |
| 180 | 272 | // in customizer the CSS is printed per option, in front-end we need to print them in bulk |
| 181 | 273 | if ( ! isset( $GLOBALS['wp_customize'] ) ) { ?> |
| 182 | -<style id="customify_fonts_output"> | |
| 183 | -<?php echo join( "\n", $this->customify_CSS_output ); ?> | |
| 184 | -</style><?php | |
| 185 | - return; | |
| 274 | + <style id="customify_fonts_output"> | |
| 275 | + <?php echo $output; ?> | |
| 276 | + </style><?php | |
| 186 | 277 | } |
| 187 | 278 | } |
| 188 | 279 | |
| 189 | - function display_webfont_script( $args ) { ?> | |
| 190 | - <script type="text/javascript"> | |
| 191 | - var customify_font_loader = function () { | |
| 192 | - var webfontargs = { | |
| 193 | - classes: false, | |
| 194 | - events: false | |
| 195 | - }; | |
| 196 | - <?php if ( ! empty( $args['google_families'] ) ) { ?> | |
| 197 | - webfontargs.google = {families: [<?php echo( rtrim( $args['google_families'], ',' ) ); ?>]}; | |
| 198 | - <?php } | |
| 199 | - if ( ! empty( $args['local_families'] ) && ! empty( $args['local_srcs'] ) ) { ?> | |
| 200 | - webfontargs.custom = { | |
| 201 | - families: [<?php echo( rtrim( $args['local_families'], ',' ) ); ?>], | |
| 202 | - urls: [<?php echo rtrim( $args['local_srcs'], ',' ) ?>] | |
| 203 | - }; | |
| 204 | - <?php } ?> | |
| 205 | - WebFont.load(webfontargs); | |
| 206 | - }; | |
| 280 | + function get_fonts_dynamic_style() { | |
| 207 | 281 | |
| 208 | - if (typeof WebFont !== 'undefined') { <?php // if there is a WebFont object, use it ?> | |
| 209 | - customify_font_loader(); | |
| 210 | - } else { <?php // basically when we don't have the WebFont object we create the google script dynamically ?> | |
| 211 | - var tk = document.createElement('script'); | |
| 212 | - tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'; | |
| 213 | - tk.type = 'text/javascript'; | |
| 282 | + $output = ''; | |
| 214 | 283 | |
| 215 | - tk.onload = tk.onreadystatechange = function () { | |
| 216 | - customify_font_loader(); | |
| 217 | - }; | |
| 218 | - var s = document.getElementsByTagName('script')[0]; | |
| 219 | - s.parentNode.insertBefore(tk, s); | |
| 284 | + /** @var PixCustomifyPlugin $local_plugin */ | |
| 285 | + $local_plugin = PixCustomifyPlugin(); | |
| 286 | + | |
| 287 | + self::$options_list = $local_plugin->get_options_details(); | |
| 288 | + $local_plugin->customizer->get_typography_fields( self::$options_list, 'type', 'font', self::$typo_settings ); | |
| 289 | + | |
| 290 | + if ( empty( self::$typo_settings ) ) { | |
| 291 | + return $output; | |
| 292 | + } | |
| 293 | + | |
| 294 | + foreach ( self::$typo_settings as $key => $font ) { | |
| 295 | + | |
| 296 | + $font_output = $this->get_font_style( $font ); | |
| 297 | + if ( empty( $font_output ) ) { | |
| 298 | + continue; | |
| 220 | 299 | } |
| 221 | - </script> | |
| 222 | - <?php | |
| 300 | + | |
| 301 | + $output .= $font_output . PHP_EOL; | |
| 302 | + } | |
| 303 | + | |
| 304 | + return $output; | |
| 223 | 305 | } |
| 224 | 306 | |
| 225 | - function output_font_style( $field, $font, $value ) { | |
| 307 | + function get_font_style( $font ) { | |
| 308 | + | |
| 309 | + if ( ! isset( $font['selector'] ) ) { | |
| 310 | + return ''; | |
| 311 | + } | |
| 312 | + | |
| 313 | + $font['selector'] = apply_filters( 'customify_font_css_selector', $font['selector'], $font ); | |
| 314 | + | |
| 315 | + if ( empty( $font['selector'] ) || empty( $font['value'] ) ) { | |
| 316 | + return ''; | |
| 317 | + } | |
| 318 | + | |
| 319 | + /** @var PixCustomifyPlugin $local_plugin */ | |
| 320 | + $local_plugin = PixCustomifyPlugin(); | |
| 321 | + | |
| 322 | + $value = $this->maybe_decode_value( $font['value'] ); | |
| 323 | + | |
| 324 | + if ( $value === null ) { | |
| 325 | + $value = $local_plugin->customizer->get_font_defaults_value( $font['value'] ); | |
| 326 | + } | |
| 327 | + | |
| 328 | + // shim the old case when the default was only the font name | |
| 329 | + if ( is_string( $value ) && ! empty( $value ) ) { | |
| 330 | + $value = array( 'font_family' => $value ); | |
| 331 | + } | |
| 332 | + | |
| 333 | + // Handle special logic for when the $value array is not an associative array | |
| 334 | + if ( ! $local_plugin->is_assoc( $value ) ) { | |
| 335 | + $value = $local_plugin->customizer->standardize_non_associative_font_default( $value ); | |
| 336 | + } | |
| 337 | + | |
| 226 | 338 | $value = $this->validate_font_values( $value ); |
| 227 | 339 | // some sanitizing |
| 228 | 340 | $load_all_weights = false; |
| 229 | 341 | if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) { |
| @@ -242,10 +354,10 @@ | ||
| 242 | 354 | |
| 243 | 355 | if ( isset( $font['callback'] ) && function_exists( $font['callback'] ) ) { |
| 244 | 356 | $output = call_user_func( $font['callback'], $value, $font ); |
| 245 | 357 | echo $output; |
| 246 | - } else { | |
| 247 | - echo $font['selector'] . " {"; | |
| 358 | + } elseif ( isset( $font['selector'] ) ) { | |
| 359 | + echo $font['selector'] . " {" . PHP_EOL; | |
| 248 | 360 | |
| 249 | 361 | // First handle the case where we have the font-family in the selected variant (usually this means a custom font from our Fonto plugin) |
| 250 | 362 | if ( ! empty( $selected_variant ) && is_array( $selected_variant ) && ! empty( $selected_variant['font-family'] ) ) { |
| 251 | 363 | //the variant's font-family |
| @@ -299,24 +411,73 @@ | ||
| 299 | 411 | $this->display_property( 'font-family', $value['font-family'] ); |
| 300 | 412 | } |
| 301 | 413 | |
| 302 | 414 | if ( ! empty( $value['font_weight'] ) ) { |
| 303 | - $italic_font = $this->display_weight_property( $value['font_weight'] ); | |
| 415 | + $this->display_weight_property( $value['font_weight'] ); | |
| 304 | 416 | } |
| 305 | 417 | |
| 306 | 418 | if ( ! empty( $value['font_size'] ) ) { |
| 307 | - $unit = $this->get_field_unit( $font, 'font-size' ); | |
| 308 | - $this->display_property( 'font-size', $value['font_size'], $unit ); | |
| 419 | + // If the value already contains a unit, go with that. | |
| 420 | + // We also handle receiving the value in a standardized format ( array with 'value' and 'unit'). | |
| 421 | + $font_size = $value['font_size']; | |
| 422 | + $unit = ''; | |
| 423 | + if ( is_numeric( $value['font_size'] ) ) { | |
| 424 | + $unit = $this->get_field_unit( $font, 'font-size' ); | |
| 425 | + } elseif ( is_array( $value['font_size'] ) ) { | |
| 426 | + if ( isset( $value['font_size']['unit'] ) ) { | |
| 427 | + $unit = $value['font_size']['unit']; | |
| 428 | + } | |
| 429 | + | |
| 430 | + if ( isset( $value['font_size']['value'] ) ) { | |
| 431 | + $font_size = $value['font_size']['value']; | |
| 432 | + } | |
| 433 | + } | |
| 434 | + | |
| 435 | + if ( isset( $value['font_size']['unit'] ) && $value['font_size']['unit'] == 'em' && $value['font_size']['value'] >= 9 ) { | |
| 436 | + $value['font_size']['unit'] = 'px'; | |
| 437 | + } | |
| 438 | + | |
| 439 | + $this->display_property( 'font-size', $font_size, $unit ); | |
| 309 | 440 | } |
| 310 | 441 | |
| 311 | - if ( ! empty( $value['line_height'] ) ) { | |
| 312 | - $unit = $this->get_field_unit( $font, 'line-height' ); | |
| 313 | - $this->display_property( 'line-height', $value['line_height'], $unit ); | |
| 442 | + if ( isset( $value['line_height'] ) ) { | |
| 443 | + // If the value already contains a unit, go with that. | |
| 444 | + // We also handle receiving the value in a standardized format ( array with 'value' and 'unit'). | |
| 445 | + $line_height = $value['line_height']; | |
| 446 | + $unit = ''; | |
| 447 | + if ( is_numeric( $value['line_height'] ) ) { | |
| 448 | + $unit = $this->get_field_unit( $font, 'line-height' ); | |
| 449 | + } elseif ( is_array( $value['line_height'] ) ) { | |
| 450 | + if ( isset( $value['line_height']['unit'] ) ) { | |
| 451 | + $unit = $value['line_height']['unit']; | |
| 452 | + } | |
| 453 | + | |
| 454 | + if ( isset( $value['line_height']['value'] ) ) { | |
| 455 | + $line_height = $value['line_height']['value']; | |
| 456 | + } | |
| 457 | + } | |
| 458 | + | |
| 459 | + $this->display_property( 'line-height', $line_height, $unit ); | |
| 314 | 460 | } |
| 315 | 461 | |
| 316 | - if ( ! empty( $value['letter_spacing'] ) ) { | |
| 317 | - $unit = $this->get_field_unit( $font, 'letter-spacing' ); | |
| 318 | - $this->display_property( 'letter-spacing', $value['letter_spacing'], $unit ); | |
| 462 | + if ( isset( $value['letter_spacing'] ) ) { | |
| 463 | + // If the value already contains a unit, go with that. | |
| 464 | + // We also handle receiving the value in a standardized format ( array with 'value' and 'unit'). | |
| 465 | + $letter_spacing = $value['letter_spacing']; | |
| 466 | + $unit = ''; | |
| 467 | + if ( is_numeric( $value['letter_spacing'] ) ) { | |
| 468 | + $unit = $this->get_field_unit( $font, 'letter-spacing' ); | |
| 469 | + } elseif ( is_array( $value['letter_spacing'] ) ) { | |
| 470 | + if ( isset( $value['letter_spacing']['unit'] ) ) { | |
| 471 | + $unit = $value['letter_spacing']['unit']; | |
| 472 | + } | |
| 473 | + | |
| 474 | + if ( isset( $value['letter_spacing']['value'] ) ) { | |
| 475 | + $letter_spacing = $value['letter_spacing']['value']; | |
| 476 | + } | |
| 477 | + } | |
| 478 | + | |
| 479 | + $this->display_property( 'letter-spacing', $letter_spacing, $unit ); | |
| 319 | 480 | } |
| 320 | 481 | |
| 321 | 482 | if ( ! empty( $value['text_align'] ) ) { |
| 322 | 483 | $this->display_property( 'text-align', $value['text_align'] ); |
| @@ -333,16 +494,67 @@ | ||
| 333 | 494 | } |
| 334 | 495 | |
| 335 | 496 | $CSS = ob_get_clean(); |
| 336 | 497 | |
| 337 | - if ( isset( $GLOBALS['wp_customize'] ) ) { ?> | |
| 338 | - <style id="customify_font_output_for_<?php echo $field; ?>"> | |
| 339 | - <?php $CSS ?> | |
| 340 | - </style><?php | |
| 341 | - return; | |
| 342 | - } else { | |
| 343 | - $this->customify_CSS_output[] = $CSS; | |
| 498 | + return $CSS; | |
| 499 | + } | |
| 500 | + | |
| 501 | + function output_webfont_script() { | |
| 502 | + $script = $this->get_fonts_dynamic_script(); | |
| 503 | + if ( ! empty( $script ) ) { ?> | |
| 504 | + <script type="text/javascript"> | |
| 505 | + <?php echo $script; ?> | |
| 506 | + </script> | |
| 507 | + <?php } | |
| 508 | + } | |
| 509 | + | |
| 510 | + function get_fonts_dynamic_script() { | |
| 511 | + $args = $this->get_fonts_args(); | |
| 512 | + | |
| 513 | + if ( ( empty ( $args['local_families'] ) && empty ( $args['google_families'] ) ) | |
| 514 | + || ! PixCustomifyPlugin()->settings->get_plugin_setting( 'typography', '1' ) | |
| 515 | + || ! PixCustomifyPlugin()->settings->get_plugin_setting( 'typography_google_fonts', 1 ) ) { | |
| 516 | + return ''; | |
| 344 | 517 | } |
| 518 | + | |
| 519 | + ob_start(); ?> | |
| 520 | +function customify_font_loader() { | |
| 521 | + var webfontargs = { | |
| 522 | + classes: false, | |
| 523 | + events: false | |
| 524 | + }; | |
| 525 | + <?php if ( ! empty( $args['google_families'] ) ) { ?> | |
| 526 | + webfontargs.google = { | |
| 527 | + families: [<?php echo join( ',', $args['google_families'] ); ?>] | |
| 528 | + }; | |
| 529 | + <?php } | |
| 530 | + if ( ! empty( $args['local_families'] ) && ! empty( $args['local_srcs'] ) ) { ?> | |
| 531 | + webfontargs.custom = { | |
| 532 | + families: [<?php echo join( ',', $args['local_families'] ); ?>], | |
| 533 | + urls: [<?php echo join( ',', $args['local_srcs'] ) ?>] | |
| 534 | + }; | |
| 535 | + <?php } ?> | |
| 536 | + WebFont.load(webfontargs); | |
| 537 | +}; | |
| 538 | + | |
| 539 | +if (typeof WebFont !== 'undefined') { | |
| 540 | + customify_font_loader(); | |
| 541 | +} else { | |
| 542 | + var tk = document.createElement('script'); | |
| 543 | + tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'; | |
| 544 | + tk.type = 'text/javascript'; | |
| 545 | + | |
| 546 | + tk.onload = tk.onreadystatechange = function () { | |
| 547 | + customify_font_loader(); | |
| 548 | + }; | |
| 549 | + | |
| 550 | + var s = document.getElementsByTagName('script')[0]; | |
| 551 | + s.parentNode.insertBefore(tk, s); | |
| 552 | +} | |
| 553 | + <?php | |
| 554 | + $output = ob_get_clean(); | |
| 555 | + | |
| 556 | + return apply_filters( 'customify_fonts_webfont_script', $output ); | |
| 345 | 557 | } |
| 346 | 558 | |
| 347 | 559 | function get_field_unit( $font, $field ) { |
| 348 | 560 | |
| @@ -385,9 +597,9 @@ | ||
| 385 | 597 | |
| 386 | 598 | } |
| 387 | 599 | |
| 388 | 600 | function display_property( $property, $value, $unit = '' ) { |
| 389 | - echo "\n" . $property . ": " . $value . $unit . ";\n"; | |
| 601 | + echo $property . ": " . $value . $unit . ";\n"; | |
| 390 | 602 | } |
| 391 | 603 | |
| 392 | 604 | // well weight sometimes comes from google as 600italic which in CSS syntax should come in two separate properties |
| 393 | 605 | function display_weight_property( $value ) { |
| @@ -395,15 +607,53 @@ | ||
| 395 | 607 | |
| 396 | 608 | if ( strpos( $value, 'italic' ) !== false ) { |
| 397 | 609 | |
| 398 | 610 | $value = str_replace( 'italic', '', $value ); |
| 399 | - echo "\n" . 'font-weight' . ": " . $value . ";\n"; | |
| 400 | - echo "\n" . 'font-style' . ": italic;\n"; | |
| 611 | + echo 'font-weight' . ": " . $value . ";\n"; | |
| 612 | + echo 'font-style' . ": italic;\n"; | |
| 401 | 613 | $has_style = true; |
| 402 | 614 | } else { |
| 403 | - echo "\n" . 'font-weight' . ": " . $value . ";\n"; | |
| 615 | + echo 'font-weight' . ": " . $value . ";\n"; | |
| 404 | 616 | } |
| 405 | 617 | |
| 406 | 618 | |
| 407 | 619 | return $has_style; |
| 408 | 620 | } |
| 621 | + | |
| 622 | + /** | |
| 623 | + * Main Customify_Font_Selector Instance | |
| 624 | + * | |
| 625 | + * Ensures only one instance of Customify_Font_Selector is loaded or can be loaded. | |
| 626 | + * | |
| 627 | + * @since 1.0.0 | |
| 628 | + * @static | |
| 629 | + * | |
| 630 | + * @return Customify_Font_Selector Main Customify_Font_Selector instance | |
| 631 | + */ | |
| 632 | + public static function instance() { | |
| 633 | + | |
| 634 | + if ( is_null( self::$_instance ) ) { | |
| 635 | + self::$_instance = new self(); | |
| 636 | + } | |
| 637 | + return self::$_instance; | |
| 638 | + } // End instance () | |
| 639 | + | |
| 640 | + /** | |
| 641 | + * Cloning is forbidden. | |
| 642 | + * | |
| 643 | + * @since 1.0.0 | |
| 644 | + */ | |
| 645 | + public function __clone() { | |
| 646 | + | |
| 647 | + _doing_it_wrong( __FUNCTION__,esc_html( __( 'Cheatin’ huh?' ) ), '' ); | |
| 648 | + } // End __clone () | |
| 649 | + | |
| 650 | + /** | |
| 651 | + * Unserializing instances of this class is forbidden. | |
| 652 | + * | |
| 653 | + * @since 1.0.0 | |
| 654 | + */ | |
| 655 | + public function __wakeup() { | |
| 656 | + | |
| 657 | + _doing_it_wrong( __FUNCTION__, esc_html( __( 'Cheatin’ huh?' ) ), '' ); | |
| 658 | + } // End __wakeup () | |
| 409 | 659 | } |