Passed
Push — master ( ece31d...41b8a6 )
by Paul
10:20 queued 04:17
created

QueryBuilder::filterSearchByTitle()   A

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