PluginProbe
Customify / 1.7.3
Customify v1.7.3
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
← All changes | features/class-Font_Selector.php +242 -116 1.4.01.7.3 View file →
@@ -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,36 +6,103 @@
6 6 * Instance of this class.
7 7 * @since 1.0.0
8 8 * @var object
9 9 */
10 - protected static $instance = null;
11 - protected $parent = null;
10 + protected static $_instance = null;
11 +
12 12 protected static $typo_settings = null;
13 13 protected static $options_list = null;
14 - static $theme_fonts = null;
14 + protected $theme_fonts = null;
15 + protected $customify_CSS_output = array();
15 16
16 17
17 - function __construct( $parent ) {
18 - global $pixcustomify_plugin;
18 + function __construct() {
19 + $this->theme_fonts = apply_filters( 'customify_theme_fonts', array() );
19 20
20 - $load_location = PixCustomifyPlugin::get_plugin_option( 'style_resources_location', 'wp_head' );
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 24
22 - add_action( $load_location, array( $this, 'output_font_dynamic_style' ), 999999999 );
25 + if ( PixCustomifyPlugin()->get_plugin_setting( 'enable_editor_style', true ) ) {
26 + add_action( 'admin_head', array( $this, 'add_customizer_settings_into_wp_editor' ) );
27 + }
23 28
24 - $this->parent = $parent;
25 - self::$theme_fonts = apply_filters( 'customify_theme_fonts', array() );
29 + }
26 30
27 - add_action( 'customify_font_family_before_options', array( $this, 'add_customify_theme_fonts' ), 11, 2 );
31 + function add_customizer_settings_into_wp_editor() {
32 +
33 + ob_start();
34 + $this->output_font_dynamic_style();
35 +
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 want 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);
47 +
48 + var script = myIframe.contentWindow.document.createElement("script");
49 + script.type = "text/javascript";
50 + script.innerHTML = scriptEl.innerHTML;
51 +
52 + myIframe.contentWindow.document.head.appendChild(script);
53 + };
54 +
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];
59 +
60 + if (typeof styleElment !== "undefined") {
61 + head.appendChild(styleElment);
62 + }
63 + };
64 +
65 + var xmlString = <?php echo json_encode( str_replace( "\n", "", $custom_css ) ); ?>,
66 + parser = new DOMParser(),
67 + doc = parser.parseFromString(xmlString, "text/html");
68 +
69 + if (typeof window.frames['content_ifr'] !== 'undefined') {
70 +
71 + $.each(doc.head.childNodes, function (key, el) {
72 + if (typeof el !== "undefined" && typeof el.tagName !== "undefined") {
73 +
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 + });
86 + }
87 + });
88 + })(jQuery);
89 + /* ]]> */
90 + </script>
91 + <?php }
92 +
93 + public function get_theme_fonts() {
94 + return $this->theme_fonts;
28 95 }
29 96
30 97 function add_customify_theme_fonts( $active_font_family, $val ) {
31 98 //first get all the published custom fonts
32 - if ( empty( self::$theme_fonts ) ) {
99 + if ( empty( $this->theme_fonts ) ) {
33 100 return;
34 101 }
35 102
36 103 echo '<optgroup label="' . esc_html__( 'Theme Fonts', 'customify' ) . '">';
37 - foreach ( self::$theme_fonts as $font ) {
104 + foreach ( $this->theme_fonts as $font ) {
38 105 if ( ! empty( $font ) ) {
39 106 //display the select option's HTML
40 107 Pix_Customize_Font_Control::output_font_option( $font['family'], $active_font_family, $font, 'theme_font' );
41 108 }
@@ -54,12 +121,15 @@
54 121 }
55 122
56 123 function output_font_dynamic_style() {
57 124
58 - self::$options_list = $this->get_options();
125 + /** @var PixCustomifyPlugin $local_plugin */
126 + $local_plugin = PixCustomifyPlugin();
59 127
60 - self::get_typography_fields( self::$options_list, 'type', 'font', self::$typo_settings );
128 + self::$options_list = $local_plugin->get_options();
61 129
130 + $local_plugin->get_typography_fields( self::$options_list, 'type', 'font', self::$typo_settings );
131 +
62 132 if ( empty( self::$typo_settings ) ) {
63 133 return;
64 134 }
65 135
@@ -66,10 +136,10 @@
66 136 $google_families = $local_families = '';
67 137
68 138 $args = array(
69 139 'google_families' => '',
70 - 'local_families' => '',
71 - 'local_srcs' => '',
140 + 'local_families' => '',
141 + 'local_srcs' => '',
72 142 );
73 143
74 144 foreach ( self::$typo_settings as $id => $font ) {
75 145 if ( isset ( $font['value'] ) ) {
@@ -84,9 +154,9 @@
84 154 $value = $this->validate_font_values( $value );
85 155
86 156 // in case the value is still null, try default value(mostly for google fonts)
87 157 if ( ! is_array( $value ) || $value === null ) {
88 - $value = $this->get_font_defaults_value( str_replace( '"', '', $font['value'] ) );
158 + $value = $local_plugin->get_font_defaults_value( str_replace( '"', '', $font['value'] ) );
89 159 }
90 160
91 161 //bail if by this time we don't have a value of some sort
92 162 if ( empty( $value ) ) {
@@ -93,10 +163,10 @@
93 163 continue;
94 164 }
95 165
96 166 //Handle special logic for when the $value array is not an associative array
97 - if ( ! self::is_assoc( $value ) ) {
98 - $value = $this->process_a_not_associative_font_default( $value );
167 + if ( ! $local_plugin->is_assoc( $value ) ) {
168 + $value = $local_plugin->process_a_not_associative_font_default( $value );
99 169 }
100 170
101 171 if ( isset( $value['font_family'] ) && isset( $value['type'] ) && $value['type'] == 'google' ) {
102 172 $args['google_families'] .= "'" . $value['font_family'];
@@ -131,26 +201,26 @@
131 201 }
132 202 }
133 203
134 204 $args['google_families'] .= '\',';
135 - } elseif ( isset( self::$theme_fonts[ $value['font_family'] ] ) ) {
205 + } elseif ( isset( $this->theme_fonts[ $value['font_family'] ] ) ) {
136 206
137 207 // $value['type'] = 'theme_font';
138 -// $args['local_srcs'] .= self::$theme_fonts[ $value['font_family'] ]['src'] . ',';
139 -// $value['variants'] = self::$theme_fonts[ $value['font_family'] ]['variants'];
208 +// $args['local_srcs'] .= $this->theme_fonts[ $value['font_family'] ]['src'] . ',';
209 +// $value['variants'] = $this->theme_fonts[ $value['font_family'] ]['variants'];
140 210
141 211 if ( false === strpos( $args['local_families'], $value['font_family'] ) ) {
142 - $args['local_families'] .= $value['font_family'];
212 + $args['local_families'] .= "'" . $value['font_family'] . "',";
143 213 }
144 214
145 - if ( false === strpos( $args['local_srcs'], self::$theme_fonts[ $value['font_family'] ]['src'] ) ) {
146 - $args['local_srcs'] .= self::$theme_fonts[ $value['font_family'] ]['src'] . ',';
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'] . "',";
147 217 }
148 218 }
149 219 }
150 220 }
151 221
152 - if ( ( ! empty ( $args['local_families'] ) || ! empty ( $args['google_families'] ) ) && self::get_plugin_option( 'typography', '1' ) && self::get_plugin_option( 'typography_google_fonts', 1 ) ) {
222 + if ( ( ! empty ( $args['local_families'] ) || ! empty ( $args['google_families'] ) ) && PixCustomifyPlugin()->get_plugin_setting( 'typography', '1' ) && PixCustomifyPlugin()->get_plugin_setting( 'typography_google_fonts', 1 ) ) {
153 223 $this->display_webfont_script( $args );
154 224 }
155 225
156 226 foreach ( self::$typo_settings as $key => $font ) {
@@ -159,9 +229,9 @@
159 229 }
160 230 $value = $this->maybe_decode_value( $font['value'] );
161 231
162 232 if ( $value === null ) {
163 - $value = $this->get_font_defaults_value( $font['value'] );
233 + $value = $local_plugin->get_font_defaults_value( $font['value'] );
164 234 }
165 235
166 236 // shim the old case when the default was only the font name
167 237 if ( is_string( $value ) && ! empty( $value ) ) {
@@ -167,15 +237,23 @@
167 237 if ( is_string( $value ) && ! empty( $value ) ) {
168 238 $value = array( 'font_family' => $value );
169 239 }
170 240
171 - //Handle special logic for when the $value array is not an associative array
172 - if ( ! self::is_assoc( $value ) ) {
173 - $value = $this->process_a_not_associative_font_default( $value );
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 );
174 244 }
175 245
176 246 $this->output_font_style( $key, $font, $value );
177 247 }
248 +
249 + // in customizer the CSS is printed per option, in front-end we need to print them in bulk
250 + 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;
255 + }
178 256 }
179 257
180 258 function display_webfont_script( $args ) { ?>
181 259 <script type="text/javascript">
@@ -184,20 +262,20 @@
184 262 classes: false,
185 263 events: false
186 264 };
187 265 <?php if ( ! empty( $args['google_families'] ) ) { ?>
188 - webfontargs.google = { families: [<?php echo( rtrim( $args['google_families'], ',' ) ); ?>] };
266 + webfontargs.google = {families: [<?php echo( rtrim( $args['google_families'], ',' ) ); ?>]};
189 267 <?php }
190 268 if ( ! empty( $args['local_families'] ) && ! empty( $args['local_srcs'] ) ) { ?>
191 269 webfontargs.custom = {
192 - families: ['<?php echo( rtrim( $args['local_families'], ',' ) ); ?>'],
193 - urls: ['<?php echo rtrim( $args['local_srcs'], ',' ) ?>']
270 + families: [<?php echo( rtrim( $args['local_families'], ',' ) ); ?>],
271 + urls: [<?php echo rtrim( $args['local_srcs'], ',' ) ?>]
194 272 };
195 273 <?php } ?>
196 274 WebFont.load(webfontargs);
197 - }
275 + };
198 276
199 - if ( typeof WebFont !== 'undefined' ) { <?php // if there is a WebFont object, use it ?>
277 + if (typeof WebFont !== 'undefined') { <?php // if there is a WebFont object, use it ?>
200 278 customify_font_loader();
201 279 } else { <?php // basically when we don't have the WebFont object we create the google script dynamically ?>
202 280 var tk = document.createElement('script');
203 281 tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
@@ -226,104 +304,114 @@
226 304 $selected_variant = $value['selected_variants'][0];
227 305 } else {
228 306 $selected_variant = $value['selected_variants'];
229 307 }
230 - } ?>
231 - <style id="customify_font_output_for_<?php echo $field; ?>">
232 - <?php
233 - if ( isset( $font['callback'] ) && function_exists( $font['callback'] ) ) {
234 - $output = call_user_func( $font['callback'], $value, $font );
235 - echo $output;
236 - } else {
237 - echo $font['selector'] . " {";
308 + }
238 309
239 - // First handle the case where we have the font-family in the selected variant (usually this means a custom font from our Fonto plugin)
240 - if ( ! empty( $selected_variant ) && is_array( $selected_variant ) && ! empty( $selected_variant['font-family'] ) ) {
241 - //the variant's font-family
242 - $this->display_property( 'font-family', $selected_variant['font-family'] );
310 + ob_start();
243 311
244 - if ( ! $load_all_weights ) {
245 - // if this is a custom font (like from our plugin Fonto) with individual styles & weights - i.e. the font-family says it all
246 - // we need to "force" the font-weight and font-style
247 - if ( ! empty( $value['type'] ) && 'custom_individual' == $value['type'] ) {
248 - $selected_variant['font-weight'] = '400 !important';
249 - $selected_variant['font-style'] = 'normal !important';
250 - }
312 + if ( isset( $font['callback'] ) && function_exists( $font['callback'] ) ) {
313 + $output = call_user_func( $font['callback'], $value, $font );
314 + echo $output;
315 + } else {
316 + echo $font['selector'] . " {" . PHP_EOL;
251 317
252 - $italic_font = false;
318 + // 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 + if ( ! empty( $selected_variant ) && is_array( $selected_variant ) && ! empty( $selected_variant['font-family'] ) ) {
320 + //the variant's font-family
321 + $this->display_property( 'font-family', $selected_variant['font-family'] );
253 322
254 - // output the font weight, if available
255 - if ( ! empty( $selected_variant['font-weight'] ) ) {
256 - echo ": " . $selected_variant['font-weight'] . ";\n";
257 - $italic_font = $this->display_weight_property( $selected_variant['font-weight'] );
258 - }
323 + if ( ! $load_all_weights ) {
324 + // if this is a custom font (like from our plugin Fonto) with individual styles & weights - i.e. the font-family says it all
325 + // we need to "force" the font-weight and font-style
326 + if ( ! empty( $value['type'] ) && 'custom_individual' == $value['type'] ) {
327 + $selected_variant['font-weight'] = '400 !important';
328 + $selected_variant['font-style'] = 'normal !important';
329 + }
259 330
260 - // output the font style, if available and if it wasn't displayed already
261 - if ( ! $italic_font && ! empty( $selected_variant['font-style'] ) ) {
262 - $this->display_property( 'font-style', $selected_variant['font-style'] );
263 - }
331 + $italic_font = false;
332 +
333 + // output the font weight, if available
334 + if ( ! empty( $selected_variant['font-weight'] ) ) {
335 + echo ": " . $selected_variant['font-weight'] . ";\n";
336 + $italic_font = $this->display_weight_property( $selected_variant['font-weight'] );
264 337 }
265 338
266 - } elseif ( isset( $value['font_family'] ) ) {
267 - // the selected font family
268 - $this->display_property( 'font-family', $value['font_family'] );
339 + // output the font style, if available and if it wasn't displayed already
340 + if ( ! $italic_font && ! empty( $selected_variant['font-style'] ) ) {
341 + $this->display_property( 'font-style', $selected_variant['font-style'] );
342 + }
343 + }
269 344
270 - if ( ! empty( $selected_variant ) && ! $load_all_weights ) {
271 - $weight_and_style = strtolower( $selected_variant );
272 - $italic_font = false;
345 + } elseif ( isset( $value['font_family'] ) ) {
346 + // the selected font family
347 + $this->display_property( 'font-family', $value['font_family'] );
273 348
274 - //determine if this is an italic font (the $weight_and_style is usually like '400' or '400italic' )
275 - if ( ! empty( $weight_and_style ) ) {
276 - //a little bit of sanity check - in case it's not a number
277 - if( $weight_and_style === 'regular' ) {
278 - $weight_and_style = 'normal';
279 - }
280 - $italic_font = $this->display_weight_property( $weight_and_style );
349 + if ( ! empty( $selected_variant ) && ! $load_all_weights ) {
350 + $weight_and_style = strtolower( $selected_variant );
351 + $italic_font = false;
352 +
353 + //determine if this is an italic font (the $weight_and_style is usually like '400' or '400italic' )
354 + if ( ! empty( $weight_and_style ) ) {
355 + //a little bit of sanity check - in case it's not a number
356 + if( $weight_and_style === 'regular' ) {
357 + $weight_and_style = 'normal';
281 358 }
359 + $italic_font = $this->display_weight_property( $weight_and_style );
360 + }
282 361
283 - // output the font style, if available
284 - if ( ! $italic_font && ! empty( $selected_variant['font-style'] ) ) {
285 - $this->display_property( 'font-style', $selected_variant['font-style'] );
286 - }
362 + // output the font style, if available
363 + if ( ! $italic_font && ! empty( $selected_variant['font-style'] ) ) {
364 + $this->display_property( 'font-style', $selected_variant['font-style'] );
287 365 }
288 - } else if ( isset( $value['font-family'] ) ) {
289 - $this->display_property( 'font-family', $value['font-family'] );
290 366 }
367 + } else if ( isset( $value['font-family'] ) ) {
368 + $this->display_property( 'font-family', $value['font-family'] );
369 + }
291 370
292 - if ( ! empty( $value['font_weight'] ) ) {
293 - $italic_font = $this->display_weight_property( $value['font_weight'] );
294 - }
371 + if ( ! empty( $value['font_weight'] ) ) {
372 + $italic_font = $this->display_weight_property( $value['font_weight'] );
373 + }
295 374
296 - if ( ! empty( $value['font_size'] ) ) {
297 - $unit = $this->get_field_unit( $font, 'font-size' );
298 - $this->display_property( 'font-size', $value['font_size'], $unit );
299 - }
375 + if ( ! empty( $value['font_size'] ) ) {
376 + $unit = $this->get_field_unit( $font, 'font-size' );
377 + $this->display_property( 'font-size', $value['font_size'], $unit );
378 + }
300 379
301 - if ( ! empty( $value['line_height'] ) ) {
302 - $unit = $this->get_field_unit( $font, 'line-height' );
303 - $this->display_property( 'line-height', $value['line_height'], $unit );
304 - }
380 + if ( isset( $value['line_height'] ) ) {
381 + $unit = $this->get_field_unit( $font, 'line-height' );
382 + $this->display_property( 'line-height', $value['line_height'], $unit );
383 + }
305 384
306 - if ( ! empty( $value['letter_spacing'] ) ) {
307 - $unit = $this->get_field_unit( $font, 'letter-spacing' );
308 - $this->display_property( 'letter-spacing', $value['letter_spacing'], $unit );
309 - }
385 + if ( isset( $value['letter_spacing'] ) ) {
386 + $unit = $this->get_field_unit( $font, 'letter-spacing' );
387 + $this->display_property( 'letter-spacing', $value['letter_spacing'], $unit );
388 + }
310 389
311 - if ( ! empty( $value['text_align'] ) ) {
312 - $this->display_property( 'text-align', $value['text_align'] );
313 - }
390 + if ( ! empty( $value['text_align'] ) ) {
391 + $this->display_property( 'text-align', $value['text_align'] );
392 + }
314 393
315 - if ( ! empty( $value['text_transform'] ) ) {
316 - $this->display_property( 'text-transform', $value['text_transform'] );
317 - }
394 + if ( ! empty( $value['text_transform'] ) ) {
395 + $this->display_property( 'text-transform', $value['text_transform'] );
396 + }
318 397
319 - if ( ! empty( $value['text_decoration'] ) ) {
320 - $this->display_property( 'text-decoration', $value['text_decoration'] );
321 - }
322 - echo "}\n";
323 - } ?>
324 - </style>
325 - <?php
398 + if ( ! empty( $value['text_decoration'] ) ) {
399 + $this->display_property( 'text-decoration', $value['text_decoration'] );
400 + }
401 + echo "}\n";
402 + }
403 +
404 + $CSS = ob_get_clean();
405 +
406 + if ( isset( $GLOBALS['wp_customize'] ) ) { ?>
407 + <style id="customify_font_output_for_<?php echo sanitize_html_class( $field ); ?>">
408 + <?php echo $CSS ?>
409 + </style><?php
410 + return;
411 + } else {
412 + $this->customify_CSS_output[] = $CSS;
413 + }
326 414 }
327 415
328 416 function get_field_unit( $font, $field ) {
329 417
@@ -366,9 +454,9 @@
366 454
367 455 }
368 456
369 457 function display_property( $property, $value, $unit = '' ) {
370 - echo "\n" . $property . ": " . $value . $unit . ";\n";
458 + echo $property . ": " . $value . $unit . ";\n";
371 459 }
372 460
373 461 // well weight sometimes comes from google as 600italic which in CSS syntax should come in two separate properties
374 462 function display_weight_property( $value ) {
@@ -376,15 +464,53 @@
376 464
377 465 if ( strpos( $value, 'italic' ) !== false ) {
378 466
379 467 $value = str_replace( 'italic', '', $value );
380 - echo "\n" . 'font-weight' . ": " . $value . ";\n";
381 - echo "\n" . 'font-style' . ": italic;\n";
468 + echo 'font-weight' . ": " . $value . ";\n";
469 + echo 'font-style' . ": italic;\n";
382 470 $has_style = true;
383 471 } else {
384 - echo "\n" . 'font-weight' . ": " . $value . ";\n";
472 + echo 'font-weight' . ": " . $value . ";\n";
385 473 }
386 474
387 475
388 476 return $has_style;
389 477 }
478 +
479 + /**
480 + * Main Customify_Font_Selector Instance
481 + *
482 + * Ensures only one instance of Customify_Font_Selector is loaded or can be loaded.
483 + *
484 + * @since 1.0.0
485 + * @static
486 + *
487 + * @return Customify_Font_Selector Main Customify_Font_Selector instance
488 + */
489 + public static function instance() {
490 +
491 + if ( is_null( self::$_instance ) ) {
492 + self::$_instance = new self();
493 + }
494 + return self::$_instance;
495 + } // End instance ()
496 +
497 + /**
498 + * Cloning is forbidden.
499 + *
500 + * @since 1.0.0
501 + */
502 + public function __clone() {
503 +
504 + _doing_it_wrong( __FUNCTION__,esc_html( __( 'Cheatin&#8217; huh?' ) ), '' );
505 + } // End __clone ()
506 +
507 + /**
508 + * Unserializing instances of this class is forbidden.
509 + *
510 + * @since 1.0.0
511 + */
512 + public function __wakeup() {
513 +
514 + _doing_it_wrong( __FUNCTION__, esc_html( __( 'Cheatin&#8217; huh?' ) ), '' );
515 + } // End __wakeup ()
390 516 }