|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Utility and wrapper functions for WP_Query. |
|
4
|
|
|
* |
|
5
|
|
|
* @since 5.5 |
|
6
|
|
|
*/ |
|
7
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Process delete options array and build query. |
|
11
|
|
|
* |
|
12
|
|
|
* @param array $delete_options Delete Options. |
|
13
|
|
|
* @param array $options (optional) Options query. |
|
14
|
|
|
* |
|
15
|
|
|
* @return array |
|
16
|
|
|
*/ |
|
17
|
|
|
function bd_build_query_options( $delete_options, $options = array() ) { |
|
18
|
|
|
// private posts. |
|
19
|
182 |
|
if ( isset( $delete_options['private'] ) ) { |
|
20
|
25 |
|
if ( $delete_options['private'] ) { |
|
21
|
1 |
|
$options['post_status'] = 'private'; |
|
22
|
|
|
} else { |
|
23
|
24 |
|
if ( ! isset( $options['post_status'] ) ) { |
|
24
|
19 |
|
$options['post_status'] = 'publish'; |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// limit to query. |
|
30
|
182 |
|
if ( $delete_options['limit_to'] > 0 ) { |
|
31
|
40 |
|
$options['showposts'] = $delete_options['limit_to']; |
|
32
|
|
|
} else { |
|
33
|
142 |
|
$options['nopaging'] = 'true'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// post type. |
|
37
|
182 |
|
if ( isset( $delete_options['post_type'] ) ) { |
|
38
|
107 |
|
$options['post_type'] = $delete_options['post_type']; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// exclude sticky posts. |
|
42
|
182 |
|
if ( isset( $delete_options['exclude_sticky'] ) && ( true === $delete_options['exclude_sticky'] ) ) { |
|
43
|
|
|
$options['post__not_in'] = get_option( 'sticky_posts' ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// date query. |
|
47
|
182 |
|
if ( $delete_options['restrict'] ) { |
|
48
|
84 |
|
if ( 'before' === $delete_options['date_op'] || 'after' === $delete_options['date_op'] ) { |
|
49
|
84 |
|
$options['date_query'] = array( |
|
50
|
|
|
array( |
|
51
|
84 |
|
'column' => 'post_date', |
|
52
|
84 |
|
$delete_options['date_op'] => "{$delete_options['days']} day ago", |
|
53
|
|
|
), |
|
54
|
|
|
); |
|
55
|
|
|
} elseif ( '=' === $delete_options['date_op'] ) { |
|
56
|
|
|
$published_date = getdate( strtotime( $delete_options['pub_date'] ) ); |
|
57
|
|
|
$options['date_query'] = [ |
|
58
|
182 |
|
[ |
|
59
|
|
|
'year' => $published_date['year'], |
|
60
|
|
|
'month' => $published_date['mon'], |
|
61
|
|
|
'day' => $published_date['mday'], |
|
62
|
|
|
], |
|
63
|
|
|
]; |
|
64
|
|
|
} elseif ( 'between' === $delete_options['date_op'] ) { |
|
65
|
|
|
$published_date_start = date( 'Y-m-d', strtotime( $delete_options['pub_date_start'] ) ); |
|
66
|
|
|
$published_date_end = date( 'Y-m-d', strtotime( $delete_options['pub_date_end'] ) ); |
|
67
|
|
|
$options['date_query'] = [ |
|
68
|
|
|
[ |
|
69
|
|
|
'after' => $published_date_start, |
|
70
|
|
|
'before' => $published_date_end, |
|
71
|
|
|
'inclusive' => true, |
|
72
|
|
|
], |
|
73
|
|
|
]; |
|
74
|
184 |
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $options; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
184 |
|
* Wrapper for WP_query. |
|
82
|
|
|
* |
|
83
|
184 |
|
* Adds some performance enhancing defaults. |
|
84
|
|
|
* |
|
85
|
|
|
* @since 5.5 |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $options List of options. |
|
88
|
|
|
* |
|
89
|
|
|
* @return array Result array |
|
90
|
|
|
*/ |
|
91
|
|
|
function bd_query( $options ) { |
|
92
|
|
|
$defaults = array( |
|
93
|
184 |
|
'cache_results' => false, // don't cache results. |
|
94
|
|
|
'update_post_meta_cache' => false, // No need to fetch post meta fields. |
|
95
|
184 |
|
'update_post_term_cache' => false, // No need to fetch taxonomy fields. |
|
96
|
|
|
'no_found_rows' => true, // No need for pagination. |
|
97
|
|
|
'fields' => 'ids', // retrieve only ids. |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$options = wp_parse_args( $options, $defaults ); |
|
101
|
|
|
|
|
102
|
|
|
$wp_query = new WP_Query(); |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
184 |
|
* This action runs before the query happens. |
|
106
|
|
|
* |
|
107
|
184 |
|
* @since 5.5 |
|
108
|
|
|
* @since 5.6 added $wp_query param. |
|
109
|
|
|
* |
|
110
|
|
|
* @param \WP_Query $wp_query Query object. |
|
111
|
|
|
*/ |
|
112
|
|
|
do_action( 'bd_before_query', $wp_query ); |
|
113
|
|
|
|
|
114
|
|
|
$posts = $wp_query->query( $options ); |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* This action runs after the query happens. |
|
118
|
|
|
* |
|
119
|
|
|
* @since 5.5 |
|
120
|
|
|
* @since 5.6 added $wp_query param. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \WP_Query $wp_query Query object. |
|
123
|
|
|
*/ |
|
124
|
|
|
do_action( 'bd_after_query', $wp_query ); |
|
125
|
|
|
|
|
126
|
|
|
return $posts; |
|
127
|
|
|
} |
|
128
|
|
|
|