Total Complexity | 41 |
Total Lines | 448 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Kirki_Control_Repeater 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.
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_Control_Repeater, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Kirki_Control_Repeater extends Kirki_Control_Base { |
||
21 | |||
22 | /** |
||
23 | * The control type. |
||
24 | * |
||
25 | * @access public |
||
26 | * @var string |
||
27 | */ |
||
28 | public $type = 'repeater'; |
||
29 | |||
30 | /** |
||
31 | * The fields that each container row will contain. |
||
32 | * |
||
33 | * @access public |
||
34 | * @var array |
||
35 | */ |
||
36 | public $fields = array(); |
||
37 | |||
38 | /** |
||
39 | * Will store a filtered version of value for advenced fields (like images). |
||
40 | * |
||
41 | * @access protected |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $filtered_value = array(); |
||
45 | |||
46 | /** |
||
47 | * The row label |
||
48 | * |
||
49 | * @access public |
||
50 | * @var array |
||
51 | */ |
||
52 | public $row_label = array(); |
||
53 | |||
54 | /** |
||
55 | * Constructor. |
||
56 | * Supplied `$args` override class property defaults. |
||
57 | * If `$args['settings']` is not defined, use the $id as the setting ID. |
||
58 | * |
||
59 | * @param WP_Customize_Manager $manager Customizer bootstrap instance. |
||
1 ignored issue
–
show
|
|||
60 | * @param string $id Control ID. |
||
61 | * @param array $args {@see WP_Customize_Control::__construct}. |
||
62 | */ |
||
63 | public function __construct( $manager, $id, $args = array() ) { |
||
64 | |||
65 | parent::__construct( $manager, $id, $args ); |
||
66 | |||
67 | // Set up defaults for row labels. |
||
68 | $this->row_label = array( |
||
69 | 'type' => 'text', |
||
70 | 'value' => esc_attr__( 'row', 'kirki' ), |
||
1 ignored issue
–
show
|
|||
71 | 'field' => false, |
||
72 | ); |
||
73 | |||
74 | // Validate row-labels. |
||
75 | $this->row_label( $args ); |
||
76 | |||
77 | if ( empty( $this->button_label ) ) { |
||
78 | /* translators: %s represents the label of the row. */ |
||
79 | $this->button_label = sprintf( esc_attr__( 'Add new %s', 'kirki' ), $this->row_label['value'] ); |
||
1 ignored issue
–
show
|
|||
80 | } |
||
81 | |||
82 | if ( empty( $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
||
83 | $args['fields'] = array(); |
||
84 | } |
||
85 | |||
86 | // An array to store keys of fields that need to be filtered. |
||
87 | $media_fields_to_filter = array(); |
||
88 | |||
89 | foreach ( $args['fields'] as $key => $value ) { |
||
90 | if ( ! isset( $value['default'] ) ) { |
||
91 | $args['fields'][ $key ]['default'] = ''; |
||
92 | } |
||
93 | if ( ! isset( $value['label'] ) ) { |
||
94 | $args['fields'][ $key ]['label'] = ''; |
||
95 | } |
||
96 | $args['fields'][ $key ]['id'] = $key; |
||
97 | |||
98 | // We check if the filed is an uploaded media ( image , file, video, etc.. ). |
||
99 | if ( isset( $value['type'] ) ) { |
||
100 | switch ( $value['type'] ) { |
||
101 | case 'image': |
||
102 | case 'cropped_image': |
||
103 | case 'upload': |
||
104 | // We add it to the list of fields that need some extra filtering/processing. |
||
105 | $media_fields_to_filter[ $key ] = true; |
||
106 | break; |
||
107 | |||
108 | case 'dropdown-pages': |
||
109 | // If the field is a dropdown-pages field then add it to args. |
||
110 | $dropdown = wp_dropdown_pages( |
||
1 ignored issue
–
show
|
|||
111 | array( |
||
112 | 'name' => '', |
||
113 | 'echo' => 0, |
||
114 | 'show_option_none' => esc_attr__( 'Select a Page', 'kirki' ), |
||
115 | 'option_none_value' => '0', |
||
116 | 'selected' => '', |
||
117 | ) |
||
118 | ); |
||
119 | // Hackily add in the data link parameter. |
||
120 | $dropdown = str_replace( '<select', '<select data-field="' . esc_attr( $args['fields'][ $key ]['id'] ) . '"' . $this->get_link(), $dropdown ); // phpcs:ignore Generic.Formatting.MultipleStatementAlignment.NotSameWarning |
||
1 ignored issue
–
show
|
|||
121 | $args['fields'][ $key ]['dropdown'] = $dropdown; |
||
122 | break; |
||
123 | } |
||
124 | } |
||
125 | } // End foreach(). |
||
126 | |||
127 | $this->fields = $args['fields']; |
||
128 | |||
129 | // Now we are going to filter the fields. |
||
130 | // First we create a copy of the value that would be used otherwise. |
||
131 | $this->filtered_value = $this->value(); |
||
132 | |||
133 | if ( is_array( $this->filtered_value ) && ! empty( $this->filtered_value ) ) { |
||
134 | |||
135 | // We iterate over the list of fields. |
||
136 | foreach ( $this->filtered_value as &$filtered_value_field ) { |
||
137 | |||
138 | if ( is_array( $filtered_value_field ) && ! empty( $filtered_value_field ) ) { |
||
139 | |||
140 | // We iterate over the list of properties for this field. |
||
141 | foreach ( $filtered_value_field as $key => &$value ) { |
||
142 | |||
143 | // We check if this field was marked as requiring extra filtering (in this case image, cropped_images, upload). |
||
144 | if ( array_key_exists( $key, $media_fields_to_filter ) ) { |
||
145 | |||
146 | // What follows was made this way to preserve backward compatibility. |
||
147 | // The repeater control use to store the URL for images instead of the attachment ID. |
||
148 | // We check if the value look like an ID (otherwise it's probably a URL so don't filter it). |
||
149 | if ( is_numeric( $value ) ) { |
||
150 | |||
151 | // "sanitize" the value. |
||
152 | $attachment_id = (int) $value; |
||
153 | |||
154 | // Try to get the attachment_url. |
||
155 | $url = wp_get_attachment_url( $attachment_id ); |
||
1 ignored issue
–
show
|
|||
156 | |||
157 | $filename = basename( get_attached_file( $attachment_id ) ); |
||
1 ignored issue
–
show
|
|||
158 | |||
159 | // If we got a URL. |
||
160 | if ( $url ) { |
||
161 | |||
162 | // 'id' is needed for form hidden value, URL is needed to display the image. |
||
163 | $value = array( |
||
164 | 'id' => $attachment_id, |
||
165 | 'url' => $url, |
||
166 | 'filename' => $filename, |
||
167 | ); |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | } // End foreach(). |
||
174 | } // End if(). |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Refresh the parameters passed to the JavaScript via JSON. |
||
179 | * |
||
180 | * @access public |
||
181 | */ |
||
182 | public function to_json() { |
||
183 | parent::to_json(); |
||
184 | |||
185 | $fields = $this->fields; |
||
186 | |||
187 | $this->json['fields'] = $fields; |
||
1 ignored issue
–
show
|
|||
188 | $this->json['row_label'] = $this->row_label; |
||
189 | |||
190 | // If filtered_value has been set and is not empty we use it instead of the actual value. |
||
191 | if ( is_array( $this->filtered_value ) && ! empty( $this->filtered_value ) ) { |
||
192 | $this->json['value'] = $this->filtered_value; |
||
193 | } |
||
194 | $this->json['value'] = apply_filters( "kirki_controls_repeater_value_{$this->id}", $this->json['value'] ); |
||
1 ignored issue
–
show
|
|||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Render the control's content. |
||
199 | * Allows the content to be overriden without having to rewrite the wrapper in $this->render(). |
||
200 | * |
||
201 | * @access protected |
||
202 | */ |
||
203 | protected function render_content() { |
||
227 | |||
228 | } |
||
229 | |||
230 | /** |
||
231 | * An Underscore (JS) template for this control's content (but not its container). |
||
232 | * Class variables for this control class are available in the `data` JS object. |
||
233 | * |
||
234 | * @access public |
||
235 | */ |
||
236 | public function repeater_js_template() { |
||
237 | ?> |
||
238 | <script type="text/html" class="customize-control-repeater-content"> |
||
239 | <# var field; var index = data.index; #> |
||
240 | |||
241 | <li class="repeater-row minimized" data-row="{{{ index }}}"> |
||
242 | |||
243 | <div class="repeater-row-header"> |
||
244 | <span class="repeater-row-label"></span> |
||
245 | <i class="dashicons dashicons-arrow-down repeater-minimize"></i> |
||
246 | </div> |
||
247 | <div class="repeater-row-content"> |
||
248 | <# _.each( data, function( field, i ) { #> |
||
249 | |||
250 | <div class="repeater-field repeater-field-{{{ field.type }}}"> |
||
251 | |||
252 | <# if ( 'text' === field.type || 'url' === field.type || 'link' === field.type || 'email' === field.type || 'tel' === field.type || 'date' === field.type || 'number' === field.type ) { #> |
||
253 | <# var fieldExtras = ''; #> |
||
254 | <# if ( 'link' === field.type ) { #> |
||
255 | <# field.type = 'url' #> |
||
256 | <# } #> |
||
257 | |||
258 | <# if ( 'number' === field.type ) { #> |
||
259 | <# if ( ! _.isUndefined( field.choices ) && ! _.isUndefined( field.choices.min ) ) { #> |
||
260 | <# fieldExtras += ' min="' + field.choices.min + '"'; #> |
||
261 | <# } #> |
||
262 | <# if ( ! _.isUndefined( field.choices ) && ! _.isUndefined( field.choices.max ) ) { #> |
||
263 | <# fieldExtras += ' max="' + field.choices.max + '"'; #> |
||
264 | <# } #> |
||
265 | <# if ( ! _.isUndefined( field.choices ) && ! _.isUndefined( field.choices.step ) ) { #> |
||
266 | <# fieldExtras += ' step="' + field.choices.step + '"'; #> |
||
267 | <# } #> |
||
268 | <# } #> |
||
269 | |||
270 | <label> |
||
271 | <# if ( field.label ) { #><span class="customize-control-title">{{{ field.label }}}</span><# } #> |
||
272 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
273 | <input type="{{field.type}}" name="" value="{{{ field.default }}}" data-field="{{{ field.id }}}"{{ fieldExtras }}> |
||
274 | </label> |
||
275 | |||
276 | <# } else if ( 'number' === field.type ) { #> |
||
277 | |||
278 | <label> |
||
279 | <# if ( field.label ) { #><span class="customize-control-title">{{{ field.label }}}</span><# } #> |
||
280 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
281 | <input type="{{ field.type }}" name="" value="{{{ field.default }}}" data-field="{{{ field.id }}}"{{ numberFieldExtras }}> |
||
282 | </label> |
||
283 | |||
284 | <# } else if ( 'hidden' === field.type ) { #> |
||
285 | |||
286 | <input type="hidden" data-field="{{{ field.id }}}" <# if ( field.default ) { #> value="{{{ field.default }}}" <# } #> /> |
||
287 | |||
288 | <# } else if ( 'checkbox' === field.type ) { #> |
||
289 | |||
290 | <label> |
||
291 | <input type="checkbox" value="{{{ field.default }}}" data-field="{{{ field.id }}}" <# if ( field.default ) { #> checked="checked" <# } #> /> {{{ field.label }}} |
||
292 | <# if ( field.description ) { #>{{{ field.description }}}<# } #> |
||
293 | </label> |
||
294 | |||
295 | <# } else if ( 'select' === field.type ) { #> |
||
296 | |||
297 | <label> |
||
298 | <# if ( field.label ) { #><span class="customize-control-title">{{{ field.label }}}</span><# } #> |
||
299 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
300 | <select data-field="{{{ field.id }}}"<# if ( ! _.isUndefined( field.multiple ) && false !== field.multiple ) { #> multiple="multiple" data-multiple="{{ field.multiple }}"<# } #>> |
||
301 | <# _.each( field.choices, function( choice, i ) { #> |
||
302 | <option value="{{{ i }}}" <# if ( field.default == i ) { #> selected="selected" <# } #>>{{ choice }}</option> |
||
303 | <# }); #> |
||
304 | </select> |
||
305 | </label> |
||
306 | |||
307 | <# } else if ( 'dropdown-pages' === field.type ) { #> |
||
308 | |||
309 | <label> |
||
310 | <# if ( field.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #> |
||
311 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
312 | <div class="customize-control-content repeater-dropdown-pages">{{{ field.dropdown }}}</div> |
||
313 | </label> |
||
314 | |||
315 | <# } else if ( 'radio' === field.type ) { #> |
||
316 | |||
317 | <label> |
||
318 | <# if ( field.label ) { #><span class="customize-control-title">{{{ field.label }}}</span><# } #> |
||
319 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
320 | |||
321 | <# _.each( field.choices, function( choice, i ) { #> |
||
322 | <label><input type="radio" name="{{{ field.id }}}{{ index }}" data-field="{{{ field.id }}}" value="{{{ i }}}" <# if ( field.default == i ) { #> checked="checked" <# } #>> {{ choice }} <br/></label> |
||
323 | <# }); #> |
||
324 | </label> |
||
325 | |||
326 | <# } else if ( 'radio-image' === field.type ) { #> |
||
327 | |||
328 | <label> |
||
329 | <# if ( field.label ) { #><span class="customize-control-title">{{{ field.label }}}</span><# } #> |
||
330 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
331 | |||
332 | <# _.each( field.choices, function( choice, i ) { #> |
||
333 | <input type="radio" id="{{{ field.id }}}_{{ index }}_{{{ i }}}" name="{{{ field.id }}}{{ index }}" data-field="{{{ field.id }}}" value="{{{ i }}}" <# if ( field.default == i ) { #> checked="checked" <# } #>> |
||
334 | <label for="{{{ field.id }}}_{{ index }}_{{{ i }}}"><img src="{{ choice }}"></label> |
||
335 | </input> |
||
336 | <# }); #> |
||
337 | </label> |
||
338 | |||
339 | <# } else if ( 'color' === field.type ) { #> |
||
340 | |||
341 | <# var defaultValue = ''; |
||
342 | if ( field.default ) { |
||
343 | defaultValue = ( '#' !== field.default.substring( 0, 1 ) ) ? '#' + field.default : field.default; |
||
344 | defaultValue = ' data-default-color=' + defaultValue; // Quotes added automatically. |
||
345 | } #> |
||
346 | <label> |
||
347 | <# if ( field.label ) { #><span class="customize-control-title">{{{ field.label }}}</span><# } #> |
||
348 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
349 | <input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value', 'kirki' ); ?>" value="{{{ field.default }}}" data-field="{{{ field.id }}}" {{ defaultValue }} /> |
||
1 ignored issue
–
show
|
|||
350 | |||
351 | </label> |
||
352 | |||
353 | <# } else if ( 'textarea' === field.type ) { #> |
||
354 | |||
355 | <# if ( field.label ) { #><span class="customize-control-title">{{{ field.label }}}</span><# } #> |
||
356 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
357 | <textarea rows="5" data-field="{{{ field.id }}}">{{ field.default }}</textarea> |
||
358 | |||
359 | <# } else if ( field.type === 'image' || field.type === 'cropped_image' ) { #> |
||
360 | |||
361 | <label> |
||
362 | <# if ( field.label ) { #><span class="customize-control-title">{{{ field.label }}}</span><# } #> |
||
363 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
364 | </label> |
||
365 | |||
366 | <figure class="kirki-image-attachment" data-placeholder="<?php esc_attr_e( 'No Image Selected', 'kirki' ); ?>" > |
||
367 | <# if ( field.default ) { #> |
||
368 | <# var defaultImageURL = ( field.default.url ) ? field.default.url : field.default; #> |
||
369 | <img src="{{{ defaultImageURL }}}"> |
||
370 | <# } else { #> |
||
371 | <?php esc_attr_e( 'No Image Selected', 'kirki' ); ?> |
||
372 | <# } #> |
||
373 | </figure> |
||
374 | |||
375 | <div class="actions"> |
||
376 | <button type="button" class="button remove-button<# if ( ! field.default ) { #> hidden<# } #>"><?php esc_attr_e( 'Remove', 'kirki' ); ?></button> |
||
377 | <button type="button" class="button upload-button" data-label=" <?php esc_attr_e( 'Add Image', 'kirki' ); ?>" data-alt-label="<?php echo esc_attr_e( 'Change Image', 'kirki' ); ?>" > |
||
378 | <# if ( field.default ) { #> |
||
379 | <?php esc_attr_e( 'Change Image', 'kirki' ); ?> |
||
380 | <# } else { #> |
||
381 | <?php esc_attr_e( 'Add Image', 'kirki' ); ?> |
||
382 | <# } #> |
||
383 | </button> |
||
384 | <# if ( field.default.id ) { #> |
||
385 | <input type="hidden" class="hidden-field" value="{{{ field.default.id }}}" data-field="{{{ field.id }}}" > |
||
386 | <# } else { #> |
||
387 | <input type="hidden" class="hidden-field" value="{{{ field.default }}}" data-field="{{{ field.id }}}" > |
||
388 | <# } #> |
||
389 | </div> |
||
390 | |||
391 | <# } else if ( field.type === 'upload' ) { #> |
||
392 | |||
393 | <label> |
||
394 | <# if ( field.label ) { #><span class="customize-control-title">{{{ field.label }}}</span><# } #> |
||
395 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
396 | </label> |
||
397 | |||
398 | <figure class="kirki-file-attachment" data-placeholder="<?php esc_attr_e( 'No File Selected', 'kirki' ); ?>" > |
||
399 | <# if ( field.default ) { #> |
||
400 | <# var defaultFilename = ( field.default.filename ) ? field.default.filename : field.default; #> |
||
401 | <span class="file"><span class="dashicons dashicons-media-default"></span> {{ defaultFilename }}</span> |
||
402 | <# } else { #> |
||
403 | <?php esc_attr_e( 'No File Selected', 'kirki' ); ?> |
||
404 | <# } #> |
||
405 | </figure> |
||
406 | |||
407 | <div class="actions"> |
||
408 | <button type="button" class="button remove-button<# if ( ! field.default ) { #> hidden<# } #>"></button> |
||
409 | <button type="button" class="button upload-button" data-label="<?php esc_attr_e( 'Add File', 'kirki' ); ?>" data-alt-label="<?php esc_attr_e( 'Change File', 'kirki' ); ?>"> |
||
410 | <# if ( field.default ) { #> |
||
411 | <?php esc_attr_e( 'Change File', 'kirki' ); ?> |
||
412 | <# } else { #> |
||
413 | <?php esc_attr_e( 'Add File', 'kirki' ); ?> |
||
414 | <# } #> |
||
415 | </button> |
||
416 | <# if ( field.default.id ) { #> |
||
417 | <input type="hidden" class="hidden-field" value="{{{ field.default.id }}}" data-field="{{{ field.id }}}" > |
||
418 | <# } else { #> |
||
419 | <input type="hidden" class="hidden-field" value="{{{ field.default }}}" data-field="{{{ field.id }}}" > |
||
420 | <# } #> |
||
421 | </div> |
||
422 | |||
423 | <# } else if ( 'custom' === field.type ) { #> |
||
424 | |||
425 | <# if ( field.label ) { #><span class="customize-control-title">{{{ field.label }}}</span><# } #> |
||
426 | <# if ( field.description ) { #><span class="description customize-control-description">{{{ field.description }}}</span><# } #> |
||
427 | <div data-field="{{{ field.id }}}">{{{ field.default }}}</div> |
||
428 | |||
429 | <# } #> |
||
430 | |||
431 | </div> |
||
432 | <# }); #> |
||
433 | <button type="button" class="button-link repeater-row-remove"><?php esc_attr_e( 'Remove', 'kirki' ); ?></button> |
||
434 | </div> |
||
435 | </li> |
||
436 | </script> |
||
437 | <?php |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Validate row-labels. |
||
442 | * |
||
443 | * @access protected |
||
444 | * @since 3.0.0 |
||
445 | * @param array $args {@see WP_Customize_Control::__construct}. |
||
446 | */ |
||
447 | protected function row_label( $args ) { |
||
468 | } |
||
469 | } |
||
470 | } |
||
471 | } |
||
472 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths