Passed
Push — 358-fix/delete-posts-by-post-s... ( 397ce3 )
by Rajan
10:52
created

TermsModule::term_ends()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 12
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
	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'] );
0 ignored issues
show
Bug introduced by
The function bd_term_query was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
		$term_ids = /** @scrutinizer ignore-call */ bd_term_query( $query, $options['taxonomy'] );
Loading history...
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
		foreach ( $term_ids as $term_id ) {
94
			$term = get_term( $term_id, $options['taxonomy'] );
95
			if( isset( $options['no_posts'] ) ){
96
				if( $term->count == 0 ){
0 ignored issues
show
Bug introduced by
The property count does not seem to exist on WP_Error.
Loading history...
97
					wp_delete_term( $term_id, $options['taxonomy'] );
98
					$count++;
99
				}
100
			}else{
101
				wp_delete_term( $term_id, $options['taxonomy'] );
102
				$count++;
103
			}
104
		}
105
106
		return $count;
107
	}
108
109
	/**
110
	 * custom string function use to get is string start with specified string.
111
	 *
112
	 * @param string $haystack.
113
	 * @param string $needle.
114
	 *
115
	 * @return boolean.
0 ignored issues
show
Documentation Bug introduced by
The doc comment boolean. at position 0 could not be parsed: Unknown type name 'boolean.' at position 0 in boolean..
Loading history...
116
	 */
117
	protected function bd_starts_with($haystack, $needle){
118
	     $length = strlen($needle);
119
120
	     return (substr($haystack, 0, $length) === $needle);
121
	}
122
123
	/**
124
	 * custom string function use to get is string ends with specified string.
125
	 *
126
	 * @param string $haystack.
127
	 * @param string $needle.
128
	 *
129
	 * @return boolean.
0 ignored issues
show
Documentation Bug introduced by
The doc comment boolean. at position 0 could not be parsed: Unknown type name 'boolean.' at position 0 in boolean..
Loading history...
130
	 */
131
	protected function bd_ends_with($haystack, $needle){
132
	    $length = strlen($needle);
133
134
	    return $length === 0 ||
135
	    (substr($haystack, -$length) === $needle);
136
	}
137
138
	/**
139
	 * get terms which is start with specified string.
140
	 *
141
	 * @param string $term_text.
142
	 * @param array  $options.
143
	 *
144
	 * @return array term ids.
145
	 */
146
	protected function term_starts( $term_text , $options ){
147
		$term_ids = array();
148
		$terms    = get_terms( $options['taxonomy'], array(
149
		    'hide_empty' => false,
150
		) );
151
152
		foreach( $terms as $term ){
153
			if( $this->bd_starts_with( $term->name, $term_text ) ){
154
				$term_ids[] = $term->term_id;
155
			}
156
		}
157
158
		return $term_ids;
159
	}
160
161
	/**
162
	 * get terms which is ends with specified string.
163
	 *
164
	 * @param string $term_text.
165
	 * @param array  $options.
166
	 *
167
	 * @return array term ids.
168
	 */
169
	protected function term_ends( $term_text , $options ){
170
		$term_ids = array();
171
		$terms    = get_terms( $options['taxonomy'], array(
172
		    'hide_empty' => false,
173
		) );
174
175
		foreach( $terms as $term ){
176
			if( $this->bd_ends_with( $term->name, $term_text ) ){
177
				$term_ids[] = $term->term_id;
178
			}
179
		}
180
181
		return $term_ids;
182
	}
183
184
	/**
185
	 * get terms which is contain specified string.
186
	 *
187
	 * @param string $term_text.
188
	 * @param array  $options.
189
	 *
190
	 * @return array term ids.
191
	 */
192
	protected function term_contains( $term_text , $options ){
193
		$term_ids = array();
194
		$terms    = get_terms( $options['taxonomy'], array(
195
		    'hide_empty' => false,
196
		) );
197
198
		foreach( $terms as $term ){
199
			if ( strpos( $term->name, $term_text ) !== false ) {
200
				$term_ids[] = $term->term_id;
201
			}
202
		}
203
204
		return $term_ids;
205
	}
206
207
	/**
208
	 * Get term ids which is have the sepcified post count .
209
	 *
210
	 * @param array $options.
211
	 *
212
	 * @return array term ids.
213
	 */
214
	protected function term_count_query( $options ){
215
		$term_ids = array();
216
		$terms    = get_terms( $options['taxonomy'], array(
217
		    'hide_empty' => false,
218
		) );
219
220
		foreach( $terms as $term ){
221
			$args = array(
222
				'post_type' => 'post',
223
				'tax_query' => array(
224
					array(
225
						'taxonomy' => $options['taxonomy'],
226
						'field'    => 'slug',
227
						'terms'    => $term->slug,
228
					),
229
				),
230
			);
231
232
			$posts = get_posts($args);
233
234
			if( count($posts) == $options['term_text'] && $options['term_opt'] == 'equal_to' ){
235
				$term_ids[] = $term->term_id;
236
			}elseif( count($posts) != $options['term_text'] && $options['term_opt'] == 'not_equal_to' ){
237
				$term_ids[] = $term->term_id;
238
			}elseif( count($posts) < $options['term_text'] && $options['term_opt'] == 'less_than' ){
239
				$term_ids[] = $term->term_id;
240
			}elseif( count($posts) > $options['term_text'] && $options['term_opt'] == 'greater_than' ){
241
				$term_ids[] = $term->term_id;
242
			}
243
		}
244
245
		return $term_ids;
246
	}
247
248
	/**
249
	 * Wrapper for WP_Term.
250
	 *
251
	 * Adds some performance enhancing defaults.
252
	 *
253
	 * @since  6.0
254
	 *
255
	 * @param array $options  List of options
256
	 * @param mixed $taxonomy
257
	 *
258
	 * @return array Result array
259
	 */
260
	public function term_query( $options, $taxonomy ) {
261
		$defaults = array(
262
			'fields'                 => 'ids', // retrieve only ids
263
			'taxonomy'				           => $taxonomy,
264
			'hide_empty'			          => 0,
265
			'count'					             => true,
266
		);
267
		$options = wp_parse_args( $options, $defaults );
268
269
		$term_query = new WP_Term_Query();
0 ignored issues
show
Bug introduced by
The type BulkWP\BulkDelete\Core\Terms\WP_Term_Query was not found. Did you mean WP_Term_Query? If so, make sure to prefix the type with \.
Loading history...
270
271
		/**
272
		 * This action runs before the query happens.
273
		 *
274
		 * @since 5.5
275
		 * @since 5.6 added $term_query param.
276
		 *
277
		 * @param \WP_Query $term_query Query object.
278
		 */
279
		do_action( 'bd_before_term_query', $term_query );
280
281
		$terms = $term_query->query( $options );
282
283
		/**
284
		 * This action runs after 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_after_term_query', $term_query );
292
293
		return $terms;
294
	}
295
}
296