Completed
Branch develop (98485c)
by Aristeides
12:56 queued 06:30
created

Kirki_Modules_PostMessage::script_var_typography()   F

Complexity

Conditions 41
Paths 804

Size

Total Lines 64
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 41
eloc 46
nc 804
nop 2
dl 0
loc 64
rs 3.125
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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' ) );
1 ignored issue
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
		/** @scrutinizer ignore-call */ 
49
  add_action( 'customize_preview_init', array( $this, 'postmessage' ) );
Loading history...
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' ), KIRKI_VERSION, true );
2 ignored issues
show
Bug introduced by
The function trailingslashit was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
		wp_enqueue_script( 'kirki_auto_postmessage', /** @scrutinizer ignore-call */ trailingslashit( Kirki::$url ) . 'modules/postmessage/postmessage.js', array( 'jquery', 'customize-preview' ), KIRKI_VERSION, true );
Loading history...
Bug introduced by
The function wp_enqueue_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
		/** @scrutinizer ignore-call */ 
75
  wp_enqueue_script( 'kirki_auto_postmessage', trailingslashit( Kirki::$url ) . 'modules/postmessage/postmessage.js', array( 'jquery', 'customize-preview' ), KIRKI_VERSION, true );
Loading history...
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 );
1 ignored issue
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
		$this->script = /** @scrutinizer ignore-call */ apply_filters( 'kirki_postmessage_script', $this->script );
Loading history...
82
		wp_add_inline_script( 'kirki_auto_postmessage', $this->script, 'after' );
1 ignored issue
show
Bug introduced by
The function wp_add_inline_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
		/** @scrutinizer ignore-call */ 
83
  wp_add_inline_script( 'kirki_auto_postmessage', $this->script, 'after' );
Loading history...
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}'||(''==='{$exclussion}'&&_.isObject(newval)&&_.isEmpty(newval))){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'] = implode( ',', $js_var['element'] );
133
				}
134
				// Replace single quotes with double quotes to avoid issues with the compiled JS.
135
				$js_var['element'] = str_replace( '\'', '"', $js_var['element'] );
136
			}
137
			if ( isset( $js_var['function'] ) && 'html' === $js_var['function'] ) {
138
				$script .= $this->script_html_var( $js_var );
139
				continue;
140
			}
141
			$js_var['index_key'] = $key;
142
			$callback            = $this->get_callback( $args );
143
			if ( is_callable( $callback ) ) {
144
				$field['scripts'][ $key ] = call_user_func_array( $callback, array( $js_var, $args ) );
145
				continue;
146
			}
147
			$field['scripts'][ $key ] = $this->script_var( $js_var );
148
		}
149
		$combo_extra_script = '';
150
		$combo_css_script   = '';
151
		foreach ( $field['scripts'] as $script_array ) {
152
			$combo_extra_script .= $script_array['script'];
153
			$combo_css_script   .= ( 'css' !== $combo_css_script ) ? $script_array['css'] : '';
154
		}
155
		$text = ( 'css' === $combo_css_script ) ? 'css' : '\'' . $combo_css_script . '\'';
0 ignored issues
show
introduced by
The condition 'css' === $combo_css_script can never be true.
Loading history...
156
157
		$script .= $combo_extra_script . "var cssContent={$text};";
158
		if ( isset( $js_var['exclude'] ) ) {
159
			$script .= 'if(true===exclude){cssContent="";}';
160
		}
161
		if ( $add_css ) {
162
			$script .= "jQuery('#{$style_id}').text(cssContent);jQuery('#{$style_id}').appendTo('head');";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $style_id does not seem to be defined for all execution paths leading up to this point.
Loading history...
163
		}
164
		$script .= '});});';
165
		return $script;
166
	}
167
168
	/**
169
	 * Generates script for a single js_var when using "html" as function.
170
	 *
171
	 * @access protected
172
	 * @since 3.0.0
173
	 * @param array $args  The arguments for this js_var.
174
	 */
175
	protected function script_html_var( $args ) {
176
177
		$script = ( isset( $args['choice'] ) ) ? "newval=newval['{$args['choice']}'];" : '';
178
179
		// Apply the value_pattern.
180
		if ( isset( $args['value_pattern'] ) && '' !== $args['value_pattern'] ) {
181
			$script .= $this->value_pattern_replacements( 'newval', $args );
182
		}
183
184
		if ( isset( $args['attr'] ) ) {
185
			$script .= "jQuery('{$args['element']}').attr('{$args['attr']}',newval);";
186
			return $script;
187
		}
188
		$script .= "jQuery('{$args['element']}').html(newval);";
189
		return $script;
190
	}
