Completed
Pull Request — dev/6.0.0 (#382)
by Rajan
14:57 queued 11:39
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 18
CRAP Score 11

Importance

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

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