Passed
Pull Request — dev/6.0.0 (#332)
by Rajan
23:08 queued 19:58
created

bd_term_contains()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
c 2
b 1
f 0
nc 3
nop 2
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 12
rs 9.8333
1
<?php
2
/**
3
 * Utility and wrapper functions for WP_Query.
4
 *
5
 * @since      5.5
6
 *
7
 * @author     Sudar
8
 *
9
 * @package    BulkDelete\Util
10
 */
11
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
12
13
/**
14
 * Process delete options array and build query.
15
 *
16
 * @param array $delete_options Delete Options
17
 * @param array $options        (optional) Options query
18
 */
19
function bd_build_query_options( $delete_options, $options = array() ) {
20
	// private posts
21 44
	if ( isset( $delete_options['private'] ) ) {
22 27
		if ( $delete_options['private'] ) {
23 1
			$options['post_status'] = 'private';
24
		} else {
25 26
			if ( ! isset( $options['post_status'] ) ) {
26 19
				$options['post_status'] = 'publish';
27
			}
28
		}
29
	}
30
31
	// limit to query
32 44
	if ( $delete_options['limit_to'] > 0 ) {
33 6
		$options['showposts'] = $delete_options['limit_to'];
34
	} else {
35 38
		$options['nopaging']  = 'true';
36
	}
37
38
	// post type
39 44
	if ( isset( $delete_options['post_type'] ) ) {
40 12
		$options['post_type'] = $delete_options['post_type'];
41
	}
42
43
	// date query
44 44
	if ( $delete_options['restrict'] ) {
45 12
		if ( 'before' === $delete_options['date_op'] || 'after' === $delete_options['date_op'] ) {
46 12
			$options['date_query'] = array(
47
				array(
48 12
					'column'                   => 'post_date',
49 12
					$delete_options['date_op'] => "{$delete_options['days']} day ago",
50
				),
51
			);
52
		}
53
	}
54
55 44
	return $options;
56
}
57
58
/**
59
 * Wrapper for WP_query.
60
 *
61
 * Adds some performance enhancing defaults.
62
 *
63
 * @since  5.5
64
 *
65
 * @param array $options List of options
66
 *
67
 * @return array Result array
68
 */
69
function bd_query( $options ) {
70
	$defaults = array(
71 51
		'cache_results'          => false, // don't cache results
72
		'update_post_meta_cache' => false, // No need to fetch post meta fields
73
		'update_post_term_cache' => false, // No need to fetch taxonomy fields
74
		'no_found_rows'          => true,  // No need for pagination
75
		'fields'                 => 'ids', // retrieve only ids
76
	);
77 51
	$options = wp_parse_args( $options, $defaults );
78
79 51
	$wp_query = new WP_Query();
80
81
	/**
82
	 * This action runs before the query happens.
83
	 *
84
	 * @since 5.5
85
	 * @since 5.6 added $wp_query param.
86
	 *
87
	 * @param \WP_Query $wp_query Query object.
88
	 */
89 51
	do_action( 'bd_before_query', $wp_query );
90
91 51
	$posts = $wp_query->query( $options );
92
93
	/**
94
	 * This action runs after the query happens.
95
	 *
96
	 * @since 5.5
97
	 * @since 5.6 added $wp_query param.
98
	 *
99
	 * @param \WP_Query $wp_query Query object.
100
	 */
101 51
	do_action( 'bd_after_query', $wp_query );
102
103 51
	return $posts;
104
}
105
106
/**
107
 * Wrapper for WP_Term.
108
 *
109
 * Adds some performance enhancing defaults.
110
 *
111
 * @since  6.0
112
 *
113
 * @param array $options  List of options
114
 * @param mixed $taxonomy
115
 *
116
 * @return array Result array
117
 */
118
function bd_term_query( $options, $taxonomy ) {
119
	$defaults = array(
120
		'fields'                 => 'ids', // retrieve only ids
121
		'taxonomy'				           => $taxonomy,
122
		'hide_empty'			          => 0,
123
		'count'					             => true,
124
	);
125
	$options = wp_parse_args( $options, $defaults );
126
127
	$term_query = new WP_Term_Query();
128
129
	/**
130
	 * This action runs before the query happens.
131
	 *
132
	 * @since 5.5
133
	 * @since 5.6 added $term_query param.
134
	 *
135
	 * @param \WP_Query $term_query Query object.
136
	 */
137
	do_action( 'bd_before_term_query', $term_query );
138
139
	$terms = $term_query->query( $options );
140
141
	/**
142
	 * This action runs after the query happens.
143
	 *
144
	 * @since 5.5
145
	 * @since 5.6 added $term_query param.
146
	 *
147
	 * @param \WP_Query $term_query Query object.
148
	 */
149
	do_action( 'bd_after_term_query', $term_query );
150
151
	return $terms;
152
}
153
154
function bd_starts_with($haystack, $needle)
155
{
156
     $length = strlen($needle);
157
158
     return (substr($haystack, 0, $length) === $needle);
159
}
160
161
function bd_ends_with($haystack, $needle)
162
{
163
    $length = strlen($needle);
164
165
    return $length === 0 ||
166
    (substr($haystack, -$length) === $needle);
167
}
168
169
function bd_term_starts( $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( bd_starts_with( $term->name, $term_text ) ){
177
			$term_ids[] = $term->term_id;
178
		}
179
	}
180
181
	return $term_ids;
182
}
183
184
function bd_term_ends( $term_text , $options ){
185
	$term_ids = array();
186
	$terms    = get_terms( $options['taxonomy'], array(
187
	    'hide_empty' => false,
188
	) );
189
190
	foreach( $terms as $term ){
191
		if( bd_ends_with( $term->name, $term_text ) ){
192
			$term_ids[] = $term->term_id;
193
		}
194
	}
195
196
	return $term_ids;
197
}
198
199
function bd_term_contains( $term_text , $options ){
200
	$term_ids = array();
201
	$terms    = get_terms( $options['taxonomy'], array(
202
	    'hide_empty' => false,
203
	) );
204
205
	foreach( $terms as $term ){
206
		if ( strpos( $term->name, $term_text ) !== false ) {
207
			$term_ids[] = $term->term_id;
208
		}
209
	}
210
211
	return $term_ids;
212
}
213