Passed
Push — 358-fix/delete-posts-by-post-s... ( 9066af...5ee68c )
by Rajan
18:36 queued 07:39
created

TermsModule::term_count_query()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 0
loc 24
ccs 0
cts 17
cp 0
crap 6
rs 9.7998
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
	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 = $this->term_query( $query, $options['taxonomy'] );
72
73
		return $this->delete_terms_by_id( $term_ids, $options );
74
	}
75
76
	/**
77
	 * Delete terms by ids.
78
	 *
79
	 * @param int[] $term_ids List of term ids to delete.
80
	 * @param mixed $options
81
	 *
82
	 * @return int Number of posts deleted.
83
	 */
84
	protected function delete_terms_by_id( $term_ids, $options ) {
85
		$count = 0;
86
87
		foreach ( $term_ids as $term_id ) {
88
			$term = get_term( $term_id, $options['taxonomy'] );
89
90
			if( is_wp_error($term) ){
91
				continue;
92
			}
93
94
			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...
95
				continue;
96
			}
97
98
			wp_delete_term( $term_id, $options['taxonomy'] );
99
			$count ++;
100
		}
101
102
		return $count;
103
	}
104
105
	/**
106
	 * custom string function use to get is string start with specified string.
107
	 *
108
	 * @param string $haystack.
109
	 * @param string $needle.
110
	 *
111
	 * @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...
112
	 */
113
	protected function bd_starts_with($haystack, $needle){
114
	     $length = strlen($needle);
115
116
	     return (substr($haystack, 0, $length) === $needle);
117
	}
118
119
	/**
120
	 * custom string function use to get is string ends with specified string.
121
	 *
122
	 * @param string $haystack.
123
	 * @param string $needle.
124
	 *
125
	 * @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...
126
	 */
127
	protected function bd_ends_with($haystack, $needle){
128
	    $length = strlen($needle);
129
130
	    return $length === 0 ||
131
	    (substr($haystack, -$length) === $needle);
132
	}
133
134
	/**
135
	 * get terms which is start with specified string.
136
	 *
137
	 * @param string $term_text.
138
	 * @param array  $options.
139
	 *
140
	 * @return array term ids.
141
	 */
142
	protected function term_starts( $term_text , $options ){
143
		$term_ids = array();
144
		$terms    = get_terms( $options['taxonomy'], array(
145
		    'hide_empty' => false,
146
		) );
147
148
		foreach( $terms as $term ){
149
			if( $this->bd_starts_with( $term->name, $term_text ) ){
150
				$term_ids[] = $term->term_id;
151
			}
152
		}
153
154
		return $term_ids;
155
	}
156
157
	/**
158
	 * get terms which is ends with specified string.
159
	 *
160
	 * @param string $term_text.
161
	 * @param array  $options.
162
	 *
163
	 * @return array term ids.
164
	 */
165
	protected function term_ends( $term_text , $options ){
166
		$term_ids = array();
167
		$terms    = get_terms( $options['taxonomy'], array(
168
		    'hide_empty' => false,
169
		) );
170
171
		foreach( $terms as $term ){
172
			if( $this->bd_ends_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 contain specified string.
182
	 *
183
	 * @param string $term_text.
184
	 * @param array  $options.
185
	 *
186
	 * @return array term ids.
187
	 */
188
	protected function term_contains( $term_text , $options ){
189
		$term_ids = array();
190
		$terms    = get_terms( $options['taxonomy'], array(
191
		    'hide_empty' => false,
192
		) );
193
194
		foreach( $terms as $term ){
195
			if ( strpos( $term->name, $term_text ) !== false ) {
196
				$term_ids[] = $term->term_id;
197
			}
198
		}
199
200
		return $term_ids;
201
	}
202
203
	/**
204
	 * Get term ids which is have the sepcified post count .
205
	 *
206
	 * @param array $options.
207
	 *
208
	 * @return array term ids.
209
	 */
210
	protected function term_count_query( $options ){
211
		$term_ids = array();
212
		$terms    = get_terms( $options['taxonomy'], array(
213
		    'hide_empty' => false,
214
		) );
215
216
		foreach( $terms as $term ){
217
			$args = array(
218
				'post_type' => 'post',
219
				'tax_query' => array(
220
					array(
221
						'taxonomy' => $options['taxonomy'],
222
						'field'    => 'slug',
223
						'terms'    => $term->slug,
224
					),
225
				),
226
			);
227
228
			$posts = get_posts($args);
229
230
			$term_ids[] = $this->get_term_id_by_name( $options['term_text'], $options['term_opt'], $term->term_id, count($posts) );
231
		}
232
233
		return $term_ids;
234
	}
235
236
	protected function get_term_id_by_name( $term_text, $term_opt, $term_id, $post_count ){
237
		switch ($term_opt) {
238
			case 'equal_to':
239
				if( $post_count == $term_text )
240
				return $term_id;
241
				break;
242
243
			case 'not_equal_to':
244
				if( $post_count != $term_text )
245
				return $term_id;
246
				break;
247
248
			case 'less_than':
249
				if( $post_count < $term_text )
250
				return $term_id;
251
				break;
252
253
			case 'greater_than':
254
				if( $post_count > $term_text )
255
				return $term_id;
256
				break;
257
		}
258
	}
259
260
	/**
261
	 * Wrapper for WP_Term.
262
	 *
263
	 * Adds some performance enhancing defaults.
264
	 *
265
	 * @since  6.0
266
	 *
267
	 * @param array $options  List of options
268
	 * @param mixed $taxonomy
269
	 *
270
	 * @return array Result array
271
	 */
272
	public function term_query( $options, $taxonomy ) {
273
		$defaults = array(
274
			'fields'     => 'ids', // retrieve only ids
275
			'taxonomy'	  => $taxonomy,
276
			'hide_empty' => 0,
277
			'count'		    => true,
278
		);
279
		$options = wp_parse_args( $options, $defaults );
280
281
		$term_query = new \WP_Term_Query();
282
283
		/**
284
		 * This action runs before the query happens.
285
		 *
286
		 * @since 5.5
287
		 * @since 5.6 added $term_query param.
288
		 *
289
		 * @param \WP_Query $term_query Query object.
290
		 */
291
		do_action( 'bd_before_term_query', $term_query );
292
293
		$terms = $term_query->query( $options );
294
295
		/**
296
		 * This action runs after the query happens.
297
		 *
298
		 * @since 5.5
299
		 * @since 5.6 added $term_query param.
300
		 *
301
		 * @param \WP_Query $term_query Query object.
302
		 */
303
		do_action( 'bd_after_term_query', $term_query );
304
305
		return $terms;
306
	}
307
}