1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers\EditorController; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Database; |
7
|
|
|
use GeminiLabs\SiteReviews\Helper; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
9
|
|
|
use WP_Post; |
10
|
|
|
|
11
|
|
|
class Metaboxes |
12
|
|
|
{ |
13
|
|
|
const META_AVERAGE = '_glsr_average'; |
14
|
|
|
const META_RANKING = '_glsr_ranking'; |
15
|
|
|
const META_REVIEW_ID = '_glsr_review_id'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Update the review count when the rating or review_type changes |
19
|
|
|
* @param string $metaKey |
20
|
|
|
* @param mixed $metaValue |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function onBeforeUpdateReview( WP_Post $review, $metaKey, $metaValue ) |
24
|
|
|
{ |
25
|
|
|
$previousValue = get_post_meta( $review->ID, $metaKey, true ); |
26
|
|
|
if( $previousValue == $metaValue )return; |
27
|
|
|
$this->decreaseReviewCount( $review ); |
28
|
|
|
$this->increaseReviewCount( $review, [$metaKey => $metaValue] ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $postData |
33
|
|
|
* @param array $meta |
34
|
|
|
* @param int $postId |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
1 |
|
public function onCreateReview( $postData, $meta, $postId ) |
38
|
|
|
{ |
39
|
1 |
|
if( get_post_field( 'post_type', $postId ) !== Application::POST_TYPE )return; |
40
|
1 |
|
$review = get_post( $postId ); |
41
|
1 |
|
$this->updateAssignedToPost( $review ); |
42
|
1 |
|
$this->increaseReviewCount( $review ); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int $postId |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function onDeleteReview( $postId ) |
50
|
|
|
{ |
51
|
|
|
if( get_post_field( 'post_type', $postId ) !== Application::POST_TYPE )return; |
52
|
|
|
$review = get_post( $postId ); |
53
|
|
|
$review->post_status = 'deleted'; // important to change the post_status here first! |
54
|
|
|
$this->updateAssignedToPost( $review ); |
55
|
|
|
$this->decreaseReviewCount( $review ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Update the review count when the post_status changes |
60
|
|
|
* @param string $status |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function onReviewStatusChange( $status, WP_Post $review ) |
64
|
|
|
{ |
65
|
|
|
if( $status == 'publish' ) { |
66
|
|
|
$this->increaseReviewCount( $review ); |
67
|
|
|
} |
68
|
|
|
else { |
69
|
|
|
$this->decreaseReviewCount( $review ); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param int $postId |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
1 |
|
public function onSaveReview( $postId, WP_Post $review ) |
78
|
|
|
{ |
79
|
1 |
|
$this->updateAssignedToPost( $review ); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param int $postId |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
1 |
|
public function saveAssignedToMetabox( $postId ) |
87
|
|
|
{ |
88
|
1 |
|
if( !wp_verify_nonce( glsr( Helper::class )->filterInput( '_nonce-assigned-to' ), 'assigned_to' ))return; |
89
|
|
|
$assignedTo = glsr( Helper::class )->filterInput( 'assigned_to' ); |
90
|
|
|
$assignedTo || $assignedTo = ''; |
91
|
|
|
if( get_post_meta( $postId, 'assigned_to', true ) != $assignedTo ) { |
92
|
|
|
$this->onDeleteReview( $postId ); |
93
|
|
|
} |
94
|
|
|
update_post_meta( $postId, 'assigned_to', $assignedTo ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param int $postId |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
1 |
|
public function saveResponseMetabox( $postId ) |
102
|
|
|
{ |
103
|
1 |
|
if( !wp_verify_nonce( glsr( Helper::class )->filterInput( '_nonce-response' ), 'response' ))return; |
104
|
|
|
$response = glsr( Helper::class )->filterInput( 'response' ); |
105
|
|
|
$response || $response = ''; |
106
|
|
|
update_post_meta( $postId, 'response', trim( wp_kses( $response, [ |
107
|
|
|
'a' => ['href' => [], 'title' => []], |
108
|
|
|
'em' => [], |
109
|
|
|
'strong' => [], |
110
|
|
|
]))); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $postId |
115
|
|
|
* @return int|false |
116
|
|
|
*/ |
117
|
1 |
|
protected function getAssignedToPostId( $postId ) |
118
|
|
|
{ |
119
|
1 |
|
$assignedTo = intval( get_post_meta( $postId, 'assigned_to', true )); |
120
|
1 |
|
if(( $post = get_post( $assignedTo )) instanceof WP_Post ) { |
121
|
|
|
return $post->ID; |
122
|
|
|
} |
123
|
1 |
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return void|array |
128
|
|
|
*/ |
129
|
1 |
|
protected function getReviewCounts( WP_Post $review, array $meta = [] ) |
130
|
|
|
{ |
131
|
1 |
|
$meta = wp_parse_args( $meta, [ |
132
|
1 |
|
'rating' => get_post_meta( $review->ID, 'rating', true ), |
133
|
1 |
|
'review_type' => get_post_meta( $review->ID, 'review_type', true ), |
134
|
|
|
]); |
135
|
1 |
|
if( !in_array( $meta['review_type'], glsr()->reviewTypes ) |
136
|
1 |
|
|| intval( $meta['rating'] ) > Rating::MAX_RATING |
137
|
1 |
|
)return; |
138
|
|
|
$counts = glsr( OptionManager::class )->get( 'counts.'.$meta['review_type'], [] ); |
|
|
|
|
139
|
|
|
foreach( range( 0, Rating::MAX_RATING ) as $rating ) { |
140
|
|
|
if( isset( $counts[$rating] ))continue; |
141
|
|
|
$counts[$rating] = 0; |
142
|
|
|
} |
143
|
|
|
ksort( $counts ); |
144
|
|
|
return $counts; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
protected function setReviewCounts( WP_Post $review, array $counts ) |
151
|
|
|
{ |
152
|
|
|
$type = get_post_meta( $review->ID, 'review_type', true ); |
153
|
|
|
if( !in_array( $type, glsr()->reviewTypes ))return; |
154
|
|
|
glsr( OptionManager::class )->set( 'counts.'.$type, $counts ); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
1 |
|
protected function increaseReviewCount( WP_Post $review, array $meta = [] ) |
161
|
|
|
{ |
162
|
1 |
|
if( $counts = $this->getReviewCounts( $review, $meta )) { |
163
|
|
|
$counts[$rating] -= 1; |
|
|
|
|
164
|
|
|
$this->setReviewCounts( $review, $counts ); |
165
|
|
|
} |
166
|
1 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
protected function decreaseReviewCount( WP_Post $review, array $meta = [] ) |
172
|
|
|
{ |
173
|
|
|
if( $counts = $this->getReviewCounts( $review, $meta )) { |
174
|
|
|
$counts[$rating] += 1; |
|
|
|
|
175
|
|
|
$this->setReviewCounts( $review, $counts ); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return int |
181
|
|
|
*/ |
182
|
|
|
protected function recalculatePostAverage( array $reviews ) |
183
|
|
|
{ |
184
|
|
|
return glsr( Rating::class )->getAverage( $reviews ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return int |
189
|
|
|
*/ |
190
|
|
|
protected function recalculatePostRanking( array $reviews ) |
191
|
|
|
{ |
192
|
|
|
return glsr( Rating::class )->getRanking( $reviews ); |
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( Database::class )->buildReviewCountsFromIds( $updatedReviewIds ); |
|
|
|
|
211
|
|
|
update_post_meta( $postId, static::META_AVERAGE, $this->recalculatePostAverage( $reviews->results )); |
|
|
|
|
212
|
|
|
update_post_meta( $postId, static::META_RANKING, $this->recalculatePostRanking( $reviews->results )); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param int $postId |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
protected function updateReviewIdOfPost( $postId, WP_Post $review, array $reviewIds ) |
221
|
|
|
{ |
222
|
|
|
if( $review->post_status != 'publish' ) { |
223
|
|
|
delete_post_meta( $postId, static::META_REVIEW_ID, $review->ID ); |
224
|
|
|
} |
225
|
|
|
else if( !in_array( $review->ID, $reviewIds )) { |
226
|
|
|
add_post_meta( $postId, static::META_REVIEW_ID, $review->ID ); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths