Completed
Push — master ( ab4944...534e9c )
by Andrew
02:30
created

ClassyQueryHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 69
rs 10
wmc 8
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A find_query() 0 20 3
A get_current_query() 0 11 1
A handle_maybe_custom_posts_page() 0 15 4
1
<?php
2
3
class ClassyQueryHelper {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	/**
6
	 * Finds or creates new query based on provided params
7
	 * 
8
	 * @param  array/boolean $args
0 ignored issues
show
Documentation introduced by
The doc-type array/boolean could not be parsed: Unknown type name "array/boolean" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
9
	 * @return object        WP_Query
10
	 */
11
	public static function find_query($args = false) {
12
13
		$default_args = array('fields' => 'ids');
14
15
		if (!$args) {
16
		
17
			return self::get_current_query();
18
		
19
		} elseif (is_array($args)) {
20
21
			$args = array_merge($default_args, $args);
22
23
			return new WP_Query($args);
24
25
		} else {
26
27
			return new WP_Query($default_args);
28
29
		}
30
	}
31
32
	/**
33
	 * Returns current WP_Query
34
	 * 
35
	 * @return object WP_Query
36
	 */
37
	public static function get_current_query() {
38
39
        global $wp_query;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
40
41
        $query =& $wp_query;
42
43
        $query = self::handle_maybe_custom_posts_page($query);
44
45
        return $query;
46
47
	}
48
49
	/**
50
	 * Checks and returns WP_Query for home posts page
51
	 * 
52
	 * @param  object $query WP_Query
53
	 * @return object        WP_Query
54
	 */
55
    private static function handle_maybe_custom_posts_page( $query ) {
56
57
    	if ($custom_posts_page = get_option('page_for_posts')) {
58
        
59
        	if ( isset($query->query['p']) && $query->query['p'] == $custom_posts_page ) {
60
        
61
        		return new WP_Query(array('post_type' => 'post'));
62
        
63
        	}
64
        
65
        }
66
        
67
        return $query;
68
69
    }
70
71
}