191
192
	/**
193
	 * Generates script for a single js_var.
194
	 *
195
	 * @access protected
196
	 * @since 3.0.0
197
	 * @param array $args  The arguments for this js_var.
198
	 */
199
	protected function script_var( $args ) {
200
		$script          = '';
201
		$property_script = '';
202
203
		$value_key        = 'newval' . $args['index_key'];
204
		$property_script .= $value_key . '=newval;';
205
206
		$args = $this->get_args( $args );
207
208
		// Apply callback to the value if a callback is defined.
209
		if ( ! empty( $args['js_callback'] ) && is_array( $args['js_callback'] ) && isset( $args['js_callback'][0] ) && ! empty( $args['js_callback'][0] ) ) {
210
			$script .= $value_key . '=' . $args['js_callback'][0] . '(' . $value_key . ',' . $args['js_callback'][1] . ');';
211
		}
212
213
		// Apply the value_pattern.
214
		if ( '' !== $args['value_pattern'] ) {
215
			$script .= $this->value_pattern_replacements( $value_key, $args );
216
		}
217
218
		// Tweak to add url() for background-images.
219
		if ( 'background-image' === $args['property'] && ( ! isset( $args['value_pattern'] ) || false === strpos( $args['value_pattern'], 'gradient' ) ) ) {
220
			$script .= 'if(-1===' . $value_key . '.indexOf(\'url(\')){' . $value_key . '=\'url("\'+' . $value_key . '+\'")\';}';
221
		}
222
223
		// Apply prefix.
224
		$value = $value_key;
225
		if ( '' !== $args['prefix'] ) {
226
			$value = "'" . $args['prefix'] . "'+" . $value_key;
227
		}
228
		$css = $args['element'] . '{' . $args['property'] . ':\'+' . $value . '+\'' . $args['units'] . $args['suffix'] . ';}';
229
		if ( isset( $args['media_query'] ) ) {
230
			$css = $args['media_query'] . '{' . $css . '}';
231
		}
232
		return array(
233
			'script' => $property_script . $script,
234
			'css'    => $css,
235
		);
236
	}
237
238
	/**
239
	 * Processes script generation for fields that save an array.
240
	 *
241
	 * @access protected
242
	 * @since 3.0.0
243
	 * @param array $args  The arguments for this js_var.
244
	 */
