| @@ -17,85 +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' ), 100 ); | |
| 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 ); | |
| 24 | 25 | |
| 25 | - if ( PixCustomifyPlugin()->get_plugin_setting( 'enable_editor_style', true ) ) { | |
| 26 | - add_action( 'admin_head', array( $this, 'add_customizer_settings_into_wp_editor' ) ); | |
| 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' ) ); | |
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | } |
| 30 | 31 | |
| 31 | - function add_customizer_settings_into_wp_editor() { | |
| 32 | + function script_to_add_customizer_settings_into_wp_editor() { | |
| 32 | 33 | |
| 33 | 34 | ob_start(); |
| 34 | - $this->output_font_dynamic_style(); | |
| 35 | + $this->output_fonts_dynamic_style(); | |
| 35 | 36 | |
| 36 | - $custom_css = ob_get_clean(); ?> | |
| 37 | - <script type="text/javascript"> | |
| 38 | - /* <![CDATA[ */ | |
| 39 | - (function ($) { | |
| 40 | - $(window).load(function () { | |
| 41 | - /** | |
| 42 | - * @param iframe_id the id of the frame you whant to append the style | |
| 43 | - * @param style_element the style element you want to append | |
| 44 | - */ | |
| 45 | - var append_script_to_iframe = function (ifrm_id, scriptEl) { | |
| 46 | - var myIframe = document.getElementById(ifrm_id); | |
| 37 | + $custom_css = ob_get_clean(); | |
| 47 | 38 | |
| 48 | - var script = myIframe.contentWindow.document.createElement("script"); | |
| 49 | - script.type = "text/javascript"; | |
| 50 | - script.innerHTML = scriptEl.innerHTML; | |
| 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); | |
| 51 | 48 | |
| 52 | - myIframe.contentWindow.document.head.appendChild(script); | |
| 53 | - }; | |
| 49 | + var script = myIframe.contentWindow.document.createElement("script"); | |
| 50 | + script.type = "text/javascript"; | |
| 51 | + script.innerHTML = scriptEl.innerHTML; | |
| 54 | 52 | |
| 55 | - var append_style_to_iframe = function (ifrm_id, styleElment) { | |
| 56 | - var ifrm = window.frames[ifrm_id]; | |
| 57 | - ifrm = ( ifrm.contentDocument || ifrm.contentDocument || ifrm.document ); | |
| 58 | - var head = ifrm.getElementsByTagName('head')[0]; | |
| 53 | + myIframe.contentWindow.document.head.appendChild(script); | |
| 54 | + }; | |
| 59 | 55 | |
| 60 | - if (typeof styleElment !== "undefined") { | |
| 61 | - head.appendChild(styleElment); | |
| 62 | - } | |
| 63 | - }; | |
| 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]; | |
| 64 | 63 | |
| 65 | - var xmlString = <?php echo json_encode( str_replace( "\n", "", $custom_css ) ); ?>, | |
| 66 | - parser = new DOMParser(), | |
| 67 | - doc = parser.parseFromString(xmlString, "text/html"); | |
| 64 | + if (typeof styleElment !== "undefined") { | |
| 65 | + head.appendChild(styleElment); | |
| 66 | + } | |
| 67 | + }; | |
| 68 | 68 | |
| 69 | - if (typeof window.frames['content_ifr'] !== 'undefined') { | |
| 69 | + var xmlString = <?php echo json_encode( str_replace( "\n", "", $custom_css ) ); ?>, | |
| 70 | + parser = new DOMParser(), | |
| 71 | + doc = parser.parseFromString(xmlString, "text/html"); | |
| 70 | 72 | |
| 71 | - $.each(doc.head.childNodes, function (key, el) { | |
| 72 | - if (typeof el !== "undefined" && typeof el.tagName !== "undefined") { | |
| 73 | + if (typeof window.frames['content_ifr'] !== 'undefined') { | |
| 73 | 74 | |
| 74 | - switch (el.tagName) { | |
| 75 | - case 'STYLE' : | |
| 76 | - append_style_to_iframe('content_ifr', el); | |
| 77 | - break; | |
| 78 | - case 'SCRIPT' : | |
| 79 | - append_script_to_iframe('content_ifr', el); | |
| 80 | - break; | |
| 81 | - default: | |
| 82 | - break; | |
| 83 | - } | |
| 84 | - } | |
| 85 | - }); | |
| 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 | + } | |
| 86 | 88 | } |
| 87 | 89 | }); |
| 88 | - })(jQuery); | |
| 89 | - /* ]]> */ | |
| 90 | - </script> | |
| 91 | - <?php } | |
| 90 | + } | |
| 91 | + }); | |
| 92 | + })(jQuery); | |
| 93 | + <?php | |
| 94 | + $script = ob_get_clean(); | |
| 95 | + wp_add_inline_script( 'editor', $script ); | |
| 96 | + } | |
| 92 | 97 | |
| 93 | 98 | public function get_theme_fonts() { |
| 94 | 99 | return $this->theme_fonts; |
| 95 | 100 | } |
| 96 | 101 | |
| 97 | - function add_customify_theme_fonts( $active_font_family, $val ) { | |
| 102 | + function add_customify_theme_fonts( $active_font_family ) { | |
| 98 | 103 | //first get all the published custom fonts |
| 99 | 104 | if ( empty( $this->theme_fonts ) ) { |
| 100 | 105 | return; |
| 101 | 106 | } |
| @@ -103,9 +108,9 @@ | ||
| 103 | 108 | echo '<optgroup label="' . esc_html__( 'Theme Fonts', 'customify' ) . '">'; |
| 104 | 109 | foreach ( $this->theme_fonts as $font ) { |
| 105 | 110 | if ( ! empty( $font ) ) { |
| 106 | 111 | //display the select option's HTML |
| 107 | - 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' ); | |
| 108 | 113 | } |
| 109 | 114 | } |
| 110 | 115 | echo "</optgroup>"; |
| 111 | 116 | } |
| @@ -119,29 +124,27 @@ | ||
| 119 | 124 | |
| 120 | 125 | return $to_return; |
| 121 | 126 | } |
| 122 | 127 | |
| 123 | - function output_font_dynamic_style() { | |
| 128 | + protected function get_fonts_args() { | |
| 124 | 129 | |
| 130 | + $args = array( | |
| 131 | + 'google_families' => array(), | |
| 132 | + 'local_families' => array(), | |
| 133 | + 'local_srcs' => array(), | |
| 134 | + ); | |
| 135 | + | |
| 125 | 136 | /** @var PixCustomifyPlugin $local_plugin */ |
| 126 | 137 | $local_plugin = PixCustomifyPlugin(); |
| 127 | 138 | |
| 128 | - self::$options_list = $local_plugin->get_options(); | |
| 139 | + self::$options_list = $local_plugin->get_options_details(); | |
| 129 | 140 | |
| 130 | - $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 ); | |
| 131 | 142 | |
| 132 | 143 | if ( empty( self::$typo_settings ) ) { |
| 133 | - return; | |
| 144 | + return $args; | |
| 134 | 145 | } |
| 135 | 146 | |
| 136 | - $google_families = $local_families = ''; | |
| 137 | - | |
| 138 | - $args = array( | |
| 139 | - 'google_families' => '', | |
| 140 | - 'local_families' => '', | |
| 141 | - 'local_srcs' => '', | |
| 142 | - ); | |
| 143 | - | |
| 144 | 147 | foreach ( self::$typo_settings as $id => $font ) { |
| 145 | 148 | if ( isset ( $font['value'] ) ) { |
| 146 | 149 | |
| 147 | 150 | $load_all_weights = false; |
| @@ -152,11 +155,11 @@ | ||
| 152 | 155 | $value = $this->maybe_decode_value( $font['value'] ); |
| 153 | 156 | |
| 154 | 157 | $value = $this->validate_font_values( $value ); |
| 155 | 158 | |
| 156 | - // 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) | |
| 157 | 160 | if ( ! is_array( $value ) || $value === null ) { |
| 158 | - $value = $local_plugin->get_font_defaults_value( str_replace( '"', '', $font['value'] ) ); | |
| 161 | + $value = $local_plugin->customizer->get_font_defaults_value( str_replace( '"', '', $font['value'] ) ); | |
| 159 | 162 | } |
| 160 | 163 | |
| 161 | 164 | //bail if by this time we don't have a value of some sort |
| 162 | 165 | if ( empty( $value ) ) { |
| @@ -162,137 +165,185 @@ | ||
| 162 | 165 | if ( empty( $value ) ) { |
| 163 | 166 | continue; |
| 164 | 167 | } |
| 165 | 168 | |
| 166 | - //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 | |
| 167 | 170 | if ( ! $local_plugin->is_assoc( $value ) ) { |
| 168 | - $value = $local_plugin->process_a_not_associative_font_default( $value ); | |
| 171 | + $value = $local_plugin->customizer->standardize_non_associative_font_default( $value ); | |
| 169 | 172 | } |
| 170 | 173 | |
| 171 | - if ( isset( $value['font_family'] ) && isset( $value['type'] ) && $value['type'] == 'google' ) { | |
| 172 | - $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 | + } | |
| 173 | 178 | |
| 174 | - if ( $load_all_weights && is_array( $value['variants'] ) ) { | |
| 175 | - $args['google_families'] .= ":" . implode( ',', $value['variants'] ); | |
| 176 | - } 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'] ) ) { | |
| 177 | 202 | if ( is_array( $value['selected_variants'] ) ) { |
| 178 | - $args['google_families'] .= ":" . implode( ',', $value['selected_variants'] ); | |
| 203 | + $family .= ":" . implode( ',', $value['selected_variants'] ); | |
| 179 | 204 | } elseif ( is_string( $value['selected_variants'] ) || is_numeric( $value['selected_variants'] ) ) { |
| 180 | - $args['google_families'] .= ":" . $value['selected_variants']; | |
| 205 | + $family .= ":" . $value['selected_variants']; | |
| 181 | 206 | } |
| 182 | - } elseif ( isset( $value['variants'] ) && ! empty( $value['variants'] ) ) { | |
| 207 | + } elseif ( ! empty( $value['variants'] ) ) { | |
| 183 | 208 | if ( is_array( $value['variants'] ) ) { |
| 184 | - $args['google_families'] .= ":" . implode( ',', $value['variants'] ); | |
| 209 | + $family .= ":" . implode( ',', $value['variants'] ); | |
| 185 | 210 | } else { |
| 186 | - $args['google_families'] .= ":" . $value['variants']; | |
| 211 | + $family .= ":" . $value['variants']; | |
| 187 | 212 | } |
| 188 | 213 | } |
| 189 | 214 | |
| 190 | - if ( isset( $value['selected_subsets'] ) && ! empty( $value['selected_subsets'] ) ) { | |
| 215 | + if ( ! empty( $value['selected_subsets'] ) ) { | |
| 191 | 216 | if ( is_array( $value['selected_subsets'] ) ) { |
| 192 | - $args['google_families'] .= ":" . implode( ',', $value['selected_subsets'] ); | |
| 217 | + $family .= ":" . implode( ',', $value['selected_subsets'] ); | |
| 193 | 218 | } else { |
| 194 | - $args['google_families'] .= ":" . $value['selected_subsets']; | |
| 219 | + $family .= ":" . $value['selected_subsets']; | |
| 195 | 220 | } |
| 196 | - } elseif ( isset( $value['subsets'] ) && ! empty( $value['subsets'] ) ) { | |
| 221 | + } elseif ( ! empty( $value['subsets'] ) ) { | |
| 197 | 222 | if ( is_array( $value['subsets'] ) ) { |
| 198 | - $args['google_families'] .= ":" . implode( ',', $value['subsets'] ); | |
| 223 | + $family .= ":" . implode( ',', $value['subsets'] ); | |
| 199 | 224 | } else { |
| 200 | - $args['google_families'] .= ":" . $value['subsets']; | |
| 225 | + $family .= ":" . $value['subsets']; | |
| 201 | 226 | } |
| 202 | 227 | } |
| 203 | 228 | |
| 204 | - $args['google_families'] .= '\','; | |
| 205 | - } elseif ( isset( $this->theme_fonts[ $value['font_family'] ] ) ) { | |
| 229 | + $family .= "'"; | |
| 206 | 230 | |
| 207 | -// $value['type'] = 'theme_font'; | |
| 208 | -// $args['local_srcs'] .= $this->theme_fonts[ $value['font_family'] ]['src'] . ','; | |
| 209 | -// $value['variants'] = $this->theme_fonts[ $value['font_family'] ]['variants']; | |
| 210 | - | |
| 211 | - if ( false === strpos( $args['local_families'], $value['font_family'] ) ) { | |
| 212 | - $args['local_families'] .= "'" . $value['font_family'] . "',"; | |
| 213 | - } | |
| 214 | - | |
| 215 | - if ( false === strpos( $args['local_srcs'], $this->theme_fonts[ $value['font_family'] ]['src'] ) ) { | |
| 216 | - $args['local_srcs'] .= "'" . $this->theme_fonts[ $value['font_family'] ]['src'] . "',"; | |
| 217 | - } | |
| 231 | + $args['google_families'][] = $family; | |
| 218 | 232 | } |
| 219 | 233 | } |
| 220 | 234 | } |
| 221 | 235 | |
| 222 | - if ( ( ! empty ( $args['local_families'] ) || ! empty ( $args['google_families'] ) ) && PixCustomifyPlugin()->get_plugin_setting( 'typography', '1' ) && PixCustomifyPlugin()->get_plugin_setting( 'typography_google_fonts', 1 ) ) { | |
| 223 | - $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; | |
| 224 | 255 | } |
| 225 | 256 | |
| 257 | + $output = ''; | |
| 258 | + | |
| 226 | 259 | foreach ( self::$typo_settings as $key => $font ) { |
| 227 | - if ( empty( $font['selector'] ) || empty( $font['value'] ) ) { | |
| 260 | + $font_output = $this->get_font_style( $font ); | |
| 261 | + if ( empty( $font_output ) ) { | |
| 228 | 262 | continue; |
| 229 | 263 | } |
| 230 | - $value = $this->maybe_decode_value( $font['value'] ); | |
| 231 | 264 | |
| 232 | - if ( $value === null ) { | |
| 233 | - $value = $local_plugin->get_font_defaults_value( $font['value'] ); | |
| 234 | - } | |
| 265 | + $output .= $font_output . "\n"; | |
| 235 | 266 | |
| 236 | - // shim the old case when the default was only the font name | |
| 237 | - if ( is_string( $value ) && ! empty( $value ) ) { | |
| 238 | - $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 | |
| 239 | 273 | } |
| 240 | - | |
| 241 | - //Handle special logic for when the $value array is not an associative array | |
| 242 | - if ( ! $local_plugin->is_assoc( $value ) ) { | |
| 243 | - $value = $local_plugin->process_a_not_associative_font_default( $value ); | |
| 244 | - } | |
| 245 | - | |
| 246 | - $this->output_font_style( $key, $font, $value ); | |
| 247 | 274 | } |
| 248 | 275 | |
| 249 | 276 | // in customizer the CSS is printed per option, in front-end we need to print them in bulk |
| 250 | 277 | if ( ! isset( $GLOBALS['wp_customize'] ) ) { ?> |
| 251 | -<style id="customify_fonts_output"> | |
| 252 | -<?php echo join( "\n", $this->customify_CSS_output ); ?> | |
| 253 | -</style><?php | |
| 254 | - return; | |
| 278 | + <style id="customify_fonts_output"> | |
| 279 | + <?php echo $output; ?> | |
| 280 | + </style><?php | |
| 255 | 281 | } |
| 256 | 282 | } |
| 257 | 283 | |
| 258 | - function display_webfont_script( $args ) { ?> | |
| 259 | - <script type="text/javascript"> | |
| 260 | - var customify_font_loader = function () { | |
| 261 | - var webfontargs = { | |
| 262 | - classes: false, | |
| 263 | - events: false | |
| 264 | - }; | |
| 265 | - <?php if ( ! empty( $args['google_families'] ) ) { ?> | |
| 266 | - webfontargs.google = {families: [<?php echo( rtrim( $args['google_families'], ',' ) ); ?>]}; | |
| 267 | - <?php } | |
| 268 | - if ( ! empty( $args['local_families'] ) && ! empty( $args['local_srcs'] ) ) { ?> | |
| 269 | - webfontargs.custom = { | |
| 270 | - families: [<?php echo( rtrim( $args['local_families'], ',' ) ); ?>], | |
| 271 | - urls: [<?php echo rtrim( $args['local_srcs'], ',' ) ?>] | |
| 272 | - }; | |
| 273 | - <?php } ?> | |
| 274 | - WebFont.load(webfontargs); | |
| 275 | - }; | |
| 284 | + function get_fonts_dynamic_style() { | |
| 276 | 285 | |
| 277 | - if (typeof WebFont !== 'undefined') { <?php // if there is a WebFont object, use it ?> | |
| 278 | - customify_font_loader(); | |
| 279 | - } else { <?php // basically when we don't have the WebFont object we create the google script dynamically ?> | |
| 280 | - var tk = document.createElement('script'); | |
| 281 | - tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'; | |
| 282 | - tk.type = 'text/javascript'; | |
| 286 | + $output = ''; | |
| 283 | 287 | |
| 284 | - tk.onload = tk.onreadystatechange = function () { | |
| 285 | - customify_font_loader(); | |
| 286 | - }; | |
| 287 | - var s = document.getElementsByTagName('script')[0]; | |
| 288 | - 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; | |
| 289 | 303 | } |
| 290 | - </script> | |
| 291 | - <?php | |
| 304 | + | |
| 305 | + $output .= $font_output . "\n"; | |
| 306 | + } | |
| 307 | + | |
| 308 | + return $output; | |
| 292 | 309 | } |
| 293 | 310 | |
| 294 | - 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 | + if ( empty( $font['selector'] ) || empty( $font['value'] ) ) { | |
| 319 | + return ''; | |
| 320 | + } | |
| 321 | + | |
| 322 | + $properties_prefix = ''; | |
| 323 | + if ( ! empty ( $font['properties_prefix'] ) ) { | |
| 324 | + $properties_prefix = $font['properties_prefix']; | |
| 325 | + } | |
| 326 | + | |
| 327 | + /** @var PixCustomifyPlugin $local_plugin */ | |
| 328 | + $local_plugin = PixCustomifyPlugin(); | |
| 329 | + | |
| 330 | + $value = $this->maybe_decode_value( $font['value'] ); | |
| 331 | + | |
| 332 | + if ( $value === null ) { | |
| 333 | + $value = $local_plugin->customizer->get_font_defaults_value( $font['value'] ); | |
| 334 | + } | |
| 335 | + | |
| 336 | + // shim the old case when the default was only the font name | |
| 337 | + if ( is_string( $value ) && ! empty( $value ) ) { | |
| 338 | + $value = array( 'font_family' => $value ); | |
| 339 | + } | |
| 340 | + | |
| 341 | + // Handle special logic for when the $value array is not an associative array | |
| 342 | + if ( ! $local_plugin->is_assoc( $value ) ) { | |
| 343 | + $value = $local_plugin->customizer->standardize_non_associative_font_default( $value ); | |
| 344 | + } | |
| 345 | + | |
| 295 | 346 | $value = $this->validate_font_values( $value ); |
| 296 | 347 | // some sanitizing |
| 297 | 348 | $load_all_weights = false; |
| 298 | 349 | if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) { |
| @@ -311,15 +362,15 @@ | ||
| 311 | 362 | |
| 312 | 363 | if ( isset( $font['callback'] ) && function_exists( $font['callback'] ) ) { |
| 313 | 364 | $output = call_user_func( $font['callback'], $value, $font ); |
| 314 | 365 | echo $output; |
| 315 | - } else { | |
| 316 | - echo $font['selector'] . " {"; | |
| 366 | + } elseif ( isset( $font['selector'] ) ) { | |
| 367 | + echo $font['selector'] . " {" . "\n"; | |
| 317 | 368 | |
| 318 | 369 | // First handle the case where we have the font-family in the selected variant (usually this means a custom font from our Fonto plugin) |
| 319 | 370 | if ( ! empty( $selected_variant ) && is_array( $selected_variant ) && ! empty( $selected_variant['font-family'] ) ) { |
| 320 | 371 | //the variant's font-family |
| 321 | - $this->display_property( 'font-family', $selected_variant['font-family'] ); | |
| 372 | + $this->display_property( 'font-family', $selected_variant['font-family'], '', $properties_prefix ); | |
| 322 | 373 | |
| 323 | 374 | if ( ! $load_all_weights ) { |
| 324 | 375 | // if this is a custom font (like from our plugin Fonto) with individual styles & weights - i.e. the font-family says it all |
| 325 | 376 | // we need to "force" the font-weight and font-style |
| @@ -332,20 +383,20 @@ | ||
| 332 | 383 | |
| 333 | 384 | // output the font weight, if available |
| 334 | 385 | if ( ! empty( $selected_variant['font-weight'] ) ) { |
| 335 | 386 | echo ": " . $selected_variant['font-weight'] . ";\n"; |
| 336 | - $italic_font = $this->display_weight_property( $selected_variant['font-weight'] ); | |
| 387 | + $italic_font = $this->display_weight_property( $selected_variant['font-weight'], $properties_prefix ); | |
| 337 | 388 | } |
| 338 | 389 | |
| 339 | 390 | // output the font style, if available and if it wasn't displayed already |
| 340 | 391 | if ( ! $italic_font && ! empty( $selected_variant['font-style'] ) ) { |
| 341 | - $this->display_property( 'font-style', $selected_variant['font-style'] ); | |
| 392 | + $this->display_property( 'font-style', $selected_variant['font-style'], '', $properties_prefix ); | |
| 342 | 393 | } |
| 343 | 394 | } |
| 344 | 395 | |
| 345 | 396 | } elseif ( isset( $value['font_family'] ) ) { |
| 346 | 397 | // the selected font family |
| 347 | - $this->display_property( 'font-family', $value['font_family'] ); | |
| 398 | + $this->display_property( 'font-family', $value['font_family'], '', $properties_prefix ); | |
| 348 | 399 | |
| 349 | 400 | if ( ! empty( $selected_variant ) && ! $load_all_weights ) { |
| 350 | 401 | $weight_and_style = strtolower( $selected_variant ); |
| 351 | 402 | $italic_font = false; |
| @@ -355,49 +406,98 @@ | ||
| 355 | 406 | //a little bit of sanity check - in case it's not a number |
| 356 | 407 | if( $weight_and_style === 'regular' ) { |
| 357 | 408 | $weight_and_style = 'normal'; |
| 358 | 409 | } |
| 359 | - $italic_font = $this->display_weight_property( $weight_and_style ); | |
| 410 | + $italic_font = $this->display_weight_property( $weight_and_style, $properties_prefix ); | |
| 360 | 411 | } |
| 361 | 412 | |
| 362 | 413 | // output the font style, if available |
| 363 | 414 | if ( ! $italic_font && ! empty( $selected_variant['font-style'] ) ) { |
| 364 | - $this->display_property( 'font-style', $selected_variant['font-style'] ); | |
| 415 | + $this->display_property( 'font-style', $selected_variant['font-style'], '', $properties_prefix ); | |
| 365 | 416 | } |
| 366 | 417 | } |
| 367 | 418 | } else if ( isset( $value['font-family'] ) ) { |
| 368 | - $this->display_property( 'font-family', $value['font-family'] ); | |
| 419 | + $this->display_property( 'font-family', $value['font-family'], '', $properties_prefix ); | |
| 369 | 420 | } |
| 370 | 421 | |
| 371 | 422 | if ( ! empty( $value['font_weight'] ) ) { |
| 372 | - $italic_font = $this->display_weight_property( $value['font_weight'] ); | |
| 423 | + $this->display_weight_property( $value['font_weight'], $properties_prefix ); | |
| 373 | 424 | } |
| 374 | 425 | |
| 375 | 426 | if ( ! empty( $value['font_size'] ) ) { |
| 376 | - $unit = $this->get_field_unit( $font, 'font-size' ); | |
| 377 | - $this->display_property( 'font-size', $value['font_size'], $unit ); | |
| 427 | + // If the value already contains a unit, go with that. | |
| 428 | + // We also handle receiving the value in a standardized format ( array with 'value' and 'unit'). | |
| 429 | + $font_size = $value['font_size']; | |
| 430 | + $unit = ''; | |
| 431 | + if ( is_numeric( $value['font_size'] ) ) { | |
| 432 | + $unit = $this->get_field_unit( $font, 'font-size' ); | |
| 433 | + } elseif ( is_array( $value['font_size'] ) ) { | |
| 434 | + if ( isset( $value['font_size']['unit'] ) ) { | |
| 435 | + $unit = $value['font_size']['unit']; | |
| 436 | + } | |
| 437 | + | |
| 438 | + if ( isset( $value['font_size']['value'] ) ) { | |
| 439 | + $font_size = $value['font_size']['value']; | |
| 440 | + } | |
| 441 | + } | |
| 442 | + | |
| 443 | + if ( isset( $value['font_size']['unit'] ) && $value['font_size']['unit'] == 'em' && $value['font_size']['value'] >= 9 ) { | |
| 444 | + $value['font_size']['unit'] = 'px'; | |
| 445 | + } | |
| 446 | + | |
| 447 | + $this->display_property( 'font-size', $font_size, $unit, $properties_prefix ); | |
| 378 | 448 | } |
| 379 | 449 | |
| 380 | 450 | if ( isset( $value['line_height'] ) ) { |
| 381 | - $unit = $this->get_field_unit( $font, 'line-height' ); | |
| 382 | - $this->display_property( 'line-height', $value['line_height'], $unit ); | |
| 451 | + // If the value already contains a unit, go with that. | |
| 452 | + // We also handle receiving the value in a standardized format ( array with 'value' and 'unit'). | |
| 453 | + $line_height = $value['line_height']; | |
| 454 | + $unit = ''; | |
| 455 | + if ( is_numeric( $value['line_height'] ) ) { | |
| 456 | + $unit = $this->get_field_unit( $font, 'line-height' ); | |
| 457 | + } elseif ( is_array( $value['line_height'] ) ) { | |
| 458 | + if ( isset( $value['line_height']['unit'] ) ) { | |
| 459 | + $unit = $value['line_height']['unit']; | |
| 460 | + } | |
| 461 | + | |
| 462 | + if ( isset( $value['line_height']['value'] ) ) { | |
| 463 | + $line_height = $value['line_height']['value']; | |
| 464 | + } | |
| 465 | + } | |
| 466 | + | |
| 467 | + $this->display_property( 'line-height', $line_height, $unit, $properties_prefix ); | |
| 383 | 468 | } |
| 384 | 469 | |
| 385 | 470 | if ( isset( $value['letter_spacing'] ) ) { |
| 386 | - $unit = $this->get_field_unit( $font, 'letter-spacing' ); | |
| 387 | - $this->display_property( 'letter-spacing', $value['letter_spacing'], $unit ); | |
| 471 | + // If the value already contains a unit, go with that. | |
| 472 | + // We also handle receiving the value in a standardized format ( array with 'value' and 'unit'). | |
| 473 | + $letter_spacing = $value['letter_spacing']; | |
| 474 | + $unit = ''; | |
| 475 | + if ( is_numeric( $value['letter_spacing'] ) ) { | |
| 476 | + $unit = $this->get_field_unit( $font, 'letter-spacing' ); | |
| 477 | + } elseif ( is_array( $value['letter_spacing'] ) ) { | |
| 478 | + if ( isset( $value['letter_spacing']['unit'] ) ) { | |
| 479 | + $unit = $value['letter_spacing']['unit']; | |
| 480 | + } | |
| 481 | + | |
| 482 | + if ( isset( $value['letter_spacing']['value'] ) ) { | |
| 483 | + $letter_spacing = $value['letter_spacing']['value']; | |
| 484 | + } | |
| 485 | + } | |
| 486 | + | |
| 487 | + $this->display_property( 'letter-spacing', $letter_spacing, $unit, $properties_prefix ); | |
| 388 | 488 | } |
| 389 | 489 | |
| 390 | 490 | if ( ! empty( $value['text_align'] ) ) { |
| 391 | - $this->display_property( 'text-align', $value['text_align'] ); | |
| 491 | + $this->display_property( 'text-align', $value['text_align'], '', $properties_prefix ); | |
| 392 | 492 | } |
| 393 | 493 | |
| 394 | 494 | if ( ! empty( $value['text_transform'] ) ) { |
| 395 | - $this->display_property( 'text-transform', $value['text_transform'] ); | |
| 495 | + $this->display_property( 'text-transform', $value['text_transform'], '', $properties_prefix ); | |
| 396 | 496 | } |
| 397 | 497 | |
| 398 | 498 | if ( ! empty( $value['text_decoration'] ) ) { |
| 399 | - $this->display_property( 'text-decoration', $value['text_decoration'] ); | |
| 499 | + $this->display_property( 'text-decoration', $value['text_decoration'], '', $properties_prefix ); | |
| 400 | 500 | } |
| 401 | 501 | echo "}\n"; |
| 402 | 502 | } |
| 403 | 503 | |
| @@ -402,21 +502,86 @@ | ||
| 402 | 502 | } |
| 403 | 503 | |
| 404 | 504 | $CSS = ob_get_clean(); |
| 405 | 505 | |
| 406 | - if ( isset( $GLOBALS['wp_customize'] ) ) { ?> | |
| 407 | - <style id="customify_font_output_for_<?php echo $field; ?>"> | |
| 408 | - <?php echo $CSS ?> | |
| 409 | - </style><?php | |
| 410 | - return; | |
| 411 | - } else { | |
| 412 | - $this->customify_CSS_output[] = $CSS; | |
| 506 | + return $CSS; | |
| 507 | + } | |
| 508 | + | |
| 509 | + function output_webfont_script() { | |
| 510 | + $script = $this->get_fonts_dynamic_script(); | |
| 511 | + if ( ! empty( $script ) ) { ?> | |
| 512 | + <script type="text/javascript"> | |
| 513 | + <?php echo $script; ?> | |
| 514 | + </script> | |
| 515 | + <?php } | |
| 516 | + } | |
| 517 | + | |
| 518 | + function get_fonts_dynamic_script() { | |
| 519 | + $args = $this->get_fonts_args(); | |
| 520 | + | |
| 521 | + if ( ( empty ( $args['local_families'] ) && empty ( $args['google_families'] ) ) | |
| 522 | + || ! PixCustomifyPlugin()->settings->get_plugin_setting( 'typography', '1' ) | |
| 523 | + || ! PixCustomifyPlugin()->settings->get_plugin_setting( 'typography_google_fonts', 1 ) ) { | |
| 524 | + return ''; | |
| 413 | 525 | } |
| 526 | + | |
| 527 | + ob_start(); ?> | |
| 528 | +function customify_font_loader() { | |
| 529 | + var webfontargs = { | |
| 530 | + classes: true, | |
| 531 | + events: true, | |
| 532 | + loading: function() { | |
| 533 | + jQuery( window ).trigger( 'wf-loading' ); | |
| 534 | + }, | |
| 535 | + active: function() { | |
| 536 | + jQuery( window ).trigger( 'wf-active' ); | |
| 537 | + }, | |
| 538 | + inactive: function() { | |
| 539 | + jQuery( window ).trigger( 'wf-inactive' ); | |
| 540 | + }, | |
| 541 | + }; | |
| 542 | + <?php if ( ! empty( $args['google_families'] ) ) { ?> | |
| 543 | + webfontargs.google = { | |
| 544 | + families: [<?php echo join( ',', $args['google_families'] ); ?>] | |
| 545 | + }; | |
| 546 | + <?php } | |
| 547 | + if ( ! empty( $args['local_families'] ) && ! empty( $args['local_srcs'] ) ) { ?> | |
| 548 | + webfontargs.custom = { | |
| 549 | + families: [<?php echo join( ',', $args['local_families'] ); ?>], | |
| 550 | + urls: [<?php echo join( ',', $args['local_srcs'] ) ?>] | |
| 551 | + }; | |
| 552 | + <?php } ?> | |
| 553 | + WebFont.load(webfontargs); | |
| 554 | +}; | |
| 555 | + | |
| 556 | +if (typeof WebFont !== 'undefined') { | |
| 557 | + customify_font_loader(); | |
| 558 | +} else { | |
| 559 | + var tk = document.createElement('script'); | |
| 560 | + tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'; | |
| 561 | + tk.type = 'text/javascript'; | |
| 562 | + | |
| 563 | + tk.onload = tk.onreadystatechange = function () { | |
| 564 | + customify_font_loader(); | |
| 565 | + }; | |
| 566 | + | |
| 567 | + var s = document.getElementsByTagName('script')[0]; | |
| 568 | + s.parentNode.insertBefore(tk, s); | |
| 569 | +} | |
| 570 | + <?php | |
| 571 | + $output = ob_get_clean(); | |
| 572 | + | |
| 573 | + return apply_filters( 'customify_fonts_webfont_script', $output ); | |
| 414 | 574 | } |
| 415 | 575 | |
| 416 | 576 | function get_field_unit( $font, $field ) { |
| 417 | 577 | |
| 418 | 578 | if ( empty( $font ) || empty( $font['fields'] ) || empty( $font['fields'][ $field ] ) ) { |
| 579 | + | |
| 580 | + if ( 'line-height' == $field ){ | |
| 581 | + return ''; | |
| 582 | + } | |
| 583 | + | |
| 419 | 584 | return 'px'; |
| 420 | 585 | } |
| 421 | 586 | |
| 422 | 587 | if ( isset( $font['fields'][ $field ]['unit'] ) ) { |
| @@ -453,24 +618,24 @@ | ||
| 453 | 618 | function display_selector( $selector ) { |
| 454 | 619 | |
| 455 | 620 | } |
| 456 | 621 | |
| 457 | - function display_property( $property, $value, $unit = '' ) { | |
| 458 | - echo "\n" . $property . ": " . $value . $unit . ";\n"; | |
| 622 | + function display_property( $property, $value, $unit = '', $prefix = '' ) { | |
| 623 | + echo $prefix . $property . ": " . $value . $unit . ";\n"; | |
| 459 | 624 | } |
| 460 | 625 | |
| 461 | 626 | // well weight sometimes comes from google as 600italic which in CSS syntax should come in two separate properties |
| 462 | - function display_weight_property( $value ) { | |
| 627 | + function display_weight_property( $value, $prefix = '' ) { | |
| 463 | 628 | $has_style = false; |
| 464 | 629 | |
| 465 | 630 | if ( strpos( $value, 'italic' ) !== false ) { |
| 466 | 631 | |
| 467 | 632 | $value = str_replace( 'italic', '', $value ); |
| 468 | - echo "\n" . 'font-weight' . ": " . $value . ";\n"; | |
| 469 | - echo "\n" . 'font-style' . ": italic;\n"; | |
| 633 | + echo $prefix . 'font-weight' . ": " . $value . ";\n"; | |
| 634 | + echo $prefix . 'font-style' . ": italic;\n"; | |
| 470 | 635 | $has_style = true; |
| 471 | 636 | } else { |
| 472 | - echo "\n" . 'font-weight' . ": " . $value . ";\n"; | |
| 637 | + echo $prefix . 'font-weight' . ": " . $value . ";\n"; | |
| 473 | 638 | } |
| 474 | 639 | |
| 475 | 640 | |
| 476 | 641 | return $has_style; |
| @@ -500,10 +665,10 @@ | ||
| 500 | 665 | * @since 1.0.0 |
| 501 | 666 | */ |
| 502 | 667 | public function __clone() { |
| 503 | 668 | |
| 504 | - _doing_it_wrong( __FUNCTION__,esc_html( __( 'Cheatin’ huh?' ) ), '' ); | |
| 505 | - } // End __clone () | |
| 669 | + _doing_it_wrong( __FUNCTION__,esc_html__( 'You should not do that!', 'customify' ), '' ); | |
| 670 | + } | |
| 506 | 671 | |
| 507 | 672 | /** |
| 508 | 673 | * Unserializing instances of this class is forbidden. |
| 509 | 674 | * |
| @@ -510,7 +675,7 @@ | ||
| 510 | 675 | * @since 1.0.0 |
| 511 | 676 | */ |
| 512 | 677 | public function __wakeup() { |
| 513 | 678 | |
| 514 | - _doing_it_wrong( __FUNCTION__, esc_html( __( 'Cheatin’ huh?' ) ), '' ); | |
| 515 | - } // End __wakeup () | |
| 679 | + _doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), '' ); | |
| 680 | + } | |
| 516 | 681 | } |