Passed
Push — 178-feature/delete-terms--by-p... ( 473746...d0fdef )
by Rajan
11:03
created

bd_term_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 34
ccs 0
cts 9
cp 0
crap 2
rs 9.376
c 4
b 1
f 0
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 56
	if ( isset( $delete_options['private'] ) ) {
22 27
		if ( $delete_options['private'] ) {
23 1
			$options['post_status'] = 'private';
24
		} else {
25 26
			if ( ! isset( $options['post_status'] ) ) {
26 19
				$options['post_status'] = 'publish';
27
			}
28
		}
29
	}
30
31
	// limit to query
32 56
	if ( $delete_options['limit_to'] > 0 ) {
33 7
		$options['showposts'] = $delete_options['limit_to'];
34
	} else {
35 49
		$options['nopaging']  = 'true';
36
	}
37
38
	// post type
39 56
	if ( isset( $delete_options['post_type'] ) ) {
40 12
		$options['post_type'] = $delete_options['post_type'];
41
	}
42
43
	// date query
44 56
	if ( $delete_options['restrict'] ) {
45 12
		if ( 'before' === $delete_options['date_op'] || 'after' === $delete_options['date_op'] ) {
46 12
			$options['date_query'] = array(
47
				array(
48 12
					'column'                   => 'post_date',
49 12
					$delete_options['date_op'] => "{$delete_options['days']} day ago",
50
				),
51
			);
52
		}
53
	}
54
55 56
	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 63
		'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 63
	$options = wp_parse_args( $options, $defaults );
78
79 63
	$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 63
	do_action( 'bd_before_query', $wp_query );
90
91 63
	$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 63
	do_action( 'bd_after_query', $wp_query );
102
103 63
	return $posts;
104
}
105