| @@ -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,59 +6,145 @@ | ||
| 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 | - protected static $theme_fonts = null; | |
| 14 | + protected $theme_fonts = null; | |
| 15 | + protected $customify_CSS_output = array(); | |
| 15 | 16 | |
| 16 | - function __construct( $parent ) { | |
| 17 | - add_action( 'customize_preview_init', array( $this, 'enqueue_admin_customizer_preview_assets' ), 10 ); | |
| 18 | - $load_location = PixCustomifyPlugin::get_plugin_option( 'style_resources_location', 'wp_head' ); | |
| 19 | - add_action( $load_location, array( $this, 'output_font_dynamic_style' ), 999999999 ); | |
| 20 | 17 | |
| 21 | - $this->parent = $parent; | |
| 22 | - self::$theme_fonts = apply_filters( 'customify_theme_fonts', array() ); | |
| 18 | + function __construct() { | |
| 19 | + $this->theme_fonts = apply_filters( 'customify_theme_fonts', array() ); | |
| 23 | 20 | |
| 24 | - 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 | + | |
| 25 | 30 | } |
| 26 | 31 | |
| 27 | - function add_customify_theme_fonts( $active_font_family, $val ) { | |
| 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 | + | |
| 98 | + public function get_theme_fonts() { | |
| 99 | + return $this->theme_fonts; | |
| 100 | + } | |
| 101 | + | |
| 102 | + function add_customify_theme_fonts( $active_font_family ) { | |
| 28 | 103 | //first get all the published custom fonts |
| 29 | - if ( empty( self::$theme_fonts ) ) { | |
| 104 | + if ( empty( $this->theme_fonts ) ) { | |
| 30 | 105 | return; |
| 31 | 106 | } |
| 32 | 107 | |
| 33 | 108 | echo '<optgroup label="' . esc_html__( 'Theme Fonts', 'customify' ) . '">'; |
| 34 | - foreach ( self::$theme_fonts as $font ) { | |
| 109 | + foreach ( $this->theme_fonts as $font ) { | |
| 35 | 110 | if ( ! empty( $font ) ) { |
| 36 | 111 | //display the select option's HTML |
| 37 | - 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' ); | |
| 38 | 113 | } |
| 39 | 114 | } |
| 40 | 115 | echo "</optgroup>"; |
| 41 | 116 | } |
| 42 | 117 | |
| 43 | - function output_font_dynamic_style() { | |
| 118 | + function maybe_decode_value( $value ) { | |
| 44 | 119 | |
| 45 | - self::$options_list = $this->get_options(); | |
| 120 | + $to_return = PixCustomifyPlugin::decodeURIComponent( $value ); | |
| 121 | + if ( is_string( $value ) ) { | |
| 122 | + $to_return = json_decode( wp_unslash( $to_return ), true ); | |
| 123 | + } | |
| 46 | 124 | |
| 47 | - self::get_typography_fields( self::$options_list, 'type', 'font', self::$typo_settings ); | |
| 125 | + return $to_return; | |
| 126 | + } | |
| 48 | 127 | |
| 49 | - if ( empty( self::$typo_settings ) ) { | |
| 50 | - return; | |
| 51 | - } | |
| 128 | + protected function get_fonts_args() { | |
| 52 | 129 | |
| 53 | - $google_families = $local_families = ''; | |
| 54 | - | |
| 55 | 130 | $args = array( |
| 56 | - 'google_families' => '', | |
| 57 | - 'local_families' => '', | |
| 58 | - 'local_srcs' => '', | |
| 131 | + 'google_families' => array(), | |
| 132 | + 'local_families' => array(), | |
| 133 | + 'local_srcs' => array(), | |
| 59 | 134 | ); |
| 60 | 135 | |
| 136 | + /** @var PixCustomifyPlugin $local_plugin */ | |
| 137 | + $local_plugin = PixCustomifyPlugin(); | |
| 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 | + | |
| 143 | + if ( empty( self::$typo_settings ) ) { | |
| 144 | + return $args; | |
| 145 | + } | |
| 146 | + | |
| 61 | 147 | foreach ( self::$typo_settings as $id => $font ) { |
| 62 | 148 | if ( isset ( $font['value'] ) ) { |
| 63 | 149 | |
| 64 | 150 | $load_all_weights = false; |
| @@ -64,15 +150,16 @@ | ||
| 64 | 150 | $load_all_weights = false; |
| 65 | 151 | if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) { |
| 66 | 152 | $load_all_weights = true; |
| 67 | 153 | } |
| 68 | - $value = json_decode( wp_unslash( PixCustomifyPlugin::decodeURIComponent( $font['value'] ) ), true ); | |
| 69 | 154 | |
| 155 | + $value = $this->maybe_decode_value( $font['value'] ); | |
| 156 | + | |
| 70 | 157 | $value = $this->validate_font_values( $value ); |
| 71 | 158 | |
| 72 | - // 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) | |
| 73 | 160 | if ( ! is_array( $value ) || $value === null ) { |
| 74 | - $value = $this->get_font_defaults_value( str_replace( '"', '', $font['value'] ) ); | |
| 161 | + $value = $local_plugin->customizer->get_font_defaults_value( str_replace( '"', '', $font['value'] ) ); | |
| 75 | 162 | } |
| 76 | 163 | |
| 77 | 164 | //bail if by this time we don't have a value of some sort |
| 78 | 165 | if ( empty( $value ) ) { |
| @@ -78,130 +165,185 @@ | ||
| 78 | 165 | if ( empty( $value ) ) { |
| 79 | 166 | continue; |
| 80 | 167 | } |
| 81 | 168 | |
| 82 | - //Handle special logic for when the $value array is not an associative array | |
| 83 | - if ( ! self::is_assoc( $value ) ) { | |
| 84 | - $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 ); | |
| 85 | 172 | } |
| 86 | 173 | |
| 87 | - if ( isset( $value['font_family'] ) && isset( $value['type'] ) && $value['type'] == 'google' ) { | |
| 88 | - $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 | + } | |
| 89 | 178 | |
| 90 | - if ( $load_all_weights && is_array( $value['variants'] ) ) { | |
| 91 | - $args['google_families'] .= ":" . implode( ',', $value['variants'] ); | |
| 92 | - } 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'] ) ) { | |
| 93 | 202 | if ( is_array( $value['selected_variants'] ) ) { |
| 94 | - $args['google_families'] .= ":" . implode( ',', $value['selected_variants'] ); | |
| 203 | + $family .= ":" . implode( ',', $value['selected_variants'] ); | |
| 95 | 204 | } elseif ( is_string( $value['selected_variants'] ) || is_numeric( $value['selected_variants'] ) ) { |
| 96 | - $args['google_families'] .= ":" . $value['selected_variants']; | |
| 205 | + $family .= ":" . $value['selected_variants']; | |
| 97 | 206 | } |
| 98 | - } elseif ( isset( $value['variants'] ) && ! empty( $value['variants'] ) ) { | |
| 207 | + } elseif ( ! empty( $value['variants'] ) ) { | |
| 99 | 208 | if ( is_array( $value['variants'] ) ) { |
| 100 | - $args['google_families'] .= ":" . implode( ',', $value['variants'] ); | |
| 209 | + $family .= ":" . implode( ',', $value['variants'] ); | |
| 101 | 210 | } else { |
| 102 | - $args['google_families'] .= ":" . $value['variants']; | |
| 211 | + $family .= ":" . $value['variants']; | |
| 103 | 212 | } |
| 104 | 213 | } |
| 105 | 214 | |
| 106 | - if ( isset( $value['selected_subsets'] ) && ! empty( $value['selected_subsets'] ) ) { | |
| 215 | + if ( ! empty( $value['selected_subsets'] ) ) { | |
| 107 | 216 | if ( is_array( $value['selected_subsets'] ) ) { |
| 108 | - $args['google_families'] .= ":" . implode( ',', $value['selected_subsets'] ); | |
| 217 | + $family .= ":" . implode( ',', $value['selected_subsets'] ); | |
| 109 | 218 | } else { |
| 110 | - $args['google_families'] .= ":" . $value['selected_subsets']; | |
| 219 | + $family .= ":" . $value['selected_subsets']; | |
| 111 | 220 | } |
| 112 | - } elseif ( isset( $value['subsets'] ) && ! empty( $value['subsets'] ) ) { | |
| 221 | + } elseif ( ! empty( $value['subsets'] ) ) { | |
| 113 | 222 | if ( is_array( $value['subsets'] ) ) { |
| 114 | - $args['google_families'] .= ":" . implode( ',', $value['subsets'] ); | |
| 223 | + $family .= ":" . implode( ',', $value['subsets'] ); | |
| 115 | 224 | } else { |
| 116 | - $args['google_families'] .= ":" . $value['subsets']; | |
| 225 | + $family .= ":" . $value['subsets']; | |
| 117 | 226 | } |
| 118 | 227 | } |
| 119 | 228 | |
| 120 | - $args['google_families'] .= '\','; | |
| 121 | - } elseif ( isset( self::$theme_fonts[ $value['font_family'] ] ) ) { | |
| 229 | + $family .= "'"; | |
| 122 | 230 | |
| 123 | -// $value['type'] = 'theme_font'; | |
| 124 | -// $args['local_srcs'] .= self::$theme_fonts[ $value['font_family'] ]['src'] . ','; | |
| 125 | -// $value['variants'] = self::$theme_fonts[ $value['font_family'] ]['variants']; | |
| 126 | - | |
| 127 | - if ( false === strpos( $args['local_families'], $value['font_family'] ) ) { | |
| 128 | - $args['local_families'] .= $value['font_family']; | |
| 129 | - } | |
| 130 | - | |
| 131 | - if ( false === strpos( $args['local_srcs'], self::$theme_fonts[ $value['font_family'] ]['src'] ) ) { | |
| 132 | - $args['local_srcs'] .= self::$theme_fonts[ $value['font_family'] ]['src'] . ','; | |
| 133 | - } | |
| 231 | + $args['google_families'][] = $family; | |
| 134 | 232 | } |
| 135 | 233 | } |
| 136 | 234 | } |
| 137 | 235 | |
| 138 | - if ( ( ! empty ( $args['local_families'] ) || ! empty ( $args['google_families'] ) ) && self::get_plugin_option( 'typography', '1' ) && self::get_plugin_option( 'typography_google_fonts', 1 ) ) { | |
| 139 | - $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; | |
| 140 | 255 | } |
| 141 | 256 | |
| 257 | + $output = ''; | |
| 258 | + | |
| 142 | 259 | foreach ( self::$typo_settings as $key => $font ) { |
| 143 | - if ( empty( $font['selector'] ) || empty( $font['value'] ) ) { | |
| 260 | + $font_output = $this->get_font_style( $font ); | |
| 261 | + if ( empty( $font_output ) ) { | |
| 144 | 262 | continue; |
| 145 | 263 | } |
| 146 | 264 | |
| 147 | - $value = json_decode( PixCustomifyPlugin::decodeURIComponent( $font['value'] ), true ); | |
| 148 | - // in case the value is still null, try default value(mostly for google fonts) | |
| 149 | - if ( $value === null ) { | |
| 150 | - $value = $this->get_font_defaults_value( $font['value'] ); | |
| 265 | + $output .= $font_output . "\n"; | |
| 266 | + | |
| 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 | |
| 151 | 273 | } |
| 274 | + } | |
| 152 | 275 | |
| 153 | - // shim the old case when the default was only the font name | |
| 154 | - if ( is_string( $value ) && ! empty( $value ) ) { | |
| 155 | - $value = array( 'font_family' => $value ); | |
| 156 | - } | |
| 276 | + // in customizer the CSS is printed per option, in front-end we need to print them in bulk | |
| 277 | + if ( ! isset( $GLOBALS['wp_customize'] ) ) { ?> | |
| 278 | + <style id="customify_fonts_output"> | |
| 279 | + <?php echo $output; ?> | |
| 280 | + </style><?php | |
| 281 | + } | |
| 282 | + } | |
| 157 | 283 | |
| 158 | - //Handle special logic for when the $value array is not an associative array | |
| 159 | - if ( ! self::is_assoc( $value ) ) { | |
| 160 | - $value = $this->process_a_not_associative_font_default( $value ); | |
| 284 | + function get_fonts_dynamic_style() { | |
| 285 | + | |
| 286 | + $output = ''; | |
| 287 | + | |
| 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; | |
| 161 | 303 | } |
| 162 | 304 | |
| 163 | - $this->output_font_style( $key, $font, $value ); | |
| 305 | + $output .= $font_output . "\n"; | |
| 164 | 306 | } |
| 307 | + | |
| 308 | + return $output; | |
| 165 | 309 | } |
| 166 | 310 | |
| 167 | - function display_webfont_script( $args ) { ?> | |
| 168 | - <script type="text/javascript"> | |
| 169 | - var customify_font_loader = function () { | |
| 170 | - var webfontargs = { | |
| 171 | - classes: false, | |
| 172 | - events: false | |
| 173 | - }; | |
| 174 | - <?php if ( ! empty( $args['google_families'] ) ) { ?> | |
| 175 | - webfontargs.google = { families: [<?php echo( rtrim( $args['google_families'], ',' ) ); ?>] }; | |
| 176 | - <?php } | |
| 177 | - if ( ! empty( $args['local_families'] ) && ! empty( $args['local_srcs'] ) ) { ?> | |
| 178 | - webfontargs.custom = { | |
| 179 | - families: ['<?php echo( rtrim( $args['local_families'], ',' ) ); ?>'], | |
| 180 | - urls: ['<?php echo rtrim( $args['local_srcs'], ',' ) ?>'] | |
| 181 | - }; | |
| 182 | - <?php } ?> | |
| 183 | - WebFont.load(webfontargs); | |
| 184 | - } | |
| 311 | + function get_font_style( $font ) { | |
| 185 | 312 | |
| 186 | - if ( typeof WebFont !== 'undefined' ) { <?php // if there is a WebFont object, use it ?> | |
| 187 | - customify_font_loader(); | |
| 188 | - } else { <?php // basically when we don't have the WebFont object we create the google script dynamically ?> | |
| 189 | - var tk = document.createElement('script'); | |
| 190 | - tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'; | |
| 191 | - tk.type = 'text/javascript'; | |
| 313 | + if ( ! isset( $font['selector'] ) ) { | |
| 314 | + return ''; | |
| 315 | + } | |
| 192 | 316 | |
| 193 | - tk.onload = tk.onreadystatechange = function () { | |
| 194 | - customify_font_loader(); | |
| 195 | - }; | |
| 196 | - var s = document.getElementsByTagName('script')[0]; | |
| 197 | - s.parentNode.insertBefore(tk, s); | |
| 198 | - } | |
| 199 | - </script> | |
| 200 | - <?php | |
| 201 | - } | |
| 317 | + $font['selector'] = apply_filters( 'customify_font_css_selector', $font['selector'], $font ); | |
| 318 | + if ( empty( $font['selector'] ) || empty( $font['value'] ) ) { | |
| 319 | + return ''; | |
| 320 | + } | |
| 202 | 321 | |
| 203 | - function output_font_style( $field, $font, $value ) { | |
| 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 | + | |
| 204 | 346 | $value = $this->validate_font_values( $value ); |
| 205 | 347 | // some sanitizing |
| 206 | 348 | $load_all_weights = false; |
| 207 | 349 | if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) { |
| @@ -206,9 +348,8 @@ | ||
| 206 | 348 | $load_all_weights = false; |
| 207 | 349 | if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) { |
| 208 | 350 | $load_all_weights = true; |
| 209 | 351 | } |
| 210 | - | |
| 211 | 352 | $selected_variant = ''; |
| 212 | 353 | if ( ! empty( $value['selected_variants'] ) ) { |
| 213 | 354 | if ( is_array( $value['selected_variants'] ) ) { |
| 214 | 355 | $selected_variant = $value['selected_variants'][0]; |
| @@ -214,111 +355,233 @@ | ||
| 214 | 355 | $selected_variant = $value['selected_variants'][0]; |
| 215 | 356 | } else { |
| 216 | 357 | $selected_variant = $value['selected_variants']; |
| 217 | 358 | } |
| 218 | - } ?> | |
| 219 | - <style id="customify_font_output_for_<?php echo $field; ?>"> | |
| 220 | - <?php | |
| 221 | - if ( isset( $font['callback'] ) && function_exists( $font['callback'] ) ) { | |
| 222 | - $output = call_user_func( $font['callback'], $value, $font ); | |
| 223 | - echo $output; | |
| 224 | - } else { | |
| 225 | - echo $font['selector'] . " {"; | |
| 359 | + } | |
| 226 | 360 | |
| 227 | - // First handle the case where we have the font-family in the selected variant (usually this means a custom font from our Fonto plugin) | |
| 228 | - if ( ! empty( $selected_variant ) && is_array( $selected_variant ) && ! empty( $selected_variant['font-family'] ) ) { | |
| 229 | - //the variant's font-family | |
| 230 | - $this->display_property( 'font-family', $selected_variant['font-family'] ); | |
| 361 | + ob_start(); | |
| 231 | 362 | |
| 232 | - if ( ! $load_all_weights ) { | |
| 233 | - // if this is a custom font (like from our plugin Fonto) with individual styles & weights - i.e. the font-family says it all | |
| 234 | - // we need to "force" the font-weight and font-style | |
| 235 | - if ( ! empty( $value['type'] ) && 'custom_individual' == $value['type'] ) { | |
| 236 | - $selected_variant['font-weight'] = '400 !important'; | |
| 237 | - $selected_variant['font-style'] = 'normal !important'; | |
| 238 | - } | |
| 363 | + if ( isset( $font['callback'] ) && function_exists( $font['callback'] ) ) { | |
| 364 | + $output = call_user_func( $font['callback'], $value, $font ); | |
| 365 | + echo $output; | |
| 366 | + } elseif ( isset( $font['selector'] ) ) { | |
| 367 | + echo $font['selector'] . " {" . "\n"; | |
| 239 | 368 | |
| 240 | - // output the font weight, if available | |
| 241 | - if ( ! empty( $selected_variant['font-weight'] ) ) { | |
| 242 | - echo ": " . $selected_variant['font-weight'] . ";\n"; | |
| 243 | - $this->display_property( 'font-weight', $selected_variant['font-weight'] ); | |
| 244 | - } | |
| 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) | |
| 370 | + if ( ! empty( $selected_variant ) && is_array( $selected_variant ) && ! empty( $selected_variant['font-family'] ) ) { | |
| 371 | + //the variant's font-family | |
| 372 | + $this->display_property( 'font-family', $selected_variant['font-family'], '', $properties_prefix ); | |
| 245 | 373 | |
| 246 | - // output the font style, if available | |
| 247 | - if ( ! empty( $selected_variant['font-style'] ) ) { | |
| 248 | - $this->display_property( 'font-style', $selected_variant['font-style'] ); | |
| 249 | - } | |
| 374 | + if ( ! $load_all_weights ) { | |
| 375 | + // if this is a custom font (like from our plugin Fonto) with individual styles & weights - i.e. the font-family says it all | |
| 376 | + // we need to "force" the font-weight and font-style | |
| 377 | + if ( ! empty( $value['type'] ) && 'custom_individual' == $value['type'] ) { | |
| 378 | + $selected_variant['font-weight'] = '400 !important'; | |
| 379 | + $selected_variant['font-style'] = 'normal !important'; | |
| 250 | 380 | } |
| 251 | 381 | |
| 252 | - } elseif ( isset( $value['font_family'] ) ) { | |
| 253 | - // the selected font family | |
| 254 | - $this->display_property( 'font-family', $value['font_family'] ); | |
| 382 | + $italic_font = false; | |
| 255 | 383 | |
| 256 | - if ( ! empty( $selected_variant ) && ! $load_all_weights ) { | |
| 257 | - $weight_and_style = strtolower( $selected_variant ); | |
| 258 | - $italic_font = false; | |
| 384 | + // output the font weight, if available | |
| 385 | + if ( ! empty( $selected_variant['font-weight'] ) ) { | |
| 386 | + echo ": " . $selected_variant['font-weight'] . ";\n"; | |
| 387 | + $italic_font = $this->display_weight_property( $selected_variant['font-weight'], $properties_prefix ); | |
| 388 | + } | |
| 259 | 389 | |
| 260 | - //determine if this is an italic font (the $weight_and_style is usually like '400' or '400italic' ) | |
| 261 | - if ( strpos( $weight_and_style, 'italic' ) !== false ) { | |
| 262 | - $weight_and_style = str_replace( 'italic', '', $weight_and_style); | |
| 263 | - $italic_font = true; | |
| 264 | - } | |
| 390 | + // output the font style, if available and if it wasn't displayed already | |
| 391 | + if ( ! $italic_font && ! empty( $selected_variant['font-style'] ) ) { | |
| 392 | + $this->display_property( 'font-style', $selected_variant['font-style'], '', $properties_prefix ); | |
| 393 | + } | |
| 394 | + } | |
| 265 | 395 | |
| 266 | - if ( ! empty( $weight_and_style ) ) { | |
| 267 | - //a little bit of sanity check - in case it's not a number | |
| 268 | - if( $weight_and_style === 'regular' ) { | |
| 269 | - $weight_and_style = 'normal'; | |
| 270 | - } | |
| 271 | - $this->display_property( 'font-weight', $weight_and_style ); | |
| 396 | + } elseif ( isset( $value['font_family'] ) ) { | |
| 397 | + // the selected font family | |
| 398 | + $this->display_property( 'font-family', $value['font_family'], '', $properties_prefix ); | |
| 399 | + | |
| 400 | + if ( ! empty( $selected_variant ) && ! $load_all_weights ) { | |
| 401 | + $weight_and_style = strtolower( $selected_variant ); | |
| 402 | + $italic_font = false; | |
| 403 | + | |
| 404 | + //determine if this is an italic font (the $weight_and_style is usually like '400' or '400italic' ) | |
| 405 | + if ( ! empty( $weight_and_style ) ) { | |
| 406 | + //a little bit of sanity check - in case it's not a number | |
| 407 | + if( $weight_and_style === 'regular' ) { | |
| 408 | + $weight_and_style = 'normal'; | |
| 272 | 409 | } |
| 410 | + $italic_font = $this->display_weight_property( $weight_and_style, $properties_prefix ); | |
| 411 | + } | |
| 273 | 412 | |
| 274 | - if ( $italic_font ) { | |
| 275 | - $this->display_property( 'font-style', 'italic' ); | |
| 276 | - } | |
| 413 | + // output the font style, if available | |
| 414 | + if ( ! $italic_font && ! empty( $selected_variant['font-style'] ) ) { | |
| 415 | + $this->display_property( 'font-style', $selected_variant['font-style'], '', $properties_prefix ); | |
| 277 | 416 | } |
| 278 | - } else if ( isset( $value['font-family'] ) ) { | |
| 279 | - $this->display_property( 'font-family', $value['font-family'] ); | |
| 280 | 417 | } |
| 418 | + } else if ( isset( $value['font-family'] ) ) { | |
| 419 | + $this->display_property( 'font-family', $value['font-family'], '', $properties_prefix ); | |
| 420 | + } | |
| 281 | 421 | |
| 282 | - if ( ! empty( $value['font_weight'] ) ) { | |
| 283 | - $this->display_property( 'font-weight', $value['font_weight'] ); | |
| 284 | - } | |
| 422 | + if ( ! empty( $value['font_weight'] ) ) { | |
| 423 | + $this->display_weight_property( $value['font_weight'], $properties_prefix ); | |
| 424 | + } | |
| 285 | 425 | |
| 286 | - if ( ! empty( $value['font_size'] ) ) { | |
| 426 | + if ( ! empty( $value['font_size'] ) ) { | |
| 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'] ) ) { | |
| 287 | 432 | $unit = $this->get_field_unit( $font, 'font-size' ); |
| 288 | - $this->display_property( 'font-size', $value['font_size'], $unit ); | |
| 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 | + } | |
| 289 | 441 | } |
| 290 | 442 | |
| 291 | - if ( ! empty( $value['line_height'] ) ) { | |
| 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 ); | |
| 448 | + } | |
| 449 | + | |
| 450 | + if ( isset( $value['line_height'] ) ) { | |
| 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'] ) ) { | |
| 292 | 456 | $unit = $this->get_field_unit( $font, 'line-height' ); |
| 293 | - $this->display_property( 'line-height', $value['line_height'], $unit ); | |
| 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 | + } | |
| 294 | 465 | } |
| 295 | 466 | |
| 296 | - if ( ! empty( $value['letter_spacing'] ) ) { | |
| 467 | + $this->display_property( 'line-height', $line_height, $unit, $properties_prefix ); | |
| 468 | + } | |
| 469 | + | |
| 470 | + if ( isset( $value['letter_spacing'] ) ) { | |
| 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'] ) ) { | |
| 297 | 476 | $unit = $this->get_field_unit( $font, 'letter-spacing' ); |
| 298 | - $this->display_property( 'letter-spacing', $value['letter_spacing'], $unit ); | |
| 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 | + } | |
| 299 | 485 | } |
| 300 | 486 | |
| 301 | - if ( ! empty( $value['text_align'] ) ) { | |
| 302 | - $this->display_property( 'text-align', $value['text_align'] ); | |
| 303 | - } | |
| 487 | + $this->display_property( 'letter-spacing', $letter_spacing, $unit, $properties_prefix ); | |
| 488 | + } | |
| 304 | 489 | |
| 305 | - if ( ! empty( $value['text_transform'] ) ) { | |
| 306 | - $this->display_property( 'text-transform', $value['text_transform'] ); | |
| 307 | - } | |
| 490 | + if ( ! empty( $value['text_align'] ) ) { | |
| 491 | + $this->display_property( 'text-align', $value['text_align'], '', $properties_prefix ); | |
| 492 | + } | |
| 308 | 493 | |
| 309 | - if ( ! empty( $value['text_decoration'] ) ) { | |
| 310 | - $this->display_property( 'text-decoration', $value['text_decoration'] ); | |
| 311 | - } | |
| 312 | - echo "}\n"; | |
| 313 | - } ?> | |
| 314 | - </style> | |
| 315 | - <?php | |
| 494 | + if ( ! empty( $value['text_transform'] ) ) { | |
| 495 | + $this->display_property( 'text-transform', $value['text_transform'], '', $properties_prefix ); | |
| 496 | + } | |
| 497 | + | |
| 498 | + if ( ! empty( $value['text_decoration'] ) ) { | |
| 499 | + $this->display_property( 'text-decoration', $value['text_decoration'], '', $properties_prefix ); | |
| 500 | + } | |
| 501 | + echo "}\n"; | |
| 502 | + } | |
| 503 | + | |
| 504 | + $CSS = ob_get_clean(); | |
| 505 | + | |
| 506 | + return $CSS; | |
| 316 | 507 | } |
| 317 | 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 ''; | |
| 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 ); | |
| 574 | + } | |
| 575 | + | |
| 318 | 576 | function get_field_unit( $font, $field ) { |
| 319 | 577 | |
| 320 | 578 | if ( empty( $font ) || empty( $font['fields'] ) || empty( $font['fields'][ $field ] ) ) { |
| 579 | + | |
| 580 | + if ( 'line-height' == $field ){ | |
| 581 | + return ''; | |
| 582 | + } | |
| 583 | + | |
| 321 | 584 | return 'px'; |
| 322 | 585 | } |
| 323 | 586 | |
| 324 | 587 | if ( isset( $font['fields'][ $field ]['unit'] ) ) { |
| @@ -355,14 +618,64 @@ | ||
| 355 | 618 | function display_selector( $selector ) { |
| 356 | 619 | |
| 357 | 620 | } |
| 358 | 621 | |
| 359 | - function display_property( $property, $value, $unit = '' ) { | |
| 360 | - echo "\n" . $property . ": " . $value . $unit . ";\n"; | |
| 622 | + function display_property( $property, $value, $unit = '', $prefix = '' ) { | |
| 623 | + echo $prefix . $property . ": " . $value . $unit . ";\n"; | |
| 361 | 624 | } |
| 362 | 625 | |
| 363 | - function enqueue_admin_customizer_preview_assets() { | |
| 364 | - $dir = plugin_dir_url( __FILE__ ); | |
| 365 | - $dir = rtrim( $dir, 'features/' ); | |
| 366 | - wp_enqueue_script( 'font_selector_preview', $dir . '/js/font_selector_preview.js', array( 'jquery' ), false, true ); | |
| 626 | + // well weight sometimes comes from google as 600italic which in CSS syntax should come in two separate properties | |
| 627 | + function display_weight_property( $value, $prefix = '' ) { | |
| 628 | + $has_style = false; | |
| 629 | + | |
| 630 | + if ( strpos( $value, 'italic' ) !== false ) { | |
| 631 | + | |
| 632 | + $value = str_replace( 'italic', '', $value ); | |
| 633 | + echo $prefix . 'font-weight' . ": " . $value . ";\n"; | |
| 634 | + echo $prefix . 'font-style' . ": italic;\n"; | |
| 635 | + $has_style = true; | |
| 636 | + } else { | |
| 637 | + echo $prefix . 'font-weight' . ": " . $value . ";\n"; | |
| 638 | + } | |
| 639 | + | |
| 640 | + | |
| 641 | + return $has_style; | |
| 642 | + } | |
| 643 | + | |
| 644 | + /** | |
| 645 | + * Main Customify_Font_Selector Instance | |
| 646 | + * | |
| 647 | + * Ensures only one instance of Customify_Font_Selector is loaded or can be loaded. | |
| 648 | + * | |
| 649 | + * @since 1.0.0 | |
| 650 | + * @static | |
| 651 | + * | |
| 652 | + * @return Customify_Font_Selector Main Customify_Font_Selector instance | |
| 653 | + */ | |
| 654 | + public static function instance() { | |
| 655 | + | |
| 656 | + if ( is_null( self::$_instance ) ) { | |
| 657 | + self::$_instance = new self(); | |
| 658 | + } | |
| 659 | + return self::$_instance; | |
| 660 | + } // End instance () | |
| 661 | + | |
| 662 | + /** | |
| 663 | + * Cloning is forbidden. | |
| 664 | + * | |
| 665 | + * @since 1.0.0 | |
| 666 | + */ | |
| 667 | + public function __clone() { | |
| 668 | + | |
| 669 | + _doing_it_wrong( __FUNCTION__,esc_html__( 'You should not do that!', 'customify' ), '' ); | |
| 670 | + } | |
| 671 | + | |
| 672 | + /** | |
| 673 | + * Unserializing instances of this class is forbidden. | |
| 674 | + * | |
| 675 | + * @since 1.0.0 | |
| 676 | + */ | |
| 677 | + public function __wakeup() { | |
| 678 | + | |
| 679 | + _doing_it_wrong( __FUNCTION__, esc_html__( 'You should not do that!', 'customify' ), '' ); | |
| 367 | 680 | } |
| 368 | 681 | } |