Completed
Push — master ( 146458...b94c12 )
by Sudar
01:55
created

query.php ➔ bd_build_query_options()   D

Complexity

Conditions 9
Paths 48

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 20
nc 48
nop 2
dl 0
loc 38
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * Utility and wrapper functions for WP_Query.
4
 *
5
 * @since      5.5
6
 * @author     Sudar
7
 * @package    BulkDelete\Util
8
 */
9
10
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
11
12
/**
13
 * Process delete options array and build query.
14
 *
15
 * @param array $delete_options Delete Options
16
 * @param array $options        (optional) Options query
17
 */
18
function bd_build_query_options( $delete_options, $options = array() ) {
19
	// private posts
20
	if ( isset( $delete_options['private'] ) ) {
21
		if ( $delete_options['private'] ) {
22
			$options['post_status'] = 'private';
23
		} else {
24
			if ( ! isset( $options['post_status'] ) ) {
25
				$options['post_status'] = 'publish';
26
			}
27
		}
28
	}
29
30
	// limit to query
31
	if ( $delete_options['limit_to'] > 0 ) {
32
		$options['showposts'] = $delete_options['limit_to'];
33
	} else {
34
		$options['nopaging']  = 'true';
35
	}
36
37
	// post type
38
	if ( isset( $delete_options['post_type'] ) ) {
39
		$options['post_type'] = $delete_options['post_type'];
40
	}
41
42
	// date query
43
	if ( $delete_options['restrict'] ) {
44
		if ( 'before' === $delete_options['date_op'] || 'after' === $delete_options['date_op'] ) {
45
			$options['date_query'] = array(
46
				array(
47
					'column'                   => 'post_date',
48
					$delete_options['date_op'] => "{$delete_options['days']} day ago",
49
				),
50
			);
51
		}
52
	}
53
54
	return $options;
55
}
56
57
/**
58
 * Wrapper for WP_query.
59
 *
60
 * Adds some performance enhancing defaults.
61
 *
62
 * @since  5.5
63
 * @param  array $options List of options
64
 * @return array          Result array
65
 */
66
function bd_query( $options ) {
67
	$defaults = array(
68
		'cache_results'          => false, // don't cache results
69
		'update_post_meta_cache' => false, // No need to fetch post meta fields
70
		'update_post_term_cache' => false, // No need to fetch taxonomy fields
71
		'no_found_rows'          => true,  // No need for pagination
72
		'fields'                 => 'ids', // retrieve only ids
73
	);
74
	$options = wp_parse_args( $options, $defaults );
75
76
	$wp_query = new WP_Query();
77
78
	/**
79
	 * This action runs before the query happens.
80
	 *
81
	 * @since 5.5
82
	 */
83
	do_action( 'bd_before_query' );
84
85
	$posts = $wp_query->query( $options );
86
87
	/**
88
	 * This action runs after the query happens.
89
	 *
90
	 * @since 5.5
91
	 */
92
	do_action( 'bd_after_query' );
93
94
	return $posts;
95
}
96
?>
97