Passed
Push — master ( 042452...c272a3 )
by Paul
05:20
created

SqlQueries::deleteAllSessions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
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}' AND m.meta_key = '{$metaKey}'
57
			GROUP BY name
58
		");
59
	}
60
61
	/**
62
	 * @param string $sessionCookiePrefix
63
	 * @param int $limit
64
	 * @return array
65
	 */
66
	public function getExpiredSessions( $sessionCookiePrefix, $limit )
67
	{
68
		return $this->db->get_results("
69
			SELECT option_name AS name, option_value AS expiration
70
			FROM {$this->db->options}
71
			WHERE option_name LIKE '{$sessionCookiePrefix}_expires_%'
72
			ORDER BY option_value ASC
73
			LIMIT 0, {$limit}
74
		");
75
	}
76
77
	/**
78
	 * @param string $metaReviewId
79
	 * @return int
80
	 */
81
	public function getReviewPostId( $metaReviewId )
82
	{
83
		$postId = $this->db->get_var("
84
			SELECT p.ID
85
			FROM {$this->db->posts} AS p
86
			INNER JOIN {$this->db->postmeta} AS pm ON p.ID = pm.post_id
87
			WHERE p.post_type = '{$this->postType}'
88
			AND pm.meta_key = 'review_id'
89
			AND pm.meta_value = '{$metaReviewId}'
90
		");
91
		return intval( $postId );
92
	}
93
94
	/**
95
	 * @param string $metaReviewType
96
	 * @return array
97
	 */
98
	public function getReviewIdsByType( $metaReviewType )
99
	{
100
		$query = $this->db->get_col("
101
			SELECT m1.meta_value AS review_id
102
			FROM {$this->db->posts} AS p
103
			INNER JOIN {$this->db->postmeta} AS m1 ON p.ID = m1.post_id
104
			INNER JOIN {$this->db->postmeta} AS m2 ON p.ID = m2.post_id
105
			WHERE p.post_type = '{$this->db->postType}'
106
			AND m1.meta_key = 'review_id'
107
			AND m2.meta_key = 'review_type'
108
			AND m2.meta_value = '{$metaReviewType}'
109
		");
110
		return array_keys( array_flip( $query ));
111
	}
112
113
	/**
114
	 * @param string|array $keys
115
	 * @param string $status
116
	 * @return array
117
	 */
118
	public function getReviewsMeta( $keys, $status )
119
	{
120
		$queryBuilder = glsr( QueryBuilder::class );
121
		$keys = $queryBuilder->buildSqlOr( $keys, "pm.meta_key = '%s'" );
122
		$status = $queryBuilder->buildSqlOr( $status, "p.post_status = '%s'" );
123
		return $this->db->get_col("
124
			SELECT DISTINCT pm.meta_value FROM {$this->db->postmeta} pm
125
			LEFT JOIN {$this->db->posts} p ON p.ID = pm.post_id
126
			WHERE p.post_type = '{$this->postType}'
127
			AND ({$keys})
128
			AND ({$status})
129
			ORDER BY pm.meta_value
130
		");
131
	}
132
}
133