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

SqlQueries::getReviewsMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
c 2
b 1
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Helper;
7
8
class SqlQueries
9
{
10
    protected $db;
11
    protected $postType;
12
13 1
    public function __construct()
14
    {
15 1
        global $wpdb;
16 1
        $this->db = $wpdb;
17 1
        $this->postType = Application::POST_TYPE;
18 1
    }
19
20
    /**
21
     * @param string $metaReviewId
22
     * @return int
23
     */
24
    public function getPostIdFromReviewId($metaReviewId)
25
    {
26
        $postId = $this->db->get_var("
27
            SELECT p.ID
28
            FROM {$this->db->posts} AS p
29
            INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id
30
            WHERE p.post_type = '{$this->postType}'
31
            AND m.meta_key = '_review_id'
32
            AND m.meta_value = '{$metaReviewId}'
33
        ");
34
        return intval($postId);
35
    }
36
37
    /**
38
     * @param int $lastPostId
39
     * @param int $limit
40
     * @return array
41
     */
42 1
    public function getReviewCounts(array $args, $lastPostId = 0, $limit = 500)
43
    {
44 1
        return (array) $this->db->get_results("
45
            SELECT DISTINCT p.ID, m1.meta_value AS rating, m2.meta_value AS type
46 1
            FROM {$this->db->posts} AS p
47 1
            INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id
48 1
            INNER JOIN {$this->db->postmeta} AS m2 ON p.ID = m2.post_id
49 1
            {$this->getInnerJoinForCounts($args)}
50 1
            WHERE p.ID > {$lastPostId}
51
            AND p.post_status = 'publish'
52 1
            AND p.post_type = '{$this->postType}'
53
            AND m1.meta_key = '_rating'
54
            AND m2.meta_key = '_review_type'
55 1
            {$this->getAndForCounts($args)}
56
            ORDER By p.ID ASC
57 1
            LIMIT {$limit}
58
        ");
59
    }
60
61
    /**
62
     * @todo remove this?
63
     * @param string $metaKey
64
     * @return array
65
     */
66
    public function getReviewCountsFor($metaKey)
67
    {
68
        $metaKey = glsr(Helper::class)->prefix('_', $metaKey);
69
        return (array) $this->db->get_results("
70
            SELECT DISTINCT m.meta_value AS name, COUNT(*) num_posts
71
            FROM {$this->db->posts} AS p
72
            INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id
73
            WHERE p.post_type = '{$this->postType}'
74
            AND m.meta_key = '{$metaKey}'
75
            GROUP BY name
76
        ");
77
    }
78
79
    /**
80
     * @todo remove this?
81
     * @param string $reviewType
82
     * @return array
83
     */
84
    public function getReviewIdsByType($reviewType)
85
    {
86
        $results = $this->db->get_col("
87
            SELECT DISTINCT m1.meta_value AS review_id
88
            FROM {$this->db->posts} AS p
89
            INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id
90
            INNER JOIN {$this->db->postmeta} AS m2 ON p.ID = m2.post_id
91
            WHERE p.post_type = '{$this->postType}'
92
            AND m1.meta_key = '_review_id'
93
            AND m2.meta_key = '_review_type'
94
            AND m2.meta_value = '{$reviewType}'
95
        ");
96
        return array_keys(array_flip($results));
97
    }
98
99
    /**
100
     * @param int $greaterThanId
101
     * @param int $limit
102
     * @return array
103
     */
104
    public function getReviewRatingsFromIds(array $postIds, $greaterThanId = 0, $limit = 100)
105
    {
106
        sort($postIds);
107
        $postIds = array_slice($postIds, intval(array_search($greaterThanId, $postIds)), $limit);
108
        $postIds = implode(',', $postIds);
109
        return (array) $this->db->get_results("
110
            SELECT p.ID, m.meta_value AS rating
111
            FROM {$this->db->posts} AS p
112
            INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id
113
            WHERE p.ID > {$greaterThanId}
114
            AND p.ID IN ('{$postIds}')
115
            AND p.post_status = 'publish'
116
            AND p.post_type = '{$this->postType}'
117
            AND m.meta_key = '_rating'
118
            GROUP BY p.ID
119
            ORDER By p.ID ASC
120
            LIMIT {$limit}
121
        ");
122
    }
123
124
    /**
125
     * @param string $key
126
     * @param string $status
127
     * @return array
128
     */
129 1
    public function getReviewsMeta($key, $status = 'publish')
130
    {
131 1
        $key = glsr(Helper::class)->prefix('_', $key);
132 1
        $values = $this->db->get_col("
133
            SELECT DISTINCT m.meta_value
134 1
            FROM {$this->db->postmeta} m
135 1
            LEFT JOIN {$this->db->posts} p ON p.ID = m.post_id
136 1
            WHERE p.post_type = '{$this->postType}'
137 1
            AND m.meta_key = '{$key}'
138
            AND m.meta_value > '' -- No empty values or ID's less than 1
139 1
            AND p.post_status = '{$status}'
140
            GROUP BY p.ID -- remove duplicate meta_value entries
141
            ORDER BY m.meta_id ASC -- sort by oldest meta_value
142
        ");
143 1
        sort($values);
144 1
        return $values;
145
    }
146
147
    /**
148
     * @param string $and
149
     * @return string
150
     */
151 1
    protected function getAndForCounts(array $args, $and = '')
152
    {
153 1
        $postIds = implode(',', array_filter(glsr_get($args, 'post_ids', [])));
154 1
        $termIds = implode(',', array_filter(glsr_get($args, 'term_ids', [])));
155 1
        if (!empty($args['type'])) {
156
            $and.= "AND m2.meta_value = '{$args['type']}' ";
157
        }
158 1
        if ($postIds) {
159
            $and.= "AND m3.meta_key = '_assigned_to' AND m3.meta_value IN ({$postIds}) ";
160
        }
161 1
        if ($termIds) {
162
            $and.= "AND tr.term_taxonomy_id IN ({$termIds}) ";
163
        }
164 1
        return apply_filters('site-reviews/query/and-for-counts', $and);
165
    }
166
167
    /**
168
     * @param string $innerJoin
169
     * @return string
170
     */
171 1
    protected function getInnerJoinForCounts(array $args, $innerJoin = '')
172
    {
173 1
        if (!empty(glsr_get($args, 'post_ids'))) {
174
            $innerJoin.= "INNER JOIN {$this->db->postmeta} AS m3 ON p.ID = m3.post_id ";
175
        }
176 1
        if (!empty(glsr_get($args, 'term_ids'))) {
177
            $innerJoin.= "INNER JOIN {$this->db->term_relationships} AS tr ON p.ID = tr.object_id ";
178
        }
179 1
        return apply_filters('site-reviews/query/inner-join-for-counts', $innerJoin);
180
    }
181
}
182