|
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
|
|
|
$js_vars_fields = array(); |
|
51
|
|
|
$fields = Kirki::$fields; |
|
52
|
|
|
foreach ( $fields as $id => $field ) { |
|
53
|
|
|
if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['js_vars'] ) && ! empty( $field['js_vars'] ) && is_array( $field['js_vars'] ) && isset( $field['settings'] ) ) { |
|
54
|
|
|
$this->script .= $this->script( $field ); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
wp_add_inline_script( 'kirki_auto_postmessage', $this->script, 'after' ); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Generates script for a single js_var. |
|
63
|
|
|
* |
|
64
|
|
|
* @access protected |
|
65
|
|
|
* @since 3.0.0 |
|
66
|
|
|
* @param array $args The arguments. |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function _script( $args ) { |
|
69
|
|
|
$script = ''; |
|
70
|
|
|
$property_script = ''; |
|
71
|
|
|
|
|
72
|
|
|
$value_key = 'newval' . $args['index_key']; |
|
73
|
|
|
$property_script .= $value_key . '=newval;'; |
|
74
|
|
|
|
|
75
|
|
|
// Make sure everything is defined to avoid "undefined index" errors. |
|
76
|
|
|
$args = wp_parse_args( $args, array( |
|
77
|
|
|
'element' => '', |
|
78
|
|
|
'property' => '', |
|
79
|
|
|
'prefix' => '', |
|
80
|
|
|
'suffix' => '', |
|
81
|
|
|
'units' => '', |
|
82
|
|
|
'js_callback' => array( '', '' ), |
|
83
|
|
|
'value_pattern' => '', |
|
84
|
|
|
)); |
|
85
|
|
|
|
|
86
|
|
|
// Element should be a string. |
|
87
|
|
|
if ( is_array( $args['element'] ) ) { |
|
88
|
|
|
$args['element'] = implode( ',', $args['element'] ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Make sure arguments that are passed-on to callbacks are strings. |
|
92
|
|
|
if ( is_array( $args['js_callback'] ) ) { |
|
93
|
|
|
if ( isset( $args['js_callback'][1] ) && is_array( $args['js_callback'][1] ) ) { |
|
94
|
|
|
$args['js_callback'][1] = json_encode( $args['js_callback'][1] ); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Apply callback to the value if a callback is defined. |
|
99
|
|
|
if ( ! empty( $args['js_callback'][0] ) ) { |
|
100
|
|
|
$script .= $value_key . '=' . $args['js_callback'][0] . '(' . $value_key . ',' . $args['js_callback'][1] . ');'; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Apply the value_pattern. |
|
104
|
|
|
if ( '' !== $args['value_pattern'] ) { |
|
105
|
|
|
$value_pattern = str_replace( '$', '\'+' . $value_key . '+\'', $value_key ); |
|
106
|
|
|
$script .= $value_key . '=' . trim( $value_pattern, '\'+' ) . ';'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Apply prefix, units, suffix. |
|
110
|
|
|
$value = $value_key; |
|
111
|
|
|
if ( '' !== $args['prefix'] ) { |
|
112
|
|
|
$value = $args['prefix'] . '+' . $value_key; |
|
113
|
|
|
} |
|
114
|
|
|
if ( '' !== $args['units'] || '' !== $args['suffix'] ) { |
|
115
|
|
|
$value .= '+' . $args['units'] . $args['suffix']; |
|
116
|
|
|
} |
|
117
|
|
|
$scripts_array = array(); |
|
118
|
|
|
$scripts_array[ sanitize_key( $args['element'] ) ][ sanitize_key( $args['property'] ) ]['script'] = $property_script . $script; |
|
119
|
|
|
$scripts_array[ sanitize_key( $args['element'] ) ][ sanitize_key( $args['property'] ) ]['css'] = $args['element'] . '{' . $args['property'] . ':\'+' . $value_key . '+\';}'; |
|
120
|
|
|
|
|
121
|
|
|
return $scripts_array; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Generates script for a single field. |
|
126
|
|
|
* |
|
127
|
|
|
* @access protected |
|
128
|
|
|
* @since 3.0.0 |
|
129
|
|
|
* @param array $args The arguments. |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function script( $args ) { |
|
132
|
|
|
|
|
133
|
|
|
$field_scripts = array(); |
|
134
|
|
|
|
|
135
|
|
|
$script = 'wp.customize(\'' . $args['settings'] . '\',function(value){value.bind(function(newval){'; |
|
136
|
|
|
// append unique style tag if not exist |
|
137
|
|
|
// The style ID. |
|
138
|
|
|
$style_id = 'kirki-postmessage-' . str_replace( array( '[', ']' ), '', $args['settings'] ); |
|
139
|
|
|
$script .= 'if(!jQuery(\'' . $style_id . '\').size()){jQuery(\'head\').append(\'<style id="' . $style_id . '"></style>\');}'; |
|
140
|
|
|
|
|
141
|
|
|
// Loop through the js_vars and generate the script. |
|
142
|
|
|
foreach ( $args['js_vars'] as $key => $js_var ) { |
|
143
|
|
|
$js_var['index_key'] = $key; |
|
144
|
|
|
$field['scripts'][ $key ] = $this->_script( $js_var ); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
$combo_extra_script = ''; |
|
147
|
|
|
$combo_css_script = ''; |
|
148
|
|
|
foreach ( $field['scripts'] as $script_l1 ) { |
|
|
|
|
|
|
149
|
|
|
foreach ( $script_l1 as $script_l2 ) { |
|
150
|
|
|
foreach ( $script_l2 as $script_array ) { |
|
151
|
|
|
$combo_extra_script .= $script_array['script']; |
|
152
|
|
|
$combo_css_script .= $script_array['css']; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
$script .= $combo_extra_script . 'jQuery(\'#' . $style_id . '\').text(\'' . $combo_css_script . '\');'; |
|
157
|
|
|
$script .= '});});'; |
|
158
|
|
|
return $script; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|