Total Complexity | 38 |
Total Lines | 305 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
16 | class Kirki_Init { |
||
17 | |||
18 | /** |
||
19 | * Control types. |
||
20 | * |
||
21 | * @access private |
||
22 | * @since 3.0.0 |
||
23 | * @var array |
||
24 | */ |
||
25 | private $control_types = array(); |
||
26 | |||
27 | /** |
||
28 | * The class constructor. |
||
29 | */ |
||
30 | public function __construct() { |
||
31 | |||
32 | self::set_url(); |
||
33 | add_action( 'after_setup_theme', array( $this, 'set_url' ) ); |
||
1 ignored issue
–
show
|
|||
34 | add_action( 'wp_loaded', array( $this, 'add_to_customizer' ), 1 ); |
||
35 | add_filter( 'kirki_control_types', array( $this, 'default_control_types' ) ); |
||
1 ignored issue
–
show
|
|||
36 | |||
37 | add_action( 'customize_register', array( $this, 'remove_panels' ), 99999 ); |
||
38 | add_action( 'customize_register', array( $this, 'remove_sections' ), 99999 ); |
||
39 | add_action( 'customize_register', array( $this, 'remove_controls' ), 99999 ); |
||
40 | |||
41 | new Kirki_Values(); |
||
42 | new Kirki_Sections(); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Properly set the Kirki URL for assets. |
||
47 | * |
||
48 | * @static |
||
49 | * @access public |
||
50 | */ |
||
51 | public static function set_url() { |
||
52 | |||
53 | if ( Kirki_Util::is_plugin() ) { |
||
54 | return; |
||
55 | } |
||
56 | |||
57 | // Get correct URL and path to wp-content. |
||
58 | $content_url = untrailingslashit( dirname( dirname( get_stylesheet_directory_uri() ) ) ); |
||
2 ignored issues
–
show
|
|||
59 | $content_dir = wp_normalize_path( untrailingslashit( WP_CONTENT_DIR ) ); |
||
2 ignored issues
–
show
|
|||
60 | |||
61 | Kirki::$url = str_replace( $content_dir, $content_url, wp_normalize_path( Kirki::$path ) ); |
||
62 | |||
63 | // Apply the kirki_config filter. |
||
64 | $config = apply_filters( 'kirki_config', array() ); |
||
1 ignored issue
–
show
|
|||
65 | if ( isset( $config['url_path'] ) ) { |
||
66 | Kirki::$url = $config['url_path']; |
||
67 | } |
||
68 | |||
69 | // Make sure the right protocol is used. |
||
70 | Kirki::$url = set_url_scheme( Kirki::$url ); |
||
1 ignored issue
–
show
|
|||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Add the default Kirki control types. |
||
75 | * |
||
76 | * @access public |
||
77 | * @since 3.0.0 |
||
78 | * @param array $control_types The control types array. |
||
79 | * @return array |
||
80 | */ |
||
81 | public function default_control_types( $control_types = array() ) { |
||
119 | |||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Helper function that adds the fields, sections and panels to the customizer. |
||
124 | */ |
||
125 | public function add_to_customizer() { |
||
126 | $this->fields_from_filters(); |
||
127 | add_action( 'customize_register', array( $this, 'register_control_types' ) ); |
||
1 ignored issue
–
show
|
|||
128 | add_action( 'customize_register', array( $this, 'add_panels' ), 97 ); |
||
129 | add_action( 'customize_register', array( $this, 'add_sections' ), 98 ); |
||
130 | add_action( 'customize_register', array( $this, 'add_fields' ), 99 ); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Register control types |
||
135 | */ |
||
136 | public function register_control_types() { |
||
137 | global $wp_customize; |
||
138 | |||
139 | $section_types = apply_filters( 'kirki_section_types', array() ); |
||
140 | foreach ( $section_types as $section_type ) { |
||
141 | $wp_customize->register_section_type( $section_type ); |
||
142 | } |
||
143 | |||
144 | $this->control_types = $this->default_control_types(); |
||
145 | foreach ( $this->control_types as $key => $classname ) { |
||
146 | if ( ! class_exists( $classname ) ) { |
||
147 | unset( $this->control_types[ $key ] ); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | $skip_control_types = apply_filters( |
||
1 ignored issue
–
show
|
|||
152 | 'kirki_control_types_exclude', array( |
||
153 | 'Kirki_Control_Repeater', |
||
154 | 'WP_Customize_Control', |
||
155 | ) |
||
156 | ); |
||
157 | |||
158 | foreach ( $this->control_types as $control_type ) { |
||
159 | if ( ! in_array( $control_type, $skip_control_types, true ) && class_exists( $control_type ) ) { |
||
160 | $wp_customize->register_control_type( $control_type ); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Register our panels to the WordPress Customizer. |
||
167 | * |
||
168 | * @access public |
||
169 | */ |
||
170 | public function add_panels() { |
||
171 | if ( ! empty( Kirki::$panels ) ) { |
||
172 | foreach ( Kirki::$panels as $panel_args ) { |
||
173 | // Extra checks for nested panels. |
||
174 | if ( isset( $panel_args['panel'] ) ) { |
||
175 | if ( isset( Kirki::$panels[ $panel_args['panel'] ] ) ) { |
||
176 | // Set the type to nested. |
||
177 | $panel_args['type'] = 'kirki-nested'; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | new Kirki_Panel( $panel_args ); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Register our sections to the WordPress Customizer. |
||
188 | * |
||
189 | * @var object The WordPress Customizer object |
||
190 | */ |
||
191 | public function add_sections() { |
||
192 | if ( ! empty( Kirki::$sections ) ) { |
||
193 | foreach ( Kirki::$sections as $section_args ) { |
||
194 | // Extra checks for nested sections. |
||
195 | if ( isset( $section_args['section'] ) ) { |
||
196 | if ( isset( Kirki::$sections[ $section_args['section'] ] ) ) { |
||
197 | // Set the type to nested. |
||
198 | $section_args['type'] = 'kirki-nested'; |
||
199 | // We need to check if the parent section is nested inside a panel. |
||
200 | $parent_section = Kirki::$sections[ $section_args['section'] ]; |
||
201 | if ( isset( $parent_section['panel'] ) ) { |
||
202 | $section_args['panel'] = $parent_section['panel']; |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | new Kirki_Section( $section_args ); |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Create the settings and controls from the $fields array and register them. |
||
213 | * |
||
214 | * @var object The WordPress Customizer object. |
||
215 | */ |
||
216 | public function add_fields() { |
||
217 | |||
218 | global $wp_customize; |
||
219 | foreach ( Kirki::$fields as $args ) { |
||
220 | |||
221 | // Create the settings. |
||
222 | new Kirki_Settings( $args ); |
||
223 | |||
224 | // Check if we're on the customizer. |
||
225 | // If we are, then we will create the controls, add the scripts needed for the customizer |
||
226 | // and any other tweaks that this field may require. |
||
227 | if ( $wp_customize ) { |
||
228 | |||
229 | // Create the control. |
||
230 | new Kirki_Control( $args ); |
||
231 | |||
232 | } |
||
233 | } |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Process fields added using the 'kirki_fields' and 'kirki_controls' filter. |
||
238 | * These filters are no longer used, this is simply for backwards-compatibility. |
||
239 | * |
||
240 | * @access private |
||
241 | * @since 2.0.0 |
||
242 | */ |
||
243 | private function fields_from_filters() { |
||
244 | |||
245 | $fields = apply_filters( 'kirki_controls', array() ); |
||
1 ignored issue
–
show
|
|||
246 | $fields = apply_filters( 'kirki_fields', $fields ); |
||
247 | |||
248 | if ( ! empty( $fields ) ) { |
||
249 | foreach ( $fields as $field ) { |
||
250 | Kirki::add_field( 'global', $field ); |
||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Alias for the is_plugin static method in the Kirki_Util class. |
||
257 | * This is here for backwards-compatibility purposes. |
||
258 | * |
||
259 | * @static |
||
260 | * @access public |
||
261 | * @since 3.0.0 |
||
262 | * @return bool |
||
263 | */ |
||
264 | public static function is_plugin() { |
||
265 | // Return result using the Kirki_Util class. |
||
266 | return Kirki_Util::is_plugin(); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Alias for the get_variables static method in the Kirki_Util class. |
||
271 | * This is here for backwards-compatibility purposes. |
||
272 | * |
||
273 | * @static |
||
274 | * @access public |
||
275 | * @since 2.0.0 |
||
276 | * @return array Formatted as array( 'variable-name' => value ). |
||
277 | */ |
||
278 | public static function get_variables() { |
||
279 | // Log error for developers. |
||
280 | _doing_it_wrong( __METHOD__, esc_attr__( 'We detected you\'re using Kirki_Init::get_variables(). Please use Kirki_Util::get_variables() instead.', 'kirki' ), '3.0.10' ); |
||
2 ignored issues
–
show
|
|||
281 | // Return result using the Kirki_Util class. |
||
282 | return Kirki_Util::get_variables(); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Remove panels. |
||
287 | * |
||
288 | * @since 3.0.17 |
||
289 | * @param object $wp_customize The customizer object. |
||
290 | * @return void |
||
291 | */ |
||
292 | public function remove_panels( $wp_customize ) { |
||
293 | foreach ( Kirki::$panels_to_remove as $panel ) { |
||
294 | $wp_customize->remove_panel( $panel ); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Remove sections. |
||
300 | * |
||
301 | * @since 3.0.17 |
||
302 | * @param object $wp_customize The customizer object. |
||
303 | * @return void |
||
304 | */ |
||
305 | public function remove_sections( $wp_customize ) { |
||
306 | foreach ( Kirki::$sections_to_remove as $section ) { |
||
307 | $wp_customize->remove_section( $section ); |
||
308 | } |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Remove controls. |
||
313 | * |
||
314 | * @since 3.0.17 |
||
315 | * @param object $wp_customize The customizer object. |
||
316 | * @return void |
||
317 | */ |
||
318 | public function remove_controls( $wp_customize ) { |
||
321 | } |
||
322 | } |
||
323 | } |
||
324 |