Passed
Pull Request — dev/6.0.0 (#332)
by Rajan
11:45
created

TermsModule::term_contains()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
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
	/**
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
	protected function do_delete( $options ) {
74
		$query = $this->build_query( $options );
75
76
		if ( empty( $query ) ) {
77
			// Short circuit deletion, if nothing needs to be deleted.
78
			return 0;
79
		}
80
81
		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
	protected function delete_terms_from_query( $query, $options ) {
93
		$term_ids = $this->term_query( $query, $options['taxonomy'] );
94
95
		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
	protected function delete_terms_by_id( $term_ids, $options ) {
107
		$count = 0;
108
109
		foreach ( $term_ids as $term_id ) {
110
			$term = get_term( $term_id, $options['taxonomy'] );
111
112
			if ( is_wp_error( $term ) ) {
113
				continue;
114
			}
115
116
			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
			wp_delete_term( $term_id, $options['taxonomy'] );
121
			$count ++;
122
		}
123
124
		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
		return ( substr( $haystack, 0, $length ) === $needle );
138
	}
139
140
	/**
141
	 * Custom string function use to get is string ends with specified string.
142
	 *
143
	 * @param string $haystack search string.
144
	 * @param string $needle find string.
145
	 *
146
	 * @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...
147
	 */
148
	protected function bd_ends_with( $haystack, $needle ) {
149
		$length = strlen( $needle );
150
151
		return $length === 0 ||
152
		( substr( $haystack, -$length ) === $needle );
153
	}
154
155
	/**
156
	 * Get terms which is start with specified string.
157
	 *
158
	 * @param string $term_text user input text.
159
	 * @param array  $options user options.
160
	 *
161
	 * @return array term ids.
162
	 */
163
	protected function term_starts( $term_text, $options ) {
164
		$term_ids = array();
165
		$terms    = get_terms(
166
			$options['taxonomy'], array(
167
				'hide_empty' => false,
168
			)
169
		);
170
171
		foreach ( $terms as $term ) {
172
			if ( $this->bd_starts_with( $term->name, $term_text ) ) {
173
				$term_ids[] = $term->term_id;
174
			}
175
		}
176
177
		return $term_ids;
178
	}
179
180
	/**
181
	 * Get terms which is ends with specified string.
182
	 *
183
	 * @param string $term_text user input text.
184
	 * @param array  $options user options.
185
	 *
186
	 * @return array term ids.
187
	 */
188
	protected function term_ends( $term_text, $options ) {
189
		$term_ids = array();
190
		$terms    = get_terms(
191
			$options['taxonomy'], array(
192
				'hide_empty' => false,
193
			)
194
		);
195
196
		foreach ( $terms as $term ) {
197
			if ( $this->bd_ends_with( $term->name, $term_text ) ) {
198
				$term_ids[] = $term->term_id;
199
			}
200
		}
201
202
		return $term_ids;
203
	}
204
205
	/**
206
	 * Get terms which is contain specified string.
207
	 *
208
	 * @param string $term_text user input text.
209
	 * @param array  $options user options.
210
	 *
211
	 * @return array term ids.
212
	 */
213
	protected function term_contains( $term_text, $options ) {
214
		$term_ids = array();
215
		$terms    = get_terms(
216
			$options['taxonomy'], array(
217
				'hide_empty' => false,
218
			)
219
		);
220
221
		foreach ( $terms as $term ) {
222
			if ( strpos( $term->name, $term_text ) !== false ) {
223
				$term_ids[] = $term->term_id;
224
			}
225
		}
226
227
		return $term_ids;
228
	}
229
230
	/**
231
	 * Get term ids which is have the sepcified post count .
232
	 *
233
	 * @param array $options user options.
234
	 *
235
	 * @return array term ids.
236
	 */
237
	protected function term_count_query( $options ) {
238
		$term_ids = array();
239
		$terms    = get_terms(
240
			$options['taxonomy'], array(
241
				'hide_empty' => false,
242
			)
243
		);
244
245
		foreach ( $terms as $term ) {
246
			$args = array(
247
				'post_type' => 'post',
248
				'tax_query' => array(
249
					array(
250
						'taxonomy' => $options['taxonomy'],
251
						'field'    => 'slug',
252
						'terms'    => $term->slug,
253
					),
254
				),
255
			);
256
257
			$posts = get_posts( $args );
258
259
			$term_ids[] = $this->get_term_id_by_name( $options['term_text'], $options['term_opt'], $term->term_id, count( $posts ) );
260
		}
261
262
		return $term_ids;
263
	}
264
265
	/**
266
	 * Get term id by name.
267
	 *
268
	 * @param string $term_text user text input.
269
	 * @param array  $term_opt user options.
270
	 * @param int    $term_id term id.
271
	 * @param int    $post_count post count.
272
	 *
273
	 * @return int term id.
274
	 */
275
	protected function get_term_id_by_name( $term_text, $term_opt, $term_id, $post_count ) {
276
		switch ( $term_opt ) {
277
			case 'equal_to':
278
				if ( $post_count === $term_text ) {
279
					return $term_id;
280
				}
281
				break;
282
283
			case 'not_equal_to':
284
				if ( $post_count !== $term_text ) {
285
					return $term_id;
286
				}
287
				break;
288
289
			case 'less_than':
290
				if ( $post_count < $term_text ) {
291
					return $term_id;
292
				}
293
				break;
294
295
			case 'greater_than':
296
				if ( $post_count > $term_text ) {
297
					return $term_id;
298
				}
299
				break;
300
		}
301
	}
302
303
	/**
304
	 * Wrapper for WP_Term.
305
	 *
306
	 * Adds some performance enhancing defaults.
307
	 *
308
	 * @since  6.0
309
	 *
310
	 * @param array $options  List of options.
311
	 * @param mixed $taxonomy List of Taxonomies.
312
	 *
313
	 * @return array Result array
314
	 */
315
	public function term_query( $options, $taxonomy ) {
316
		$defaults = array(
317
			'fields'     => 'ids', // retrieve only ids.
318
			'taxonomy'   => $taxonomy,
319
			'hide_empty' => 0,
320
			'count'      => true,
321
		);
322
		$options  = wp_parse_args( $options, $defaults );
323
324
		$term_query = new \WP_Term_Query();
325
326
		/**
327
		 * This action runs before the query happens.
328
		 *
329
		 * @since 5.5
330
		 * @since 5.6 added $term_query param.
331
		 *
332
		 * @param \WP_Query $term_query Query object.
333
		 */
334
		do_action( 'bd_before_term_query', $term_query );
335
336
		$terms = $term_query->query( $options );
337
338
		/**
339
		 * This action runs after the query happens.
340
		 *
341
		 * @since 5.5
342
		 * @since 5.6 added $term_query param.
343
		 *
344
		 * @param \WP_Query $term_query Query object.
345
		 */
346
		do_action( 'bd_after_term_query', $term_query );
347
348
		return $terms;
349
	}
350
}
351