Test Failed
Push — master ( 87eb8d...25783a )
by Paul
03:58
created

QueryBuilder   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 149
ccs 0
cts 98
cp 0
rs 10
c 0
b 0
f 0
wmc 29

9 Methods

Rating   Name   Duplication   Size   Complexity  
A buildQuery() 0 13 5
A buildQueryCategory() 0 7 2
A buildQueryRating() 0 7 3
A buildSqlLines() 0 12 4
A getPaged() 0 8 3
A buildQueryType() 0 6 2
A buildSqlOr() 0 10 2
A buildQueryAssignedTo() 0 7 2
A filterSearchByTitle() 0 17 6
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, __FUNCTION__ );
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
	 * @return string
32
	 */
33
	public function buildSqlLines( array $values, array $conditions )
34
	{
35
		$string = '';
36
		$values = array_filter( $values );
37
		foreach( $conditions as $key => $value ) {
38
			if( !isset( $values[$key] ))continue;
39
			$values[$key] = implode( ',', (array)$values[$key] );
40
			$string .= strpos( $value, '%s' ) !== false
41
				? sprintf( $value, strval( $values[$key] ))
42
				: $value;
43
		}
44
		return $string;
45
	}
46
47
	/**
48
	 * Build a SQL 'OR' string from an array
49
	 * @param string|array $values
50
	 * @param string $sprintfFormat
51
	 * @return string
52
	 */
53
	public function buildSqlOr( $values, $sprintfFormat )
54
	{
55
		if( !is_array( $values )) {
56
			$values = explode( ',', $values );
57
		}
58
		$values = array_filter( array_map( 'trim', (array)$values ));
59
		$values = array_map( function( $value ) use( $sprintfFormat ) {
60
			return sprintf( $sprintfFormat, $value );
61
		}, $values );
62
		return implode( ' OR ', $values );
63
	}
64
65
	/**
66
	 * Search SQL filter for matching against post title only.
67
	 * @link http://wordpress.stackexchange.com/a/11826/1685
68
	 * @param string $search
69
	 * @return string
70
	 * @filter posts_search
71
	 */
72
	public function filterSearchByTitle( $search, WP_Query $query )
73
	{
74
		if( empty( $search ) || empty( $query->get( 'search_terms' ))) {
75
			return $search;
76
		}
77
		global $wpdb;
78
		$n = empty( $query->get( 'exact' ))
79
			? '%'
80
			: '';
81
		$search = [];
82
		foreach( (array)$query->get( 'search_terms' ) as $term ) {
83
			$search[] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $n.$wpdb->esc_like( $term ).$n );
84
		}
85
		if( !is_user_logged_in() ) {
86
			$search[] = "{$wpdb->posts}.post_password = ''";
87
		}
88
		return ' AND '.implode( ' AND ', $search );
89
	}
90
91
	/**
92
	 * Get the current page number from the global query
93
	 * @param bool $isEnabled
94
	 * @return int
95
	 */
96
	public function getPaged( $isEnabled = true )
97
	{
98
		$pagedQuery = !is_front_page()
99
			? Application::PAGED_QUERY_VAR
100
			: 'page';
101
		return $isEnabled
102
			? max( 1, intval( get_query_var( $pagedQuery )))
103
			: 1;
104
	}
105
106
	/**
107
	 * @param string $value
108
	 * @return void|array
109
	 */
110
	protected function buildQueryAssignedTo( $value )
111
	{
112
		if( empty( $value ))return;
113
		return [
114
			'compare' => 'IN',
115
			'key' => 'assigned_to',
116
			'value' => array_filter( array_map( 'trim', explode( ',', $value )), 'is_numeric' ),
117
		];
118
	}
119
120
	/**
121
	 * @param array $value
122
	 * @return void|array
123
	 */
124
	protected function buildQueryCategory( $value )
125
	{
126
		if( empty( $value ))return;
127
		return [
128
			'field' => 'term_id',
129
			'taxonomy' => Application::TAXONOMY,
130
			'terms' => $value,
131
		];
132
	}
133
134
	/**
135
	 * @param string $value
136
	 * @return void|array
137
	 */
138
	protected function buildQueryRating( $value )
139
	{
140
		if( !is_numeric( $value ) || !in_array( intval( $value ), range( 1, 5 )))return;
141
		return [
142
			'compare' => '>=',
143
			'key' => 'rating',
144
			'value' => $value,
145
		];
146
	}
147
148
	/**
149
	 * @param string $value
150
	 * @return void|array
151
	 */
152
	protected function buildQueryType( $value )
153
	{
154
		if( in_array( $value, ['','all'] ))return;
155
		return [
156
			'key' => 'review_type',
157
			'value' => $value,
158
		];
159
	}
160
}
161