Passed
Push — develop ( fe986b...95ca01 )
by Aristeides
04:19
created

Kirki::add_field()   B

Complexity

Conditions 9
Paths 18

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 9
nc 18
nop 2
dl 0
loc 31
rs 8.0555
c 1
b 1
f 0
1
<?php
2
/**
3
 * The Kirki API class.
4
 * Takes care of adding panels, sections & fields to the customizer.
5
 * For documentation please see https://github.com/aristath/kirki/wiki
6
 *
7
 * @package     Kirki
8
 * @category    Core
9
 * @author      Aristeides Stathopoulos
10
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
11
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
12
 * @since       1.0
13
 */
14
15
// Exit if accessed directly.
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
18
}
19
20
/**
21
 * This class acts as an interface.
22
 * Developers may use this object to add configurations, fields, panels and sections.
23
 * You can also access all available configurations, fields, panels and sections
24
 * by accessing the object's static properties.
25
 */
26
class Kirki extends Kirki_Init {
27
28
	/**
29
	 * Absolute path to the Kirki folder.
30
	 *
31
	 * @static
32
	 * @access public
33
	 * @var string
34
	 */
35
	public static $path;
36
37
	/**
38
	 * URL to the Kirki folder.
39
	 *
40
	 * @static
41
	 * @access public
42
	 * @var string
43
	 */
44
	public static $url;
45
46
	/**
47
	 * An array containing all configurations.
48
	 *
49
	 * @static
50
	 * @access public
51
	 * @var array
52
	 */
53
	public static $config = array();
54
55
	/**
56
	 * An array containing all fields.
57
	 *
58
	 * @static
59
	 * @access public
60
	 * @var array
61
	 */
62
	public static $fields = array();
63
64
	/**
65
	 * An array containing all panels.
66
	 *
67
	 * @static
68
	 * @access public
69
	 * @var array
70
	 */
71
	public static $panels = array();
72
73
	/**
74
	 * An array containing all sections.
75
	 *
76
	 * @static
77
	 * @access public
78
	 * @var array
79
	 */
80
	public static $sections = array();
81
82
	/**
83
	 * An array containing all panels to be removed.
84
	 *
85
	 * @static
86
	 * @access public
87
	 * @since 3.0.17
88
	 * @var array
89
	 */
90
	public static $panels_to_remove = array();
91
92
	/**
93
	 * An array containing all sections to be removed.
94
	 *
95
	 * @static
96
	 * @access public
97
	 * @since 3.0.17
98
	 * @var array
99
	 */
100
	public static $sections_to_remove = array();
101
102
	/**
103
	 * An array containing all controls to be removed.
104
	 *
105
	 * @static
106
	 * @access public
107
	 * @since 3.0.17
108
	 * @var array
109
	 */
110
	public static $controls_to_remove = array();
111
112
	/**
113
	 * Modules object.
114
	 *
115
	 * @access public
116
	 * @since 3.0.0
117
	 * @var object
118
	 */
119
	public $modules;
120
121
	/**
122
	 * Get the value of an option from the db.
123
	 *
124
	 * @static
125
	 * @access public
126
	 * @param string $config_id The ID of the configuration corresponding to this field.
127
	 * @param string $field_id  The field_id (defined as 'settings' in the field arguments).
128
	 * @return mixed The saved value of the field.
129
	 */
130
	public static function get_option( $config_id = '', $field_id = '' ) {
131
132
		return Kirki_Values::get_value( $config_id, $field_id );
133
	}
134
135
	/**
136
	 * Sets the configuration options.
137
	 *
138
	 * @static
139
	 * @access public
140
	 * @param string $config_id The configuration ID.
141
	 * @param array  $args      The configuration options.
142
	 */
143
	public static function add_config( $config_id, $args = array() ) {
144
145
		$config                             = Kirki_Config::get_instance( $config_id, $args );
146
		$config_args                        = $config->get_config();
147
		self::$config[ $config_args['id'] ] = $config_args;
148
	}
149
150
	/**
151
	 * Create a new panel.
152
	 *
153
	 * @static
154
	 * @access public
155
	 * @param string $id   The ID for this panel.
156
	 * @param array  $args The panel arguments.
157
	 */
158
	public static function add_panel( $id = '', $args = array() ) {
159
160
		$args['id']          = esc_attr( $id );
161
		$args['description'] = ( isset( $args['description'] ) ) ? $args['description'] : '';
162
		$args['priority']    = ( isset( $args['priority'] ) ) ? absint( $args['priority'] ) : 10;
163
		$args['type']        = ( isset( $args['type'] ) ) ? $args['type'] : 'default';
164
		$args['type']        = 'kirki-' . $args['type'];
165
166
		self::$panels[ $args['id'] ] = $args;
167
	}
168
169
	/**
170
	 * Remove a panel.
171
	 *
172
	 * @static
173
	 * @access public
174
	 * @since 3.0.17
175
	 * @param string $id   The ID for this panel.
176
	 */
177
	public static function remove_panel( $id = '' ) {
178
		if ( ! in_array( $id, self::$panels_to_remove, true ) ) {
179
			self::$panels_to_remove[] = $id;
180
		}
181
	}
182
183
	/**
184
	 * Create a new section.
185
	 *
186
	 * @static
187
	 * @access public
188
	 * @param string $id   The ID for this section.
189
	 * @param array  $args The section arguments.
190
	 */
191
	public static function add_section( $id, $args ) {
192
193
		$args['id']          = esc_attr( $id );
194
		$args['panel']       = ( isset( $args['panel'] ) ) ? esc_attr( $args['panel'] ) : '';
195
		$args['description'] = ( isset( $args['description'] ) ) ? $args['description'] : '';
196
		$args['priority']    = ( isset( $args['priority'] ) ) ? absint( $args['priority'] ) : 10;
197
		$args['type']        = ( isset( $args['type'] ) ) ? $args['type'] : 'default';
198
		$args['type']        = 'kirki-' . $args['type'];
199
200
		self::$sections[ $args['id'] ] = $args;
201
	}
202
203
	/**
204
	 * Remove a section.
205
	 *
206
	 * @static
207
	 * @access public
208
	 * @since 3.0.17
209
	 * @param string $id   The ID for this panel.
210
	 */
211
	public static function remove_section( $id = '' ) {
212
		if ( ! in_array( $id, self::$sections_to_remove, true ) ) {
213
			self::$sections_to_remove[] = $id;
214
		}
215
	}
216
217
	/**
218
	 * Create a new field.
219
	 *
220
	 * @static
221
	 * @access public
222
	 * @param string $config_id The configuration ID for this field.
223
	 * @param array  $args      The field arguments.
224
	 */
225
	public static function add_field( $config_id, $args ) {
226
227
		if ( doing_action( 'customize_register' ) ) {
228
			_doing_it_wrong( __METHOD__, esc_attr__( 'Kirki fields should not be added on customize_register. Please add them directly, or on init.', 'kirki' ), '3.0.10' );
229
		}
230
231
		// Early exit if 'type' is not defined.
232
		if ( ! isset( $args['type'] ) ) {
233
			return;
234
		}
235
236
		// If the field is font-awesome, enqueue the icons on the frontend.
237
		if ( class_exists( 'Kirki_Modules_CSS' ) && ( 'fontawesome' === $args['type'] || 'kirki-fontawesome' === $args['type'] ) ) {
238
			Kirki_Modules_CSS::add_fontawesome_script();
239
		}
240
241
		$str       = str_replace( array( '-', '_' ), ' ', $args['type'] );
242
		$classname = 'Kirki_Field_' . str_replace( ' ', '_', ucwords( $str ) );
243
		if ( class_exists( $classname ) ) {
244
			new $classname( $config_id, $args );
245
			return;
246
		}
247
		if ( false !== strpos( $classname, 'Kirki_Field_Kirki_' ) ) {
248
			$classname = str_replace( 'Kirki_Field_Kirki_', 'Kirki_Field_', $classname );
249
			if ( class_exists( $classname ) ) {
250
				new $classname( $config_id, $args );
251
				return;
252
			}
253
		}
254
255
		new Kirki_Field( $config_id, $args );
256
257
	}
258
259
	/**
260
	 * Remove a control.
261
	 *
262
	 * @static
263
	 * @access public
264
	 * @since 3.0.17
265
	 * @param string $id The field ID.
266
	 */
267
	public static function remove_control( $id ) {
268
		if ( ! in_array( $id, self::$controls_to_remove, true ) ) {
269
			self::$controls_to_remove[] = $id;
270
		}
271
	}
272
273
	/**
274
	 * Gets a parameter for a config-id.
275
	 *
276
	 * @static
277
	 * @access public
278
	 * @since 3.0.10
279
	 * @param string $id    The config-ID.
280
	 * @param string $param The parameter we want.
281
	 * @return string
282
	 */
283
	public static function get_config_param( $id, $param ) {
284
285
		if ( ! isset( self::$config[ $id ] ) || ! isset( self::$config[ $id ][ $param ] ) ) {
286
			return '';
287
		}
288
		return self::$config[ $id ][ $param ];
289
	}
290
}
291