Passed
Push — master ( 64f340...ccb079 )
by Paul
08:17 queued 03:57
created

SqlQueries::deleteTermCountMetaKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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