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
|
177 |
|
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
|
177 |
|
if ( $delete_options['limit_to'] > 0 ) { |
31
|
39 |
|
$options['showposts'] = $delete_options['limit_to']; |
32
|
|
|
} else { |
33
|
138 |
|
$options['nopaging'] = 'true'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// post type. |
37
|
177 |
|
if ( isset( $delete_options['post_type'] ) ) { |
38
|
107 |
|
$options['post_type'] = $delete_options['post_type']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// exclude sticky posts. |
42
|
177 |
|
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
|
177 |
|
if ( $delete_options['restrict'] ) { |
48
|
82 |
|
if ( 'before' === $delete_options['date_op'] || 'after' === $delete_options['date_op'] ) { |
49
|
82 |
|
$options['date_query'] = array( |
50
|
|
|
array( |
51
|
82 |
|
'column' => 'post_date', |
52
|
82 |
|
$delete_options['date_op'] => "{$delete_options['days']} day ago", |
53
|
|
|
), |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
177 |
|
return $options; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Wrapper for WP_query. |
63
|
|
|
* |
64
|
|
|
* Adds some performance enhancing defaults. |
65
|
|
|
* |
66
|
|
|
* @since 5.5 |
67
|
|
|
* |
68
|
|
|
* @param array $options List of options. |
69
|
|
|
* |
70
|
|
|
* @return array Result array |
71
|
|
|
*/ |
72
|
|
|
function bd_query( $options ) { |
73
|
|
|
$defaults = array( |
74
|
179 |
|
'cache_results' => false, // don't cache results. |
75
|
|
|
'update_post_meta_cache' => false, // No need to fetch post meta fields. |
76
|
|
|
'update_post_term_cache' => false, // No need to fetch taxonomy fields. |
77
|
|
|
'no_found_rows' => true, // No need for pagination. |
78
|
|
|
'fields' => 'ids', // retrieve only ids. |
79
|
|
|
); |
80
|
|
|
|
81
|
179 |
|
$options = wp_parse_args( $options, $defaults ); |
82
|
|
|
|
83
|
179 |
|
$wp_query = new WP_Query(); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* This action runs before the query happens. |
87
|
|
|
* |
88
|
|
|
* @since 5.5 |
89
|
|
|
* @since 5.6 added $wp_query param. |
90
|
|
|
* |
91
|
|
|
* @param \WP_Query $wp_query Query object. |
92
|
|
|
*/ |
93
|
179 |
|
do_action( 'bd_before_query', $wp_query ); |
94
|
|
|
|
95
|
179 |
|
$posts = $wp_query->query( $options ); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* This action runs after the query happens. |
99
|
|
|
* |
100
|
|
|
* @since 5.5 |
101
|
|
|
* @since 5.6 added $wp_query param. |
102
|
|
|
* |
103
|
|
|
* @param \WP_Query $wp_query Query object. |
104
|
|
|
*/ |
105
|
179 |
|
do_action( 'bd_after_query', $wp_query ); |
106
|
|
|
|
107
|
179 |
|
return $posts; |
108
|
|
|
} |
109
|
|
|
|