|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Automatic postMessage scripts calculation for Kirki controls. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Kirki |
|
6
|
|
|
* @category Modules |
|
7
|
|
|
* @author Aristeides Stathopoulos |
|
8
|
|
|
* @copyright Copyright (c) 2017, Aristeides Stathopoulos |
|
9
|
|
|
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT |
|
10
|
|
|
* @since 3.0.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
// Exit if accessed directly. |
|
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
15
|
|
|
exit; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Adds styles to the customizer. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Kirki_Modules_PostMessage { |
|
22
|
|
|
/** |
|
23
|
|
|
* The object instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @static |
|
26
|
|
|
* @access private |
|
27
|
|
|
* @since 3.0.0 |
|
28
|
|
|
* @var object |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $instance; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The script. |
|
34
|
|
|
* |
|
35
|
|
|
* @access protected |
|
36
|
|
|
* @since 3.0.0 |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $script = ''; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @access protected |
|
45
|
|
|
* @since 3.0.0 |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function __construct() { |
|
48
|
|
|
add_action( 'customize_preview_init', array( $this, 'postmessage' ) ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Gets an instance of this object. |
|
53
|
|
|
* Prevents duplicate instances which avoid artefacts and improves performance. |
|
54
|
|
|
* |
|
55
|
|
|
* @static |
|
56
|
|
|
* @access public |
|
57
|
|
|
* @since 3.0.0 |
|
58
|
|
|
* @return object |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function get_instance() { |
|
61
|
|
|
if ( ! self::$instance ) { |
|
62
|
|
|
self::$instance = new self(); |
|
63
|
|
|
} |
|
64
|
|
|
return self::$instance; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Enqueues the postMessage script |
|
69
|
|
|
* and adds variables to it using the wp_localize_script function. |
|
70
|
|
|
* The rest is handled via JS. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function postmessage() { |
|
73
|
|
|
|
|
74
|
|
|
wp_enqueue_script( 'kirki_auto_postmessage', trailingslashit( Kirki::$url ) . 'modules/postmessage/postmessage.js', array( 'jquery', 'customize-preview' ), false, true ); |
|
75
|
|
|
$fields = Kirki::$fields; |
|
76
|
|
|
foreach ( $fields as $field ) { |
|
77
|
|
|
if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['js_vars'] ) && ! empty( $field['js_vars'] ) && is_array( $field['js_vars'] ) && isset( $field['settings'] ) ) { |
|
78
|
|
|
$this->script .= $this->script( $field ); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
$this->script = apply_filters( 'kirki/postmessage/script', $this->script ); |
|
82
|
|
|
wp_add_inline_script( 'kirki_auto_postmessage', $this->script, 'after' ); |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Generates script for a single field. |
|
88
|
|
|
* |
|
89
|
|
|
* @access protected |
|
90
|
|
|
* @since 3.0.0 |
|
91
|
|
|
* @param array $args The arguments. |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function script( $args ) { |
|
94
|
|
|
|
|
95
|
|
|
$script = 'wp.customize(\'' . $args['settings'] . '\',function(value){value.bind(function(newval){'; |
|
96
|
|
|
|
|
97
|
|
|
$add_css = false; |
|
98
|
|
|
foreach ( $args['js_vars'] as $js_var ) { |
|
99
|
|
|
if ( ! isset( $js_var['function'] ) || 'html' !== $js_var['function'] ) { |
|
100
|
|
|
$add_css = true; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
if ( $add_css ) { |
|
105
|
|
|
|
|
106
|
|
|
// append unique style tag if not exist |
|
107
|
|
|
// The style ID. |
|
108
|
|
|
$style_id = 'kirki-postmessage-' . str_replace( array( '[', ']' ), '', $args['settings'] ); |
|
109
|
|
|
$script .= 'if(null===document.getElementById(\'' . $style_id . '\')||\'undefined\'===typeof document.getElementById(\'' . $style_id . '\')){jQuery(\'head\').append(\'<style id="' . $style_id . '"></style>\');}'; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// Add anything we need before the main script. |
|
113
|
|
|
$script .= $this->before_script( $args ); |
|
114
|
|
|
|
|
115
|
|
|
$field = array( |
|
116
|
|
|
'scripts' => array(), |
|
117
|
|
|
); |
|
118
|
|
|
// Loop through the js_vars and generate the script. |
|
119
|
|
|
foreach ( $args['js_vars'] as $key => $js_var ) { |
|
120
|
|
|
|
|
121
|
|
|
// Skip styles if "exclude" is defined and value is excluded. |
|
122
|
|
|
if ( isset( $js_var['exclude'] ) ) { |
|
123
|
|
|
$js_var['exclude'] = (array) $js_var['exclude']; |
|
124
|
|
|
$script .= 'exclude=false;'; |
|
125
|
|
|
foreach ( $js_var['exclude'] as $exclussion ) { |
|
126
|
|
|
$script .= "if(newval=={$exclussion}){exclude=true;}"; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
if ( isset( $js_var['element'] ) ) { |
|
130
|
|
|
// Array to string. |
|
131
|
|
|
if ( is_array( $js_var['element'] ) ) { |
|
132
|
|
|
$js_var['element'] = array_unique( $js_var['element'] ); |
|
133
|
|
|
$js_var['element'] = implode( ',', $js_var['element'] ); |
|
134
|
|
|
} |
|
135
|
|
|
// Replace single quotes with double quotes to avoid issues with the compiled JS. |
|
136
|
|
|
$js_var['element'] = str_replace( '\'', '"', $js_var['element'] ); |
|
137
|
|
|
} |
|
138
|
|
|
if ( isset( $js_var['function'] ) && 'html' === $js_var['function'] ) { |
|
139
|
|
|
$script .= $this->script_html_var( $js_var ); |
|
140
|
|
|
continue; |
|
141
|
|
|
} |
|
142
|
|
|
$js_var['index_key'] = $key; |
|
143
|
|
|
$callback = $this->get_callback( $args ); |
|
144
|
|
|
if ( is_callable( $callback ) ) { |
|
145
|
|
|
$field['scripts'][ $key ] = call_user_func_array( $callback, array( $js_var, $args ) ); |
|
146
|
|
|
continue; |
|
147
|
|
|
} |
|
148
|
|
|
$field['scripts'][ $key ] = $this->script_var( $js_var ); |
|
149
|
|
|
} |
|
150
|
|
|
$combo_extra_script = ''; |
|
151
|
|
|
$combo_css_script = ''; |
|
152
|
|
|
foreach ( $field['scripts'] as $script_array ) { |
|
153
|
|
|
$combo_extra_script .= $script_array['script']; |
|
154
|
|
|
$combo_css_script .= ( 'css' !== $combo_css_script ) ? $script_array['css'] : ''; |
|
155
|
|
|
} |
|
156
|
|
|
$text = ( 'css' === $combo_css_script ) ? 'css' : '\'' . $combo_css_script . '\''; |
|
157
|
|
|
|
|
158
|
|
|
$script .= $combo_extra_script; |
|
159
|
|
|
$script .= "var cssContent={$text};"; |
|
160
|
|
|
if ( isset( $js_var['exclude'] ) ) { |
|
161
|
|
|
$script .= 'if(true===exclude){cssContent="";}'; |
|
162
|
|
|
} |
|
163
|
|
|
if ( $add_css ) { |
|
164
|
|
|
$script .= "jQuery('#{$style_id}').text(cssContent);"; |
|
165
|
|
|
$script .= "jQuery('#{$style_id}').appendTo('head');"; |
|
166
|
|
|
} |
|
167
|
|
|
$script .= '});});'; |
|
168
|
|
|
return $script; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Generates script for a single js_var when using "html" as function. |
|
173
|
|
|
* |
|
174
|
|
|
* @access protected |
|
175
|
|
|
* @since 3.0.0 |
|
176
|
|
|
* @param array $args The arguments for this js_var. |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function script_html_var( $args ) { |
|
179
|
|
|
|
|
180
|
|
|
$script = ( isset( $args['choice'] ) ) ? "newval=newval['{$args['choice']}'];" : ''; |
|
181
|
|
|
|
|
182
|
|
|
// Apply the value_pattern. |
|
183
|
|
|
if ( isset( $args['value_pattern'] ) && '' !== $args['value_pattern'] ) { |
|
184
|
|
|
$script .= $this->value_pattern_replacements( 'newval', $args ); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if ( isset( $args['attr'] ) ) { |
|
188
|
|
|
$script .= "jQuery('{$args['element']}').attr('{$args['attr']}',newval);"; |
|
189
|
|
|
return $script; |
|
190
|
|
|
} |
|
191
|
|
|
$script .= "jQuery('{$args['element']}').html(newval);"; |
|
192
|
|
|
return $script; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Generates script for a single js_var. |
|
197
|
|
|
* |
|
198
|
|
|
* @access protected |
|
199
|
|
|
* @since 3.0.0 |
|
200
|
|
|
* @param array $args The arguments for this js_var. |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function script_var( $args ) { |
|
203
|
|
|
$script = ''; |
|
204
|
|
|
$property_script = ''; |
|
205
|
|
|
|
|
206
|
|
|
$value_key = 'newval' . $args['index_key']; |
|
207
|
|
|
$property_script .= $value_key . '=newval;'; |
|
208
|
|
|
|
|
209
|
|
|
$args = $this->get_args( $args ); |
|
210
|
|
|
|
|
211
|
|
|
// Apply callback to the value if a callback is defined. |
|
212
|
|
|
if ( ! empty( $args['js_callback'] ) && is_array( $args['js_callback'] ) && isset( $args['js_callback'][0] ) && ! empty( $args['js_callback'][0] ) ) { |
|
213
|
|
|
$script .= $value_key . '=' . $args['js_callback'][0] . '(' . $value_key . ',' . $args['js_callback'][1] . ');'; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
// Apply the value_pattern. |
|
217
|
|
|
if ( '' !== $args['value_pattern'] ) { |
|
218
|
|
|
$script .= $this->value_pattern_replacements( $value_key, $args ); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
// Tweak to add url() for background-images. |
|
222
|
|
|
if ( 'background-image' === $args['property'] && ( ! isset( $args['value_pattern'] ) || false === strpos( $args['value_pattern'], 'gradient' ) ) ) { |
|
223
|
|
|
$script .= 'if(-1===' . $value_key . '.indexOf(\'url(\')){' . $value_key . '=\'url("\'+' . $value_key . '+\'")\';}'; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
// Apply prefix. |
|
227
|
|
|
$value = $value_key; |
|
228
|
|
|
if ( '' !== $args['prefix'] ) { |
|
229
|
|
|
$value = "'" . $args['prefix'] . "'+" . $value_key; |
|
230
|
|
|
} |
|
231
|
|
|
$css = $args['element'] . '{' . $args['property'] . ':\'+' . $value . '+\'' . $args['units'] . $args['suffix'] . ';}'; |
|
232
|
|
|
if ( isset( $args['media_query'] ) ) { |
|
233
|
|
|
$css = $args['media_query'] . '{' . $css . '}'; |
|
234
|
|
|
} |
|
235
|
|
|
return array( |
|
236
|
|
|
'script' => $property_script . $script, |
|
237
|
|
|
'css' => $css, |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Processes script generation for fields that save an array. |
|
243
|
|
|
* |
|
244
|
|
|
* @access protected |
|
245
|
|
|
* @since 3.0.0 |
|
246
|
|
|
* @param array $args The arguments for this js_var. |
|
247
|
|
|
*/ |
|
248
|
|
|
protected function script_var_array( $args ) { |
|
249
|
|
|
|
|
250
|
|
|
$script = ( 0 === $args['index_key'] ) ? 'css=\'\';' : ''; |
|
251
|
|
|
$property_script = ''; |
|
252
|
|
|
|
|
253
|
|
|
// Define choice. |
|
254
|
|
|
$choice = ( isset( $args['choice'] ) && '' !== $args['choice'] ) ? $args['choice'] : ''; |
|
255
|
|
|
|
|
256
|
|
|
$value_key = 'newval' . $args['index_key']; |
|
257
|
|
|
$property_script .= $value_key . '=newval;'; |
|
258
|
|
|
|
|
259
|
|
|
$args = $this->get_args( $args ); |
|
260
|
|
|
|
|
261
|
|
|
// Apply callback to the value if a callback is defined. |
|
262
|
|
|
if ( ! empty( $args['js_callback'] ) && is_array( $args['js_callback'] ) && isset( $args['js_callback'][0] ) && ! empty( $args['js_callback'][0] ) ) { |
|
263
|
|
|
$script .= $value_key . '=' . $args['js_callback'][0] . '(' . $value_key . ',' . $args['js_callback'][1] . ');'; |
|
264
|
|
|
} |
|
265
|
|
|
$script .= '_.each(' . $value_key . ', function(subValue,subKey){'; |
|
266
|
|
|
|
|
267
|
|
|
// Apply the value_pattern. |
|
268
|
|
|
if ( '' !== $args['value_pattern'] ) { |
|
269
|
|
|
$script .= $this->value_pattern_replacements( 'subValue', $args ); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
// Tweak to add url() for background-images. |
|
273
|
|
|
if ( '' === $choice || 'background-image' === $choice ) { |
|
274
|
|
|
$script .= 'if(\'background-image\'===\'' . $args['property'] . '\'||\'background-image\'===subKey){'; |
|
275
|
|
|
$script .= 'if(-1===subValue.indexOf(\'url(\')){subValue=\'url("\'+subValue+\'")\';}'; |
|
276
|
|
|
$script .= '}'; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
// Apply prefix. |
|
280
|
|
|
$value = $value_key; |
|
281
|
|
|
if ( '' !== $args['prefix'] ) { |
|
282
|
|
|
$value = '\'' . $args['prefix'] . '\'+subValue'; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
// Mostly used for padding, margin & position properties. |
|
286
|
|
|
$direction_script = 'if(_.contains([\'top\',\'bottom\',\'left\',\'right\'],subKey)){'; |
|
287
|
|
|
$direction_script .= 'css+=\'' . $args['element'] . '{' . $args['property'] . '-\'+subKey+\':\'+subValue+\'' . $args['units'] . $args['suffix'] . ';}\';}'; |
|
288
|
|
|
// Allows us to apply this just for a specific choice in the array of the values. |
|
289
|
|
|
if ( '' !== $choice ) { |
|
290
|
|
|
$choice_is_direction = ( false !== strpos( $choice, 'top' ) || false !== strpos( $choice, 'bottom' ) || false !== strpos( $choice, 'left' ) || false !== strpos( $choice, 'right' ) ); |
|
291
|
|
|
$script .= 'if(\'' . $choice . '\'===subKey){'; |
|
292
|
|
|
$script .= ( $choice_is_direction ) ? $direction_script . 'else{' : ''; |
|
293
|
|
|
$script .= 'css+=\'' . $args['element'] . '{' . $args['property'] . ':\'+subValue+\';}\';'; |
|
294
|
|
|
$script .= ( $choice_is_direction ) ? '}' : ''; |
|
295
|
|
|
$script .= '}'; |
|
296
|
|
|
} else { |
|
297
|
|
|
$script .= $direction_script . 'else{'; |
|
298
|
|
|
|
|
299
|
|
|
// This is where most object-based fields will go. |
|
300
|
|
|
$script .= 'css+=\'' . $args['element'] . '{\'+subKey+\':\'+subValue+\'' . $args['units'] . $args['suffix'] . ';}\';'; |
|
301
|
|
|
$script .= '}'; |
|
302
|
|
|
} |
|
303
|
|
|
$script .= '});'; |
|
304
|
|
|
|
|
305
|
|
|
if ( isset( $args['media_query'] ) ) { |
|
306
|
|
|
$script .= 'css=\'' . $args['media_query'] . '{\'+css+\'}\';'; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
return array( |
|
310
|
|
|
'script' => $property_script . $script, |
|
311
|
|
|
'css' => 'css', |
|
312
|
|
|
); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Processes script generation for typography fields. |
|
317
|
|
|
* |
|
318
|
|
|
* @access protected |
|
319
|
|
|
* @since 3.0.0 |
|
320
|
|
|
* @param array $args The arguments for this js_var. |
|
321
|
|
|
* @param array $field The field arguments. |
|
322
|
|
|
*/ |
|
323
|
|
|
protected function script_var_typography( $args, $field ) { |
|
324
|
|
|
|
|
325
|
|
|
$script = ''; |
|
326
|
|
|
$css = ''; |
|
327
|
|
|
|
|
328
|
|
|
// Load the font using WenFontloader. |
|
329
|
|
|
// This is a bit ugly because wp_add_inline_script doesn't allow adding <script> directly. |
|
330
|
|
|
$webfont_loader = 'sc=\'a\';jQuery(\'head\').append(sc.replace(\'a\',\'<\')+\'script>if(!_.isUndefined(WebFont)&&fontFamily){WebFont.load({google:{families:["\'+fontFamily.replace( /\"/g, \'"\' )+\':\'+variant+subsetsString+\'"]}});}\'+sc.replace(\'a\',\'<\')+\'/script>\');'; |
|
331
|
|
|
|
|
332
|
|
|
// Add the css. |
|
333
|
|
|
$css_build_array = array( |
|
334
|
|
|
'font-family' => 'fontFamily', |
|
335
|
|
|
'font-size' => 'fontSize', |
|
336
|
|
|
'line-height' => 'lineHeight', |
|
337
|
|
|
'letter-spacing' => 'letterSpacing', |
|
338
|
|
|
'word-spacing' => 'wordSpacing', |
|
339
|
|
|
'text-align' => 'textAlign', |
|
340
|
|
|
'text-transform' => 'textTransform', |
|
341
|
|
|
'color' => 'color', |
|
342
|
|
|
'font-weight' => 'fontWeight', |
|
343
|
|
|
'font-style' => 'fontStyle', |
|
344
|
|
|
); |
|
345
|
|
|
$choice_condition = ( isset( $args['choice'] ) && '' !== $args['choice'] && isset( $css_build_array[ $args['choice'] ] ) ); |
|
346
|
|
|
$script .= ( ! $choice_condition ) ? $webfont_loader : ''; |
|
347
|
|
|
foreach ( $css_build_array as $property => $var ) { |
|
348
|
|
|
if ( $choice_condition && $property !== $args['choice'] ) { |
|
349
|
|
|
continue; |
|
350
|
|
|
} |
|
351
|
|
|
// Fixes https://github.com/aristath/kirki/issues/1436. |
|
352
|
|
|
if ( ! isset( $field['default'] ) || ( |
|
353
|
|
|
( 'font-family' === $property && ! isset( $field['default']['font-family'] ) ) || |
|
354
|
|
|
( 'font-size' === $property && ! isset( $field['default']['font-size'] ) ) || |
|
355
|
|
|
( 'line-height' === $property && ! isset( $field['default']['line-height'] ) ) || |
|
356
|
|
|
( 'letter-spacing' === $property && ! isset( $field['default']['letter-spacing'] ) ) || |
|
357
|
|
|
( 'word-spacing' === $property && ! isset( $field['default']['word-spacing'] ) ) || |
|
358
|
|
|
( 'text-align' === $property && ! isset( $field['default']['text-align'] ) ) || |
|
359
|
|
|
( 'text-transform' === $property && ! isset( $field['default']['text-transform'] ) ) || |
|
360
|
|
|
( 'color' === $property && ! isset( $field['default']['color'] ) ) || |
|
361
|
|
|
( 'font-weight' === $property && ! isset( $field['default']['variant'] ) && ! isset( $field['default']['font-weight'] ) ) || |
|
362
|
|
|
( 'font-style' === $property && ! isset( $field['default']['variant'] ) && ! isset( $field['default']['font-style'] ) ) |
|
363
|
|
|
) ) { |
|
364
|
|
|
continue; |
|
365
|
|
|
} |
|
366
|
|
|
$script .= ( $choice_condition && 'font-family' === $args['choice'] ) ? $webfont_loader : ''; |
|
367
|
|
|
|
|
368
|
|
|
if ( 'font-family' === $property || ( isset( $args['choice'] ) && 'font-family' === $args['choice'] ) ) { |
|
369
|
|
|
$css .= 'fontFamilyCSS=fontFamily;if(0<fontFamily.indexOf(\' \')&&-1===fontFamily.indexOf(\'"\')){fontFamilyCSS=\'"\'+fontFamily+\'"\';}'; |
|
370
|
|
|
$var = 'fontFamilyCSS'; |
|
371
|
|
|
} |
|
372
|
|
|
$css .= 'css+=(\'\'!==' . $var . ')?\'' . $args['element'] . '\'+\'{' . $property . ':\'+' . $var . '+\';}\':\'\';'; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
$script .= $css; |
|
376
|
|
|
if ( isset( $args['media_query'] ) ) { |
|
377
|
|
|
$script .= 'css=\'' . $args['media_query'] . '{\'+css+\'}\';'; |
|
378
|
|
|
} |
|
379
|
|
|
return array( |
|
380
|
|
|
'script' => $script, |
|
381
|
|
|
'css' => 'css', |
|
382
|
|
|
); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Processes script generation for typography fields. |
|
387
|
|
|
* |
|
388
|
|
|
* @access protected |
|
389
|
|
|
* @since 3.0.0 |
|
390
|
|
|
* @param array $args The arguments for this js_var. |
|
391
|
|
|
*/ |
|
392
|
|
|
protected function script_var_image( $args ) { |
|
393
|
|
|
$return = $this->script_var( $args ); |
|
394
|
|
|
return array( |
|
395
|
|
|
'script' => 'newval=(!_.isUndefined(newval.url))?newval.url:newval;' . $return['script'], |
|
396
|
|
|
'css' => $return['css'], |
|
397
|
|
|
); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Adds anything we need before the main script. |
|
402
|
|
|
* |
|
403
|
|
|
* @access private |
|
404
|
|
|
* @since 3.0.0 |
|
405
|
|
|
* @param array $args The field args. |
|
406
|
|
|
* @return string |
|
407
|
|
|
*/ |
|
408
|
|
|
private function before_script( $args ) { |
|
409
|
|
|
|
|
410
|
|
|
$script = ''; |
|
411
|
|
|
|
|
412
|
|
|
if ( isset( $args['type'] ) ) { |
|
413
|
|
|
switch ( $args['type'] ) { |
|
414
|
|
|
case 'kirki-typography': |
|
415
|
|
|
$script .= 'fontFamily=(_.isUndefined(newval[\'font-family\']))?\'\':newval[\'font-family\'];'; |
|
416
|
|
|
$script .= 'variant=(_.isUndefined(newval.variant))?\'400\':newval.variant;'; |
|
417
|
|
|
$script .= 'subsets=(_.isUndefined(newval.subsets))?[]:newval.subsets;'; |
|
418
|
|
|
$script .= 'subsetsString=(_.isObject(newval.subsets))?\':\'+newval.subsets.join(\',\'):\'\';'; |
|
419
|
|
|
$script .= 'fontSize=(_.isUndefined(newval[\'font-size\']))?\'\':newval[\'font-size\'];'; |
|
420
|
|
|
$script .= 'lineHeight=(_.isUndefined(newval[\'line-height\']))?\'\':newval[\'line-height\'];'; |
|
421
|
|
|
$script .= 'letterSpacing=(_.isUndefined(newval[\'letter-spacing\']))?\'\':newval[\'letter-spacing\'];'; |
|
422
|
|
|
$script .= 'wordSpacing=(_.isUndefined(newval[\'word-spacing\']))?\'\':newval[\'word-spacing\'];'; |
|
423
|
|
|
$script .= 'textAlign=(_.isUndefined(newval[\'text-align\']))?\'\':newval[\'text-align\'];'; |
|
424
|
|
|
$script .= 'textTransform=(_.isUndefined(newval[\'text-transform\']))?\'\':newval[\'text-transform\'];'; |
|
425
|
|
|
$script .= 'color=(_.isUndefined(newval.color))?\'\':newval.color;'; |
|
426
|
|
|
|
|
427
|
|
|
$script .= 'fw=(!_.isString(newval.variant))?\'400\':newval.variant.match(/\d/g);'; |
|
428
|
|
|
$script .= 'fontWeight=(!_.isObject(fw))?400:fw.join(\'\');'; |
|
429
|
|
|
$script .= 'fontStyle=(-1!==variant.indexOf(\'italic\'))?\'italic\':\'normal\';'; |
|
430
|
|
|
$script .= 'css=\'\';'; |
|
431
|
|
|
break; |
|
432
|
|
|
} |
|
433
|
|
|
} |
|
434
|
|
|
return $script; |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
/** |
|
438
|
|
|
* Sanitizes the arguments and makes sure they are all there. |
|
439
|
|
|
* |
|
440
|
|
|
* @access private |
|
441
|
|
|
* @since 3.0.0 |
|
442
|
|
|
* @param array $args The arguments. |
|
443
|
|
|
* @return array |
|
444
|
|
|
*/ |
|
445
|
|
|
private function get_args( $args ) { |
|
446
|
|
|
|
|
447
|
|
|
// Make sure everything is defined to avoid "undefined index" errors. |
|
448
|
|
|
$args = wp_parse_args( $args, array( |
|
449
|
|
|
'element' => '', |
|
450
|
|
|
'property' => '', |
|
451
|
|
|
'prefix' => '', |
|
452
|
|
|
'suffix' => '', |
|
453
|
|
|
'units' => '', |
|
454
|
|
|
'js_callback' => array( '', '' ), |
|
455
|
|
|
'value_pattern' => '', |
|
456
|
|
|
)); |
|
457
|
|
|
|
|
458
|
|
|
// Element should be a string. |
|
459
|
|
|
if ( is_array( $args['element'] ) ) { |
|
460
|
|
|
$args['element'] = implode( ',', $args['element'] ); |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
// Make sure arguments that are passed-on to callbacks are strings. |
|
464
|
|
|
if ( is_array( $args['js_callback'] ) && isset( $args['js_callback'][1] ) && is_array( $args['js_callback'][1] ) ) { |
|
465
|
|
|
$args['js_callback'][1] = wp_json_encode( $args['js_callback'][1] ); |
|
466
|
|
|
} |
|
467
|
|
|
return $args; |
|
468
|
|
|
|
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
/** |
|
472
|
|
|
* Returns script for value_pattern & replacements. |
|
473
|
|
|
* |
|
474
|
|
|
* @access private |
|
475
|
|
|
* @since 3.0.0 |
|
476
|
|
|
* @param string $value The value placeholder. |
|
477
|
|
|
* @param array $js_vars The js_vars argument. |
|
478
|
|
|
* @return string The script. |
|
479
|
|
|
*/ |
|
480
|
|
|
private function value_pattern_replacements( $value, $js_vars ) { |
|
481
|
|
|
$script = ''; |
|
482
|
|
|
$alias = $value; |
|
483
|
|
|
if ( ! isset( $js_vars['value_pattern'] ) ) { |
|
484
|
|
|
return $value; |
|
485
|
|
|
} |
|
486
|
|
|
$value = $js_vars['value_pattern']; |
|
487
|
|
|
if ( isset( $js_vars['pattern_replace'] ) ) { |
|
488
|
|
|
$script .= 'settings=window.wp.customize.get();'; |
|
489
|
|
|
foreach ( $js_vars['pattern_replace'] as $search => $replace ) { |
|
490
|
|
|
$replace = '\'+settings["' . $replace . '"]+\''; |
|
491
|
|
|
$value = str_replace( $search, $replace, $js_vars['value_pattern'] ); |
|
492
|
|
|
$value = trim( $value, '+' ); |
|
493
|
|
|
} |
|
494
|
|
|
} |
|
495
|
|
|
$value_compiled = str_replace( '$', '\'+' . $alias . '+\'', $value ); |
|
496
|
|
|
$value_compiled = trim( $value_compiled, '+' ); |
|
497
|
|
|
|
|
498
|
|
|
return $script . $alias . '=\'' . $value_compiled . '\';'; |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
/** |
|
502
|
|
|
* Get the callback function/method we're going to use for this field. |
|
503
|
|
|
* |
|
504
|
|
|
* @access private |
|
505
|
|
|
* @since 3.0.0 |
|
506
|
|
|
* @param array $args The field args. |
|
507
|
|
|
* @return string|array A callable function or method. |
|
508
|
|
|
*/ |
|
509
|
|
|
protected function get_callback( $args ) { |
|
510
|
|
|
|
|
511
|
|
|
switch ( $args['type'] ) { |
|
512
|
|
|
case 'kirki-background': |
|
513
|
|
|
case 'kirki-dimensions': |
|
514
|
|
|
case 'kirki-multicolor': |
|
515
|
|
|
case 'kirki-sortable': |
|
516
|
|
|
$callback = array( $this, 'script_var_array' ); |
|
517
|
|
|
break; |
|
518
|
|
|
case 'kirki-typography': |
|
519
|
|
|
$callback = array( $this, 'script_var_typography' ); |
|
520
|
|
|
break; |
|
521
|
|
|
case 'kirki-image': |
|
522
|
|
|
$callback = array( $this, 'script_var_image' ); |
|
523
|
|
|
break; |
|
524
|
|
|
default: |
|
525
|
|
|
$callback = array( $this, 'script_var' ); |
|
526
|
|
|
} |
|
527
|
|
|
return $callback; |
|
528
|
|
|
} |
|
529
|
|
|
} |
|
530
|
|
|
|