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