Completed
Pull Request — develop (#1554)
by Aristeides
02:12
created

Kirki_Control_Base::enqueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Controls Base.
4
 *
5
 * Extend this in other controls.
6
 *
7
 * @package     Kirki
8
 * @subpackage  Controls
9
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
10
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
11
 * @since       3.0.10
12
 */
13
14
/**
15
 * A base for controls.
16
 */
17
class Kirki_Control_Base extends WP_Customize_Control {
18
19
	/**
20
	 * Used to automatically generate all CSS output.
21
	 *
22
	 * @access public
23
	 * @var array
24
	 */
25
	public $output = array();
26
27
	/**
28
	 * Data type
29
	 *
30
	 * @access public
31
	 * @var string
32
	 */
33
	public $option_type = 'theme_mod';
34
35
	/**
36
	 * The kirki_config we're using for this control
37
	 *
38
	 * @access public
39
	 * @var string
40
	 */
41
	public $kirki_config = 'global';
42
43
	/**
44
	 * Extra script dependencies.
45
	 *
46
	 * @since 3.1.0
47
	 * @return array
48
	 */
49
	public function kirki_script_dependencies() {
50
		return array();
51
	}
52
53
	/**
54
	 * Enqueue control related scripts/styles.
55
	 *
56
	 * @access public
57
	 */
58
	public function enqueue() {
59
60
		$dependencies = array_merge( array( 'jquery', 'customize-base' ), $this->kirki_script_dependencies() );
61
		wp_enqueue_script( 'kirki-control', trailingslashit( Kirki::$url ) . 'controls/js/kirki.control.js', $dependencies, false, true );
62
		wp_enqueue_script( 'kirki-input', trailingslashit( Kirki::$url ) . 'controls/js/kirki.input.js', $dependencies, false, true );
63
		wp_enqueue_script( 'kirki-setting', trailingslashit( Kirki::$url ) . 'controls/js/kirki.setting.js', $dependencies, false, true );
64
		wp_enqueue_script( 'kirki-util', trailingslashit( Kirki::$url ) . 'controls/js/kirki.util.js', $dependencies, false, true );
65
		wp_enqueue_script( 'kirki-value', trailingslashit( Kirki::$url ) . 'controls/js/kirki.value.js', $dependencies, false, true );
66
		wp_enqueue_script( 'kirki-dynamic-control', trailingslashit( Kirki::$url ) . 'controls/js/dynamic-control.js', array( 'jquery', 'customize-base', 'kirki-control', 'kirki-input', 'kirki-setting', 'kirki-util', 'kirki-value' ), false, true );
67
68
		wp_enqueue_script( 'kirki-controls', trailingslashit( Kirki::$url ) . 'controls/js/controls.js', array( 'jquery', 'kirki-dynamic-control', 'customize-base' ), false, true );
69
		wp_enqueue_style( 'kirki-controls-css', trailingslashit( Kirki::$url ) . 'controls/css/controls.css', null );
70
	}
71
72
	/**
73
	 * Refresh the parameters passed to the JavaScript via JSON.
74
	 *
75
	 * @see WP_Customize_Control::to_json()
76
	 */
77
	public function to_json() {
78
		// Get the basics from the parent class.
79
		parent::to_json();
80
		// Default.
81
		$this->json['default'] = $this->setting->default;
82
		if ( isset( $this->default ) ) {
83
			$this->json['default'] = $this->default;
84
		}
85
		// Output.
86
		$this->json['output'] = $this->output;
87
		// Value.
88
		$this->json['value'] = $this->value();
89
		// Choices.
90
		$this->json['choices'] = $this->choices;
91
		// The link.
92
		$this->json['link'] = $this->get_link();
93
		// The ID.
94
		$this->json['id'] = $this->id;
95
		// Translation strings.
96
		$this->json['l10n'] = $this->l10n();
97
		// The ajaxurl in case we need it.
98
		$this->json['ajaxurl'] = admin_url( 'admin-ajax.php' );
99
		// Input attributes.
100
		$this->json['inputAttrs'] = '';
101
		foreach ( $this->input_attrs as $attr => $value ) {
102
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
103
		}
104
	}
105
106
	/**
107
	 * Render the control's content.
108
	 *
109
	 * Allows the content to be overridden without having to rewrite the wrapper in `$this::render()`.
110
	 *
111
	 * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
112
	 * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
113
	 *
114
	 * Control content can alternately be rendered in JS. See WP_Customize_Control::print_template().
115
	 *
116
	 * @since 3.4.0
117
	 */
118
	protected function render_content() {}
119
120
	/**
121
	 * An Underscore (JS) template for this control's content (but not its container).
122
	 *
123
	 * Class variables for this control class are available in the `data` JS object;
124
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
125
	 *
126
	 * @see WP_Customize_Control::print_template()
127
	 *
128
	 * @access protected
129
	 */
130
	protected function content_template() {
131
		// This HTML will be replaced when the control is loaded.
132
		echo '<h4>' . esc_attr__( 'Please wait while we load the control', 'kirki' ) . '</h4>';
133
	}
134
135
	/**
136
	 * Returns an array of translation strings.
137
	 *
138
	 * @access protected
139
	 * @since 3.0.0
140
	 * @return array
141
	 */
142
	protected function l10n() {
143
		return array();
144
	}
145
}
146