Completed
Pull Request — master (#1592)
by Aristeides
05:37 queued 03:31
created

Kirki_Modules_Post_Meta::get_instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customize_Queried_Post_Info class.
4
 *
5
 * @package CustomizeQueriedPostInfo
6
 */
7
8
/**
9
 * Class Customize_Queried_Post_Info.
10
 */
11
class Kirki_Modules_Post_Meta {
12
13
	/**
14
	 * The object instance.
15
	 *
16
	 * @static
17
	 * @access private
18
	 * @since 3.0.0
19
	 * @var object
20
	 */
21
	private static $instance;
22
23
	/**
24
	 * Gets an instance of this object.
25
	 * Prevents duplicate instances which avoid artefacts and improves performance.
26
	 *
27
	 * @static
28
	 * @access public
29
	 * @since 3.0.0
30
	 * @return object
31
	 */
32
	public static function get_instance() {
33
		if ( ! self::$instance ) {
34
			self::$instance = new self();
35
		}
36
		return self::$instance;
37
	}
38
39
	/**
40
	 * Constructor.
41
	 *
42
	 * @access protected
43
	 * @since 3.1.0
44
	 */
45
	protected function __construct() {
46
47
		add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) );
48
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
49
	}
50
51
	/**
52
	 * Enqueue Customizer control scripts.
53
	 *
54
	 * @access public
55
	 * @since 3.1.0
56
	 */
57
	public function enqueue_control_scripts() {
58
59
		wp_enqueue_script( 'kirki_post_meta_previewed_controls', trailingslashit( Kirki::$url ) . 'modules/post-meta/customize-controls.js', array( 'jquery', 'customize-controls' ), false, true );
60
	}
61
62
	/**
63
	 * Initialize Customizer preview.
64
	 *
65
	 * @access public
66
	 * @since 3.1.0
67
	 */
68
	public function customize_preview_init() {
69
70
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
71
	}
72
73
	/**
74
	 * Enqueue script for Customizer preview.
75
	 *
76
	 * @access public
77
	 * @since 3.1.0
78
	 */
79
	public function enqueue_preview_scripts() {
80
81
		wp_enqueue_script( 'kirki_post_meta_previewed_preview', trailingslashit( Kirki::$url ) . 'modules/post-meta/customize-preview.js', array( 'jquery', 'customize-preview' ), false, true );
82
83
		$wp_scripts   = wp_scripts();
84
		$queried_post = null;
85
		if ( is_singular() && get_queried_object() ) {
86
			$queried_post = get_queried_object();
87
			$queried_post->meta = get_post_custom( $queried_post->id );
88
		}
89
		$wp_scripts->add_data( 'kirki_post_meta_previewed_preview', 'data', sprintf( 'var _customizePostPreviewedQueriedObject = %s;', wp_json_encode( $queried_post ) ) );
90
	}
91
}
92