|
1
|
|
|
<?php |
|
2
|
|
|
namespace BulkWP\BulkDelete\Core\Terms; |
|
3
|
|
|
|
|
4
|
|
|
use BulkWP\BulkDelete\Core\Base\BaseModule; |
|
5
|
|
|
|
|
6
|
1 |
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Module for deleting terms. |
|
10
|
|
|
* |
|
11
|
|
|
* @since 6.0.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class TermsModule extends BaseModule { |
|
14
|
|
|
/** |
|
15
|
|
|
* Get the list of terms ids that need to be deleted. |
|
16
|
|
|
* |
|
17
|
|
|
* Return an empty query array to short-circuit deletion. |
|
18
|
|
|
* |
|
19
|
|
|
* @param array $options Delete options. |
|
20
|
|
|
* |
|
21
|
|
|
* @return int[] List of term ids to delete. |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract protected function get_term_ids_to_delete( $options ); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Item type. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string Item Type. Possible values 'posts', 'pages', 'users', 'terms' etc. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $item_type = 'terms'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Handle common filters. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $request Request array. |
|
36
|
|
|
* |
|
37
|
|
|
* @return array User options. |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function parse_common_filters( $request ) { |
|
40
|
|
|
$options = array(); |
|
41
|
|
|
|
|
42
|
|
|
$options['taxonomy'] = sanitize_text_field( bd_array_get( $request, 'smbd_' . $this->field_slug . '_taxonomy' ) ); |
|
43
|
|
|
|
|
44
|
|
|
return $options; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Perform the deletion. |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $options Array of Delete options. |
|
51
|
|
|
* |
|
52
|
|
|
* @return int Number of items that were deleted. |
|
53
|
|
|
*/ |
|
54
|
25 |
|
protected function do_delete( $options ) { |
|
55
|
25 |
|
$term_ids_to_delete = $this->get_term_ids_to_delete( $options ); |
|
56
|
|
|
|
|
57
|
25 |
|
if ( $term_ids_to_delete <= 0 ) { |
|
58
|
|
|
// Short circuit deletion, if nothing needs to be deleted. |
|
59
|
|
|
return 0; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
25 |
|
return $this->delete_terms_by_id( $term_ids_to_delete, $options ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Delete terms by ids. |
|
67
|
|
|
* |
|
68
|
|
|
* @param int[] $term_ids List of term ids to delete. |
|
69
|
|
|
* @param array $options User options. |
|
70
|
|
|
* |
|
71
|
|
|
* @return int Number of terms deleted. |
|
72
|
|
|
*/ |
|
73
|
25 |
|
protected function delete_terms_by_id( $term_ids, $options ) { |
|
74
|
25 |
|
$count = 0; |
|
75
|
|
|
|
|
76
|
25 |
|
foreach ( $term_ids as $term_id ) { |
|
77
|
18 |
|
if ( wp_delete_term( $term_id, $options['taxonomy'] ) ) { |
|
78
|
18 |
|
$count ++; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
25 |
|
return $count; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get all terms from a taxonomy. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $taxonomy Taxonomy name. |
|
89
|
|
|
* |
|
90
|
|
|
* @return \WP_Term[] List of terms. |
|
91
|
|
|
*/ |
|
92
|
23 |
|
protected function get_all_terms( $taxonomy ) { |
|
93
|
|
|
$args = array( |
|
94
|
23 |
|
'taxonomy' => $taxonomy, |
|
95
|
23 |
|
'fields' => 'all', |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
23 |
|
return $this->query_terms( $args ); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Query terms using WP_Term_Query. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $query Query args. |
|
105
|
|
|
* |
|
106
|
|
|
* @return array List of terms. |
|
107
|
|
|
*/ |
|
108
|
25 |
|
protected function query_terms( $query ) { |
|
109
|
|
|
$defaults = array( |
|
110
|
25 |
|
'fields' => 'ids', // retrieve only ids. |
|
111
|
|
|
'hide_empty' => false, |
|
112
|
|
|
'count' => false, |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
25 |
|
$query = wp_parse_args( $query, $defaults ); |
|
116
|
|
|
|
|
117
|
25 |
|
$term_query = new \WP_Term_Query(); |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* This action runs before the query happens. |
|
121
|
|
|
* |
|
122
|
|
|
* @since 6.0.0 |
|
123
|
|
|
* |
|
124
|
|
|
* @param \WP_Term_Query $term_query Query object. |
|
125
|
|
|
*/ |
|
126
|
25 |
|
do_action( 'bd_before_query', $term_query ); |
|
127
|
|
|
|
|
128
|
25 |
|
$terms = $term_query->query( $query ); |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* This action runs after the query happens. |
|
132
|
|
|
* |
|
133
|
|
|
* @since 6.0.0 |
|
134
|
|
|
* |
|
135
|
|
|
* @param \WP_Term_Query $term_query Query object. |
|
136
|
|
|
*/ |
|
137
|
25 |
|
do_action( 'bd_after_query', $term_query ); |
|
138
|
|
|
|
|
139
|
25 |
|
return $terms; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function get_success_message( $items_deleted ) { |
|
143
|
|
|
/* translators: 1 Number of terms deleted */ |
|
144
|
|
|
return _n( 'Deleted %d term with the selected options', 'Deleted %d terms with the selected options', $items_deleted, 'bulk-delete' ); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|