Passed
Push — master ( 78492a...4f268f )
by Paul
04:26
created

QueryBuilder::buildQueryCategory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 6
rs 10
c 0
b 0
f 0
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, __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;
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
		$pagedQuery = !is_front_page()
82
			? Application::PAGED_QUERY_VAR
83
			: 'page';
84
		return $isEnabled
85
			? max( 1, intval( get_query_var( $pagedQuery )))
86
			: 1;
87
	}
88
89
	/**
90
	 * @param string $value
91
	 * @return void|array
92
	 */
93
	protected function buildQueryAssignedTo( $value )
94
	{
95
		if( empty( $value ))return;
96
		return [
97
			'compare' => 'IN',
98
			'key' => 'assigned_to',
99
			'value' => array_filter( array_map( 'trim', explode( ',', $value )), 'is_numeric' ),
100
		];
101
	}
102
103
	/**
104
	 * @param array $value
105
	 * @return void|array
106
	 */
107
	protected function buildQueryCategory( $value )
108
	{
109
		if( empty( $value ))return;
110
		return [
111
			'field' => 'term_id',
112
			'taxonomy' => Application::TAXONOMY,
113
			'terms' => $value,
114
		];
115
	}
116
117
	/**
118
	 * @param string $value
119
	 * @return void|array
120
	 */
121
	protected function buildQueryRating( $value )
122
	{
123
		if( !is_numeric( $value ) || !in_array( intval( $value ), range( 1, 5 )))return;
124
		return [
125
			'compare' => '>=',
126
			'key' => 'rating',
127
			'value' => $value,
128
		];
129
	}
130
131
	/**
132
	 * @param string $value
133
	 * @return void|array
134
	 */
135
	protected function buildQueryType( $value )
136
	{
137
		if( in_array( $value, ['','all'] ))return;
138
		return [
139
			'key' => 'review_type',
140
			'value' => $value,
141
		];
142
	}
143
}
144