Completed
Push — develop ( d894e5...bbcbea )
by Aristeides
02:26
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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B postmessage() 0 13 8
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) 2016, Aristeides Stathopoulos
9
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
10
 * @since       2.4.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
	 * Constructor.
25
	 *
26
	 * @access public
27
	 * @since 2.4.0
28
	 */
29
	public function __construct() {
30
		add_action( 'customize_preview_init', array( $this, 'postmessage' ) );
31
	}
32
33
	/**
34
	 * Enqueues the postMessage script
35
	 * and adds variables to it using the wp_localize_script function.
36
	 * The rest is handled via JS.
37
	 */
38
	public function postmessage() {
39
40
		wp_enqueue_script( 'kirki_auto_postmessage', trailingslashit( Kirki::$url ) . 'modules/postmessage/postmessage.js', array( 'customize-preview' ), false, true );
41
		$js_vars_fields = array();
42
		$fields = Kirki::$fields;
43
		foreach ( $fields as $field ) {
44
			if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['js_vars'] ) && ! empty( $field['js_vars'] ) && is_array( $field['js_vars'] ) && isset( $field['settings'] ) ) {
45
				$js_vars_fields[ $field['settings'] ] = $field['js_vars'];
46
			}
47
		}
48
		wp_localize_script( 'kirki_auto_postmessage', 'jsvars', $js_vars_fields );
49
50
	}
51
}
52