|
1
|
|
|
<?php |
|
2
|
|
|
namespace BulkWP\BulkDelete\Core\Terms; |
|
3
|
|
|
|
|
4
|
|
|
use BulkWP\BulkDelete\Core\Base\BaseModule; |
|
5
|
|
|
|
|
6
|
|
|
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
|
|
|
* Build query params for WP_Query by using delete options. |
|
16
|
|
|
* |
|
17
|
|
|
* Return an empty query array to short-circuit deletion. |
|
18
|
|
|
* |
|
19
|
|
|
* @param array $options Delete options. |
|
20
|
|
|
* |
|
21
|
|
|
* @return array Query. |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract protected function build_query( $options ); |
|
24
|
|
|
|
|
25
|
|
|
protected $item_type = 'terms'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Handle common filters. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $request Request array. |
|
31
|
|
|
* |
|
32
|
|
|
* @return array User options. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function parse_common_filters( $request ) { |
|
35
|
|
|
$options = array(); |
|
36
|
|
|
|
|
37
|
|
|
$options['restrict'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false ); |
|
38
|
|
|
$options['limit_to'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) ); |
|
39
|
|
|
$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false ); |
|
40
|
|
|
|
|
41
|
|
|
$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' ); |
|
42
|
|
|
$options['days'] = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) ); |
|
43
|
|
|
|
|
44
|
|
|
return $options; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function filter_js_array( $js_array ) { |
|
48
|
|
|
return $js_array; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function do_delete( $options ) { |
|
52
|
|
|
$query = $this->build_query( $options ); |
|
53
|
|
|
|
|
54
|
|
|
if ( empty( $query ) ) { |
|
55
|
|
|
// Short circuit deletion, if nothing needs to be deleted. |
|
56
|
|
|
return 0; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->delete_terms_from_query( $query, $options ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Build the query using query params and then Delete posts. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $query Params for WP Query. |
|
66
|
|
|
* @param array $options Delete Options. |
|
67
|
|
|
* |
|
68
|
|
|
* @return int Number of posts deleted. |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function delete_terms_from_query( $query, $options ) { |
|
71
|
|
|
$term_ids = bd_term_query( $query, $options['taxonomy'] ); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
return $this->delete_terms_by_id( $term_ids, $options ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Render the "private post" setting fields. |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function render_private_post_settings() { |
|
80
|
|
|
bd_render_private_post_settings( $this->field_slug ); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Delete terms by ids. |
|
85
|
|
|
* |
|
86
|
|
|
* @param int[] $term_ids List of term ids to delete. |
|
87
|
|
|
* @param mixed $options |
|
88
|
|
|
* |
|
89
|
|
|
* @return int Number of posts deleted. |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function delete_terms_by_id( $term_ids, $options ) { |
|
92
|
|
|
$count = 0; |
|
93
|
|
|
|
|
94
|
|
|
foreach ( $term_ids as $term_id ) { |
|
95
|
|
|
$term = get_term( $term_id, $options['taxonomy'] ); |
|
96
|
|
|
|
|
97
|
|
|
if ( isset( $options['no_posts'] ) && $term->count > 0 ) { |
|
|
|
|
|
|
98
|
|
|
continue; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
wp_delete_term( $term_id, $options['taxonomy'] ); |
|
102
|
|
|
$count ++; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $count; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* custom string function use to get is string start with specified string. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $haystack. |
|
112
|
|
|
* @param string $needle. |
|
113
|
|
|
* |
|
114
|
|
|
* @return boolean. |
|
|
|
|
|
|
115
|
|
|
*/ |
|
116
|
|
|
protected function bd_starts_with($haystack, $needle){ |
|
117
|
|
|
$length = strlen($needle); |
|
118
|
|
|
|
|
119
|
|
|
return (substr($haystack, 0, $length) === $needle); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* custom string function use to get is string ends with specified string. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $haystack. |
|
126
|
|
|
* @param string $needle. |
|
127
|
|
|
* |
|
128
|
|
|
* @return boolean. |
|
|
|
|
|
|
129
|
|
|
*/ |
|
130
|
|
|
protected function bd_ends_with($haystack, $needle){ |
|
131
|
|
|
$length = strlen($needle); |
|
132
|
|
|
|
|
133
|
|
|
return $length === 0 || |
|
134
|
|
|
(substr($haystack, -$length) === $needle); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* get terms which is start with specified string. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $term_text. |
|
141
|
|
|
* @param array $options. |
|
142
|
|
|
* |
|
143
|
|
|
* @return array term ids. |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function term_starts( $term_text , $options ){ |
|
146
|
|
|
$term_ids = array(); |
|
147
|
|
|
$terms = get_terms( $options['taxonomy'], array( |
|
148
|
|
|
'hide_empty' => false, |
|
149
|
|
|
) ); |
|
150
|
|
|
|
|
151
|
|
|
foreach( $terms as $term ){ |
|
152
|
|
|
if( $this->bd_starts_with( $term->name, $term_text ) ){ |
|
153
|
|
|
$term_ids[] = $term->term_id; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $term_ids; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* get terms which is ends with specified string. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $term_text. |
|
164
|
|
|
* @param array $options. |
|
165
|
|
|
* |
|
166
|
|
|
* @return array term ids. |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function term_ends( $term_text , $options ){ |
|
169
|
|
|
$term_ids = array(); |
|
170
|
|
|
$terms = get_terms( $options['taxonomy'], array( |
|
171
|
|
|
'hide_empty' => false, |
|
172
|
|
|
) ); |
|
173
|
|
|
|
|
174
|
|
|
foreach( $terms as $term ){ |
|
175
|
|
|
if( $this->bd_ends_with( $term->name, $term_text ) ){ |
|
176
|
|
|
$term_ids[] = $term->term_id; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $term_ids; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* get terms which is contain specified string. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $term_text. |
|
187
|
|
|
* @param array $options. |
|
188
|
|
|
* |
|
189
|
|
|
* @return array term ids. |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function term_contains( $term_text , $options ){ |
|
192
|
|
|
$term_ids = array(); |
|
193
|
|
|
$terms = get_terms( $options['taxonomy'], array( |
|
194
|
|
|
'hide_empty' => false, |
|
195
|
|
|
) ); |
|
196
|
|
|
|
|
197
|
|
|
foreach( $terms as $term ){ |
|
198
|
|
|
if ( strpos( $term->name, $term_text ) !== false ) { |
|
199
|
|
|
$term_ids[] = $term->term_id; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $term_ids; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Get term ids which is have the sepcified post count . |
|
208
|
|
|
* |
|
209
|
|
|
* @param array $options. |
|
210
|
|
|
* |
|
211
|
|
|
* @return array term ids. |
|
212
|
|
|
*/ |
|
213
|
|
|
protected function term_count_query( $options ){ |
|
214
|
|
|
$term_ids = array(); |
|
215
|
|
|
$terms = get_terms( $options['taxonomy'], array( |
|
216
|
|
|
'hide_empty' => false, |
|
217
|
|
|
) ); |
|
218
|
|
|
|
|
219
|
|
|
foreach( $terms as $term ){ |
|
220
|
|
|
$args = array( |
|
221
|
|
|
'post_type' => 'post', |
|
222
|
|
|
'tax_query' => array( |
|
223
|
|
|
array( |
|
224
|
|
|
'taxonomy' => $options['taxonomy'], |
|
225
|
|
|
'field' => 'slug', |
|
226
|
|
|
'terms' => $term->slug, |
|
227
|
|
|
), |
|
228
|
|
|
), |
|
229
|
|
|
); |
|
230
|
|
|
|
|
231
|
|
|
$posts = get_posts($args); |
|
232
|
|
|
|
|
233
|
|
|
if( count($posts) == $options['term_text'] && $options['term_opt'] == 'equal_to' ){ |
|
234
|
|
|
$term_ids[] = $term->term_id; |
|
235
|
|
|
}elseif( count($posts) != $options['term_text'] && $options['term_opt'] == 'not_equal_to' ){ |
|
236
|
|
|
$term_ids[] = $term->term_id; |
|
237
|
|
|
}elseif( count($posts) < $options['term_text'] && $options['term_opt'] == 'less_than' ){ |
|
238
|
|
|
$term_ids[] = $term->term_id; |
|
239
|
|
|
}elseif( count($posts) > $options['term_text'] && $options['term_opt'] == 'greater_than' ){ |
|
240
|
|
|
$term_ids[] = $term->term_id; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $term_ids; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Wrapper for WP_Term. |
|
249
|
|
|
* |
|
250
|
|
|
* Adds some performance enhancing defaults. |
|
251
|
|
|
* |
|
252
|
|
|
* @since 6.0 |
|
253
|
|
|
* |
|
254
|
|
|
* @param array $options List of options |
|
255
|
|
|
* @param mixed $taxonomy |
|
256
|
|
|
* |
|
257
|
|
|
* @return array Result array |
|
258
|
|
|
*/ |
|
259
|
|
|
public function term_query( $options, $taxonomy ) { |
|
260
|
|
|
$defaults = array( |
|
261
|
|
|
'fields' => 'ids', // retrieve only ids |
|
262
|
|
|
'taxonomy' => $taxonomy, |
|
263
|
|
|
'hide_empty' => 0, |
|
264
|
|
|
'count' => true, |
|
265
|
|
|
); |
|
266
|
|
|
$options = wp_parse_args( $options, $defaults ); |
|
267
|
|
|
|
|
268
|
|
|
$term_query = new WP_Term_Query(); |
|
|
|
|
|
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* This action runs before the query happens. |
|
272
|
|
|
* |
|
273
|
|
|
* @since 5.5 |
|
274
|
|
|
* @since 5.6 added $term_query param. |
|
275
|
|
|
* |
|
276
|
|
|
* @param \WP_Query $term_query Query object. |
|
277
|
|
|
*/ |
|
278
|
|
|
do_action( 'bd_before_term_query', $term_query ); |
|
279
|
|
|
|
|
280
|
|
|
$terms = $term_query->query( $options ); |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* This action runs after the query happens. |
|
284
|
|
|
* |
|
285
|
|
|
* @since 5.5 |
|
286
|
|
|
* @since 5.6 added $term_query param. |
|
287
|
|
|
* |
|
288
|
|
|
* @param \WP_Query $term_query Query object. |
|
289
|
|
|
*/ |
|
290
|
|
|
do_action( 'bd_after_term_query', $term_query ); |
|
291
|
|
|
|
|
292
|
|
|
return $terms; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|