Total Complexity | 40 |
Total Lines | 466 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Renderer 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 Renderer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class Renderer extends Fetcher { |
||
16 | /** |
||
17 | * Slug for the form fields. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $field_slug; |
||
22 | |||
23 | /** |
||
24 | * Render post status including custom post status. |
||
25 | * |
||
26 | * @param string $post_type The post type for which the post status should be displayed. |
||
27 | */ |
||
28 | protected function render_post_status( $post_type = 'post' ) { |
||
29 | $post_statuses = $this->get_post_statuses(); |
||
30 | $post_count = wp_count_posts( $post_type ); |
||
31 | |||
32 | foreach ( $post_statuses as $post_status ) : ?> |
||
33 | <tr> |
||
34 | <td> |
||
35 | <input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" id="smbd_<?php echo esc_attr( $post_status->name ); ?>" |
||
36 | value="<?php echo esc_attr( $post_status->name ); ?>" type="checkbox"> |
||
37 | |||
38 | <label for="smbd_<?php echo esc_attr( $post_status->name ); ?>"> |
||
39 | <?php echo esc_html( $post_status->label ), ' '; ?> |
||
40 | <?php if ( property_exists( $post_count, $post_status->name ) ) : ?> |
||
41 | (<?php echo absint( $post_count->{ $post_status->name } ) . ' ', __( 'Posts', 'bulk-delete' ); ?>) |
||
42 | <?php endif; ?> |
||
43 | </label> |
||
44 | </td> |
||
45 | </tr> |
||
46 | <?php endforeach; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Render Post Types as radio buttons. |
||
51 | */ |
||
52 | protected function render_post_type_as_radios() { |
||
53 | $post_types = $this->get_post_types(); |
||
54 | ?> |
||
55 | |||
56 | <?php foreach ( $post_types as $post_type ) : ?> |
||
57 | |||
58 | <tr> |
||
59 | <td scope="row"> |
||
60 | <input type="radio" name="<?php echo esc_attr( $this->field_slug ); ?>_post_type" |
||
61 | value="<?php echo esc_attr( $post_type->name ); ?>" |
||
62 | id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>"> |
||
63 | |||
64 | <label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>"> |
||
65 | <?php echo esc_html( $post_type->label ); ?> |
||
66 | </label> |
||
67 | </td> |
||
68 | </tr> |
||
69 | |||
70 | <?php endforeach; ?> |
||
71 | <?php |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Render Post type with status and post count checkboxes. |
||
76 | */ |
||
77 | protected function render_post_type_with_status() { |
||
78 | $post_types_by_status = $this->get_post_types_by_status(); |
||
79 | ?> |
||
80 | <tr> |
||
81 | <td scope="row" colspan="2"> |
||
82 | <select class="enhanced-post-types-with-status" multiple="multiple" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]"> |
||
83 | <?php foreach ( $post_types_by_status as $post_type => $all_status ) : ?> |
||
84 | <optgroup label="<?php echo esc_html( $post_type ); ?>"> |
||
85 | <?php foreach ( $all_status as $status_key => $status_value ) : ?> |
||
86 | <option value="<?php echo esc_attr( $status_key ); ?>"><?php echo esc_html( $status_value ); ?></option> |
||
87 | <?php endforeach; ?> |
||
88 | </optgroup> |
||
89 | <?php endforeach; ?> |
||
90 | </select> |
||
91 | </td> |
||
92 | </tr> |
||
93 | <?php |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Split post type and status. |
||
98 | * |
||
99 | * @param string $str Post type and status combination. |
||
100 | * |
||
101 | * @return array Post type and status as elements of array. |
||
102 | */ |
||
103 | protected function split_post_type_and_status( $str ) { |
||
104 | $type_status = array(); |
||
105 | |||
106 | $str_arr = explode( '-', $str ); |
||
107 | |||
108 | if ( count( $str_arr ) > 1 ) { |
||
109 | $type_status['status'] = end( $str_arr ); |
||
110 | $type_status['type'] = implode( '-', array_slice( $str_arr, 0, - 1 ) ); |
||
111 | } else { |
||
112 | $type_status['status'] = 'publish'; |
||
113 | $type_status['type'] = $str; |
||
114 | } |
||
115 | |||
116 | return $type_status; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Render user role dropdown. |
||
121 | */ |
||
122 | protected function render_user_role_dropdown() { |
||
123 | global $wp_roles; |
||
124 | ?> |
||
125 | |||
126 | <select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_roles[]" class="enhanced-role-dropdown" |
||
127 | multiple="multiple" data-placeholder="<?php _e( 'Select User Role', 'bulk-delete' ); ?>"> |
||
128 | |||
129 | <?php foreach ( $wp_roles->roles as $role => $role_details ) : ?> |
||
130 | <option value="<?php echo esc_attr( $role ); ?>"> |
||
131 | <?php echo esc_html( $role_details['name'] ), ' (', absint( $this->get_user_count_by_role( $role ) ), ' ', __( 'Users', 'bulk-delete' ), ')'; ?> |
||
132 | </option> |
||
133 | <?php endforeach; ?> |
||
134 | </select> |
||
135 | |||
136 | <?php |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Render Post type dropdown. |
||
141 | */ |
||
142 | protected function render_post_type_dropdown() { |
||
143 | bd_render_post_type_dropdown( $this->field_slug ); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Render Taxonomy dropdown. |
||
148 | */ |
||
149 | protected function render_taxonomy_dropdown() { |
||
150 | $taxonomies = get_taxonomies( array(), 'objects' ); |
||
151 | ?> |
||
152 | |||
153 | <select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_taxonomy" class="enhanced-taxonomy-list" data-placeholder="<?php _e( 'Select Taxonomy', 'bulk-delete' ); ?>"> |
||
154 | <?php foreach ( $taxonomies as $taxonomy ) : ?> |
||
155 | <option value="<?php echo esc_attr( $taxonomy->name ); ?>"> |
||
156 | <?php echo esc_html( $taxonomy->label . ' (' . $taxonomy->name . ')' ); ?> |
||
157 | </option> |
||
158 | <?php endforeach; ?> |
||
159 | </select> |
||
160 | <?php |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Render Category dropdown. |
||
165 | */ |
||
166 | protected function render_category_dropdown() { |
||
167 | $categories = $this->get_categories(); |
||
168 | ?> |
||
169 | |||
170 | <select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>" |
||
171 | class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>" |
||
172 | data-taxonomy="category" multiple> |
||
173 | |||
174 | <option value="all"> |
||
175 | <?php _e( 'All Categories', 'bulk-delete' ); ?> |
||
176 | </option> |
||
177 | |||
178 | <?php foreach ( $categories as $category ) : ?> |
||
179 | <option value="<?php echo absint( $category->cat_ID ); ?>"> |
||
180 | <?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?> |
||
181 | </option> |
||
182 | <?php endforeach; ?> |
||
183 | |||
184 | </select> |
||
185 | <?php |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Render String based comparison operators dropdown. |
||
190 | */ |
||
191 | protected function render_string_comparison_operators() { |
||
192 | ?> |
||
193 | <select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_operator"> |
||
194 | <option value="equal_to"><?php _e( 'equal to', 'bulk-delete' ); ?></option> |
||
195 | <option value="not_equal_to"><?php _e( 'not equal to', 'bulk-delete' ); ?></option> |
||
196 | <option value="starts_with"><?php _e( 'starts with', 'bulk-delete' ); ?></option> |
||
197 | <option value="ends_with"><?php _e( 'ends with', 'bulk-delete' ); ?></option> |
||
198 | <option value="contains"><?php _e( 'contains', 'bulk-delete' ); ?></option> |
||
199 | <option value="not_contains"><?php _e( 'not contains', 'bulk-delete' ); ?></option> |
||
200 | </select> |
||
201 | <?php |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Render number based comparison operators dropdown. |
||
206 | */ |
||
207 | protected function render_number_comparison_operators() { |
||
208 | ?> |
||
209 | <select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_operator"> |
||
210 | <option value="equal_to"><?php _e( 'equal to', 'bulk-delete' ); ?></option> |
||
211 | <option value="not_equal_to"><?php _e( 'not equal to', 'bulk-delete' ); ?></option> |
||
212 | <option value="less_than"><?php _e( 'less than', 'bulk-delete' ); ?></option> |
||
213 | <option value="greater_than"><?php _e( 'greater than', 'bulk-delete' ); ?></option> |
||
214 | </select> |
||
215 | <?php |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Render Tags dropdown. |
||
220 | */ |
||
221 | protected function render_tags_dropdown() { |
||
222 | $tags = $this->get_tags(); |
||
223 | ?> |
||
224 | |||
225 | <select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>" |
||
226 | class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>" |
||
227 | data-taxonomy="post_tag" multiple> |
||
228 | |||
229 | <option value="all"> |
||
230 | <?php _e( 'All Tags', 'bulk-delete' ); ?> |
||
231 | </option> |
||
232 | |||
233 | <?php foreach ( $tags as $tag ) : ?> |
||
234 | <option value="<?php echo absint( $tag->term_id ); ?>"> |
||
235 | <?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?> |
||
236 | </option> |
||
237 | <?php endforeach; ?> |
||
238 | </select> |
||
239 | <?php |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Get the class name for select2 dropdown based on the number of items present. |
||
244 | * |
||
245 | * @param int $count The number of items present. |
||
246 | * @param string $class_name Primary class name. |
||
247 | * |
||
248 | * @return string Class name. |
||
249 | */ |
||
250 | protected function enable_ajax_if_needed_to_dropdown_class_name( $count, $class_name ) { |
||
251 | if ( $count >= $this->get_enhanced_select_threshold() ) { |
||
252 | $class_name .= '-ajax'; |
||
253 | } |
||
254 | |||
255 | return $class_name; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Render Sticky Posts dropdown. |
||
260 | */ |
||
261 | protected function render_sticky_posts_dropdown() { |
||
262 | $sticky_posts = $this->get_sticky_posts(); |
||
263 | ?> |
||
264 | |||
265 | <table class="optiontable"> |
||
266 | <?php if ( count( $sticky_posts ) > 1 ) : ?> |
||
267 | <tr> |
||
268 | <td scope="row"> |
||
269 | <label> |
||
270 | <input type="checkbox" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="all" checked> |
||
271 | <?php echo __( 'All sticky posts', 'bulk-delete' ), ' (', count( $sticky_posts ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?> |
||
272 | </label> |
||
273 | </td> |
||
274 | </tr> |
||
275 | <?php endif; ?> |
||
276 | |||
277 | <?php foreach ( $sticky_posts as $post ) : ?> |
||
278 | <?php $author = get_userdata( $post->post_author ); ?> |
||
279 | <tr> |
||
280 | <td scope="row"> |
||
281 | <label> |
||
282 | <input type="checkbox" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>"> |
||
283 | <?php |
||
284 | echo esc_html( $post->post_title ), ' - ', |
||
285 | __( 'Published on', 'bulk-delete' ), ' ', get_the_date( get_option( 'date_format' ), $post->ID ), |
||
286 | __( ' by ', 'bulk-delete' ), esc_html( $author->display_name ); |
||
287 | ?> |
||
288 | </label> |
||
289 | </td> |
||
290 | </tr> |
||
291 | <?php endforeach; ?> |
||
292 | </table> |
||
293 | <?php |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Render Post Types as checkboxes. |
||
298 | * |
||
299 | * @since 5.6.0 |
||
300 | * |
||
301 | * @param string $name Name of post type checkboxes. |
||
302 | */ |
||
303 | protected function render_post_type_checkboxes( $name ) { |
||
304 | $post_types = bd_get_post_types(); |
||
305 | ?> |
||
306 | |||
307 | <?php foreach ( $post_types as $post_type ) : ?> |
||
308 | |||
309 | <tr> |
||
310 | <td scope="row"> |
||
311 | <input type="checkbox" name="<?php echo esc_attr( $name ); ?>[]" value="<?php echo esc_attr( $post_type->name ); ?>" |
||
312 | id="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>" checked> |
||
313 | |||
314 | <label for="smbd_post_type_<?php echo esc_html( $post_type->name ); ?>"> |
||
315 | <?php echo esc_html( $post_type->label ); ?> |
||
316 | </label> |
||
317 | </td> |
||
318 | </tr> |
||
319 | |||
320 | <?php endforeach; ?> |
||
321 | <?php |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Render the "private post" setting fields. |
||
326 | */ |
||
327 | protected function render_private_post_settings() { |
||
328 | bd_render_private_post_settings( $this->field_slug ); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Get the threshold after which enhanced select should be used. |
||
333 | * |
||
334 | * @return int Threshold. |
||
335 | */ |
||
336 | protected function get_enhanced_select_threshold() { |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Render sticky settings. |
||
349 | */ |
||
350 | protected function render_sticky_action_settings() { |
||
351 | ?> |
||
352 | <tr> |
||
353 | <td scope="row" colspan="2"> |
||
354 | <label> |
||
355 | <input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_sticky_action" value="unsticky" type="radio" checked> |
||
356 | <?php _e( 'Remove Sticky', 'bulk-delete' ); ?> |
||
357 | </label> |
||
358 | <label> |
||
359 | <input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_sticky_action" value="delete" type="radio"> |
||
360 | <?php _e( 'Delete Post', 'bulk-delete' ); ?> |
||
361 | </label> |
||
362 | </td> |
||
363 | </tr> |
||
364 | <?php |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Render filtering table header. |
||
369 | */ |
||
370 | protected function render_filtering_table_header() { |
||
371 | bd_render_filtering_table_header(); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Render restrict settings. |
||
376 | */ |
||
377 | protected function render_restrict_settings() { |
||
378 | bd_render_restrict_settings( $this->field_slug, $this->item_type ); |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Render delete settings. |
||
383 | */ |
||
384 | protected function render_delete_settings() { |
||
385 | bd_render_delete_settings( $this->field_slug ); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Render limit settings. |
||
390 | */ |
||
391 | protected function render_limit_settings() { |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Render cron settings based on whether scheduler is present or not. |
||
397 | */ |
||
398 | protected function render_cron_settings() { |
||
399 | $disabled_attr = 'disabled'; |
||
400 | if ( empty( $this->scheduler_url ) ) { |
||
401 | $disabled_attr = ''; |
||
402 | } |
||
403 | ?> |
||
404 | |||
405 | <tr> |
||
406 | <td scope="row" colspan="2"> |
||
407 | <label><input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="false" type="radio" |
||
408 | checked="checked"> <?php _e( 'Delete now', 'bulk-delete' ); ?></label> |
||
409 | <label><input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" value="true" type="radio" |
||
410 | id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron" <?php echo esc_attr( $disabled_attr ); ?>> <?php _e( 'Schedule', 'bulk-delete' ); ?></label> |
||
411 | <input name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_start" |
||
412 | id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_start" value="now" |
||
413 | type="text" <?php echo esc_attr( $disabled_attr ); ?>><?php _e( 'repeat ', 'bulk-delete' ); ?> |
||
414 | |||
415 | <select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" |
||
416 | id="smbd_<?php echo esc_attr( $this->field_slug ); ?>_cron_freq" <?php echo esc_attr( $disabled_attr ); ?>> |
||
417 | |||
418 | <option value="-1"><?php _e( "Don't repeat", 'bulk-delete' ); ?></option> |
||
419 | <?php |
||
420 | /** |
||
421 | * List of cron schedules. |
||
422 | * |
||
423 | * @since 6.0.0 |
||
424 | * |
||
425 | * @param array $cron_schedules List of cron schedules. |
||
426 | * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module Module. |
||
427 | */ |
||
428 | $cron_schedules = apply_filters( 'bd_cron_schedules', wp_get_schedules(), $this ); |
||
429 | ?> |
||
430 | |||
431 | <?php foreach ( $cron_schedules as $key => $value ) : ?> |
||
432 | <option |
||
433 | value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value['display'] ); ?></option> |
||
434 | <?php endforeach; ?> |
||
435 | </select> |
||
436 | |||
437 | <?php if ( ! empty( $this->scheduler_url ) ) : ?> |
||
438 | <?php |
||
439 | $pro_class = 'bd-' . str_replace( '_', '-', $this->field_slug ) . '-pro'; |
||
440 | |||
441 | /** |
||
442 | * HTML class of the span that displays the 'Pro only feature' message. |
||
443 | * |
||
444 | * @since 6.0.0 |
||
445 | * |
||
446 | * @param string $pro_class HTML class. |
||
447 | * @param string $field_slug Field Slug of module. |
||
448 | * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module Module. |
||
449 | */ |
||
450 | apply_filters( 'bd_pro_only_feature_class', $pro_class, $this->field_slug, $this ) |
||
451 | ?> |
||
452 | |||
453 | <span class="<?php echo sanitize_html_class( $pro_class ); ?>" style="color:red"> |
||
454 | <?php _e( 'Only available in Pro Addon', 'bulk-delete' ); ?> <a |
||
455 | href="<?php echo esc_url( $this->scheduler_url ); ?>">Buy now</a> |
||
456 | </span> |
||
457 | <?php endif; ?> |
||
458 | </td> |
||
459 | </tr> |
||
460 | |||
461 | <tr class="<?php echo sanitize_html_class( $pro_class ); ?>" style="display: none;"> |
||
462 | <td scope="row" colspan="2"> |
||
463 | <?php |
||
464 | _e( 'Enter time in <strong>Y-m-d H:i:s</strong> format or enter <strong>now</strong> to use current time.', 'bulk-delete' ); |
||
465 | $link = '<a href="https://bulkwp.com/docs/add-a-new-cron-schedule/">' . __( 'Click here', 'bulk-delete' ) . '</a>'; |
||
466 | $markup = sprintf( __( 'Want to add new a Cron schedule? %s', 'bulk-delete' ), $link ); |
||
467 | |||
468 | $content = __( 'Learn how to add your desired Cron schedule.', 'bulk-delete' ); |
||
469 | echo ' ' . bd_generate_help_tooltip( $markup, $content ); |
||
470 | ?> |
||
471 | </td> |
||
472 | </tr> |
||
473 | <?php |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Render submit button. |
||
478 | */ |
||
479 | protected function render_submit_button() { |
||
481 | } |
||
482 | } |
||
483 |