Passed
Push — master ( 97923e...577901 )
by Paul
04:41
created

Metaboxes::saveAssignedToMetabox()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 5
nop 1
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Editor;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Helper;
8
use WP_Post;
9
10
class Metaboxes
11
{
12
	const META_AVERAGE = '_glsr_average';
13
	const META_RANKING = '_glsr_ranking';
14
	const META_REVIEW_ID = '_glsr_review_id';
15
16
	/**
17
	 * @param array $postData
18
	 * @param array $meta
19
	 * @param int $postId
20
	 * @return void
21
	 */
22
	public function onCreateReview( $postData, $meta, $postId )
23
	{
24
		if( !$this->isReviewPostType( $review = get_post( $postId )))return;
25
		$this->updateAssignedToPost( $review );
26
	}
27
28
	/**
29
	 * @param int $postId
30
	 * @return void
31
	 */
32
	public function onDeleteReview( $postId )
33
	{
34
		if( !$this->isReviewPostType( $review = get_post( $postId )))return;
35
		$review->post_status = 'deleted'; // important to change the post_status here first!
36
		$this->updateAssignedToPost( $review );
37
	}
38
39
	/**
40
	 * @param int $postId
41
	 * @return void
42
	 */
43
	public function onSaveReview( $postId, WP_Post $review )
44
	{
45
		$this->updateAssignedToPost( $review );
46
	}
47
48
	/**
49
	 * @param int $postId
50
	 * @return void
51
	 */
52
	public function saveAssignedToMetabox( $postId )
53
	{
54
		if( !wp_verify_nonce( filter_input( INPUT_POST, '_nonce-assigned-to' ), 'assigned_to' ))return;
55
		$assignedTo = filter_input( INPUT_POST, 'assigned_to' );
56
		$assignedTo || $assignedTo = '';
57
		if( get_post_meta( $postId, 'assigned_to', true ) != $assignedTo ) {
58
			$this->onDeleteReview( $postId );
59
		}
60
		update_post_meta( $postId, 'assigned_to', $assignedTo );
61
	}
62
63
	/**
64
	 * @param int $postId
65
	 * @return mixed
66
	 */
67
	public function saveResponseMetabox( $postId )
68
	{
69
		if( !wp_verify_nonce( filter_input( INPUT_POST, '_nonce-response' ), 'response' ))return;
70
		$response = filter_input( INPUT_POST, 'response' );
71
		$response || $response = '';
72
		update_post_meta( $postId, 'response', trim( wp_kses( $response, [
73
			'a' => ['href' => [], 'title' => []],
74
			'em' => [],
75
			'strong' => [],
76
		])));
77
	}
78
79
	/**
80
	 * @param int $postId
81
	 * @return int|false
82
	 */
83
	protected function getAssignedToPostId( $postId )
84
	{
85
		$assignedTo = get_post_meta( $postId, 'assigned_to', true );
86
		if(( $post = get_post( $assignedTo )) instanceof WP_Post ) {
0 ignored issues
show
Bug introduced by
It seems like $assignedTo can also be of type false and string; however, parameter $post of get_post() does only seem to accept null|integer|WP_Post, maybe add an additional type check? ( Ignorable by Annotation )

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

86
		if(( $post = get_post( /** @scrutinizer ignore-type */ $assignedTo )) instanceof WP_Post ) {
Loading history...
87
			return $post->ID;
88
		}
89
		return false;
90
	}
91
92
	/**
93
	 * @param mixed $post
94
	 * @return bool
95
	 */
96
	protected function isReviewPostType( $post )
97
	{
98
		return $post instanceof WP_Post && $post->post_type == Application::POST_TYPE;
99
	}
100
101
	/**
102
	 * @return int
103
	 */
104
	protected function recalculatePostAverage( array $reviews )
105
	{
106
		return glsr( Rating::class )->getAverage( $reviews );
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Modules\Editor\Rating 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...
107
	}
108
109
	/**
110
	 * @return int
111
	 */
112
	protected function recalculatePostRanking( array $reviews )
113
	{
114
		return glsr( Rating::class )->getRanking( $reviews );
115
	}
116
117
	/**
118
	 * @return void
119
	 */
120
	protected function updateAssignedToPost( WP_Post $review )
121
	{
122
		if( !( $postId = $this->getAssignedToPostId( $review->ID )))return;
123
		$reviewIds = get_post_meta( $postId, static::META_REVIEW_ID );
124
		$updatedReviewIds = array_filter( (array)get_post_meta( $postId, static::META_REVIEW_ID ));
125
		$this->updateReviewIdOfPost( $postId, $review, $reviewIds );
0 ignored issues
show
Bug introduced by
It seems like $reviewIds can also be of type false; however, parameter $reviewIds of GeminiLabs\SiteReviews\M...:updateReviewIdOfPost() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

125
		$this->updateReviewIdOfPost( $postId, $review, /** @scrutinizer ignore-type */ $reviewIds );
Loading history...
126
		if( empty( $updatedReviewIds )) {
127
			delete_post_meta( $postId, static::META_RANKING );
128
			delete_post_meta( $postId, static::META_REVIEW_ID );
129
		}
130
		else if( !glsr( Helper::class )->compareArrays( $reviewIds, $updatedReviewIds )) {
131
			$reviews = glsr( Database::class )->getReviews([
132
				'count' => -1,
133
				'post__in' => $updatedReviewIds,
134
			]);
135
			update_post_meta( $postId, static::META_AVERAGE, $this->recalculatePostAverage( $reviews->results ));
136
			update_post_meta( $postId, static::META_RANKING, $this->recalculatePostRanking( $reviews->results ));
137
		}
138
	}
139
140
	/**
141
	 * @param int $postId
142
	 * @return void
143
	 */
144
	protected function updateReviewIdOfPost( $postId, WP_Post $review, array $reviewIds )
145
	{
146
		if( $review->post_status != 'publish' ) {
147
			delete_post_meta( $postId, static::META_REVIEW_ID, $review->ID );
148
		}
149
		else if( !in_array( $review->ID, $reviewIds )) {
150
			add_post_meta( $postId, static::META_REVIEW_ID, $review->ID );
151
		}
152
	}
153
}
154