Completed
Pull Request — develop (#1708)
by Aristeides
07:21
created

Kirki_Modules_PostMessage::get_args()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
nc 8
nop 1
dl 0
loc 29
rs 9.1111
c 2
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) 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
	 * 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
	 * Constructor.
34
	 *
35
	 * @access protected
36
	 * @since 3.0.0
37
	 */
38
	protected function __construct() {
39
		add_action( 'customize_preview_init', array( $this, 'postmessage' ) );
40
	}
41
42
	/**
43
	 * Gets an instance of this object.
44
	 * Prevents duplicate instances which avoid artefacts and improves performance.
45
	 *
46
	 * @static
47
	 * @access public
48
	 * @since 3.0.0
49
	 * @return object
50
	 */
51
	public static function get_instance() {
52
		if ( ! self::$instance ) {
53
			self::$instance = new self();
54
		}
55
		return self::$instance;
56
	}
57
58
	/**
59
	 * Enqueues the postMessage script
60
	 * and adds variables to it using the wp_localize_script function.
61
	 * The rest is handled via JS.
62
	 */
63
	public function postmessage() {
64
65
		wp_enqueue_script( 'kirki_auto_postmessage', trailingslashit( Kirki::$url ) . 'modules/postmessage/postmessage.js', array( 'jquery', 'customize-preview' ), KIRKI_VERSION, true );
66
		$fields = Kirki::$fields;
67
		$data   = array();
68
		foreach ( $fields as $field ) {
69
			if ( isset( $field['transport'] ) && 'postMessage' === $field['transport'] && isset( $field['js_vars'] ) && ! empty( $field['js_vars'] ) && is_array( $field['js_vars'] ) && isset( $field['settings'] ) ) {
70
				$data[] = $field;
71
			}
72
		}
73
		wp_localize_script( 'kirki_auto_postmessage', 'kirkiPostMessageFields', $data );
74
	}
75
}
76