1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers\EditorController; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Database\CountsManager; |
7
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
8
|
|
|
use GeminiLabs\SiteReviews\Helper; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
10
|
|
|
use WP_Post; |
11
|
|
|
|
12
|
|
|
class Metaboxes |
13
|
|
|
{ |
14
|
|
|
const META_AVERAGE = '_glsr_average'; |
15
|
|
|
const META_RANKING = '_glsr_ranking'; |
16
|
|
|
const META_REVIEW_ID = '_glsr_review_id'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param int $postId |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function onBeforeDeleteReview( $postId ) |
23
|
|
|
{ |
24
|
|
|
if( get_post_field( 'post_type', $postId ) !== Application::POST_TYPE )return; |
25
|
|
|
$review = get_post( $postId ); |
26
|
|
|
$review->post_status = 'deleted'; // change post_status first! |
27
|
|
|
$this->updateAssignedToPost( $review ); |
28
|
|
|
$this->decreaseReviewCount( $review ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Update the review count when the rating or review_type changes |
33
|
|
|
* @param string $metaKey |
34
|
|
|
* @param mixed $metaValue |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function onBeforeUpdateReview( WP_Post $review, $metaKey, $newValue ) |
38
|
|
|
{ |
39
|
|
|
$previousValue = get_post_meta( $review->ID, $metaKey, true ); |
40
|
|
|
if( $previousValue == $newValue )return; |
41
|
|
|
$this->decreaseReviewCount( $review ); |
42
|
|
|
$this->increaseReviewCount( $review, [$metaKey => $newValue] ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $postData |
47
|
|
|
* @param array $meta |
48
|
|
|
* @param int $postId |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
1 |
|
public function onCreateReview( $postData, $meta, $postId ) |
52
|
|
|
{ |
53
|
1 |
|
if( get_post_field( 'post_type', $postId ) !== Application::POST_TYPE )return; |
54
|
1 |
|
$review = get_post( $postId ); |
55
|
1 |
|
$this->updateAssignedToPost( $review ); |
56
|
1 |
|
$this->increaseReviewCount( $review ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Update the review count when the post_status changes |
61
|
|
|
* @param string $status |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function onReviewStatusChange( $status, WP_Post $review ) |
65
|
|
|
{ |
66
|
|
|
if( $status == 'publish' ) { |
67
|
|
|
$this->increaseReviewCount( $review ); |
68
|
|
|
} |
69
|
|
|
else { |
70
|
|
|
$this->decreaseReviewCount( $review ); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param int $postId |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
1 |
|
public function onSaveReview( $postId, WP_Post $review ) |
79
|
|
|
{ |
80
|
1 |
|
$this->updateAssignedToPost( $review ); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param int $postId |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
1 |
|
public function saveAssignedToMetabox( $postId ) |
88
|
|
|
{ |
89
|
1 |
|
if( !wp_verify_nonce( glsr( Helper::class )->filterInput( '_nonce-assigned-to' ), 'assigned_to' ))return; |
90
|
|
|
$assignedTo = glsr( Helper::class )->filterInput( 'assigned_to' ); |
91
|
|
|
$assignedTo || $assignedTo = ''; |
92
|
|
|
if( get_post_meta( $postId, 'assigned_to', true ) != $assignedTo ) { |
93
|
|
|
$this->onBeforeDeleteReview( $postId ); |
94
|
|
|
} |
95
|
|
|
update_post_meta( $postId, 'assigned_to', $assignedTo ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param int $postId |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
1 |
|
public function saveResponseMetabox( $postId ) |
103
|
|
|
{ |
104
|
1 |
|
if( !wp_verify_nonce( glsr( Helper::class )->filterInput( '_nonce-response' ), 'response' ))return; |
105
|
|
|
$response = glsr( Helper::class )->filterInput( 'response' ); |
106
|
|
|
$response || $response = ''; |
107
|
|
|
update_post_meta( $postId, 'response', trim( wp_kses( $response, [ |
108
|
|
|
'a' => ['href' => [], 'title' => []], |
109
|
|
|
'em' => [], |
110
|
|
|
'strong' => [], |
111
|
|
|
]))); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int $postId |
116
|
|
|
* @return int|false |
117
|
|
|
*/ |
118
|
1 |
|
protected function getAssignedToPostId( $postId ) |
119
|
|
|
{ |
120
|
1 |
|
$assignedTo = intval( get_post_meta( $postId, 'assigned_to', true )); |
121
|
1 |
|
if(( $post = get_post( $assignedTo )) instanceof WP_Post ) { |
122
|
|
|
return $post->ID; |
123
|
|
|
} |
124
|
1 |
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
1 |
|
protected function getReviewCounts( WP_Post $review, array $meta = [] ) |
131
|
|
|
{ |
132
|
1 |
|
$meta = wp_parse_args( $meta, [ |
133
|
1 |
|
'rating' => get_post_meta( $review->ID, 'rating', true ), |
134
|
1 |
|
'review_type' => get_post_meta( $review->ID, 'review_type', true ), |
135
|
|
|
]); |
136
|
1 |
|
return glsr( CountsManager::class )->get([ |
137
|
|
|
'max' => $meta['rating'], |
138
|
|
|
'min' => 0, |
139
|
|
|
'types' => $meta['review_type'], |
140
|
|
|
]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
protected function setReviewCounts( WP_Post $review, array $counts ) |
147
|
|
|
{ |
148
|
|
|
$type = strval( get_post_meta( $review->ID, 'review_type', true )); |
149
|
|
|
if( !array_key_exists( $type, glsr()->reviewTypes ))return; |
150
|
|
|
glsr( OptionManager::class )->set( 'counts.'.$type, $counts ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
1 |
|
protected function increaseReviewCount( WP_Post $review, array $meta = [] ) |
157
|
|
|
{ |
158
|
1 |
|
if( $counts = $this->getReviewCounts( $review, $meta )) { |
159
|
|
|
$rating = isset( $meta['rating'] ) |
160
|
|
|
? $meta['rating'] |
161
|
|
|
: intval( get_post_meta( $review->ID, 'rating', true )); |
162
|
|
|
$counts[$rating] -= 1; |
163
|
|
|
$this->setReviewCounts( $review, $counts ); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
protected function decreaseReviewCount( WP_Post $review, array $meta = [] ) |
171
|
|
|
{ |
172
|
|
|
if( $counts = $this->getReviewCounts( $review, $meta )) { |
173
|
|
|
$rating = intval( get_post_meta( $review->ID, 'rating', true )); |
174
|
|
|
$counts[$rating] += 1; |
175
|
|
|
$this->setReviewCounts( $review, $counts ); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return int |
181
|
|
|
*/ |
182
|
|
|
protected function recalculatePostAverage( array $ratingCounts ) |
183
|
|
|
{ |
184
|
|
|
return glsr( Rating::class )->getAverage( $ratingCounts ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return int |
189
|
|
|
*/ |
190
|
|
|
protected function recalculatePostRanking( array $ratingCounts ) |
191
|
|
|
{ |
192
|
|
|
return glsr( Rating::class )->getRanking( $ratingCounts ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
1 |
|
protected function updateAssignedToPost( WP_Post $review ) |
199
|
|
|
{ |
200
|
1 |
|
if( !( $postId = $this->getAssignedToPostId( $review->ID )))return; |
201
|
|
|
$reviewIds = array_filter( (array)get_post_meta( $postId, static::META_REVIEW_ID )); |
202
|
|
|
if( empty( $reviewIds ))return; |
203
|
|
|
$this->updateReviewIdOfPost( $postId, $review, $reviewIds ); |
204
|
|
|
$updatedReviewIds = array_filter( (array)get_post_meta( $postId, static::META_REVIEW_ID )); |
205
|
|
|
if( empty( $updatedReviewIds )) { |
206
|
|
|
delete_post_meta( $postId, static::META_RANKING ); |
207
|
|
|
delete_post_meta( $postId, static::META_REVIEW_ID ); |
208
|
|
|
} |
209
|
|
|
else if( !glsr( Helper::class )->compareArrays( $reviewIds, $updatedReviewIds )) { |
210
|
|
|
$counts = glsr( CountsManager::class )->buildFromIds( $updatedReviewIds ); |
211
|
|
|
$counts = glsr( CountsManager::class )->flatten( $counts ); |
212
|
|
|
update_post_meta( $postId, static::META_AVERAGE, $this->recalculatePostAverage( $counts )); |
213
|
|
|
update_post_meta( $postId, static::META_RANKING, $this->recalculatePostRanking( $counts )); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param int $postId |
219
|
|
|
* @return void |
220
|
|
|
*/ |
221
|
|
|
protected function updateReviewIdOfPost( $postId, WP_Post $review, array $reviewIds ) |
222
|
|
|
{ |
223
|
|
|
if( $review->post_status != 'publish' ) { |
224
|
|
|
delete_post_meta( $postId, static::META_REVIEW_ID, $review->ID ); |
225
|
|
|
} |
226
|
|
|
else if( !in_array( $review->ID, $reviewIds )) { |
227
|
|
|
add_post_meta( $postId, static::META_REVIEW_ID, $review->ID ); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|