Passed
Push — master ( 577901...9b3d8a )
by Paul
04:24
created

Metaboxes::updateAssignedToPost()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 1
dl 0
loc 18
ccs 0
cts 15
cp 0
crap 30
rs 8.8571
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 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
	 * @param array $postData
19
	 * @param array $meta
20
	 * @param int $postId
21
	 * @return void
22
	 */
23
	public function onCreateReview( $postData, $meta, $postId )
24
	{
25
		if( !$this->isReviewPostType( $review = get_post( $postId )))return;
26
		$this->updateAssignedToPost( $review );
27
	}
28
29
	/**
30
	 * @param int $postId
31
	 * @return void
32
	 */
33
	public function onDeleteReview( $postId )
34
	{
35
		if( !$this->isReviewPostType( $review = get_post( $postId )))return;
36
		$review->post_status = 'deleted'; // important to change the post_status here first!
37
		$this->updateAssignedToPost( $review );
38
	}
39
40
	/**
41
	 * @param int $postId
42
	 * @return void
43
	 */
44
	public function onSaveReview( $postId, WP_Post $review )
45
	{
46
		$this->updateAssignedToPost( $review );
47
	}
48
49
	/**
50
	 * @param int $postId
51
	 * @return void
52
	 */
53
	public function saveAssignedToMetabox( $postId )
54
	{
55
		if( !wp_verify_nonce( filter_input( INPUT_POST, '_nonce-assigned-to' ), 'assigned_to' ))return;
56
		$assignedTo = filter_input( INPUT_POST, 'assigned_to' );
57
		$assignedTo || $assignedTo = '';
58
		if( get_post_meta( $postId, 'assigned_to', true ) != $assignedTo ) {
59
			$this->onDeleteReview( $postId );
60
		}
61
		update_post_meta( $postId, 'assigned_to', $assignedTo );
62
	}
63
64
	/**
65
	 * @param int $postId
66
	 * @return mixed
67
	 */
68
	public function saveResponseMetabox( $postId )
69
	{
70
		if( !wp_verify_nonce( filter_input( INPUT_POST, '_nonce-response' ), 'response' ))return;
71
		$response = filter_input( INPUT_POST, 'response' );
72
		$response || $response = '';
73
		update_post_meta( $postId, 'response', trim( wp_kses( $response, [
74
			'a' => ['href' => [], 'title' => []],
75
			'em' => [],
76
			'strong' => [],
77
		])));
78
	}
79
80
	/**
81
	 * @param int $postId
82
	 * @return int|false
83
	 */
84
	protected function getAssignedToPostId( $postId )
85
	{
86
		$assignedTo = get_post_meta( $postId, 'assigned_to', true );
87
		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

87
		if(( $post = get_post( /** @scrutinizer ignore-type */ $assignedTo )) instanceof WP_Post ) {
Loading history...
88
			return $post->ID;
89
		}
90
		return false;
91
	}
92
93
	/**
94
	 * @param mixed $post
95
	 * @return bool
96
	 */
97
	protected function isReviewPostType( $post )
98
	{
99
		return $post instanceof WP_Post && $post->post_type == Application::POST_TYPE;
100
	}
101
102
	/**
103
	 * @return int
104
	 */
105
	protected function recalculatePostAverage( array $reviews )
106
	{
107
		return glsr( Rating::class )->getAverage( $reviews );
108
	}
109
110
	/**
111
	 * @return int
112
	 */
113
	protected function recalculatePostRanking( array $reviews )
114
	{
115
		return glsr( Rating::class )->getRanking( $reviews );
116
	}
117
118
	/**
119
	 * @return void
120
	 */
121
	protected function updateAssignedToPost( WP_Post $review )
122
	{
123
		if( !( $postId = $this->getAssignedToPostId( $review->ID )))return;
124
		$reviewIds = array_filter( (array)get_post_meta( $postId, static::META_REVIEW_ID ));
125
		if( !is_array( $reviewIds ))return;
0 ignored issues
show
introduced by
The condition is_array($reviewIds) is always true.
Loading history...
126
		$this->updateReviewIdOfPost( $postId, $review, $reviewIds );
127
		$updatedReviewIds = array_filter( (array)get_post_meta( $postId, static::META_REVIEW_ID ));
128
		if( empty( $updatedReviewIds )) {
129
			delete_post_meta( $postId, static::META_RANKING );
130
			delete_post_meta( $postId, static::META_REVIEW_ID );
131
		}
132
		else if( !glsr( Helper::class )->compareArrays( $reviewIds, $updatedReviewIds )) {
133
			$reviews = glsr( Database::class )->getReviews([
134
				'count' => -1,
135
				'post__in' => $updatedReviewIds,
136
			]);
137
			update_post_meta( $postId, static::META_AVERAGE, $this->recalculatePostAverage( $reviews->results ));
138
			update_post_meta( $postId, static::META_RANKING, $this->recalculatePostRanking( $reviews->results ));
139
		}
140
	}
141
142
	/**
143
	 * @param int $postId
144
	 * @return void
145
	 */
146
	protected function updateReviewIdOfPost( $postId, WP_Post $review, array $reviewIds )
147
	{
148
		if( $review->post_status != 'publish' ) {
149
			delete_post_meta( $postId, static::META_REVIEW_ID, $review->ID );
150
		}
151
		else if( !in_array( $review->ID, $reviewIds )) {
152
			add_post_meta( $postId, static::META_REVIEW_ID, $review->ID );
153
		}
154
	}
155
}
156