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 |
|
sort( $termIds ); |
27
|
1 |
|
sort( $oldTermIds ); |
28
|
1 |
|
if( $termIds === $oldTermIds || !$this->isReviewPostId( $postId ))return; |
29
|
|
|
$review = glsr( ReviewManager::class )->single( get_post( $postId )); |
30
|
|
|
$ignoredTerms = array_intersect( $oldTermIds, $termIds ); |
31
|
|
|
if( $review->term_ids = array_diff( $oldTermIds, $ignoredTerms )) { |
32
|
|
|
glsr( CountsManager::class )->decreaseTermCounts( $review ); |
33
|
|
|
} |
34
|
|
|
if( $review->term_ids = array_diff( $termIds, $ignoredTerms )) { |
35
|
|
|
glsr( CountsManager::class )->increaseTermCounts( $review ); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return void |
41
|
|
|
* @action site-reviews/review/created |
42
|
|
|
*/ |
43
|
1 |
|
public function onAfterCreate( Review $review ) |
44
|
|
|
{ |
45
|
1 |
|
glsr( CountsManager::class )->increase( $review ); |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param int $postId |
50
|
|
|
* @return void |
51
|
|
|
* @action before_delete_post |
52
|
|
|
*/ |
53
|
|
|
public function onBeforeDelete( $postId ) |
54
|
|
|
{ |
55
|
|
|
if( !$this->isReviewPostId( $postId ))return; |
56
|
|
|
$review = glsr( ReviewManager::class )->single( get_post( $postId )); |
57
|
|
|
glsr( CountsManager::class )->decrease( $review ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param int $metaId |
62
|
|
|
* @param int $postId |
63
|
|
|
* @param string $metaKey |
64
|
|
|
* @param mixed $metaValue |
65
|
|
|
* @return void |
66
|
|
|
* @action update_postmeta |
67
|
|
|
*/ |
68
|
|
|
public function onBeforeUpdate( $metaId, $postId, $metaKey, $metaValue ) |
69
|
|
|
{ |
70
|
|
|
if( !$this->isReviewPostId( $postId ) |
71
|
|
|
|| !in_array( $metaKey, ['assigned_to', 'rating', 'review_type'] ) |
72
|
|
|
)return; |
73
|
|
|
$review = glsr( ReviewManager::class )->single( get_post( $postId )); |
74
|
|
|
if( $review->$metaKey == $metaValue )return; |
75
|
|
|
$method = glsr( Helper::class )->buildMethodName( $metaKey, 'onBeforeChange' ); |
76
|
|
|
call_user_func( [$this, $method], $review, $metaValue ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string|int $assignedTo |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function onBeforeChangeAssignedTo( Review $review, $assignedTo ) |
84
|
|
|
{ |
85
|
|
|
glsr( CountsManager::class )->decreasePostCounts( $review ); |
86
|
|
|
$review->assigned_to = $assignedTo; |
87
|
|
|
glsr( CountsManager::class )->increasePostCounts( $review ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string|int $rating |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function onBeforeChangeRating( Review $review, $rating ) |
95
|
|
|
{ |
96
|
|
|
glsr( CountsManager::class )->decrease( $review ); |
97
|
|
|
$review->rating = $rating; |
98
|
|
|
glsr( CountsManager::class )->increase( $review ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $reviewType |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function onBeforeChangeReviewType( Review $review, $reviewType ) |
106
|
|
|
{ |
107
|
|
|
glsr( CountsManager::class )->decrease( $review ); |
108
|
|
|
$review->review_type = $reviewType; |
109
|
|
|
glsr( CountsManager::class )->increase( $review ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $oldStatus |
114
|
|
|
* @param string $newStatus |
115
|
|
|
* @return void |
116
|
|
|
* @action transition_post_status |
117
|
|
|
*/ |
118
|
1 |
|
public function onChangeStatus( $newStatus, $oldStatus, WP_Post $post ) |
119
|
|
|
{ |
120
|
1 |
|
if( $post->post_type != Application::POST_TYPE || in_array( $oldStatus, ['new', $newStatus] ))return; |
121
|
|
|
$review = glsr( ReviewManager::class )->single( get_post( $post->ID )); |
122
|
|
|
if( $post->post_status == 'publish' ) { |
123
|
|
|
glsr( CountsManager::class )->increase( $review ); |
124
|
|
|
} |
125
|
|
|
else { |
126
|
|
|
glsr( CountsManager::class )->decrease( $review ); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|