Conditions | 35 |
Paths | > 20000 |
Total Lines | 285 |
Lines | 105 |
Ratio | 36.84 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
94 | function render_field( $field ) { |
||
95 | |||
96 | // vars |
||
97 | $div = array( |
||
98 | 'class' => 'acf-repeater', |
||
99 | 'data-min' => $field['min'], |
||
100 | 'data-max' => $field['max'] |
||
101 | ); |
||
102 | |||
103 | |||
104 | // ensure value is an array |
||
105 | if( empty($field['value']) ) { |
||
106 | |||
107 | $field['value'] = array(); |
||
108 | |||
109 | $div['class'] .= ' -empty'; |
||
110 | |||
111 | } |
||
112 | |||
113 | |||
114 | // rows |
||
115 | $field['min'] = empty($field['min']) ? 0 : $field['min']; |
||
116 | $field['max'] = empty($field['max']) ? 0 : $field['max']; |
||
117 | |||
118 | |||
119 | // populate the empty row data (used for acfcloneindex and min setting) |
||
120 | $empty_row = array(); |
||
121 | |||
122 | |||
123 | // If there are less values than min, populate the extra values |
||
124 | View Code Duplication | if( $field['min'] ) { |
|
125 | |||
126 | for( $i = 0; $i < $field['min']; $i++ ) { |
||
127 | |||
128 | // continue if already have a value |
||
129 | if( array_key_exists($i, $field['value']) ) { |
||
130 | |||
131 | continue; |
||
132 | |||
133 | } |
||
134 | |||
135 | |||
136 | // populate values |
||
137 | $field['value'][ $i ] = $empty_row; |
||
138 | |||
139 | } |
||
140 | |||
141 | } |
||
142 | |||
143 | |||
144 | // If there are more values than man, remove some values |
||
145 | View Code Duplication | if( $field['max'] ) { |
|
146 | |||
147 | for( $i = 0; $i < count($field['value']); $i++ ) { |
||
148 | |||
149 | if( $i >= $field['max'] ) { |
||
150 | |||
151 | unset( $field['value'][ $i ] ); |
||
152 | |||
153 | } |
||
154 | |||
155 | } |
||
156 | |||
157 | } |
||
158 | |||
159 | |||
160 | // setup values for row clone |
||
161 | $field['value']['acfcloneindex'] = $empty_row; |
||
162 | |||
163 | |||
164 | // show columns |
||
165 | $show_order = true; |
||
166 | $show_add = true; |
||
167 | $show_remove = true; |
||
168 | |||
169 | |||
170 | if( $field['max'] ) { |
||
171 | |||
172 | if( $field['max'] == 1 ) { |
||
173 | |||
174 | $show_order = false; |
||
175 | |||
176 | } |
||
177 | |||
178 | if( $field['max'] <= $field['min'] ) { |
||
179 | |||
180 | $show_remove = false; |
||
181 | $show_add = false; |
||
182 | |||
183 | } |
||
184 | |||
185 | } |
||
186 | |||
187 | |||
188 | // field wrap |
||
189 | $el = 'td'; |
||
190 | $before_fields = ''; |
||
191 | $after_fields = ''; |
||
192 | |||
193 | if( $field['layout'] == 'row' ) { |
||
194 | |||
195 | $el = 'div'; |
||
196 | $before_fields = '<td class="acf-fields -left">'; |
||
197 | $after_fields = '</td>'; |
||
198 | |||
199 | } elseif( $field['layout'] == 'block' ) { |
||
200 | |||
201 | $el = 'div'; |
||
202 | |||
203 | $before_fields = '<td class="acf-fields">'; |
||
204 | $after_fields = '</td>'; |
||
205 | |||
206 | } |
||
207 | |||
208 | |||
209 | // layout |
||
210 | $div['class'] .= ' -' . $field['layout']; |
||
211 | |||
212 | |||
213 | // hidden input |
||
214 | acf_hidden_input(array( |
||
215 | 'type' => 'hidden', |
||
216 | 'name' => $field['name'], |
||
217 | )); |
||
218 | |||
219 | |||
220 | // collapsed |
||
221 | $collapsed = array(); |
||
222 | |||
223 | if( $field['collapsed'] ) { |
||
224 | |||
225 | // get user setting |
||
226 | $collapsed = acf_get_user_setting('collapsed_' . $field['key'], ''); |
||
227 | $collapsed = explode(',', $collapsed); |
||
228 | $collapsed = array_filter($collapsed, 'is_numeric'); |
||
229 | |||
230 | |||
231 | // add target class |
||
232 | foreach( array_keys($field['sub_fields']) as $i ) { |
||
233 | |||
234 | if( $field['sub_fields'][ $i ]['key'] === $field['collapsed'] ) { |
||
235 | |||
236 | $field['sub_fields'][ $i ]['wrapper']['class'] .= ' -collapsed-target'; |
||
237 | |||
238 | } |
||
239 | |||
240 | } |
||
241 | |||
242 | } |
||
243 | |||
244 | ?> |
||
245 | <div <?php acf_esc_attr_e($div); ?>> |
||
246 | <table class="acf-table"> |
||
247 | |||
248 | View Code Duplication | <?php if( $field['layout'] == 'table' ): ?> |
|
249 | <thead> |
||
250 | <tr> |
||
251 | <?php if( $show_order ): ?> |
||
252 | <th class="acf-row-handle"><span></span></th> |
||
253 | <?php endif; ?> |
||
254 | |||
255 | <?php foreach( $field['sub_fields'] as $sub_field ): |
||
256 | |||
257 | $atts = array( |
||
258 | 'class' => 'acf-th', |
||
259 | 'data-key' => $sub_field['key'], |
||
260 | ); |
||
261 | |||
262 | |||
263 | // add type |
||
264 | $atts['class'] .= ' acf-th-' . $sub_field['type']; |
||
265 | |||
266 | |||
267 | // Add custom width |
||
268 | if( $sub_field['wrapper']['width'] ) { |
||
269 | |||
270 | $atts['data-width'] = $sub_field['wrapper']['width']; |
||
271 | |||
272 | } |
||
273 | |||
274 | ?> |
||
275 | <th <?php acf_esc_attr_e( $atts ); ?>> |
||
276 | <?php echo acf_get_field_label( $sub_field ); ?> |
||
277 | <?php if( $sub_field['instructions'] ): ?> |
||
278 | <p class="description"><?php echo $sub_field['instructions']; ?></p> |
||
279 | <?php endif; ?> |
||
280 | </th> |
||
281 | |||
282 | <?php endforeach; ?> |
||
283 | |||
284 | <?php if( $show_remove ): ?> |
||
285 | <th class="acf-row-handle"><span></span></th> |
||
286 | <?php endif; ?> |
||
287 | </tr> |
||
288 | </thead> |
||
289 | <?php endif; ?> |
||
290 | |||
291 | <tbody> |
||
292 | <?php foreach( $field['value'] as $i => $row ): |
||
293 | |||
294 | $row_class = 'acf-row'; |
||
295 | |||
296 | if( $i === 'acfcloneindex' ) { |
||
297 | |||
298 | $row_class .= ' acf-clone'; |
||
299 | |||
300 | } elseif( in_array($i, $collapsed) ) { |
||
301 | |||
302 | $row_class .= ' -collapsed'; |
||
303 | |||
304 | } |
||
305 | |||
306 | ?> |
||
307 | <tr class="<?php echo $row_class; ?>" data-id="<?php echo $i; ?>"> |
||
308 | |||
309 | <?php if( $show_order ): ?> |
||
310 | <td class="acf-row-handle order" title="<?php _e('Drag to reorder','acf'); ?>"> |
||
311 | <?php if( $field['collapsed'] ): ?> |
||
312 | <a class="acf-icon -collapse small" href="#" data-event="collapse-row" title="<?php _e('Click to toggle','acf'); ?>"></a> |
||
313 | <?php endif; ?> |
||
314 | <span><?php echo intval($i) + 1; ?></span> |
||
315 | </td> |
||
316 | <?php endif; ?> |
||
317 | |||
318 | <?php echo $before_fields; ?> |
||
319 | |||
320 | View Code Duplication | <?php foreach( $field['sub_fields'] as $sub_field ): |
|
321 | |||
322 | // prevent repeater field from creating multiple conditional logic items for each row |
||
323 | if( $i !== 'acfcloneindex' ) { |
||
324 | |||
325 | $sub_field['conditional_logic'] = 0; |
||
326 | |||
327 | } |
||
328 | |||
329 | |||
330 | // add value |
||
331 | if( isset($row[ $sub_field['key'] ]) ) { |
||
332 | |||
333 | // this is a normal value |
||
334 | $sub_field['value'] = $row[ $sub_field['key'] ]; |
||
335 | |||
336 | } elseif( isset($sub_field['default_value']) ) { |
||
337 | |||
338 | // no value, but this sub field has a default value |
||
339 | $sub_field['value'] = $sub_field['default_value']; |
||
340 | |||
341 | } |
||
342 | |||
343 | |||
344 | // update prefix to allow for nested values |
||
345 | $sub_field['prefix'] = "{$field['name']}[{$i}]"; |
||
346 | |||
347 | |||
348 | // render input |
||
349 | acf_render_field_wrap( $sub_field, $el ); ?> |
||
350 | |||
351 | <?php endforeach; ?> |
||
352 | |||
353 | <?php echo $after_fields; ?> |
||
354 | |||
355 | <?php if( $show_remove ): ?> |
||
356 | <td class="acf-row-handle remove"> |
||
357 | <a class="acf-icon -plus small" href="#" data-event="add-row" title="<?php _e('Add row','acf'); ?>"></a> |
||
358 | <a class="acf-icon -minus small" href="#" data-event="remove-row" title="<?php _e('Remove row','acf'); ?>"></a> |
||
359 | </td> |
||
360 | <?php endif; ?> |
||
361 | |||
362 | </tr> |
||
363 | <?php endforeach; ?> |
||
364 | </tbody> |
||
365 | </table> |
||
366 | <?php if( $show_add ): ?> |
||
367 | |||
368 | <ul class="acf-hl"> |
||
369 | <li class="acf-fr"> |
||
370 | <a href="#" class="acf-button blue" data-event="add-row"><?php echo $field['button_label']; ?></a> |
||
371 | </li> |
||
372 | </ul> |
||
373 | |||
374 | <?php endif; ?> |
||
375 | </div> |
||
376 | <?php |
||
377 | |||
378 | } |
||
379 | |||
964 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.