Passed
Push — master ( 642023...49ec8d )
by Paul
03:40
created

ReviewController::isReviewPostType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\CountsManager;
7
use GeminiLabs\SiteReviews\Database\ReviewManager;
8
use GeminiLabs\SiteReviews\Helper;
9
use GeminiLabs\SiteReviews\Review;
10
use WP_Post;
11
12
class ReviewController extends Controller
13
{
14
	/**
15
	 * @param int $postId
16
	 * @param array $terms
17
	 * @param array $termIds
18
	 * @param string $taxonomySlug
19
	 * @param bool $append
20
	 * @param array $oldTermIds
21
	 * @return void
22
	 * @action set_object_terms
23
	 */
24 1
	public function onAfterChangeCategory( $postId, $terms, $termIds, $taxonomySlug, $append, $oldTermIds )
25
	{
26 1
		if( !$this->isReviewPostId( $postId ))return;
27
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
28
		$ignoredTerms = array_intersect( $oldTermIds, $termIds );
29
		$review->term_ids = array_diff( $oldTermIds, $ignoredTerms );
30
		glsr( CountsManager::class )->decreaseTermCounts( $review );
31
		$review->term_ids = array_diff( $termIds, $ignoredTerms );
32
		glsr( CountsManager::class )->increaseTermCounts( $review );
33
	}
34
35
	/**
36
	 * @param array $postData
37
	 * @param array $meta
38
	 * @param int $postId
39
	 * @return void
40
	 * @action site-reviews/create/review
41
	 */
42 1
	public function onAfterCreate( $postData, $meta, $postId )
43
	{
44 1
		if( !$this->isReviewPostId( $postId ))return;
45 1
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
46 1
		glsr( CountsManager::class )->increase( $review );
47 1
	}
48
49
	/**
50
	 * @param int $postId
51
	 * @return void
52
	 * @action before_delete_post
53
	 */
54
	public function onBeforeDelete( $postId )
55
	{
56
		if( !$this->isReviewPostId( $postId ))return;
57
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
58
		glsr( CountsManager::class )->decrease( $review );
59
	}
60
61
	/**
62
	 * @param int $metaId
63
	 * @param int $postId
64
	 * @param string $metaKey
65
	 * @param mixed $metaValue
66
	 * @return void
67
	 * @action update_postmeta
68
	 */
69
	public function onBeforeUpdate( $metaId, $postId, $metaKey, $metaValue )
70
	{
71
		if( !$this->isReviewPostId( $postId )
72
			|| !in_array( $metaKey, ['assigned_to', 'rating', 'review_type'] )
73
		)return;
74
		$review = glsr( ReviewManager::class )->single( get_post( $postId ));
75
		if( $review->$metaKey == $metaValue )return;
76
		$method = glsr( Helper::class )->buildMethodName( $metaKey, 'onBeforeChange' );
77
		call_user_func( [$this, $method], $review, $metaValue );
78
	}
79
80
	/**
81
	 * @param string|int $assignedTo
82
	 * @return void
83
	 */
84
	public function onBeforeChangeAssignedTo( Review $review, $assignedTo )
85
	{
86
		glsr( CountsManager::class )->decreasePostCounts( $review );
87
		$review->assigned_to = $assignedTo;
88
		glsr( CountsManager::class )->increasePostCounts( $review );
89
	}
90
91
	/**
92
	 * @param string|int $rating
93
	 * @return void
94
	 */
95
	public function onBeforeChangeRating( Review $review, $rating )
96
	{
97
		glsr( CountsManager::class )->decrease( $review );
98
		$review->rating = $rating;
99
		glsr( CountsManager::class )->increase( $review );
100
	}
101
102
	/**
103
	 * @param string $reviewType
104
	 * @return void
105
	 */
106
	public function onBeforeChangeReviewType( Review $review, $reviewType )
107
	{
108
		glsr( CountsManager::class )->decrease( $review );
109
		$review->review_type = $reviewType;
110
		glsr( CountsManager::class )->increase( $review );
111
	}
112
113
	/**
114
	 * @param string $oldStatus
115
	 * @param string $newStatus
116
	 * @return void
117
	 * @action transition_post_status
118
	 */
119 1
	public function onChangeStatus( $newStatus, $oldStatus, WP_Post $post )
120
	{
121 1
		if( $post->post_type != Application::POST_TYPE || in_array( $oldStatus, ['new', $newStatus] ))return;
122
		$review = glsr( ReviewManager::class )->single( get_post( $post->ID ));
123
		if( $post->post_status == 'publish' ) {
124
			glsr( CountsManager::class )->increase( $review );
125
		}
126
		else {
127
			glsr( CountsManager::class )->decrease( $review );
128
		}
129
	}
130
}
131