245
	protected function script_var_array( $args ) {
246
247
		$script          = ( 0 === $args['index_key'] ) ? 'css=\'\';' : '';
248
		$property_script = '';
249
250
		// Define choice.
251
		$choice = ( isset( $args['choice'] ) && '' !== $args['choice'] ) ? $args['choice'] : '';
252
253
		$value_key        = 'newval' . $args['index_key'];
254
		$property_script .= $value_key . '=newval;';
255
256
		$args = $this->get_args( $args );
257
258
		// Apply callback to the value if a callback is defined.
259
		if ( ! empty( $args['js_callback'] ) && is_array( $args['js_callback'] ) && isset( $args['js_callback'][0] ) && ! empty( $args['js_callback'][0] ) ) {
260
			$script .= $value_key . '=' . $args['js_callback'][0] . '(' . $value_key . ',' . $args['js_callback'][1] . ');';
261
		}
262
		$script .= '_.each(' . $value_key . ', function(subValue,subKey){';
263
264
		// Apply the value_pattern.
265
		if ( '' !== $args['value_pattern'] ) {
266
			$script .= $this->value_pattern_replacements( 'subValue', $args );
267
		}
268
269
		// Tweak to add url() for background-images.
270
		if ( '' === $choice || 'background-image' === $choice ) {
271
			$script .= 'if(\'background-image\'===\'' . $args['property'] . '\'||\'background-image\'===subKey){if(-1===subValue.indexOf(\'url(\')){subValue=\'url("\'+subValue+\'")\';}}';
272
		}
273
274
		// Apply prefix.
275
		$value = $value_key;
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
276
		if ( '' !== $args['prefix'] ) {
277
			$value = '\'' . $args['prefix'] . '\'+subValue';
278
		}
279
280
		// Mostly used for padding, margin & position properties.
281
		$direction_script  = 'if(_.contains([\'top\',\'bottom\',\'left\',\'right\'],subKey)){';
282
		$direction_script .= 'css+=\'' . $args['element'] . '{' . $args['property'] . '-\'+subKey+\':\'+subValue+\'' . $args['units'] . $args['suffix'] . ';}\';}';
283
		// Allows us to apply this just for a specific choice in the array of the values.
284
		if ( '' !== $choice ) {
285
			$choice_is_direction = ( false !== strpos( $choice, 'top' ) || false !== strpos( $choice, 'bottom' ) || false !== strpos( $choice, 'left' ) || false !== strpos( $choice, 'right' ) );
286
			// The script.
287
			$script .= 'if(\'' . $choice . '\'===subKey){';
288
			$script .= ( $choice_is_direction ) ? $direction_script . 'else{' : '';
289
			$script .= 'css+=\'' . $args['element'] . '{' . $args['property'] . ':\'+subValue+\';}\';';
290
			$script .= ( $choice_is_direction ) ? '}' : '';
291
			$script .= '}';
292
		} else {
293
294
			// This is where most object-based fields will go.
295
			$script .= $direction_script . 'else{css+=\'' . $args['element'] . '{\'+subKey+\':\'+subValue+\'' . $args['units'] . $args['suffix'] . ';}\';}';
296
		}
297
		$script .= '});';
298
299
		if ( isset( $args['media_query'] ) ) {
300
			$script .= 'css=\'' . $args['media_query'] . '{\'+css+\'}\';';
301
		}
302
303
		return array(
304
			'script' => $property_script . $script,
305
			'css'    => 'css',
306
		);
307
	}
308
309
	/**
310
	 * Processes script generation for typography fields.
311
	 *
312
	 * @access protected
313
	 * @since 3.0.0
314
	 * @param array $args  The arguments for this js_var.
315
	 * @param array $field The field arguments.
316
	 */
