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 everything is defined to avoid "undefined index" errors. |
75
|
|
|
$args = wp_parse_args( $args, array( |
76
|
|
|
'element' => '', |
77
|
|
|
'property' => '', |
78
|
|
|
'prefix' => '', |
79
|
|
|
'suffix' => '', |
80
|
|
|
'units' => '', |
81
|
|
|
'js_callback' => array( '', '' ), |
82
|
|
|
'value_pattern' => '', |
83
|
|
|
)); |
84
|
|
|
|
85
|
|
|
// Element should be a string. |
86
|
|
|
if ( is_array( $args['element'] ) ) { |
87
|
|
|
$args['element'] = implode( ',', $args['element'] ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Make sure arguments that are passed-on to callbacks are strings. |
91
|
|
|
if ( is_array( $args['js_callback'] ) && isset( $args['js_callback'][1] ) && is_array( $args['js_callback'][1] ) ) { |
92
|
|
|
$args['js_callback'][1] = wp_json_encode( $args['js_callback'][1] ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Apply callback to the value if a callback is defined. |
96
|
|
|
if ( ! empty( $args['js_callback'][0] ) ) { |
97
|
|
|
$script .= $value_key . '=' . $args['js_callback'][0] . '(' . $value_key . ',' . $args['js_callback'][1] . ');'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Apply the value_pattern. |
101
|
|
|
if ( '' !== $args['value_pattern'] ) { |
102
|
|
|
$value_pattern = str_replace( '$', '\'+' . $value_key . '+\'', $value_key ); |
103
|
|
|
$script .= $value_key . '=' . trim( $value_pattern, '\'+' ) . ';'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Apply prefix, units, suffix. |
107
|
|
|
$value = $value_key; |
108
|
|
|
if ( '' !== $args['prefix'] ) { |
109
|
|
|
$value = $args['prefix'] . '+' . $value_key; |
110
|
|
|
} |
111
|
|
|
if ( '' !== $args['units'] || '' !== $args['suffix'] ) { |
112
|
|
|
$value .= '+' . $args['units'] . $args['suffix']; |
113
|
|
|
} |
114
|
|
|
$scripts_array = array(); |
115
|
|
|
$scripts_array[ sanitize_key( $args['element'] ) ][ sanitize_key( $args['property'] ) ]['script'] = $property_script . $script; |
116
|
|
|
$scripts_array[ sanitize_key( $args['element'] ) ][ sanitize_key( $args['property'] ) ]['css'] = $args['element'] . '{' . $args['property'] . ':\'+' . $value_key . '+\';}'; |
117
|
|
|
|
118
|
|
|
return $scripts_array; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Generates script for a single field. |
123
|
|
|
* |
124
|
|
|
* @access protected |
125
|
|
|
* @since 3.0.0 |
126
|
|
|
* @param array $args The arguments. |
127
|
|
|
*/ |
128
|
|
|
protected function script( $args ) { |
129
|
|
|
|
130
|
|
|
$script = 'wp.customize(\'' . $args['settings'] . '\',function(value){value.bind(function(newval){'; |
131
|
|
|
// append unique style tag if not exist |
132
|
|
|
// The style ID. |
133
|
|
|
$style_id = 'kirki-postmessage-' . str_replace( array( '[', ']' ), '', $args['settings'] ); |
134
|
|
|
$script .= 'if(!jQuery(\'' . $style_id . '\').size()){jQuery(\'head\').append(\'<style id="' . $style_id . '"></style>\');}'; |
135
|
|
|
|
136
|
|
|
// Loop through the js_vars and generate the script. |
137
|
|
|
foreach ( $args['js_vars'] as $key => $js_var ) { |
138
|
|
|
$js_var['index_key'] = $key; |
139
|
|
|
$field['scripts'][ $key ] = $this->_script( $js_var ); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
$combo_extra_script = ''; |
142
|
|
|
$combo_css_script = ''; |
143
|
|
|
foreach ( $field['scripts'] as $script_l1 ) { |
|
|
|
|
144
|
|
|
foreach ( $script_l1 as $script_l2 ) { |
145
|
|
|
foreach ( $script_l2 as $script_array ) { |
146
|
|
|
$combo_extra_script .= $script_array['script']; |
147
|
|
|
$combo_css_script .= $script_array['css']; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
$script .= $combo_extra_script . 'jQuery(\'#' . $style_id . '\').text(\'' . $combo_css_script . '\');'; |
152
|
|
|
$script .= '});});'; |
153
|
|
|
return $script; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.