Completed
Pull Request — develop (#1334)
by Aristeides
02:44
created

Kirki_Modules_PostMessage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1
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
	/**
24
	 * The script.
25
	 *
26
	 * @access protected
27
	 * @since 3.0.0
28
	 * @var string
29
	 */
30
	protected $script = '';
31
32
	/**
33
	 * Constructor.
34
	 *
35
	 * @access public
36
	 * @since 3.0.0
37
	 */
38
	public function __construct() {
39
		add_action( 'customize_preview_init', array( $this, 'postmessage' ) );
40
	}
41
42
	/**
43
	 * Enqueues the postMessage script
44
	 * and adds variables to it using the wp_localize_script function.
45
	 * The rest is handled via JS.
46
	 */
47
	public function postmessage() {
48
49
		wp_enqueue_script( 'kirki_auto_postmessage', trailingslashit( Kirki::$url ) . 'modules/postmessage/postmessage.js', array( 'jquery', 'customize-preview' ), false, true );
50
		$fields = Kirki::$fields;
51
		foreach ( $fields as $field ) {
52
			if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['js_vars'] ) && ! empty( $field['js_vars'] ) && is_array( $field['js_vars'] ) && isset( $field['settings'] ) ) {
53
				$this->script .= $this->script( $field );
54
			}
55
		}
56
		wp_add_inline_script( 'kirki_auto_postmessage', $this->script, 'after' );
57
58
	}
59
60
	/**
61
	 * Generates script for a single js_var.
62
	 *
63
	 * @access protected
64
	 * @since 3.0.0
65
	 * @param array $args The arguments.
66
	 */
67
	protected function _script( $args ) {
68
		$script = '';
69
		$property_script = '';
70
71
		$value_key = 'newval' . $args['index_key'];
72
		$property_script .= $value_key . '=newval;';
73
74
		// Make sure arguments that are passed-on to callbacks are strings.
75
		if ( is_array( $args['js_callback'] ) && isset( $args['js_callback'][1] ) && is_array( $args['js_callback'][1] ) ) {
76
			$args['js_callback'][1] = wp_json_encode( $args['js_callback'][1] );
77
		}
78
79
		// Apply callback to the value if a callback is defined.
80
		if ( ! empty( $args['js_callback'][0] ) ) {
81
			$script .= $value_key . '=' . $args['js_callback'][0] . '(' . $value_key . ',' . $args['js_callback'][1] . ');';
82
		}
83
84
		// Apply the value_pattern.
85
		if ( '' !== $args['value_pattern'] ) {
86
			$value_pattern = str_replace( '$', '\'+' . $value_key . '+\'', $value_key );
87
			$script .= $value_key . '=' . trim( $value_pattern, '\'+' ) . ';';
88
		}
89
90
		// Apply prefix, units, suffix.
91
		$value = $value_key;
92
		if ( '' !== $args['prefix'] ) {
93
			$value = $args['prefix'] . '+' . $value_key;
94
		}
95
		if ( '' !== $args['units'] || '' !== $args['suffix'] ) {
96
			$value .= '+' . $args['units'] . $args['suffix'];
97
		}
98
		return array(
99
			'script' => $property_script . $script,
100
			'css'    => $args['element'] . '{' . $args['property'] . ':\'+' . $value_key . '+\';}',
101
		);
102
	}
103
104
	/**
105
	 * Generates script for a single field.
106
	 *
107
	 * @access protected
108
	 * @since 3.0.0
109
	 * @param array $args The arguments.
110
	 */
111
	protected function script( $args ) {
112
113
		$script = 'wp.customize(\'' . $args['settings'] . '\',function(value){value.bind(function(newval){';
114
		// append unique style tag if not exist
115
		// The style ID.
116
		$style_id = 'kirki-postmessage-' . str_replace( array( '[', ']' ), '', $args['settings'] );
117
		$script .= 'if(!jQuery(\'' . $style_id . '\').size()){jQuery(\'head\').append(\'<style id="' . $style_id . '"></style>\');}';
118
119
		// Loop through the js_vars and generate the script.
120
		foreach ( $args['js_vars'] as $key => $js_var ) {
121
			$js_var['index_key'] = $key;
122
123
			// Element should be a string.
124
			if ( isset( $js_var['element'] ) && is_array( $js_var['element'] ) ) {
125
				$js_var['element'] = implode( ',', $js_var['element'] );
126
			}
127
128
			$field['scripts'][ $key ] = $this->_script( wp_parse_args( $js_var, array(
129
				'element'       => '',
130
				'property'      => '',
131
				'prefix'        => '',
132
				'suffix'        => '',
133
				'units'         => '',
134
				'js_callback'   => array( '', '' ),
135
				'value_pattern' => '',
136
			) );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ';', expecting ',' or ')'
Loading history...
137
		}
138
		$combo_extra_script = '';
139
		$combo_css_script   = '';
140
		foreach ( $field['scripts'] as $script_array ) {
141
			$combo_extra_script .= $script_array['script'];
142
			$combo_css_script   .= $script_array['css'];
143
		}
144
		$script .= $combo_extra_script . 'jQuery(\'#' . $style_id . '\').text(\'' . $combo_css_script . '\');';
145
		$script .= '});});';
146
		return $script;
147
	}
148
}
149