Passed
Push — master ( 6b8ca8...3384db )
by Paul
04:57
created

QueryBuilder::filterSearchByTitle()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 17
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 12
nc 9
nop 2
crap 42
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Helper;
7
use WP_Query;
8
9
class QueryBuilder
10
{
11
	/**
12
	 * Build a WP_Query meta_query/tax_query
13
	 * @return array
14
	 */
15
	public function buildQuery( array $keys = [], array $values = [] )
16
	{
17
		$queries = [];
18
		foreach( $keys as $key ) {
19
			if( !array_key_exists( $key, $values ))continue;
20
			$methodName = glsr( Helper::class )->buildMethodName( $key, $prefix = __METHOD__ );
21
			if( !method_exists( $this, $methodName ))continue;
22
			$query = call_user_func( [$this, $methodName], $values[$key] );
23
			if( is_array( $query )) {
24
				$queries[] = $query;
25
			}
26
		}
27
		return $queries;
28
	}
29
30
	/**
31
	 * Build a SQL 'OR' string from an array
32
	 * @param string|array $values
33
	 * @param string $sprintfFormat
34
	 * @return string
35
	 */
36
	public function buildSqlOr( $values, $sprintfFormat )
37
	{
38
		if( !is_array( $values )) {
39
			$values = explode( ',', $values );
40
		}
41
		$values = array_filter( array_map( 'trim', (array)$values ));
42
		$values = array_map( function( $value ) use( $sprintfFormat ) {
43
			return sprintf( $sprintfFormat, $value );
44
		}, $values );
45
		return implode( ' OR ', $values );
46
	}
47
48
	/**
49
	 * Search SQL filter for matching against post title only.
50
	 * @link http://wordpress.stackexchange.com/a/11826/1685
51
	 * @param string $search
52
	 * @return string
53
	 * @filter posts_search
54
	 */
55
	public function filterSearchByTitle( $search, WP_Query $query )
56
	{
57
		if( empty( $search ) || empty( $query->get( 'search_terms' ))) {
58
			return $search;
59
		}
60
		global $wpdb;
1 ignored issue
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...
61
		$n = empty( $query->get( 'exact' ))
62
			? '%'
63
			: '';
64
		$search = [];
65
		foreach( (array)$query->get( 'search_terms' ) as $term ) {
66
			$search[] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $n.$wpdb->esc_like( $term ).$n );
67
		}
68
		if( !is_user_logged_in() ) {
69
			$search[] = "{$wpdb->posts}.post_password = ''";
70
		}
71
		return ' AND '.implode( ' AND ', $search );
72
	}
73
74
	/**
75
	 * Get the current page number from the global query
76
	 * @param bool $isEnabled
77
	 * @return int
78
	 */
79
	public function getPaged( $isEnabled = true )
80
	{
81
		$paged = $isEnabled
82
			? intval( get_query_var( is_front_page() ? 'page' : Application::PAGED_QUERY_VAR ))
83
			: 1;
84
		return max( 1, $paged );
85
	}
86
87
	/**
88
	 * @param string $value
89
	 * @return void|array
90
	 */
91
	protected function buildQueryAssignedTo( $value )
92
	{
93
		if( empty( $value ))return;
94
		return [
95
			'compare' => 'IN',
96
			'key' => 'assigned_to',
97
			'value' => array_filter( array_map( 'trim', explode( ',', $value )), 'is_numeric' ),
98
		];
99
	}
100
101
	/**
102
	 * @param array $value
103
	 * @return void|array
104
	 */
105
	protected function buildQueryCategory( $value )
106
	{
107
		if( empty( $value ))return;
108
		return [
109
			'field' => 'term_id',
110
			'taxonomy' => Application::TAXONOMY,
111
			'terms' => $value,
112
		];
113
	}
114
115
	/**
116
	 * @param string $value
117
	 * @return void|array
118
	 */
119
	protected function buildQueryRating( $value )
120
	{
121
		if( !is_numeric( $value ) || !in_array( intval( $value ), range( 1, 5 )))return;
122
		return [
123
			'compare' => '>=',
124
			'key' => 'rating',
125
			'value' => $value,
126
		];
127
	}
128
129
	/**
130
	 * @param string $value
131
	 * @return void|array
132
	 */
133
	protected function buildQueryType( $value )
134
	{
135
		if( in_array( $value, ['','all'] ))return;
136
		return [
137
			'key' => 'review_type',
138
			'value' => $value,
139
		];
140
	}
141
}
142