| 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 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class ReviewController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	 * @param int $postId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 * @param array $terms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * @param array $termIds | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * @param string $taxonomySlug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * @param bool $append | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @param array $oldTermIds | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @action set_object_terms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	public function onAfterChangeCategory( $postId, $terms, $termIds, $taxonomySlug, $append, $oldTermIds ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		if( !$this->isReviewPostType( $postId ))return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		$review = glsr( ReviewManager::class )->single( get_post( $postId )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$ignoredTerms = array_intersect( $oldTermIds, $termIds ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		$review->term_ids = array_diff( $oldTermIds, $ignoredTerms ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		glsr( CountsManager::class )->decreaseTermCounts( $review ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		$review->term_ids = array_diff( $termIds, $ignoredTerms ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		glsr( CountsManager::class )->increaseTermCounts( $review ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * @param array $postData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * @param array $meta | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * @param int $postId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @action site-reviews/create/review | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	public function onAfterCreate( $postData, $meta, $postId ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		if( !$this->isReviewPostType( $postId ))return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$review = glsr( ReviewManager::class )->single( get_post( $postId )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		glsr( CountsManager::class )->increase( $review ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param int $postId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * @action before_delete_post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	public function onBeforeDelete( $postId ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		if( !$this->isReviewPostType( $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->isReviewPostType( $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 |  |  | 	public function onBeforeChangeAssignedTo( Review $review, $metaValue ) | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	public function onBeforeChangeRating( Review $review, $metaValue ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	public function onBeforeChangeReviewType( Review $review, $metaValue ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 * @param string $oldStatus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * @param string $newStatus | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @action transition_post_status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	public function onChangeStatus( $newStatus, $oldStatus, WP_Post $post ) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		if( $post->post_type != Application::POST_TYPE || in_array( $oldStatus, ['new', $newStatus] ))return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$review = glsr( ReviewManager::class )->single( get_post( $postId )); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		if( $status == 'publish' ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			glsr( CountsManager::class )->increase( $review ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			glsr( CountsManager::class )->decrease( $review ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @param int $postId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	protected function isReviewPostType( $postId ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		return get_post_field( 'post_type', $postId ) !== Application::POST_TYPE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  |