PluginProbe
Customify / 1.4.1
Customify v1.4.1
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
customify / features / class-Font_Selector.php

class-Font_Selector.php in Customify 1.4.1, at features/class-Font_Selector.php

391 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Customify_Font_Selector extends PixCustomifyPlugin {
4
5 /**
6 * Instance of this class.
7 * @since 1.0.0
8 * @var object
9 */
10 protected static $instance = null;
11 protected $parent = null;
12 protected static $typo_settings = null;
13 protected static $options_list = null;
14 static $theme_fonts = null;
15
16
17 function __construct( $parent ) {
18 global $pixcustomify_plugin;
19
20 $load_location = PixCustomifyPlugin::get_plugin_option( 'style_resources_location', 'wp_head' );
21
22 add_action( $load_location, array( $this, 'output_font_dynamic_style' ), 999999999 );
23
24 $this->parent = $parent;
25 self::$theme_fonts = apply_filters( 'customify_theme_fonts', array() );
26
27 add_action( 'customify_font_family_before_options', array( $this, 'add_customify_theme_fonts' ), 11, 2 );
28 }
29
30 function add_customify_theme_fonts( $active_font_family, $val ) {
31 //first get all the published custom fonts
32 if ( empty( self::$theme_fonts ) ) {
33 return;
34 }
35
36 echo '<optgroup label="' . esc_html__( 'Theme Fonts', 'customify' ) . '">';
37 foreach ( self::$theme_fonts as $font ) {
38 if ( ! empty( $font ) ) {
39 //display the select option's HTML
40 Pix_Customize_Font_Control::output_font_option( $font['family'], $active_font_family, $font, 'theme_font' );
41 }
42 }
43 echo "</optgroup>";
44 }
45
46 function maybe_decode_value( $value ) {
47
48 $to_return = PixCustomifyPlugin::decodeURIComponent( $value );
49 if ( is_string( $value ) ) {
50 $to_return = json_decode( wp_unslash( $to_return ), true );
51 }
52
53 return $to_return;
54 }
55
56 function output_font_dynamic_style() {
57
58 self::$options_list = $this->get_options();
59
60 self::get_typography_fields( self::$options_list, 'type', 'font', self::$typo_settings );
61
62 if ( empty( self::$typo_settings ) ) {
63 return;
64 }
65
66 $google_families = $local_families = '';
67
68 $args = array(
69 'google_families' => '',
70 'local_families' => '',
71 'local_srcs' => '',
72 );
73
74 foreach ( self::$typo_settings as $id => $font ) {
75 if ( isset ( $font['value'] ) ) {
76
77 $load_all_weights = false;
78 if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) {
79 $load_all_weights = true;
80 }
81
82 $value = $this->maybe_decode_value( $font['value'] );
83
84 $value = $this->validate_font_values( $value );
85
86 // in case the value is still null, try default value(mostly for google fonts)
87 if ( ! is_array( $value ) || $value === null ) {
88 $value = $this->get_font_defaults_value( str_replace( '"', '', $font['value'] ) );
89 }
90
91 //bail if by this time we don't have a value of some sort
92 if ( empty( $value ) ) {
93 continue;
94 }
95
96 //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 );
99 }
100
101 if ( isset( $value['font_family'] ) && isset( $value['type'] ) && $value['type'] == 'google' ) {
102 $args['google_families'] .= "'" . $value['font_family'];
103
104 if ( $load_all_weights && is_array( $value['variants'] ) ) {
105 $args['google_families'] .= ":" . implode( ',', $value['variants'] );
106 } elseif ( isset( $value['selected_variants'] ) && ! empty( $value['selected_variants'] ) ) {
107 if ( is_array( $value['selected_variants'] ) ) {
108 $args['google_families'] .= ":" . implode( ',', $value['selected_variants'] );
109 } elseif ( is_string( $value['selected_variants'] ) || is_numeric( $value['selected_variants'] ) ) {
110 $args['google_families'] .= ":" . $value['selected_variants'];
111 }
112 } elseif ( isset( $value['variants'] ) && ! empty( $value['variants'] ) ) {
113 if ( is_array( $value['variants'] ) ) {
114 $args['google_families'] .= ":" . implode( ',', $value['variants'] );
115 } else {
116 $args['google_families'] .= ":" . $value['variants'];
117 }
118 }
119
120 if ( isset( $value['selected_subsets'] ) && ! empty( $value['selected_subsets'] ) ) {
121 if ( is_array( $value['selected_subsets'] ) ) {
122 $args['google_families'] .= ":" . implode( ',', $value['selected_subsets'] );
123 } else {
124 $args['google_families'] .= ":" . $value['selected_subsets'];
125 }
126 } elseif ( isset( $value['subsets'] ) && ! empty( $value['subsets'] ) ) {
127 if ( is_array( $value['subsets'] ) ) {
128 $args['google_families'] .= ":" . implode( ',', $value['subsets'] );
129 } else {
130 $args['google_families'] .= ":" . $value['subsets'];
131 }
132 }
133
134 $args['google_families'] .= '\',';
135 } elseif ( isset( self::$theme_fonts[ $value['font_family'] ] ) ) {
136
137 // $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'];
140
141 if ( false === strpos( $args['local_families'], $value['font_family'] ) ) {
142 $args['local_families'] .= "'" . $value['font_family'] . "',";
143 }
144
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'] . "',";
147 }
148 }
149 }
150 }
151
152 if ( ( ! empty ( $args['local_families'] ) || ! empty ( $args['google_families'] ) ) && self::get_plugin_option( 'typography', '1' ) && self::get_plugin_option( 'typography_google_fonts', 1 ) ) {
153 $this->display_webfont_script( $args );
154 }
155
156 foreach ( self::$typo_settings as $key => $font ) {
157 if ( empty( $font['selector'] ) || empty( $font['value'] ) ) {
158 continue;
159 }
160 $value = $this->maybe_decode_value( $font['value'] );
161
162 if ( $value === null ) {
163 $value = $this->get_font_defaults_value( $font['value'] );
164 }
165
166 // shim the old case when the default was only the font name
167 if ( is_string( $value ) && ! empty( $value ) ) {
168 $value = array( 'font_family' => $value );
169 }
170
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 );
174 }
175
176 $this->output_font_style( $key, $font, $value );
177 }
178 }
179
180 function display_webfont_script( $args ) { ?>
181 <script type="text/javascript">
182 var customify_font_loader = function () {
183 var webfontargs = {
184 classes: false,
185 events: false
186 };
187 <?php if ( ! empty( $args['google_families'] ) ) { ?>
188 webfontargs.google = { families: [<?php echo( rtrim( $args['google_families'], ',' ) ); ?>] };
189 <?php }
190 if ( ! empty( $args['local_families'] ) && ! empty( $args['local_srcs'] ) ) { ?>
191 webfontargs.custom = {
192 families: [<?php echo( rtrim( $args['local_families'], ',' ) ); ?>],
193 urls: [<?php echo rtrim( $args['local_srcs'], ',' ) ?>]
194 };
195 <?php } ?>
196 WebFont.load(webfontargs);
197 };
198
199 if ( typeof WebFont !== 'undefined' ) { <?php // if there is a WebFont object, use it ?>
200 customify_font_loader();
201 } else { <?php // basically when we don't have the WebFont object we create the google script dynamically ?>
202 var tk = document.createElement('script');
203 tk.src = '//ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
204 tk.type = 'text/javascript';
205
206 tk.onload = tk.onreadystatechange = function () {
207 customify_font_loader();
208 };
209 var s = document.getElementsByTagName('script')[0];
210 s.parentNode.insertBefore(tk, s);
211 }
212 </script>
213 <?php
214 }
215
216 function output_font_style( $field, $font, $value ) {
217 $value = $this->validate_font_values( $value );
218 // some sanitizing
219 $load_all_weights = false;
220 if ( isset( $font['load_all_weights'] ) && $font['load_all_weights'] == 'true' ) {
221 $load_all_weights = true;
222 }
223 $selected_variant = '';
224 if ( ! empty( $value['selected_variants'] ) ) {
225 if ( is_array( $value['selected_variants'] ) ) {
226 $selected_variant = $value['selected_variants'][0];
227 } else {
228 $selected_variant = $value['selected_variants'];
229 }
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'] . " {";
238
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'] );
243
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 }
251
252 $italic_font = false;
253
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 }
259
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 }
264 }
265
266 } elseif ( isset( $value['font_family'] ) ) {
267 // the selected font family
268 $this->display_property( 'font-family', $value['font_family'] );
269
270 if ( ! empty( $selected_variant ) && ! $load_all_weights ) {
271 $weight_and_style = strtolower( $selected_variant );
272 $italic_font = false;
273
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 );
281 }
282
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 }
287 }
288 } else if ( isset( $value['font-family'] ) ) {
289 $this->display_property( 'font-family', $value['font-family'] );
290 }
291
292 if ( ! empty( $value['font_weight'] ) ) {
293 $italic_font = $this->display_weight_property( $value['font_weight'] );
294 }
295
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 }
300
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 }
305
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 }
310
311 if ( ! empty( $value['text_align'] ) ) {
312 $this->display_property( 'text-align', $value['text_align'] );
313 }
314
315 if ( ! empty( $value['text_transform'] ) ) {
316 $this->display_property( 'text-transform', $value['text_transform'] );
317 }
318
319 if ( ! empty( $value['text_decoration'] ) ) {
320 $this->display_property( 'text-decoration', $value['text_decoration'] );
321 }
322 echo "}\n";
323 } ?>
324 </style>
325 <?php
326 }
327
328 function get_field_unit( $font, $field ) {
329
330 if ( empty( $font ) || empty( $font['fields'] ) || empty( $font['fields'][ $field ] ) ) {
331 return 'px';
332 }
333
334 if ( isset( $font['fields'][ $field ]['unit'] ) ) {
335 return $font['fields'][ $field ]['unit'];
336 }
337
338 if ( isset( $font['fields'][ $field ][3] ) ) {
339 return $font['fields'][ $field ][3];
340 }
341
342 return 'px';
343 }
344
345 function validate_font_values( $values ) {
346
347 if ( empty( $values ) ) {
348 return array();
349 }
350
351 foreach ( $values as $key => $value ) {
352
353 if ( strpos( $key, '-' ) !== false ) {
354 $new_key = str_replace( '-', '_', $key );
355
356 $values[ $new_key ] = $value;
357
358 unset( $values[ $key ] );
359 }
360 }
361
362 return $values;
363 }
364
365 function display_selector( $selector ) {
366
367 }
368
369 function display_property( $property, $value, $unit = '' ) {
370 echo "\n" . $property . ": " . $value . $unit . ";\n";
371 }
372
373 // well weight sometimes comes from google as 600italic which in CSS syntax should come in two separate properties
374 function display_weight_property( $value ) {
375 $has_style = false;
376
377 if ( strpos( $value, 'italic' ) !== false ) {
378
379 $value = str_replace( 'italic', '', $value );
380 echo "\n" . 'font-weight' . ": " . $value . ";\n";
381 echo "\n" . 'font-style' . ": italic;\n";
382 $has_style = true;
383 } else {
384 echo "\n" . 'font-weight' . ": " . $value . ";\n";
385 }
386
387
388 return $has_style;
389 }
390 }
391