|
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
|
|
|
return (substr($haystack, 0, $length) === $needle); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
function bd_ends_with($haystack, $needle) |
|
161
|
|
|
{ |
|
162
|
|
|
$length = strlen($needle); |
|
163
|
|
|
|
|
164
|
|
|
return $length === 0 || |
|
165
|
|
|
(substr($haystack, -$length) === $needle); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
function bd_term_starts( $term_text , $options ){ |
|
169
|
|
|
$term_ids = array(); |
|
170
|
|
|
$terms = get_terms( $options['taxonomy'], array( |
|
171
|
|
|
'hide_empty' => false, |
|
172
|
|
|
) ); |
|
173
|
|
|
|
|
174
|
|
|
foreach( $terms as $term ){ |
|
175
|
|
|
if( bd_starts_with( $term->name, $term_text ) ){ |
|
176
|
|
|
$term_ids[] = $term->term_id; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
return $term_ids; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
function bd_term_ends( $term_text , $options ){ |
|
183
|
|
|
$term_ids = array(); |
|
184
|
|
|
$terms = get_terms( $options['taxonomy'], array( |
|
185
|
|
|
'hide_empty' => false, |
|
186
|
|
|
) ); |
|
187
|
|
|
|
|
188
|
|
|
foreach( $terms as $term ){ |
|
189
|
|
|
if( bd_ends_with( $term->name, $term_text ) ){ |
|
190
|
|
|
$term_ids[] = $term->term_id; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
return $term_ids; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
function bd_term_contains( $term_text , $options ){ |
|
197
|
|
|
$term_ids = array(); |
|
198
|
|
|
$terms = get_terms( $options['taxonomy'], array( |
|
199
|
|
|
'hide_empty' => false, |
|
200
|
|
|
) ); |
|
201
|
|
|
|
|
202
|
|
|
foreach( $terms as $term ){ |
|
203
|
|
|
if ( strpos( $term->name, $term_text ) !== false ) { |
|
204
|
|
|
$term_ids[] = $term->term_id; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
return $term_ids; |
|
208
|
|
|
} |