317
	protected function script_var_typography( $args, $field ) {
318
319
		$args = $this->get_args( $args );
320
321
		$script = '';
322
		$css    = '';
323
324
		// Load the font using WenFontloader.
325
		// This is a bit ugly because wp_add_inline_script doesn't allow adding <script> directly.
326
		$webfont_loader = 'sc=\'a\';jQuery(\'head\').append(sc.replace(\'a\',\'<\')+\'script>if(!_.isUndefined(WebFont)&&fontFamily){WebFont.load({google:{families:["\'+fontFamily.replace( /\"/g, \'&quot;\' )+\':\'+variant+\'cyrillic,cyrillic-ext,devanagari,greek,greek-ext,khmer,latin,latin-ext,vietnamese,hebrew,arabic,bengali,gujarati,tamil,telugu,thai"]}});}\'+sc.replace(\'a\',\'<\')+\'/script>\');';
327
328
		// Add the css.
329
		$css_build_array  = array(
330
			'font-family'     => 'fontFamily',
331
			'font-size'       => 'fontSize',
332
			'line-height'     => 'lineHeight',
333
			'letter-spacing'  => 'letterSpacing',
334
			'word-spacing'    => 'wordSpacing',
335
			'text-align'      => 'textAlign',
336
			'text-transform'  => 'textTransform',
337
			'text-decoration' => 'textDecoration',
338
			'color'           => 'color',
339
			'font-weight'     => 'fontWeight',
340
			'font-style'      => 'fontStyle',
341
		);
342
		$choice_condition = ( isset( $args['choice'] ) && '' !== $args['choice'] && isset( $css_build_array[ $args['choice'] ] ) );
343
		$script          .= ( ! $choice_condition ) ? $webfont_loader : '';
344
		foreach ( $css_build_array as $property => $var ) {
345
			if ( $choice_condition && $property !== $args['choice'] ) {
346
				continue;
347
			}
348
			// Fixes https://github.com/aristath/kirki/issues/1436.
349
			if ( ! isset( $field['default'] ) || (
350
				( 'font-family' === $property && ! isset( $field['default']['font-family'] ) ) ||
351
				( 'font-size' === $property && ! isset( $field['default']['font-size'] ) ) ||
352
				( 'line-height' === $property && ! isset( $field['default']['line-height'] ) ) ||
353
				( 'letter-spacing' === $property && ! isset( $field['default']['letter-spacing'] ) ) ||
354
				( 'word-spacing' === $property && ! isset( $field['default']['word-spacing'] ) ) ||
355
				( 'text-align' === $property && ! isset( $field['default']['text-align'] ) ) ||
356
				( 'text-transform' === $property && ! isset( $field['default']['text-transform'] ) ) ||
357
				( 'text-decoration' === $property && ! isset( $field['default']['text-decoration'] ) ) ||
358
				( 'color' === $property && ! isset( $field['default']['color'] ) ) ||
359
				( 'font-weight' === $property && ! isset( $field['default']['variant'] ) && ! isset( $field['default']['font-weight'] ) ) ||
360
				( 'font-style' === $property && ! isset( $field['default']['variant'] ) && ! isset( $field['default']['font-style'] ) )
361
				) ) {
362
				continue;
363
			}
364
			$script .= ( $choice_condition && 'font-family' === $args['choice'] ) ? $webfont_loader : '';
365
366
			if ( 'font-family' === $property || ( isset( $args['choice'] ) && 'font-family' === $args['choice'] ) ) {
367
				$css .= 'fontFamilyCSS=fontFamily;if(0<fontFamily.indexOf(\' \')&&-1===fontFamily.indexOf(\'"\')){fontFamilyCSS=\'"\'+fontFamily+\'"\';}';
368
				$var  = 'fontFamilyCSS';
369
			}
370
			$var  = ( ( empty( $args['prefix'] ) ) ? '' : '\'' . $args['prefix'] . '\'+' ) . $var . ( ( empty( $args['units'] ) ) ? '' : '+\'' . $args['units'] . '\'' ) . ( ( empty( $args['suffix'] ) ) ? '' : '+\'' . $args['suffix'] . '\'' );
371
			$css .= 'css+=(\'\'!==' . $var . ')?\'' . $args['element'] . '\'+\'{' . $property . ':\'+' . $var . '+\';}\':\'\';';
372
		}
373
374
		$script .= $css;
375
		if ( isset( $args['media_query'] ) ) {
376
			$script .= 'css=\'' . $args['media_query'] . '{\'+css+\'}\';';
377
		}
378
		return array(
379
			'script' => $script,
380
			'css'    => 'css',
381
		);
382
	}
383
384
	/**
385
	 * Processes script generation for typography fields.
386
	 *
387
	 * @access protected
388
	 * @since 3.0.0
389
	 * @param array $args  The arguments for this js_var.
390
	 */
391
	protected function script_var_image( $args ) {
392
		$return = $this->script_var( $args );
393
		return array(
394
			'script' => 'newval=(!_.isUndefined(newval.url))?newval.url:newval;' . $return['script'],
395
			'css'    => $return['css'],
396
		);
397
	}
398
399
	/**
400
	 * Adds anything we need before the main script.
401
	 *
402
	 * @access private
403
	 * @since 3.0.0
404
	 * @param array $args The field args.
405
	 * @return string
406
	 */
407
	private function before_script( $args ) {
408
409
		$script = '';
410
411
		if ( isset( $args['type'] ) ) {
412
			switch ( $args['type'] ) {
413
				case 'kirki-typography':
414
					$script .= 'fontFamily=(_.isUndefined(newval[\'font-family\']))?\'\':newval[\'font-family\'];variant=(_.isUndefined(newval.variant))?\'400\':newval.variant;fontSize=(_.isUndefined(newval[\'font-size\']))?\'\':newval[\'font-size\'];lineHeight=(_.isUndefined(newval[\'line-height\']))?\'\':newval[\'line-height\'];letterSpacing=(_.isUndefined(newval[\'letter-spacing\']))?\'\':newval[\'letter-spacing\'];wordSpacing=(_.isUndefined(newval[\'word-spacing\']))?\'\':newval[\'word-spacing\'];textAlign=(_.isUndefined(newval[\'text-align\']))?\'\':newval[\'text-align\'];textTransform=(_.isUndefined(newval[\'text-transform\']))?\'\':newval[\'text-transform\'];textDecoration=(_.isUndefined(newval[\'text-decoration\']))?\'\':newval[\'text-decoration\'];color=(_.isUndefined(newval.color))?\'\':newval.color;fw=(!_.isString(newval.variant))?\'400\':newval.variant.match(/\d/g);fontWeight=(!_.isObject(fw))?400:fw.join(\'\');fontStyle=(-1!==variant.indexOf(\'italic\'))?\'italic\':\'normal\';css=\'\';';
415
					break;
416
			}
417
		}
418
		return $script;
419
	}
