Completed
Push — 178-feature/delete-terms--by-p... ( e8fd14...3ebd05 )
by Rajan
14:37 queued 06:53
created

TermsModule   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Test Coverage

Coverage 54.05%

Importance

Changes 0
Metric Value
eloc 103
dl 0
loc 344
ccs 60
cts 111
cp 0.5405
rs 9.6
c 0
b 0
f 0
wmc 35

13 Methods

Rating   Name   Duplication   Size   Complexity  
A delete_terms_by_id() 0 19 5
A filter_js_array() 0 2 1
A delete_terms_from_query() 0 4 1
A parse_common_filters() 0 11 1
A bd_starts_with() 0 4 1
A do_delete() 0 9 2
A bd_ends_with() 0 6 2
A term_contains() 0 15 3
B get_term_id_by_name() 0 25 9
A term_starts() 0 15 3
A term_count_query() 0 32 3
A term_ends() 0 15 3
A term_query() 0 34 1
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
		return $length === $zero ||
154
		( $substr === $needle );
155
	}
156
157
	/**
158
	 * Get terms which is start with specified string.
159
	 *
160
	 * @param string $term_text user input text.
161
	 * @param array  $options   user options.
162
	 *
163
	 * @return array term ids.
164
	 */
165
	protected function term_starts( $term_text, $options ) {
166
		$term_ids = array();
167
		$terms    = get_terms(
168
			$options['taxonomy'], array(
169
				'hide_empty' => false,
170
			)
171
		);
172
173
		foreach ( $terms as $term ) {
174
			if ( $this->bd_starts_with( $term->name, $term_text ) ) {
175
				$term_ids[] = $term->term_id;
176
			}
177
		}
178
179
		return $term_ids;
180
	}
181
182
	/**
183
	 * Get terms which is ends with specified string.
184
	 *
185
	 * @param string $term_text user input text.
186
	 * @param array  $options   user options.
187
	 *
188
	 * @return array term ids.
189
	 */
190
	protected function term_ends( $term_text, $options ) {
191
		$term_ids = array();
192
		$terms    = get_terms(
193
			$options['taxonomy'], array(
194
				'hide_empty' => false,
195
			)
196
		);
197
198
		foreach ( $terms as $term ) {
199
			if ( $this->bd_ends_with( $term->name, $term_text ) ) {
200
				$term_ids[] = $term->term_id;
201
			}
202
		}
203
204
		return $term_ids;
205
	}
206
207
	/**
208
	 * Get terms which is contain specified string.
209
	 *
210
	 * @param string $term_text user input text.
211
	 * @param array  $options   user options.
212
	 *
213
	 * @return array term ids.
214
	 */
215
	protected function term_contains( $term_text, $options ) {
216
		$term_ids = array();
217
		$terms    = get_terms(
218
			$options['taxonomy'], array(
219
				'hide_empty' => false,
220
			)
221
		);
222
223
		foreach ( $terms as $term ) {
224
			if ( strpos( $term->name, $term_text ) !== false ) {
225
				$term_ids[] = $term->term_id;
226
			}
227
		}
228
229
		return $term_ids;
230
	}
231
232
	/**
233
	 * Get term ids which is have the sepcified post count .
234
	 *
235
	 * @param array $options user options.
236
	 *
237
	 * @return array term ids.
238
	 */
239 4
	protected function term_count_query( $options ) {
240 4
		$term_ids = array();
241 4
		$terms    = get_terms(
242 4
			$options['taxonomy'], array(
243 4
				'hide_empty' => false,
244
			)
245
		);
246 4
		foreach ( $terms as $term ) {
247
			$args = array(
248 4
				'post_type' => $options['post_type'],
249
				'tax_query' => array(
250
					array(
251 4
						'taxonomy' => $options['taxonomy'],
252 4
						'field'    => 'slug',
253 4
						'terms'    => $term->slug,
254
					),
255
				),
256
			);
257
258 4
			$posts = get_posts( $args );
259
260 4
			$term_id = $this->get_term_id_by_name( $options['term_text'], $options['term_opt'], $term->term_id, count( $posts ) );
261 4
			if ( ! empty( $term_id ) ) {
262 4
				$term_ids['include'][] = $term->term_id;
263
264 4
				continue;
265
			}
266
267 2
			$term_ids['exclude'][] = $term->term_id;
268
		}
269
270 4
		return $term_ids;
271
	}
272
273
	/**
274
	 * Get term id by name.
275
	 *
276
	 * @param string $term_text  user text input.
277
	 * @param array  $term_opt   user options.
278
	 * @param int    $term_id    term id.
279
	 * @param int    $post_count post count.
280
	 *
281
	 * @return int term id.
282
	 */
283 4
	protected function get_term_id_by_name( $term_text, $term_opt, $term_id, $post_count ) {
284 4
		switch ( $term_opt ) {
285 4
			case 'equal_to':
286 1
				if ( $post_count == $term_text ) {
287 1
					return $term_id;
288
				}
289 1
				break;
290
291 3
			case 'not_equal_to':
292 1
				if ( $post_count != $term_text ) {
293 1
					return $term_id;
294
				}
295
				break;
296
297 2
			case 'less_than':
298 1
				if ( $post_count < $term_text ) {
299 1
					return $term_id;
300
				}
301
				break;
302
303 1
			case 'greater_than':
304 1
				if ( $post_count > $term_text ) {
305 1
					return $term_id;
306
				}
307 1
				break;
308
		}
309 2
	}
310
311
	/**
312
	 * Wrapper for WP_Term.
313
	 *
314
	 * Adds some performance enhancing defaults.
315
	 *
316
	 * @since  6.0
317
	 *
318
	 * @param array $options  List of options.
319
	 * @param mixed $taxonomy List of Taxonomies.
320
	 *
321
	 * @return array Result array
322
	 */
323 4
	public function term_query( $options, $taxonomy ) {
324
		$defaults = array(
325 4
			'fields'     => 'ids', // retrieve only ids.
326 4
			'taxonomy'   => $taxonomy,
327 4
			'hide_empty' => 0,
328
			'count'      => true,
329
		);
330 4
		$options  = wp_parse_args( $options, $defaults );
331
332 4
		$term_query = new \WP_Term_Query();
333
334
		/**
335
		 * This action runs before the query happens.
336
		 *
337
		 * @since 5.5
338
		 * @since 5.6 added $term_query param.
339
		 *
340
		 * @param \WP_Query $term_query Query object.
341
		 */
342 4
		do_action( 'bd_before_term_query', $term_query );
343
344 4
		$terms = $term_query->query( $options );
345
346
		/**
347
		 * This action runs after the query happens.
348
		 *
349
		 * @since 5.5
350
		 * @since 5.6 added $term_query param.
351
		 *
352
		 * @param \WP_Query $term_query Query object.
353
		 */
354 4
		do_action( 'bd_after_term_query', $term_query );
355
356 4
		return $terms;
357
	}
358
}
359