PostQuery.php
5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace FootyRoom\Queries;
use FootyRoom\Queries\Post\Post;
use FootyRoom\Support\AutoMapper;
use Illuminate\Database\DatabaseManager;
class PostQuery
{
/**
* @var \Illuminate\Database\DatabaseManager
*/
protected $mysql;
/**
* Constructor.
*
* @param \Illuminate\Database\DatabaseManager $db
*/
public function __construct(DatabaseManager $db)
{
$this->mysql = $db;
}
/**
* Find post by its id.
*
* @param string $id
*
* @return object[]
*/
public function findById($id)
{
return $this->mysql
->table('wp_posts')
->select([
'id as id',
'post_title as title',
'post_name as slug'
])
->join('wp_term_relationships', 'object_id', '=', 'id')
->join('wp_terms', 'term_id', '=', 'term_taxonomy_id')
->where('id', '=', $id)
->where('wp_terms.slug', '=', 'lemix')
->where('post_status', '=', 'publish')
->first();
}
/**
* Find post by its slug.
*
* @param string $slug
*
* @return object[]
*/
public function findBySlug($slug)
{
return $this->mysql
->table('wp_posts')
->select([
'id as id',
'post_title as title',
])
->where('post_name', '=', $slug)
->first();
}
/**
* Find meta.
*
* @param int $postId
* @param string[] $metaKeys List of meta keys to return.
*
* @return string[string][int]
*/
public function findMetaByPostId($postId, $metaKeys = null)
{
$query = $this->mysql
->table('wp_postmeta')
->select([
'meta_key as key',
'meta_value as value',
])
->where('post_id', '=', $postId);
if ($metaKeys) {
$query->whereIn('meta_key', $metaKeys);
}
$metaDtos = $query->get();
$meta = [];
foreach ($metaDtos as $metaDto) {
$meta[$metaDto->key][] = $metaDto->value;
}
return $meta;
}
/**
* Returns posts in the same category as given post id.
*
* @param int $postId
* @param int $limit
*
* @return object[]
*/
public function findRelated($postId, $limit)
{
return $this->mysql->select(
'SELECT
p.id,
p.post_title as title,
p.post_date as date,
p.post_name as slug,
pm.meta_value AS thumbnailUrl
FROM wp_posts as p
STRAIGHT_JOIN wp_term_relationships AS otr ON otr.object_id = ?
JOIN wp_term_relationships AS rtr ON rtr.object_id = p.ID AND rtr.term_taxonomy_id = otr.term_taxonomy_id
LEFT JOIN wp_postmeta AS pm ON pm.meta_key = "pft_widescreen" AND pm.post_id = p.id
WHERE p.post_status = "publish" AND p.post_type = "post"
HAVING p.ID != ?
ORDER BY p.post_date DESC
LIMIT 0, ?',
[$postId, $postId, $limit]
);
}
/**
* Finds posts in specific categories.
*
* @param array $categoryIds
* @param int $offset
* @param int $limit
*
* @return \FootyRoom\Queries\Post\Post[]
*/
public function findByCategoryIds(array $categoryIds, $offset = 0, $limit = 24)
{
$postDtos = $this->mysql->select(
'SELECT
p.id,
p.post_title as title,
p.post_date as date,
p.post_name as slug,
p.sticky,
p.comment_count as commentCount,
p.view_count as viewCount,
t.name as categoryName,
pm.meta_value AS thumbnailUrl
FROM wp_posts as p
STRAIGHT_JOIN wp_term_relationships AS tr ON tr.object_id = p.ID AND tr.term_taxonomy_id IN(?)
JOIN wp_terms AS t ON t.term_id = tr.term_taxonomy_id
LEFT JOIN wp_postmeta AS pm ON pm.meta_key = "pft_widescreen" AND pm.post_id = p.id
WHERE p.post_status = "publish" AND p.post_type = "post"
ORDER BY p.post_date DESC
LIMIT ?, ?',
[implode(',', $categoryIds), $offset, $limit]
);
$posts = [];
foreach ($postDtos as $post) {
$posts[] = AutoMapper::map($post, Post::class);
}
return $posts;
}
/**
* Returns view count of a post.
*
* @param int $postId
*
* @return int
*/
public function getViewCount($postId)
{
return $this->mysql
->table('wp_posts')
->where('ID', '=', $postId)
->value('view_count');
}
/**
* Increment view count.
*
* @param int $postId
* @param int $value
*/
public function incrementViewCount($postId, $value = 1)
{
$this->mysql
->table('wp_posts')
->where('ID', '=', $postId)
->increment('view_count', $value);
}
/**
* Update data in wp_posts.
*
* @param int $postId
* @param int $userId
* @param int $commentId
* @param date $commentDate
*/
public function updateLastComment($postId, $userId, $commentId, $commentDate)
{
$this->mysql
->table('wp_posts')
->where('ID', '=', $postId)
->update([
'last_comment_id' => $commentId,
'last_comment_user_id' => $userId,
'last_comment_date' => $commentDate,
]);
}
}