Kirki   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 61
dl 0
loc 269
rs 10
c 3
b 2
f 0
wmc 30

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get_config_param() 0 5 3
B add_field() 0 29 9
A remove_panel() 0 3 2
A remove_section() 0 3 2
A add_panel() 0 15 5
A remove_control() 0 3 2
A get_option() 0 2 1
A add_config() 0 4 1
A add_section() 0 16 5
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    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
		return Kirki_Values::get_value( $config_id, $field_id );
132
	}
133
134
	/**
135
	 * Sets the configuration options.
136
	 *
137
	 * @static
138
	 * @access public
139
	 * @param string $config_id The configuration ID.
140
	 * @param array  $args      The configuration options.
141
	 */
142
	public static function add_config( $config_id, $args = array() ) {
143
		$config                             = Kirki_Config::get_instance( $config_id, $args );
144
		$config_args                        = $config->get_config();
145
		self::$config[ $config_args['id'] ] = $config_args;
146
	}
147
148
	/**
149
	 * Create a new panel.
150
	 *
151
	 * @static
152
	 * @access public
153
	 * @param string $id   The ID for this panel.
154
	 * @param array  $args The panel arguments.
155
	 */
156
	public static function add_panel( $id = '', $args = array() ) {
157
		$args['id'] = $id;
158
		if ( ! isset( $args['description'] ) ) {
159
			$args['description'] = '';
160
		}
161
		if ( ! isset( $args['priority'] ) ) {
162
			$args['priority'] = 10;
163
		}
164
		if ( ! isset( $args['type'] ) ) {
165
			$args['type'] = 'default';
166
		}
167
		if ( false === strpos( $args['type'], 'kirki-' ) ) {
168
			$args['type'] = 'kirki-' . $args['type'];
169
		}
170
		self::$panels[ $id ] = $args;
171
	}
172
173
	/**
174
	 * Remove a panel.
175
	 *
176
	 * @static
177
	 * @access public
178
	 * @since 3.0.17
179
	 * @param string $id   The ID for this panel.
180
	 */
181
	public static function remove_panel( $id = '' ) {
182
		if ( ! in_array( $id, self::$panels_to_remove, true ) ) {
183
			self::$panels_to_remove[] = $id;
184
		}
185
	}
186
187
	/**
188
	 * Create a new section.
189
	 *
190
	 * @static
191
	 * @access public
192
	 * @param string $id   The ID for this section.
193
	 * @param array  $args The section arguments.
194
	 */
195
	public static function add_section( $id, $args ) {
196
		$args['id'] = $id;
197
		if ( ! isset( $args['description'] ) ) {
198
			$args['description'] = '';
199
		}
200
		if ( ! isset( $args['priority'] ) ) {
201
			$args['priority'] = 10;
202
		}
203
		if ( ! isset( $args['type'] ) ) {
204
			$args['type'] = 'default';
205
		}
206
		if ( false === strpos( $args['type'], 'kirki-' ) ) {
207
			$args['type'] = 'kirki-' . $args['type'];
208
		}
209
210
		self::$sections[ $id ] = $args;
211
	}
212
213
	/**
214
	 * Remove a section.
215
	 *
216
	 * @static
217
	 * @access public
218
	 * @since 3.0.17
219
	 * @param string $id   The ID for this panel.
220
	 */
221
	public static function remove_section( $id = '' ) {
222
		if ( ! in_array( $id, self::$sections_to_remove, true ) ) {
223
			self::$sections_to_remove[] = $id;
224
		}
225
	}
226
227
	/**
228
	 * Create a new field.
229
	 *
230
	 * @static
231
	 * @access public
232
	 * @param string $config_id The configuration ID for this field.
233
	 * @param array  $args      The field arguments.
234
	 */
235
	public static function add_field( $config_id, $args ) {
236
		if ( doing_action( 'customize_register' ) ) {
237
			_doing_it_wrong( __METHOD__, esc_html__( 'Kirki fields should not be added on customize_register. Please add them directly, or on init.', 'kirki' ), '3.0.10' );
238
		}
239
240
		// Early exit if 'type' is not defined.
241
		if ( ! isset( $args['type'] ) ) {
242
			return;
243
		}
244
245
		// If the field is font-awesome, enqueue the icons on the frontend.
246
		if ( class_exists( 'Kirki_Modules_CSS' ) && ( 'fontawesome' === $args['type'] || 'kirki-fontawesome' === $args['type'] ) ) {
247
			Kirki_Modules_CSS::add_fontawesome_script();
248
		}
249
250
		$str       = str_replace( array( '-', '_' ), ' ', $args['type'] );
251
		$classname = 'Kirki_Field_' . str_replace( ' ', '_', ucwords( $str ) );
252
		if ( class_exists( $classname ) ) {
253
			new $classname( $config_id, $args );
254
			return;
255
		}
256
		if ( false !== strpos( $classname, 'Kirki_Field_Kirki_' ) ) {
257
			$classname = str_replace( 'Kirki_Field_Kirki_', 'Kirki_Field_', $classname );
258
			if ( class_exists( $classname ) ) {
259
				new $classname( $config_id, $args );
260
				return;
261
			}
262
		}
263
		new Kirki_Field( $config_id, $args );
264
	}
265
266
	/**
267
	 * Remove a control.
268
	 *
269
	 * @static
270
	 * @access public
271
	 * @since 3.0.17
272
	 * @param string $id The field ID.
273
	 */
274
	public static function remove_control( $id ) {
275
		if ( ! in_array( $id, self::$controls_to_remove, true ) ) {
276
			self::$controls_to_remove[] = $id;
277
		}
278
	}
279
280
	/**
281
	 * Gets a parameter for a config-id.
282
	 *
283
	 * @static
284
	 * @access public
285
	 * @since 3.0.10
286
	 * @param string $id    The config-ID.
287
	 * @param string $param The parameter we want.
288
	 * @return string
289
	 */
290
	public static function get_config_param( $id, $param ) {
291
		if ( ! isset( self::$config[ $id ] ) || ! isset( self::$config[ $id ][ $param ] ) ) {
292
			return '';
293
		}
294
		return self::$config[ $id ][ $param ];
295
	}
296
}
297