1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Commands\CreateReview; |
7
|
|
|
use GeminiLabs\SiteReviews\Database\CountsManager; |
8
|
|
|
use GeminiLabs\SiteReviews\Database\DefaultsManager; |
9
|
|
|
use GeminiLabs\SiteReviews\Database\QueryBuilder; |
10
|
|
|
use GeminiLabs\SiteReviews\Database\SqlQueries; |
11
|
|
|
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults; |
12
|
|
|
use GeminiLabs\SiteReviews\Defaults\ReviewsDefaults; |
13
|
|
|
use GeminiLabs\SiteReviews\Defaults\SiteReviewsSummaryDefaults; |
14
|
|
|
use GeminiLabs\SiteReviews\Helper; |
15
|
|
|
use GeminiLabs\SiteReviews\Review; |
16
|
|
|
use GeminiLabs\SiteReviews\Reviews; |
17
|
|
|
use WP_Post; |
18
|
|
|
use WP_Query; |
19
|
|
|
|
20
|
|
|
class ReviewManager |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @return false|Review |
24
|
|
|
*/ |
25
|
1 |
|
public function create( CreateReview $command ) |
26
|
|
|
{ |
27
|
1 |
|
$reviewValues = glsr( CreateReviewDefaults::class )->restrict( (array)$command ); |
28
|
1 |
|
$reviewValues = apply_filters( 'site-reviews/create/review-values', $reviewValues, $command ); |
29
|
|
|
$postValues = [ |
30
|
1 |
|
'comment_status' => 'closed', |
31
|
1 |
|
'meta_input' => $reviewValues, |
32
|
1 |
|
'ping_status' => 'closed', |
33
|
1 |
|
'post_content' => $reviewValues['content'], |
34
|
1 |
|
'post_date' => $reviewValues['date'], |
35
|
1 |
|
'post_date_gmt' => get_gmt_from_date( $reviewValues['date'] ), |
36
|
1 |
|
'post_name' => $reviewValues['review_type'].'-'.$reviewValues['review_id'], |
37
|
1 |
|
'post_status' => $this->getNewPostStatus( $reviewValues, $command->blacklisted ), |
38
|
1 |
|
'post_title' => $reviewValues['title'], |
39
|
|
|
'post_type' => Application::POST_TYPE, |
40
|
|
|
]; |
41
|
1 |
|
$postId = wp_insert_post( $postValues, true ); |
42
|
1 |
|
if( is_wp_error( $postId )) { |
43
|
|
|
glsr_log()->error( $postId->get_error_message() )->debug( $postValues ); |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
1 |
|
$this->setTerms( $postId, $command->category ); |
47
|
1 |
|
$review = $this->single( get_post( $postId )); |
48
|
1 |
|
do_action( 'site-reviews/review/created', $review, $command ); |
49
|
1 |
|
return $review; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $metaReviewId |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function delete( $metaReviewId ) |
57
|
|
|
{ |
58
|
|
|
if( $postId = $this->getPostId( $metaReviewId )) { |
59
|
|
|
wp_delete_post( $postId, true ); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return object |
65
|
|
|
*/ |
66
|
|
|
public function get( array $args = [] ) |
67
|
|
|
{ |
68
|
|
|
$args = glsr( ReviewsDefaults::class )->restrict( $args ); |
69
|
|
|
$metaQuery = glsr( QueryBuilder::class )->buildQuery( |
70
|
|
|
['assigned_to', 'type', 'rating'], |
71
|
|
|
$args |
72
|
|
|
); |
73
|
|
|
$taxQuery = glsr( QueryBuilder::class )->buildQuery( |
74
|
|
|
['category'], |
75
|
|
|
['category' => $this->normalizeTermIds( $args['category'] )] |
76
|
|
|
); |
77
|
|
|
$paged = glsr( QueryBuilder::class )->getPaged( |
78
|
|
|
wp_validate_boolean( $args['pagination'] ) |
79
|
|
|
); |
80
|
|
|
$parameters = [ |
81
|
|
|
'meta_key' => 'pinned', |
82
|
|
|
'meta_query' => $metaQuery, |
83
|
|
|
'offset' => $args['offset'], |
84
|
|
|
'order' => $args['order'], |
85
|
|
|
'orderby' => 'meta_value '.$args['orderby'], |
86
|
|
|
'paged' => $paged, |
87
|
|
|
'post__in' => $args['post__in'], |
88
|
|
|
'post__not_in' => $args['post__not_in'], |
89
|
|
|
'post_status' => 'publish', |
90
|
|
|
'post_type' => Application::POST_TYPE, |
91
|
|
|
'posts_per_page' => $args['count'], |
92
|
|
|
'tax_query' => $taxQuery, |
93
|
|
|
]; |
94
|
|
|
$parameters = apply_filters( 'site-reviews/get/reviews/query', $parameters, $args ); |
95
|
|
|
$query = new WP_Query( $parameters ); |
96
|
|
|
$results = array_map( [$this, 'single'], $query->posts ); |
97
|
|
|
$reviews = new Reviews( $results, $query->max_num_pages, $args ); |
98
|
|
|
return apply_filters( 'site-reviews/get/reviews', $reviews, $query ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $metaReviewId |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function getPostId( $metaReviewId ) |
106
|
|
|
{ |
107
|
|
|
return glsr( SqlQueries::class )->getPostIdFromReviewId( $metaReviewId ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function getRatingCounts( array $args = [] ) |
114
|
|
|
{ |
115
|
|
|
$args = glsr( SiteReviewsSummaryDefaults::class )->filter( $args ); |
116
|
|
|
$counts = glsr( CountsManager::class )->get([ |
117
|
|
|
'post_ids' => glsr( Helper::class )->convertStringToArray( $args['assigned_to'] ), |
118
|
|
|
'term_ids' => $this->normalizeTermIds( $args['category'] ), |
119
|
|
|
'type' => $args['type'], |
120
|
|
|
]); |
121
|
|
|
return glsr( CountsManager::class )->flatten( $counts, [ |
122
|
|
|
'min' => $args['rating'], |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $commaSeparatedTermIds |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
1 |
|
public function normalizeTermIds( $commaSeparatedTermIds ) |
131
|
|
|
{ |
132
|
1 |
|
$termIds = glsr_array_column( $this->normalizeTerms( $commaSeparatedTermIds ), 'term_id' ); |
133
|
1 |
|
return array_unique( array_map( 'intval', $termIds )); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $commaSeparatedTermIds |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
1 |
|
public function normalizeTerms( $commaSeparatedTermIds ) |
141
|
|
|
{ |
142
|
1 |
|
$terms = []; |
143
|
1 |
|
$termIds = glsr( Helper::class )->convertStringToArray( $commaSeparatedTermIds ); |
144
|
1 |
|
foreach( $termIds as $termId ) { |
145
|
|
|
if( is_numeric( $termId )) { |
146
|
|
|
$termId = intval( $termId ); |
147
|
|
|
} |
148
|
|
|
$term = term_exists( $termId, Application::TAXONOMY ); |
149
|
|
|
if( !isset( $term['term_id'] ))continue; |
150
|
|
|
$terms[] = $term; |
151
|
|
|
} |
152
|
1 |
|
return $terms; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param int $postId |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
public function revert( $postId ) |
160
|
|
|
{ |
161
|
|
|
if( get_post_field( 'post_type', $postId ) != Application::POST_TYPE )return; |
162
|
|
|
delete_post_meta( $postId, '_edit_last' ); |
163
|
|
|
$result = wp_update_post([ |
164
|
|
|
'ID' => $postId, |
165
|
|
|
'post_content' => get_post_meta( $postId, 'content', true ), |
166
|
|
|
'post_date' => get_post_meta( $postId, 'date', true ), |
167
|
|
|
'post_title' => get_post_meta( $postId, 'title', true ), |
168
|
|
|
]); |
169
|
|
|
if( is_wp_error( $result )) { |
170
|
|
|
glsr_log()->error( $result->get_error_message() ); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return Review |
176
|
|
|
*/ |
177
|
1 |
|
public function single( WP_Post $post ) |
178
|
|
|
{ |
179
|
1 |
|
if( $post->post_type != Application::POST_TYPE ) { |
180
|
|
|
$post = new WP_Post( (object)[] ); |
181
|
|
|
} |
182
|
1 |
|
$review = new Review( $post ); |
183
|
1 |
|
return apply_filters( 'site-reviews/get/review', $review, $post ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param bool $isBlacklisted |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
1 |
|
protected function getNewPostStatus( array $review, $isBlacklisted ) |
191
|
|
|
{ |
192
|
1 |
|
$requireApproval = glsr( OptionManager::class )->getBool( 'settings.general.require.approval' ); |
193
|
1 |
|
return $review['review_type'] == 'local' && ( $requireApproval || $isBlacklisted ) |
194
|
|
|
? 'pending' |
195
|
1 |
|
: 'publish'; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param int $postId |
200
|
|
|
* @param string $termIds |
201
|
|
|
* @return void |
202
|
|
|
*/ |
203
|
1 |
|
protected function setTerms( $postId, $termIds ) |
204
|
|
|
{ |
205
|
1 |
|
$termIds = $this->normalizeTermIds( $termIds ); |
206
|
1 |
|
if( empty( $termIds ))return; |
207
|
|
|
$termTaxonomyIds = wp_set_object_terms( $postId, $termIds, Application::TAXONOMY ); |
208
|
|
|
if( is_wp_error( $termTaxonomyIds )) { |
209
|
|
|
glsr_log()->error( $termTaxonomyIds->get_error_message() ); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|