Test Failed
Push — master ( 59b255...755753 )
by Paul
04:04
created

Metaboxes::saveAssignedToMetabox()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 9.8312

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 5
nop 1
dl 0
loc 9
ccs 2
cts 7
cp 0.2857
crap 9.8312
rs 10
c 0
b 0
f 0
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 1
	public function onBeforeUpdateReview( WP_Post $review, $metaKey, $metaValue )
24
	{
25 1
		$previousValue = get_post_meta( $review->ID, $metaKey, true );
26 1
		if( $previousValue == $metaValue )return;
27 1
		$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
	public function onCreateReview( $postData, $meta, $postId )
38
	{
39
		if( get_post_field( 'post_type', $postId ) !== Application::POST_TYPE )return;
40
		$review = get_post( $postId )
41
		$this->updateAssignedToPost( $review );
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_VARIABLE on line 41 at column 2
Loading history...
42
		$this->increaseReviewCount( $review );
43
	}
44 1
45
	/**
46 1
	 * @param int $postId
47 1
	 * @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 1
		$review->post_status = 'deleted'; // important to change the post_status here first!
54
		$this->updateAssignedToPost( $review );
55 1
		$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 1
		else {
69
			$this->decreaseReviewCount( $review );
70 1
		}
71
	}
72
73
	/**
74
	 * @param int $postId
75
	 * @return void
76
	 */
77
	public function onSaveReview( $postId, WP_Post $review )
78
	{
79
		$this->updateAssignedToPost( $review );
80
	}
81
82
	/**
83
	 * @param int $postId
84 1
	 * @return void
85
	 */
86 1
	public function saveAssignedToMetabox( $postId )
87 1
	{
88
		if( !wp_verify_nonce( glsr( Helper::class )->filterInput( '_nonce-assigned-to' ), 'assigned_to' ))return;
89
		$assignedTo = glsr( Helper::class )->filterInput( 'assigned_to' );
90 1
		$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 1
	/**
98
	 * @param int $postId
99 1
	 * @return mixed
100
	 */
101
	public function saveResponseMetabox( $postId )
102
	{
103
		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
	protected function getAssignedToPostId( $postId )
118
	{
119
		$assignedTo = intval( get_post_meta( $postId, 'assigned_to', true ));
120
		if(( $post = get_post( $assignedTo )) instanceof WP_Post ) {
121 1
			return $post->ID;
122
		}
123 1
		return false;
124
	}
125
126
	/**
127
	 * @return void|array
128
	 */
129
	protected function getReviewCounts( WP_Post $review, array $meta = [] )
130
	{
131
		$meta = wp_parse_args( $meta, [
132
			'rating' => get_post_meta( $review->ID, 'rating', true ),
133
			'review_type' => get_post_meta( $review->ID, 'review_type', true ),
134
		]);
135
		if( !in_array( $meta['review_type'], glsr()->reviewTypes )
136
			|| intval( $meta['rating'] ) > Rating::MAX_RATING
137
		)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
	protected function increaseReviewCount( WP_Post $review, array $meta = [] )
161
	{
162
		if( $counts = $this->getReviewCounts( $review, $meta )) {
163
			$counts[$rating] -= 1;
164
			$this->setReviewCounts( $review, $counts );
165
		}
166
	}
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
	protected function updateAssignedToPost( WP_Post $review )
199
	{
200
		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
			$reviews = glsr( Database::class )->getReviews([
211
				'count' => -1,
212
				'post__in' => $updatedReviewIds,
213
			]);
214
			update_post_meta( $postId, static::META_AVERAGE, $this->recalculatePostAverage( $reviews->results ));
215
			update_post_meta( $postId, static::META_RANKING, $this->recalculatePostRanking( $reviews->results ));
216
		}
217
	}
218
219
	/**
220
	 * @param int $postId
221
	 * @return void
222
	 */
223
	protected function updateReviewIdOfPost( $postId, WP_Post $review, array $reviewIds )
224
	{
225
		if( $review->post_status != 'publish' ) {
226
			delete_post_meta( $postId, static::META_REVIEW_ID, $review->ID );
227
		}
228
		else if( !in_array( $review->ID, $reviewIds )) {
229
			add_post_meta( $postId, static::META_REVIEW_ID, $review->ID );
230
		}
231
	}
232
}
233