Passed
Pull Request — dev/6.0.0 (#332)
by Rajan
29:40 queued 26:28
created

TermsModule::term_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 2
dl 0
loc 34
ccs 10
cts 10
cp 1
crap 1
rs 9.9
c 0
b 0
f 0
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
	 * 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
	/**
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['restrict']     = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false );
43
		$options['limit_to']     = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) );
44
		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false );
45
46
		$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' );
47
		$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) );
48
49
		return $options;
50
	}
51
52
	/**
53
	 * Filter the js array.
54
	 * This function will be overridden by the child classes.
55
	 *
56
	 * @since 5.5
57
	 *
58
	 * @param array $js_array JavaScript Array.
59
	 *
60
	 * @return array Modified JavaScript Array.
61
	 */
62
	public function filter_js_array( $js_array ) {
63
		return $js_array;
64
	}
65
66
	/**
67
	 * Perform the deletion.
68
	 *
69
	 * @param array $options Array of Delete options.
70
	 *
71
	 * @return int Number of items that were deleted.
72
	 */
73 4
	protected function do_delete( $options ) {
74 4
		$query = $this->build_query( $options );
75
76 4
		if ( empty( $query ) ) {
77
			// Short circuit deletion, if nothing needs to be deleted.
78
			return 0;
79
		}
80
81 4
		return $this->delete_terms_from_query( $query, $options );
82
	}
83
84
	/**
85
	 * Build the query using query params and then Delete posts.
86
	 *
87
	 * @param array $query   Params for WP Query.
88
	 * @param array $options Delete Options.
89
	 *
90
	 * @return int Number of posts deleted.
91
	 */
92 4
	protected function delete_terms_from_query( $query, $options ) {
93 4
		$term_ids = $this->term_query( $query, $options['taxonomy'] );
94
95 4
		return $this->delete_terms_by_id( $term_ids, $options );
96
	}
97
98
	/**
99
	 * Delete terms by ids.
100
	 *
101
	 * @param int[] $term_ids List of term ids to delete.
102
	 * @param mixed $options  user options.
103
	 *
104
	 * @return int Number of posts deleted.
105
	 */
106 4
	protected function delete_terms_by_id( $term_ids, $options ) {
107 4
		$count = 0;
108
109 4
		foreach ( $term_ids as $term_id ) {
110 4
			$term = get_term( $term_id, $options['taxonomy'] );
111
112 4
			if ( is_wp_error( $term ) ) {
113
				continue;
114
			}
115
116 4
			if ( isset( $options['no_posts'] ) && $term->count > 0 ) {
0 ignored issues
show
Bug introduced by
The property count does not seem to exist on WP_Error.
Loading history...
117
				continue;
118
			}
119
120 4
			wp_delete_term( $term_id, $options['taxonomy'] );
121 4
			$count ++;
122
		}
123
124 4
		return $count;
125
	}
126
127
	/**
128
	 * Custom string function use to get is string start with specified string.
129
	 *
130
	 * @param string $haystack search string.
131
	 * @param string $needle   find string.
132
	 *
133
	 * @return bool.
0 ignored issues
show
Documentation Bug introduced by
The doc comment bool. at position 0 could not be parsed: Unknown type name 'bool.' at position 0 in bool..
Loading history...
134
	 */
135
	protected function bd_starts_with( $haystack, $needle ) {
136
		$length = strlen( $needle );
137
138
		return ( substr( $haystack, 0, $length ) === $needle );
139
	}
140
141
	/**
142
	 * Custom string function use to get is string ends with specified string.
143
	 *
144
	 * @param string $haystack search string.
145
	 * @param string $needle   find string.
146
	 *
147
	 * @return bool.
0 ignored issues
show
Documentation Bug introduced by
The doc comment bool. at position 0 could not be parsed: Unknown type name 'bool.' at position 0 in bool..
Loading history...
148
	 */
149
	protected function bd_ends_with( $haystack, $needle ) {
150
		$length = strlen( $needle );
151
		$substr = substr( $haystack, -$length );
152
		$zero   = 0;
153
154
		return $length === $zero ||
155
		( $substr === $needle );
156
	}
157
158
	/**
159
	 * Get terms which is start with specified string.
160
	 *
161
	 * @param string $term_text user input text.
162
	 * @param array  $options   user options.
163
	 *
164
	 * @return array term ids.
165
	 */
166
	protected function term_starts( $term_text, $options ) {
167
		$term_ids = array();
168
		$terms    = get_terms(
169
			$options['taxonomy'], array(
170
				'hide_empty' => false,
171
			)
172
		);
173
174
		foreach ( $terms as $term ) {
175
			if ( $this->bd_starts_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 ends with specified string.
185
	 *
186
	 * @param string $term_text user input text.
187
	 * @param array  $options   user options.
188
	 *
189
	 * @return array term ids.
190
	 */
191
	protected function term_ends( $term_text, $options ) {
192
		$term_ids = array();
193
		$terms    = get_terms(
194
			$options['taxonomy'], array(
195
				'hide_empty' => false,
196
			)
197
		);
198
199
		foreach ( $terms as $term ) {
200
			if ( $this->bd_ends_with( $term->name, $term_text ) ) {
201
				$term_ids[] = $term->term_id;
202
			}
203
		}
204
205
		return $term_ids;
206
	}
207
208
	/**
209
	 * Get terms which is contain specified string.
210
	 *
211
	 * @param string $term_text user input text.
212
	 * @param array  $options   user options.
213
	 *
214
	 * @return array term ids.
215
	 */
216
	protected function term_contains( $term_text, $options ) {
217
		$term_ids = array();
218
		$terms    = get_terms(
219
			$options['taxonomy'], array(
220
				'hide_empty' => false,
221
			)
222
		);
223
224
		foreach ( $terms as $term ) {
225
			if ( strpos( $term->name, $term_text ) !== false ) {
226
				$term_ids[] = $term->term_id;
227
			}
228
		}
229
230
		return $term_ids;
231
	}
232
233
	/**
234
	 * Get term ids which is have the sepcified post count .
235
	 *
236
	 * @param array $options user options.
237
	 *
238
	 * @return array term ids.
239
	 */
240 4
	protected function term_count_query( $options ) {
241 4
		$term_ids = array();
242 4
		$terms    = get_terms(
243 4
			$options['taxonomy'], array(
244 4
				'hide_empty' => false,
245
			)
246
		);
247 4
		foreach ( $terms as $term ) {
248
			$args = array(
249 4
				'post_type' => $options['post_type'],
250
				'tax_query' => array(
251
					array(
252 4
						'taxonomy' => $options['taxonomy'],
253 4
						'field'    => 'slug',
254 4
						'terms'    => $term->slug,
255
					),
256
				),
257
			);
258
259 4
			$posts = get_posts( $args );
260
261 4
			$term_id = $this->get_term_id_by_name( $options['term_text'], $options['term_opt'], $term->term_id, count( $posts ) );
262 4
			if ( ! empty( $term_id ) ) {
263 4
				$term_ids['include'][] = $term->term_id;
264
265 4
				continue;
266
			}
267
268 2
			$term_ids['exclude'][] = $term->term_id;
269
		}
270
271 4
		return $term_ids;
272
	}
273
274
	/**
275
	 * Get term id by name.
276
	 *
277
	 * @param string $term_text  user text input.
278
	 * @param array  $term_opt   user options.
279
	 * @param int    $term_id    term id.
280
	 * @param int    $post_count post count.
281
	 *
282
	 * @return int term id.
283
	 */
284 4
	protected function get_term_id_by_name( $term_text, $term_opt, $term_id, $post_count ) {
285 4
		switch ( $term_opt ) {
286 4
			case 'equal_to':
287 1
				if ( $post_count == $term_text ) {
288 1
					return $term_id;
289
				}
290 1
				break;
291
292 3
			case 'not_equal_to':
293 1
				if ( $post_count != $term_text ) {
294 1
					return $term_id;
295
				}
296
				break;
297
298 2
			case 'less_than':
299 1
				if ( $post_count < $term_text ) {
300 1
					return $term_id;
301
				}
302
				break;
303
304 1
			case 'greater_than':
305 1
				if ( $post_count > $term_text ) {
306 1
					return $term_id;
307
				}
308 1
				break;
309
		}
310 2
	}
311
312
	/**
313
	 * Wrapper for WP_Term.
314
	 *
315
	 * Adds some performance enhancing defaults.
316
	 *
317
	 * @since  6.0
318
	 *
319
	 * @param array $options  List of options.
320
	 * @param mixed $taxonomy List of Taxonomies.
321
	 *
322
	 * @return array Result array
323
	 */
324 4
	public function term_query( $options, $taxonomy ) {
325
		$defaults = array(
326 4
			'fields'     => 'ids', // retrieve only ids.
327 4
			'taxonomy'   => $taxonomy,
328 4
			'hide_empty' => 0,
329
			'count'      => true,
330
		);
331 4
		$options  = wp_parse_args( $options, $defaults );
332
333 4
		$term_query = new \WP_Term_Query();
334
335
		/**
336
		 * This action runs before the query happens.
337
		 *
338
		 * @since 5.5
339
		 * @since 5.6 added $term_query param.
340
		 *
341
		 * @param \WP_Query $term_query Query object.
342
		 */
343 4
		do_action( 'bd_before_term_query', $term_query );
344
345 4
		$terms = $term_query->query( $options );
346
347
		/**
348
		 * This action runs after the query happens.
349
		 *
350
		 * @since 5.5
351
		 * @since 5.6 added $term_query param.
352
		 *
353
		 * @param \WP_Query $term_query Query object.
354
		 */
355 4
		do_action( 'bd_after_term_query', $term_query );
356
357 4
		return $terms;
358
	}
359
}
360