Skip to content

Commit eef86e6

Browse files
committed
Added reviews total and categories to post
1 parent a32bb07 commit eef86e6

File tree

4 files changed

+75
-23
lines changed

4 files changed

+75
-23
lines changed

model/blog/post.php

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3-
class ModelBlogPost extends Model {
3+
class ModelBlogPost extends Model
4+
{
45

5-
public function getPost( $post_id ) {
6+
public function getPost($post_id)
7+
{
68
global $wpdb;
79

810
$sql = "SELECT
@@ -19,12 +21,13 @@ public function getPost( $post_id ) {
1921

2022
$sql .= " GROUP BY p.ID";
2123

22-
$result = $wpdb->get_row( $sql );
24+
$result = $wpdb->get_row($sql);
2325

2426
return $result;
2527
}
2628

27-
public function getPosts( $data = array() ) {
29+
public function getPosts($data = array())
30+
{
2831
global $wpdb;
2932

3033
$sql = "SELECT
@@ -36,7 +39,7 @@ public function getPosts( $data = array() ) {
3639

3740
$implode = array();
3841

39-
if ( isset( $data['filter_category_id'] ) ) {
42+
if (isset($data['filter_category_id'])) {
4043
$implode[] = "'" . (int) $data['filter_category_id'] . "' IN (SELECT
4144
tt.`term_id`
4245
FROM
@@ -47,8 +50,8 @@ public function getPosts( $data = array() ) {
4750
AND tr.`object_id` = p.`ID`)";
4851
}
4952

50-
if ( count( $implode ) > 0 ) {
51-
$sql .= ' AND ' . implode( ' AND ', $implode );
53+
if (count($implode) > 0) {
54+
$sql .= ' AND ' . implode(' AND ', $implode);
5255
}
5356

5457
$sql .= " GROUP BY ID";
@@ -57,36 +60,37 @@ public function getPosts( $data = array() ) {
5760
'ID'
5861
);
5962

60-
if ( isset( $data['sort'] ) && in_array( $data['sort'], $sort_data ) ) {
63+
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
6164
$sql .= " ORDER BY " . $data['sort'];
6265
} else {
6366
$sql .= " ORDER BY ID";
6467
}
6568

66-
if ( isset( $data['order'] ) && ( $data['order'] == 'DESC' ) ) {
69+
if (isset($data['order']) && ($data['order'] == 'DESC')) {
6770
$sql .= " DESC";
6871
} else {
6972
$sql .= " ASC";
7073
}
7174

72-
if ( isset( $data['start'] ) || isset( $data['limit'] ) ) {
73-
if ( $data['start'] < 0 ) {
75+
if (isset($data['start']) || isset($data['limit'])) {
76+
if ($data['start'] < 0) {
7477
$data['start'] = 0;
7578
}
7679

77-
if ( $data['limit'] < 1 ) {
80+
if ($data['limit'] < 1) {
7881
$data['limit'] = 20;
7982
}
8083

8184
$sql .= " LIMIT " . (int) $data['start'] . "," . (int) $data['limit'];
8285
}
8386

84-
$results = $wpdb->get_results( $sql );
87+
$results = $wpdb->get_results($sql);
8588

8689
return $results;
8790
}
8891

89-
public function getTotalPosts( $data = array() ) {
92+
public function getTotalPosts($data = array())
93+
{
9094
global $wpdb;
9195

9296
$sql = "SELECT count(*) as total
@@ -97,7 +101,7 @@ public function getTotalPosts( $data = array() ) {
97101

98102
$implode = array();
99103

100-
if ( isset( $data['filter_category_id'] ) ) {
104+
if (isset($data['filter_category_id'])) {
101105
$implode[] = "'" . (int) $data['filter_category_id'] . "' IN (SELECT
102106
tt.`term_id`
103107
FROM
@@ -108,12 +112,31 @@ public function getTotalPosts( $data = array() ) {
108112
AND tr.`object_id` = p.`ID`)";
109113
}
110114

111-
if ( count( $implode ) > 0 ) {
112-
$sql .= ' AND ' . implode( ' AND ', $implode );
115+
if (count($implode) > 0) {
116+
$sql .= ' AND ' . implode(' AND ', $implode);
113117
}
114118

115-
$result = $wpdb->get_row( $sql );
119+
$result = $wpdb->get_row($sql);
116120

117121
return $result->total;
118122
}
119-
}
123+
124+
public function getCategoriesByPost($post_id)
125+
{
126+
global $wpdb;
127+
128+
$sql = "SELECT
129+
tt.`term_id`
130+
FROM
131+
`wp_term_relationships` tr
132+
LEFT JOIN `wp_term_taxonomy` tt
133+
ON tr.`term_taxonomy_id` = tt.`term_taxonomy_id`
134+
WHERE tt.`taxonomy` = 'category'
135+
AND tr.`object_id` = '" . $post_id . "'";
136+
137+
138+
$result = $wpdb->get_results($sql);
139+
140+
return $result;
141+
}
142+
}

resolver/blog/post.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public function get($args)
4848
'parent' => $root,
4949
'args' => $args
5050
));
51+
},
52+
'categories' => function ($root, $args) {
53+
return $this->load->resolver('blog/post/categories', array(
54+
'parent' => $root,
55+
'args' => $args
56+
));
5157
}
5258
);
5359
}
@@ -88,17 +94,31 @@ public function getList($args)
8894
);
8995
}
9096

97+
public function categories($args)
98+
{
99+
$post_info = $args['parent'];
100+
$this->load->model('blog/post');
101+
102+
$result = $this->model_blog_post->getCategoriesByPost($post_info['id']);
103+
$categories = array();
104+
105+
foreach ($result as $category) {
106+
$categories[] = $this->load->resolver('blog/category/get', array('id'=> $category->term_id));
107+
}
108+
109+
return $categories;
110+
}
91111
public function prev($args)
92112
{
93113
$post_info = $args['parent'];
94114
global $post;
95115
$post = get_post($post_info['id']);
96116
$previous_post = get_previous_post(false);
97117

98-
if(!$previous_post) {
118+
if (!$previous_post) {
99119
return null;
100120
}
101-
121+
102122
return $this->get(array('id' => $previous_post->ID));
103123
}
104124

resolver/blog/review.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public function get($data) {
3636
);
3737
}
3838

39-
return $comments;
39+
return array(
40+
'content' => $comments,
41+
'totalElements' => count($comments)
42+
);
4043
}
4144
}

schema.graphql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ type PageResult {
139139
totalElements: Int
140140
}
141141

142+
type postReviewResult {
143+
content: [postReview]
144+
totalElements: Int
145+
}
146+
142147
type Post {
143148
id: ID
144149
title: String @deprecated(reason: "Changed to name!")
@@ -149,7 +154,8 @@ type Post {
149154
imageLazy: String
150155
keyword: String
151156
rating: Float
152-
reviews: [postReview]
157+
reviews: postReviewResult
158+
categories: [categoryBlog]
153159
datePublished: String
154160
next: Post
155161
prev: Post

0 commit comments

Comments
 (0)