1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BulkWP\BulkDelete\Core\Terms\Modules; |
4
|
|
|
|
5
|
|
|
use BulkWP\BulkDelete\Core\Terms\TermsModule; |
6
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Delete Terms by Post Count. |
11
|
|
|
* |
12
|
|
|
* @since 6.0.0 |
13
|
|
|
*/ |
14
|
|
|
class DeleteTermsByPostCountModule extends TermsModule { |
15
|
|
|
protected function initialize() { |
16
|
|
|
$this->item_type = 'terms'; |
17
|
|
|
$this->field_slug = 'terms_by_post_count'; |
18
|
|
|
$this->meta_box_slug = 'bd_delete_terms_by_post_count'; |
19
|
|
|
$this->action = 'delete_terms_by_post_count'; |
20
|
|
|
$this->messages = array( |
21
|
|
|
'box_label' => __( 'Delete Terms by Post Count', 'bulk-delete' ), |
22
|
|
|
'scheduled' => __( 'The selected terms are scheduled for deletion', 'bulk-delete' ), |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function render() { |
27
|
|
|
?> |
28
|
|
|
|
29
|
|
|
<fieldset class="options"> |
30
|
|
|
<h4><?php _e( 'Select the taxonomy from which you want to delete terms', 'bulk-delete' ); ?></h4> |
31
|
|
|
|
32
|
|
|
<?php $this->render_taxonomy_dropdown(); ?> |
33
|
|
|
|
34
|
|
|
<h4><?php _e( 'Choose your filtering options', 'bulk-delete' ); ?></h4> |
35
|
|
|
|
36
|
|
|
<?php _e( 'Delete Terms if the post count is ', 'bulk-delete' ); ?> |
37
|
|
|
<?php $this->render_number_comparison_operators(); ?> |
38
|
|
|
<input type="number" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_post_count" placeholder="<?php _e( 'Post count', 'bulk-delete' ); ?>"> |
39
|
|
|
</fieldset> |
40
|
|
|
|
41
|
|
|
<?php |
42
|
|
|
$this->render_submit_button(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function filter_js_array( $js_array ) { |
46
|
|
|
$js_array['validators'][ $this->action ] = 'validatePostTypeSelect2'; |
47
|
|
|
$js_array['error_msg'][ $this->action ] = 'selectPostType'; |
48
|
|
|
$js_array['msg']['selectPostType'] = __( 'Please select at least one post type', 'bulk-delete' ); |
49
|
|
|
|
50
|
|
|
$js_array['pre_action_msg'][ $this->action ] = 'deleteTermsWarning'; |
51
|
|
|
$js_array['msg']['deleteTermsWarning'] = __( 'Are you sure you want to delete all the terms based on the selected option?', 'bulk-delete' ); |
52
|
|
|
|
53
|
|
|
return $js_array; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function convert_user_input_to_options( $request, $options ) { |
57
|
|
|
$options['operator'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_operator' ) ); |
58
|
|
|
$options['post_count'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_post_count' ) ); |
59
|
|
|
|
60
|
|
|
return $options; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function get_term_ids_to_delete( $options ) { |
64
|
|
|
$term_ids = array(); |
65
|
|
|
|
66
|
|
|
$terms = $this->get_all_terms( $options['taxonomy'] ); |
67
|
|
|
foreach ( $terms as $term ) { |
68
|
|
|
if ( $this->should_delete_term_based_on_post_count( $term->count, $options['operator'], $options['post_count'] ) ) { |
69
|
|
|
$term_ids[] = $term->term_id; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $term_ids; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Determine if a term should be deleted based on post count. |
78
|
|
|
* |
79
|
|
|
* @param int $term_post_count Number of posts associated with a term. |
80
|
|
|
* @param string $operator Operator. |
81
|
|
|
* @param int $compared_to The user entered value to which the comparison should be made. |
82
|
|
|
* |
83
|
|
|
* @return int term id. |
84
|
|
|
*/ |
85
|
|
|
protected function should_delete_term_based_on_post_count( $term_post_count, $operator, $compared_to ) { |
86
|
|
|
switch ( $operator ) { |
87
|
|
|
case 'equal_to': |
88
|
|
|
return $term_post_count === $compared_to; |
89
|
|
|
case 'not_equal_to': |
90
|
|
|
return $term_post_count !== $compared_to; |
91
|
|
|
case 'less_than': |
92
|
|
|
return $term_post_count < $compared_to; |
93
|
|
|
case 'greater_than': |
94
|
|
|
return $term_post_count > $compared_to; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|