Completed
Pull Request — develop (#1677)
by
unknown
02:48
created

Kirki::remove_panel_helper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
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
	 * Modules object.
84
	 *
85
	 * @access public
86
	 * @since 3.0.0
87
	 * @var object
88
	 */
89
	public $modules;
90
91
	/**
92
	 * Panel id to remove.
93
	 *
94
	 * @static
95
	 * @access public
96
	 * @var string
97
	 */
98
	public static $rpanelid;
99
100
	/**
101
	 * Get the value of an option from the db.
102
	 *
103
	 * @static
104
	 * @access public
105
	 * @param string $config_id The ID of the configuration corresponding to this field.
106
	 * @param string $field_id  The field_id (defined as 'settings' in the field arguments).
107
	 * @return mixed The saved value of the field.
108
	 */
109
	public static function get_option( $config_id = '', $field_id = '' ) {
110
111
		return Kirki_Values::get_value( $config_id, $field_id );
112
	}
113
114
	/**
115
	 * Sets the configuration options.
116
	 *
117
	 * @static
118
	 * @access public
119
	 * @param string $config_id The configuration ID.
120
	 * @param array  $args      The configuration options.
121
	 */
122
	public static function add_config( $config_id, $args = array() ) {
123
124
		$config = Kirki_Config::get_instance( $config_id, $args );
125
		$config_args = $config->get_config();
126
		self::$config[ $config_args['id'] ] = $config_args;
127
	}
128
129
	/**
130
	 * Create a new panel.
131
	 *
132
	 * @static
133
	 * @access public
134
	 * @param string $id   The ID for this panel.
135
	 * @param array  $args The panel arguments.
136
	 */
137
	public static function add_panel( $id = '', $args = array() ) {
138
139
		$args['id']          = esc_attr( $id );
140
		$args['description'] = ( isset( $args['description'] ) ) ? esc_textarea( $args['description'] ) : '';
141
		$args['priority']    = ( isset( $args['priority'] ) ) ? esc_attr( $args['priority'] ) : 10;
142
		$args['type']        = ( isset( $args['type'] ) ) ? $args['type'] : 'default';
143
		$args['type']        = 'kirki-' . $args['type'];
144
		if ( ! isset( $args['active_callback'] ) ) {
145
			$args['active_callback'] = ( isset( $args['required'] ) ) ? array( 'Kirki_Active_Callback', 'evaluate' ) : '__return_true';
146
		}
147
148
		self::$panels[ $args['id'] ] = $args;
149
	}
150
151
	/**
152
	 * Remove a panel.
153
	 * 
154
	 * @static
155
	 * @access public
156
	 * @param string $id   The ID for this panel.
157
	 */
158
	public static function remove_panel( $id = '' ) {
159
		self::$rpanelid = $id;
160
		add_action( 'customize_register', array( 'Kirki', 'remove_panel_helper' ), 999, 1 );
161
	}
162
163
	/**
164
	 * Remove a panel helper.
165
	 * 
166
	 * @static
167
	 * @access public
168
	 * @param string $wp_customize   The customize object.
169
	 */
170
	public static function remove_panel_helper( $wp_customize ) {
171
		$wp_customize->remove_panel( self::$rpanelid );
0 ignored issues
show
Bug introduced by
The method remove_panel cannot be called on $wp_customize (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
172
	}
173
174
	/**
175
	 * Create a new section.
176
	 *
177
	 * @static
178
	 * @access public
179
	 * @param string $id   The ID for this section.
180
	 * @param array  $args The section arguments.
181
	 */
182
	public static function add_section( $id, $args ) {
183
184
		$args['id']          = esc_attr( $id );
185
		$args['panel']       = ( isset( $args['panel'] ) ) ? esc_attr( $args['panel'] ) : '';
186
		$args['description'] = ( isset( $args['description'] ) ) ? esc_textarea( $args['description'] ) : '';
187
		$args['priority']    = ( isset( $args['priority'] ) ) ? esc_attr( $args['priority'] ) : 10;
188
		$args['type']        = ( isset( $args['type'] ) ) ? $args['type'] : 'default';
189
		$args['type']        = 'kirki-' . $args['type'];
190
		if ( ! isset( $args['active_callback'] ) ) {
191
			$args['active_callback'] = ( isset( $args['required'] ) ) ? array( 'Kirki_Active_Callback', 'evaluate' ) : '__return_true';
192
		}
193
194
		self::$sections[ $args['id'] ] = $args;
195
	}
196
197
	/**
198
	 * Create a new field.
199
	 *
200
	 * @static
201
	 * @access public
202
	 * @param string $config_id The configuration ID for this field.
203
	 * @param array  $args      The field arguments.
204
	 */
205
	public static function add_field( $config_id, $args ) {
206
207
		if ( doing_action( 'customize_register' ) ) {
208
			_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' );
209
		}
210
211
		// Early exit if 'type' is not defined.
212
		if ( ! isset( $args['type'] ) ) {
213
			return;
214
		}
215
216
		$str = str_replace( array( '-', '_' ), ' ', $args['type'] );
217
		$classname = 'Kirki_Field_' . str_replace( ' ', '_', ucwords( $str ) );
218
		if ( class_exists( $classname ) ) {
219
			new $classname( $config_id, $args );
220
			return;
221
		}
222
		if ( false !== strpos( $classname, 'Kirki_Field_Kirki_' ) ) {
223
			$classname = str_replace( 'Kirki_Field_Kirki_', 'Kirki_Field_', $classname );
224
			if ( class_exists( $classname ) ) {
225
				new $classname( $config_id, $args );
226
				return;
227
			}
228
		}
229
230
		new Kirki_Field( $config_id, $args );
231
232
	}
233
234
	/**
235
	 * Gets a parameter for a config-id.
236
	 *
237
	 * @static
238
	 * @access public
239
	 * @since 3.0.10
240
	 * @param string $id    The config-ID.
241
	 * @param string $param The parameter we want.
242
	 * @return string
243
	 */
244
	public static function get_config_param( $id, $param ) {
245
246
		if ( ! isset( self::$config[ $id ] ) || ! isset( self::$config[ $id ][ $param ] ) ) {
247
			return '';
248
		}
249
		return self::$config[ $id ][ $param ];
250
	}
251
}
252