420
421
	/**
422
	 * Sanitizes the arguments and makes sure they are all there.
423
	 *
424
	 * @access private
425
	 * @since 3.0.0
426
	 * @param array $args The arguments.
427
	 * @return array
428
	 */
429
	private function get_args( $args ) {
430
431
		// Make sure everything is defined to avoid "undefined index" errors.
432
		$args = wp_parse_args(
1 ignored issue
show
Bug introduced by
The function wp_parse_args was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

432
		$args = /** @scrutinizer ignore-call */ wp_parse_args(
Loading history...
433
			$args, array(
434
				'element'       => '',
435
				'property'      => '',
436
				'prefix'        => '',
437
				'suffix'        => '',
438
				'units'         => '',
439
				'js_callback'   => array( '', '' ),
440
				'value_pattern' => '',
441
			)
442
		);
443
444
		// Element should be a string.
445
		if ( is_array( $args['element'] ) ) {
446
			$args['element'] = implode( ',', $args['element'] );
447
		}
448
449
		// Make sure arguments that are passed-on to callbacks are strings.
450
		if ( is_array( $args['js_callback'] ) && isset( $args['js_callback'][1] ) && is_array( $args['js_callback'][1] ) ) {
451
			$args['js_callback'][1] = wp_json_encode( $args['js_callback'][1] );
1 ignored issue
show
Bug introduced by
The function wp_json_encode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

451
			$args['js_callback'][1] = /** @scrutinizer ignore-call */ wp_json_encode( $args['js_callback'][1] );
Loading history...
452
		}
453
454
		if ( ! isset( $args['js_callback'][1] ) ) {
455
			$args['js_callback'][1] = '';
456
		}
457
		return $args;
458
459
	}
460
461
	/**
462
	 * Returns script for value_pattern & replacements.
463
	 *
464
	 * @access private
465
	 * @since 3.0.0
466
	 * @param string $value   The value placeholder.
467
	 * @param array  $js_vars The js_vars argument.
468
	 * @return string         The script.
469
	 */
470
	private function value_pattern_replacements( $value, $js_vars ) {
471
		$script = '';
472
		$alias  = $value;
473
		if ( ! isset( $js_vars['value_pattern'] ) ) {
474
			return $value;
475
		}
476
		$value = $js_vars['value_pattern'];
477
		if ( isset( $js_vars['pattern_replace'] ) ) {
478
			$script .= 'settings=window.wp.customize.get();';
479
			foreach ( $js_vars['pattern_replace'] as $search => $replace ) {
480
				$replace = '\'+settings["' . $replace . '"]+\'';
481
				$value   = str_replace( $search, $replace, $js_vars['value_pattern'] );
482
				$value   = trim( $value, '+' );
483
			}
484
		}
485
		$value_compiled = str_replace( '$', '\'+' . $alias . '+\'', $value );
486
		$value_compiled = trim( $value_compiled, '+' );
487
488
		return $script . $alias . '=\'' . $value_compiled . '\';';
489
	}
490
491
	/**
492
	 * Get the callback function/method we're going to use for this field.
493
	 *
494
	 * @access private
495
	 * @since 3.0.0
496
	 * @param array $args The field args.
497
	 * @return string|array A callable function or method.
498
	 */
499
	protected function get_callback( $args ) {
500
501
		switch ( $args['type'] ) {
502
			case 'kirki-background':
503
			case 'kirki-dimensions':
504
			case 'kirki-multicolor':
505
			case 'kirki-sortable':
506
				$callback = array( $this, 'script_var_array' );
507
				break;
508
			case 'kirki-typography':
509
				$callback = array( $this, 'script_var_typography' );
510
				break;
511
			case 'kirki-image':
512
				$callback = array( $this, 'script_var_image' );
513
				break;
514
			default:
515
				$callback = array( $this, 'script_var' );
516
		}
517
		return $callback;
518
	}
519
}
520