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
|
|
|
if ( isset( $delete_options['private'] ) ) { |
22
|
|
|
if ( $delete_options['private'] ) { |
23
|
|
|
$options['post_status'] = 'private'; |
24
|
|
|
} else { |
25
|
|
|
if ( ! isset( $options['post_status'] ) ) { |
26
|
|
|
$options['post_status'] = 'publish'; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// limit to query |
32
|
|
|
if ( $delete_options['limit_to'] > 0 ) { |
33
|
|
|
$options['showposts'] = $delete_options['limit_to']; |
34
|
|
|
} else { |
35
|
|
|
$options['nopaging'] = 'true'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// post type |
39
|
|
|
if ( isset( $delete_options['post_type'] ) ) { |
40
|
|
|
$options['post_type'] = $delete_options['post_type']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// date query |
44
|
|
|
if ( $delete_options['restrict'] ) { |
45
|
|
|
if ( 'before' === $delete_options['date_op'] || 'after' === $delete_options['date_op'] ) { |
46
|
|
|
$options['date_query'] = array( |
47
|
|
|
array( |
48
|
|
|
'column' => 'post_date', |
49
|
|
|
$delete_options['date_op'] => "{$delete_options['days']} day ago", |
50
|
|
|
), |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
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
|
|
|
'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
|
|
|
$options = wp_parse_args( $options, $defaults ); |
78
|
|
|
|
79
|
|
|
$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
|
|
|
do_action( 'bd_before_query', $wp_query ); |
90
|
|
|
|
91
|
|
|
$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
|
|
|
do_action( 'bd_after_query', $wp_query ); |
102
|
|
|
|
103
|
|
|
return $posts; |
104
|
|
|
} |
105
|
|
|
|