Passed
Push — master ( a42385...9b46a7 )
by Paul
04:14
created

QueryBuilder   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 11.94%

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 150
ccs 8
cts 67
cp 0.1194
rs 10
c 0
b 0
f 0
wmc 29

9 Methods

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