Complex classes like Kirki_Init often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Kirki_Init, and based on these observations, apply Extract Interface, too.
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() { |
||
39 | |||
40 | /** |
||
41 | * Properly set the Kirki URL for assets. |
||
42 | * Determines if Kirki is installed as a plugin, in a child theme, or a parent theme |
||
43 | * and then does some calculations to get the proper URL for its CSS & JS assets. |
||
44 | */ |
||
45 | public function set_url() { |
||
92 | |||
93 | /** |
||
94 | * Add the default Kirki control types. |
||
95 | * |
||
96 | * @access public |
||
97 | * @since 3.0.0 |
||
98 | * @param array $control_types The control types array. |
||
99 | * @return array |
||
100 | */ |
||
101 | public function default_control_types( $control_types = array() ) { |
||
102 | |||
103 | $this->control_types = array( |
||
104 | 'checkbox' => 'WP_Customize_Control', |
||
105 | 'kirki-background' => 'Kirki_Control_Background', |
||
106 | 'kirki-code' => 'Kirki_Control_Code', |
||
107 | 'kirki-color' => 'Kirki_Control_Color', |
||
108 | 'kirki-color-palette' => 'Kirki_Control_Color_Palette', |
||
109 | 'kirki-custom' => 'Kirki_Control_Custom', |
||
110 | 'kirki-date' => 'Kirki_Control_Date', |
||
111 | 'kirki-dashicons' => 'Kirki_Control_Dashicons', |
||
112 | 'kirki-dimension' => 'Kirki_Control_Dimension', |
||
113 | 'kirki-dimensions' => 'Kirki_Control_Dimensions', |
||
114 | 'kirki-editor' => 'Kirki_Control_Editor', |
||
115 | 'kirki-fontawesome' => 'Kirki_Control_FontAwesome', |
||
116 | 'kirki-gradient' => 'Kirki_Control_Gradient', |
||
117 | 'kirki-image-array' => 'Kirki_Control_Image_Array', |
||
118 | 'kirki-multicolor' => 'Kirki_Control_Multicolor', |
||
119 | 'kirki-multicheck' => 'Kirki_Control_MultiCheck', |
||
120 | 'kirki-number' => 'Kirki_Control_Number', |
||
121 | 'kirki-palette' => 'Kirki_Control_Palette', |
||
122 | 'kirki-preset' => 'Kirki_Control_Preset', |
||
123 | 'kirki-radio' => 'Kirki_Control_Radio', |
||
124 | 'kirki-radio-buttonset' => 'Kirki_Control_Radio_ButtonSet', |
||
125 | 'kirki-radio-image' => 'Kirki_Control_Radio_Image', |
||
126 | 'repeater' => 'Kirki_Control_Repeater', |
||
127 | 'kirki-select' => 'Kirki_Control_Select', |
||
128 | 'kirki-slider' => 'Kirki_Control_Slider', |
||
129 | 'kirki-sortable' => 'Kirki_Control_Sortable', |
||
130 | 'kirki-spacing' => 'Kirki_Control_Dimensions', |
||
131 | 'kirki-switch' => 'Kirki_Control_Switch', |
||
132 | 'kirki-generic' => 'Kirki_Control_Generic', |
||
133 | 'kirki-toggle' => 'Kirki_Control_Toggle', |
||
134 | 'kirki-typography' => 'Kirki_Control_Typography', |
||
135 | 'image' => 'WP_Customize_Image_Control', |
||
136 | 'cropped_image' => 'WP_Customize_Cropped_Image_Control', |
||
137 | 'upload' => 'WP_Customize_Upload_Control', |
||
138 | ); |
||
139 | return array_merge( $control_types, $this->control_types ); |
||
140 | |||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Helper function that adds the fields, sections and panels to the customizer. |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | public function add_to_customizer() { |
||
149 | $this->fields_from_filters(); |
||
150 | add_action( 'customize_register', array( $this, 'register_control_types' ) ); |
||
151 | add_action( 'customize_register', array( $this, 'add_panels' ), 97 ); |
||
152 | add_action( 'customize_register', array( $this, 'add_sections' ), 98 ); |
||
153 | add_action( 'customize_register', array( $this, 'add_fields' ), 99 ); |
||
154 | /* Kirki_Modules_Loading::get_instance(); */ |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Register control types |
||
159 | * |
||
160 | * @return void |
||
161 | */ |
||
162 | public function register_control_types() { |
||
163 | global $wp_customize; |
||
164 | |||
165 | $section_types = apply_filters( 'kirki/section_types', array() ); |
||
166 | foreach ( $section_types as $section_type ) { |
||
167 | $wp_customize->register_section_type( $section_type ); |
||
168 | } |
||
169 | if ( empty( $this->control_types ) ) { |
||
170 | $this->control_types = $this->default_control_types(); |
||
171 | } |
||
172 | $do_not_register_control_types = apply_filters( 'kirki/control_types/exclude', array( |
||
173 | 'Kirki_Control_Repeater', |
||
174 | ) ); |
||
175 | foreach ( $this->control_types as $control_type ) { |
||
176 | if ( 0 === strpos( $control_type, 'Kirki' ) && ! in_array( $control_type, $do_not_register_control_types, true ) && class_exists( $control_type ) ) { |
||
177 | $wp_customize->register_control_type( $control_type ); |
||
178 | } |
||
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Register our panels to the WordPress Customizer. |
||
184 | * |
||
185 | * @access public |
||
186 | */ |
||
187 | public function add_panels() { |
||
188 | if ( ! empty( Kirki::$panels ) ) { |
||
189 | foreach ( Kirki::$panels as $panel_args ) { |
||
190 | // Extra checks for nested panels. |
||
191 | if ( isset( $panel_args['panel'] ) ) { |
||
192 | if ( isset( Kirki::$panels[ $panel_args['panel'] ] ) ) { |
||
193 | // Set the type to nested. |
||
194 | $panel_args['type'] = 'kirki-nested'; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | new Kirki_Panel( $panel_args ); |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Register our sections to the WordPress Customizer. |
||
205 | * |
||
206 | * @var object The WordPress Customizer object |
||
207 | * @return void |
||
208 | */ |
||
209 | public function add_sections() { |
||
210 | if ( ! empty( Kirki::$sections ) ) { |
||
211 | foreach ( Kirki::$sections as $section_args ) { |
||
212 | // Extra checks for nested sections. |
||
213 | if ( isset( $section_args['section'] ) ) { |
||
214 | if ( isset( Kirki::$sections[ $section_args['section'] ] ) ) { |
||
215 | // Set the type to nested. |
||
216 | $section_args['type'] = 'kirki-nested'; |
||
217 | // We need to check if the parent section is nested inside a panel. |
||
218 | $parent_section = Kirki::$sections[ $section_args['section'] ]; |
||
219 | if ( isset( $parent_section['panel'] ) ) { |
||
220 | $section_args['panel'] = $parent_section['panel']; |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | new Kirki_Section( $section_args ); |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Create the settings and controls from the $fields array and register them. |
||
231 | * |
||
232 | * @var object The WordPress Customizer object |
||
233 | * @return void |
||
234 | */ |
||
235 | public function add_fields() { |
||
236 | |||
237 | global $wp_customize; |
||
238 | foreach ( Kirki::$fields as $args ) { |
||
239 | |||
240 | // Create the settings. |
||
241 | new Kirki_Settings( $args ); |
||
242 | |||
243 | // Check if we're on the customizer. |
||
244 | // If we are, then we will create the controls, add the scripts needed for the customizer |
||
245 | // and any other tweaks that this field may require. |
||
246 | if ( $wp_customize ) { |
||
247 | |||
248 | // Create the control. |
||
249 | new Kirki_Control( $args ); |
||
250 | |||
251 | } |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Build the variables. |
||
257 | * |
||
258 | * @return array ('variable-name' => value) |
||
259 | */ |
||
260 | public static function get_variables() { |
||
261 | |||
262 | $variables = array(); |
||
263 | |||
264 | // Loop through all fields. |
||
265 | foreach ( Kirki::$fields as $field ) { |
||
266 | |||
267 | // Check if we have variables for this field. |
||
268 | if ( isset( $field['variables'] ) && $field['variables'] && ! empty( $field['variables'] ) ) { |
||
269 | |||
270 | // Loop through the array of variables. |
||
271 | foreach ( $field['variables'] as $field_variable ) { |
||
272 | |||
273 | // Is the variable ['name'] defined? If yes, then we can proceed. |
||
274 | if ( isset( $field_variable['name'] ) ) { |
||
275 | |||
276 | // Sanitize the variable name. |
||
277 | $variable_name = esc_attr( $field_variable['name'] ); |
||
278 | |||
279 | // Do we have a callback function defined? If not then set $variable_callback to false. |
||
280 | $variable_callback = ( isset( $field_variable['callback'] ) && is_callable( $field_variable['callback'] ) ) ? $field_variable['callback'] : false; |
||
281 | |||
282 | // If we have a variable_callback defined then get the value of the option |
||
283 | // and run it through the callback function. |
||
284 | // If no callback is defined (false) then just get the value. |
||
285 | if ( $variable_callback ) { |
||
286 | $variables[ $variable_name ] = call_user_func( $field_variable['callback'], Kirki::get_option( $field['settings'] ) ); |
||
287 | } else { |
||
288 | $variables[ $variable_name ] = Kirki::get_option( $field['settings'] ); |
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | } |
||
294 | |||
295 | // Pass the variables through a filter ('kirki/variable') and return the array of variables. |
||
296 | return apply_filters( 'kirki/variable', $variables ); |
||
297 | |||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Process fields added using the 'kirki/fields' and 'kirki/controls' filter. |
||
302 | * These filters are no longer used, this is simply for backwards-compatibility. |
||
303 | */ |
||
304 | public function fields_from_filters() { |
||
305 | |||
306 | $fields = apply_filters( 'kirki/controls', array() ); |
||
307 | $fields = apply_filters( 'kirki/fields', $fields ); |
||
308 | |||
309 | if ( ! empty( $fields ) ) { |
||
310 | foreach ( $fields as $field ) { |
||
311 | Kirki::add_field( 'global', $field ); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Changes select2 version in ACF. |
||
319 | * Fixes a plugin conflict that was causing select fields to crash |
||
320 | * because of a version mismatch between ACF's and Kirki's select2 scripts. |
||
321 | * Props @hellor0bot |
||
322 | * |
||
323 | * @see https://github.com/aristath/kirki/issues/1302 |
||
324 | * @access public |
||
325 | * @since 3.0.0 |
||
326 | * @param string $ver The Select2 script version. |
||
327 | * @return int |
||
|
|||
328 | */ |
||
329 | public function acf_pro_compatibility( $ver ) { |
||
330 | if ( is_customize_preview() ) { |
||
331 | add_filter( 'acf/settings/enqueue_select2', '__return_false', 99 ); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Determine if Kirki is installed as a plugin. |
||
337 | * |
||
338 | * @static |
||
339 | * @access public |
||
340 | * @since 3.0.0 |
||
341 | * @return bool |
||
342 | */ |
||
343 | public static function is_plugin() { |
||
379 | } |
||
380 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.