Passed
Push — master ( 6f6d9e...e32a3c )
by Paul
04:05
created

Metaboxes   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Test Coverage

Coverage 29.59%

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 216
ccs 29
cts 98
cp 0.2959
rs 9.28
c 0
b 0
f 0
wmc 39

16 Methods

Rating   Name   Duplication   Size   Complexity  
A saveResponseMetabox() 0 9 3
A decreaseReviewCount() 0 5 2
A onBeforeUpdateReview() 0 6 2
A onReviewStatusChange() 0 7 2
A updateReviewIdOfPost() 0 7 3
A saveAssignedToMetabox() 0 9 4
A setReviewCounts() 0 5 2
A onCreateReview() 0 6 2
A increaseReviewCount() 0 5 2
A recalculatePostAverage() 0 3 1
A getReviewCounts() 0 16 5
A onDeleteReview() 0 7 2
A onSaveReview() 0 3 1
A getAssignedToPostId() 0 7 2
A recalculatePostRanking() 0 3 1
A updateAssignedToPost() 0 15 5
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'], [] );
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\C...ontroller\OptionManager was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 );
0 ignored issues
show
Bug introduced by
Are you sure $type of type mixed|false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

154
		glsr( OptionManager::class )->set( 'counts.'./** @scrutinizer ignore-type */ $type, $counts );
Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rating seems to be never defined.
Loading history...
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rating seems to be never defined.
Loading history...
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 );
0 ignored issues
show
Unused Code introduced by
The assignment to $counts is dead and can be removed.
Loading history...
211
			update_post_meta( $postId, static::META_AVERAGE, $this->recalculatePostAverage( $reviews->results ));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $reviews does not exist. Did you maybe mean $review?
Loading history...
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