Total Complexity | 126 |
Total Lines | 497 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Kirki_Modules_PostMessage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Kirki_Modules_PostMessage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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
|
|||
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
|
|||
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
|
|||
82 | wp_add_inline_script( 'kirki_auto_postmessage', $this->script, 'after' ); |
||
1 ignored issue
–
show
|
|||
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 . '\''; |
||
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');"; |
||
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; |
||
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 ) { |
||
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 ) { |
||
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
|
|||
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
|
|||
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 ) { |
||
518 | } |
||
519 | } |
||
520 |