Completed
Push — develop ( d894e5...bbcbea )
by Aristeides
02:26
created

Kirki_Modules_PostMessage::postmessage()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 8
nc 3
nop 0
dl 0
loc 13
rs 7.7777
c 0
b 0
f 0
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