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