Skip to content

Commit a32bb07

Browse files
committed
Added previous and next posts and datePublished
1 parent 16a4748 commit a32bb07

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed

model/blog/post.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public function getPost( $post_id ) {
1010
p.`post_title` AS title,
1111
p.`post_content` AS description,
1212
p.`post_excerpt` AS shortDescription,
13+
p.`post_date` AS dateAdded,
1314
(SELECT `meta_value` FROM `wp_postmeta` WHERE `post_id` = p.`ID` AND `meta_key` = '_thumbnail_id') AS image_id
1415
FROM
1516
`wp_posts` p

resolver/blog/post.php

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,48 @@
22

33
class ResolverBlogPost extends Resolver
44
{
5-
public function get($args) {
6-
$this->load->model('blog/post');
7-
$post = $this->model_blog_post->getPost($args['id']);
8-
9-
if($post->imageId) {
10-
$thumb = wp_get_attachment_image_src($post->image_id, 'full');
11-
$thumbLazy = wp_get_attachment_image_src($post->image_id, array( 10, 10 ));
12-
} else {
13-
$thumb = '';
14-
$thumbLazy = '';
5+
public function get($args)
6+
{
7+
$this->load->model('blog/post');
8+
$post = $this->model_blog_post->getPost($args['id']);
9+
10+
if ($post->imageId) {
11+
$thumb = wp_get_attachment_image_src($post->image_id, 'full');
12+
$thumbLazy = wp_get_attachment_image_src($post->image_id, array(10, 10));
13+
} else {
14+
$thumb = '';
15+
$thumbLazy = '';
1516
}
16-
17+
1718
$keyword = str_replace(get_site_url(), '', get_permalink($post->ID));
1819
$keyword = trim($keyword, '/?');
1920
$keyword = trim($keyword, '/');
2021

22+
$date_format = '%A %d %B %Y';
23+
2124
return array(
2225
'id' => $post->ID,
2326
'name' => $post->title,
2427
'title' => $post->title,
2528
'shortDescription' => $post->shortDescription,
2629
'description' => $post->description,
30+
'datePublished' => iconv(mb_detect_encoding(strftime($date_format, strtotime($post->dateAdded))), "utf-8//IGNORE", strftime($date_format, strtotime($post->dateAdded))),
2731
'keyword' => $keyword,
2832
'image' => $thumb,
2933
'imageLazy' => $thumbLazy,
30-
'reviews' => function($root, $args) {
34+
'prev' => function ($root, $args) {
35+
return $this->load->resolver('blog/post/prev', array(
36+
'parent' => $root,
37+
'args' => $args
38+
));
39+
},
40+
'next' => function ($root, $args) {
41+
return $this->load->resolver('blog/post/next', array(
42+
'parent' => $root,
43+
'args' => $args
44+
));
45+
},
46+
'reviews' => function ($root, $args) {
3147
return $this->load->resolver('blog/review/get', array(
3248
'parent' => $root,
3349
'args' => $args
@@ -36,27 +52,28 @@ public function get($args) {
3652
);
3753
}
3854

39-
public function getList($args) {
40-
$this->load->model('blog/post');
55+
public function getList($args)
56+
{
57+
$this->load->model('blog/post');
4158
$filter_data = array(
4259
'limit' => $args['size'],
4360
'start' => ($args['page'] - 1) * $args['size'],
4461
'sort' => $args['sort'],
4562
'order' => $args['order']
4663
);
4764

48-
if ($args['category_id'] !== 0) {
65+
if ($args['category_id'] != 0 && $args['category_id'] != '') {
4966
$filter_data['filter_category_id'] = $args['category_id'];
5067
}
51-
68+
5269
$results = $this->model_blog_post->getPosts($filter_data);
5370

5471
$product_total = $this->model_blog_post->getTotalPosts($filter_data);
5572

5673
$posts = array();
5774

5875
foreach ($results as $post) {
59-
$posts[] = $this->get(array( 'id' => $post->ID ));
76+
$posts[] = $this->get(array('id' => $post->ID));
6077
}
6178

6279
return array(
@@ -70,4 +87,32 @@ public function getList($args) {
7087
'totalElements' => (int) $product_total,
7188
);
7289
}
73-
}
90+
91+
public function prev($args)
92+
{
93+
$post_info = $args['parent'];
94+
global $post;
95+
$post = get_post($post_info['id']);
96+
$previous_post = get_previous_post(false);
97+
98+
if(!$previous_post) {
99+
return null;
100+
}
101+
102+
return $this->get(array('id' => $previous_post->ID));
103+
}
104+
105+
public function next($args)
106+
{
107+
$post_info = $args['parent'];
108+
global $post;
109+
$post = get_post($post_info['id']);
110+
$next_post = get_next_post(false);
111+
112+
if (!$next_post) {
113+
return null;
114+
}
115+
116+
return $this->get(array('id' => $next_post->ID));
117+
}
118+
}

schema.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ type Post {
148148
image: String
149149
imageLazy: String
150150
keyword: String
151+
rating: Float
151152
reviews: [postReview]
153+
datePublished: String
154+
next: Post
155+
prev: Post
152156
}
153157

154158
type PostResult {

0 commit comments

Comments
 (0)