Passed
Pull Request — dev/6.0.0 (#382)
by Rajan
15:18
created

bd_build_query_options()   C

Complexity

Conditions 11
Paths 192

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 11

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 21
nc 192
nop 2
dl 0
loc 39
ccs 16
cts 16
cp 1
crap 11
rs 6.55
c 2
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 70
	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 70
	$limit_to = isset( $delete_options['limit_to'] ) ? $delete_options['limit_to'] : false;
33 7
	if ( $limit_to ) {
34
		$options['showposts'] = $delete_options['limit_to'];
35 63
	} else {
36
		$options['nopaging'] = 'true';
37
	}
38
39 70
	// post type.
40 12
	if ( isset( $delete_options['post_type'] ) ) {
41
		$options['post_type'] = $delete_options['post_type'];
42
	}
43
44 70
	// date query.
45 18
	$restrict = isset( $delete_options['restrict'] ) ? $delete_options['restrict'] : false;
46 18
	if ( $restrict ) {
47
		if ( 'before' === $delete_options['date_op'] || 'after' === $delete_options['date_op'] ) {
48 18
			$options['date_query'] = array(
49 18
				array(
50
					'column'                   => 'post_date',
51
					$delete_options['date_op'] => "{$delete_options['days']} day ago",
52
				),
53
			);
54
		}
55 70
	}
56
57
	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 77
function bd_query( $options ) {
72
	$defaults = array(
73
		'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 77
		'fields'                 => 'ids', // retrieve only ids.
78
	);
79 77
	$options  = wp_parse_args( $options, $defaults );
80
81
	$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 77
	 * @param \WP_Query $wp_query Query object.
90
	 */
91 77
	do_action( 'bd_before_query', $wp_query );
92
93
	$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 77
	 * @param \WP_Query $wp_query Query object.
102
	 */
103 77
	do_action( 'bd_after_query', $wp_query );
104
105
	return $posts;
106
}
107