Passed
Push — master ( 6f6d9e...e32a3c )
by Paul
04:05
created

SqlQueries::getReviewRatingsFromIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 3
dl 0
loc 17
ccs 0
cts 12
cp 0
crap 2
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\QueryBuilder;
7
8
class SqlQueries
9
{
10
	protected $db;
11
	protected $postType;
12
13
	public function __construct()
14
	{
15
		global $wpdb;
16
		$this->db = $wpdb;
17
		$this->postType = Application::POST_TYPE;
18
	}
19
20
	/**
21
	 * @param string $sessionCookiePrefix
22
	 * @return int|false
23
	 */
24
	public function deleteAllSessions( $sessionCookiePrefix )
25
	{
26
		return $this->db->query("
27
			DELETE
28
			FROM {$this->db->options}
29
			WHERE option_name LIKE '{$sessionCookiePrefix}_%'
30
		");
31
	}
32
33
	/**
34
	 * @param string $expiredSessions
35
	 * @return int|false
36
	 */
37
	public function deleteExpiredSessions( $expiredSessions )
38
	{
39
		return $this->db->query("
40
			DELETE
41
			FROM {$this->db->options}
42
			WHERE option_name IN ('{$expiredSessions}')
43
		");
44
	}
45
46
	/**
47
	 * @param string $metaKey
48
	 * @return array
49
	 */
50
	public function getReviewCounts( $metaKey )
51
	{
52
		return (array) $this->db->get_results("
53
			SELECT m.meta_value AS name, COUNT(*) num_posts
54
			FROM {$this->db->posts} AS p
55
			INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id
56
			WHERE p.post_type = '{$this->postType}'
57
			AND m.meta_key = '{$metaKey}'
58
			GROUP BY name
59
		");
60
	}
61
62
	/**
63
	 * @param string $sessionCookiePrefix
64
	 * @param int $limit
65
	 * @return array
66
	 */
67
	public function getExpiredSessions( $sessionCookiePrefix, $limit )
68
	{
69
		return $this->db->get_results("
70
			SELECT option_name AS name, option_value AS expiration
71
			FROM {$this->db->options}
72
			WHERE option_name LIKE '{$sessionCookiePrefix}_expires_%'
73
			ORDER BY option_value ASC
74
			LIMIT 0, {$limit}
75
		");
76
	}
77
78
	/**
79
	 * @param string $metaReviewId
80
	 * @return int
81
	 */
82
	public function getReviewPostId( $metaReviewId )
83
	{
84
		$postId = $this->db->get_var("
85
			SELECT p.ID
86
			FROM {$this->db->posts} AS p
87
			INNER JOIN {$this->db->postmeta} AS pm ON p.ID = pm.post_id
88
			WHERE p.post_type = '{$this->postType}'
89
			AND pm.meta_key = 'review_id'
90
			AND pm.meta_value = '{$metaReviewId}'
91
		");
92
		return intval( $postId );
93
	}
94
95
	/**
96
	 * @param string $metaReviewType
97
	 * @return array
98
	 */
99
	public function getReviewIdsByType( $metaReviewType )
100
	{
101
		$query = $this->db->get_col("
102
			SELECT m1.meta_value AS review_id
103
			FROM {$this->db->posts} AS p
104
			INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id
105
			INNER JOIN {$this->db->postmeta} AS m2 ON p.ID = m2.post_id
106
			WHERE p.post_type = '{$this->db->postType}'
107
			AND m1.meta_key = 'review_id'
108
			AND m2.meta_key = 'review_type'
109
			AND m2.meta_value = '{$metaReviewType}'
110
		");
111
		return array_keys( array_flip( $query ));
112
	}
113
114
	/**
115
	 * @param int $greaterThanId
116
	 * @param int $limit
117
	 * @return array
118
	 */
119
	public function getReviewRatings( $greaterThanId = 0, $limit = 100 )
120
	{
121
		return $this->db->get_results("
122
			SELECT p.ID, m1.meta_value as rating, m2.meta_value as type
123
			FROM {$this->db->posts} AS p
124
			INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id
125
			INNER JOIN {$this->db->postmeta} AS m2 ON p.ID = m2.post_id
126
			WHERE p.post_type = '{$this->postType}'
127
			AND p.ID > {$greaterThanId}
128
			AND p.post_status = 'publish'
129
			AND m1.meta_key = 'rating'
130
			AND m2.meta_key = 'review_type'
131
			ORDER By p.ID
132
			ASC LIMIT {$limit}
133
		", OBJECT );
134
	}
135
136
	/**
137
	 * @param int $greaterThanId
138
	 * @param int $limit
139
	 * @return array
140
	 */
141
	public function getReviewRatingsFromIds( array $postIds, $greaterThanId = 0, $limit = 100 )
142
	{
143
		sort( $postIds );
144
		$postIds = array_slice( $postIds, array_search( $greaterThanId, $postIds ), $limit );
0 ignored issues
show
Bug introduced by
It seems like array_search($greaterThanId, $postIds) can also be of type string and false; however, parameter $offset of array_slice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
		$postIds = array_slice( $postIds, /** @scrutinizer ignore-type */ array_search( $greaterThanId, $postIds ), $limit );
Loading history...
145
		$postIds = implode( ',', $postIds );
146
		return $this->db->get_results("
147
			SELECT p.ID, m.meta_value as rating
148
			FROM {$this->db->posts} AS p
149
			INNER JOIN {$this->db->postmeta} AS m ON p.ID = m.post_id
150
			WHERE p.post_type = '{$this->postType}'
151
			AND p.ID IN ('{$postIds}')
152
			AND p.ID > {$greaterThanId}
153
			AND p.post_status = 'publish'
154
			AND m.meta_key = 'rating'
155
			ORDER By p.ID
156
			ASC LIMIT {$limit}
157
		", OBJECT );
158
	}
159
160
	/**
161
	 * @param string $key
162
	 * @param string $status
163
	 * @return array
164
	 */
165
	public function getReviewsMeta( $key, $status )
166
	{
167
		$queryBuilder = glsr( QueryBuilder::class );
168
		$key = $queryBuilder->buildSqlOr( $key, "pm.meta_key = '%s'" );
169
		$status = $queryBuilder->buildSqlOr( $status, "p.post_status = '%s'" );
170
		return $this->db->get_col("
171
			SELECT DISTINCT pm.meta_value FROM {$this->db->postmeta} pm
172
			LEFT JOIN {$this->db->posts} p ON p.ID = pm.post_id
173
			WHERE p.post_type = '{$this->postType}'
174
			AND ({$key})
175
			AND ({$status})
176
			ORDER BY pm.meta_value
177
		");
178
	}
179
}
180