Passed
Push — master ( ecb0f6...008868 )
by Paul
04:35
created

ReviewManager::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 1
dl 0
loc 31
ccs 0
cts 24
cp 0
crap 2
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Commands\CreateReview;
7
use GeminiLabs\SiteReviews\Database\DefaultsManager;
8
use GeminiLabs\SiteReviews\Database\QueryBuilder;
9
use GeminiLabs\SiteReviews\Database\SqlQueries;
10
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults;
11
use GeminiLabs\SiteReviews\Defaults\ReviewsDefaults;
12
use GeminiLabs\SiteReviews\Helper;
13
use GeminiLabs\SiteReviews\Review;
14
use WP_Post;
15
use WP_Query;
16
17
class ReviewManager
18
{
19
	/**
20
	 * @return false|Review
21
	 */
22 1
	public function create( CreateReview $command )
23
	{
24 1
		$reviewValues = glsr( CreateReviewDefaults::class )->restrict( (array)$command );
25 1
		$reviewValues = apply_filters( 'site-reviews/create/review-values', $reviewValues, $command );
26
		$postValues = [
27 1
			'comment_status' => 'closed',
28 1
			'meta_input' => $reviewValues,
29 1
			'ping_status' => 'closed',
30 1
			'post_content' => $reviewValues['content'],
31 1
			'post_date' => $reviewValues['date'],
32 1
			'post_name' => $reviewValues['review_type'].'-'.$reviewValues['review_id'],
33 1
			'post_status' => $this->getNewPostStatus( $reviewValues, $command->blacklisted ),
34 1
			'post_title' => $reviewValues['title'],
35
			'post_type' => Application::POST_TYPE,
36
		];
37 1
		$postId = wp_insert_post( $postValues, true );
38 1
		if( is_wp_error( $postId )) {
39
			glsr_log()->error( $postId->get_error_message() )->debug( $postValues );
40
			return false;
41
		}
42 1
		$this->setTerms( $postId, $command->category );
43 1
		$review = $this->single( get_post( $postId ));
1 ignored issue
show
Bug introduced by
It seems like get_post($postId) can also be of type null and array and array; however, parameter $post of GeminiLabs\SiteReviews\D...ReviewManager::single() does only seem to accept 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

43
		$review = $this->single( /** @scrutinizer ignore-type */ get_post( $postId ));
Loading history...
44 1
		do_action( 'site-reviews/review/created', $review, $command );
45 1
		return $review;
46
	}
47
48
	/**
49
	 * @param string $metaReviewId
50
	 * @return void
51
	 */
52
	public function delete( $metaReviewId )
53
	{
54
		if( $postId = $this->getPostId( $metaReviewId )) {
55
			wp_delete_post( $postId, true );
56
		}
57
	}
58
59
	/**
60
	 * @return object
61
	 */
62
	public function get( array $args = [] )
63
	{
64
		$args = glsr( ReviewsDefaults::class )->restrict( $args );
65
		$metaQuery = glsr( QueryBuilder::class )->buildQuery(
66
			['assigned_to', 'type', 'rating'],
67
			$args
68
		);
69
		$taxQuery = glsr( QueryBuilder::class )->buildQuery(
70
			['category'],
71
			['category' => $this->normalizeTerms( $args['category'] )]
72
		);
73
		$paged = glsr( QueryBuilder::class )->getPaged(
74
			wp_validate_boolean( $args['pagination'] )
75
		);
76
		$reviews = new WP_Query([
77
			'meta_key' => 'pinned',
78
			'meta_query' => $metaQuery,
79
			'offset' => $args['offset'],
80
			'order' => $args['order'],
81
			'orderby' => 'meta_value '.$args['orderby'],
82
			'paged' => $paged,
83
			'post__in' => $args['post__in'],
84
			'post__not_in' => $args['post__not_in'],
85
			'post_status' => 'publish',
86
			'post_type' => Application::POST_TYPE,
87
			'posts_per_page' => $args['count'],
88
			'tax_query' => $taxQuery,
89
		]);
90
		return (object)[
91
			'results' => array_map( [$this, 'single'], $reviews->posts ),
92
			'max_num_pages' => $reviews->max_num_pages,
93
		];
94
	}
95
96
	/**
97
	 * @param string $metaReviewId
98
	 * @return int
99
	 */
100
	public function getPostId( $metaReviewId )
101
	{
102
		return glsr( SqlQueries::class )->getReviewPostId( $metaReviewId );
103
	}
104
105
	/**
106
	 * @param string $commaSeparatedTermIds
107
	 * @return array
108
	 */
109 1
	public function normalizeTerms( $commaSeparatedTermIds )
110
	{
111 1
		$terms = [];
112 1
		$termIds = glsr( Helper::class )->convertStringToArray( $commaSeparatedTermIds );
113 1
		foreach( $termIds as $termId ) {
114
			$term = get_term( $termId, Application::TAXONOMY );
115
			if( !isset( $term->term_id ))continue;
116
			$terms[] = $term->term_id;
117
		}
118 1
		return $terms;
119
	}
120
121
	/**
122
	 * @param int $postId
123
	 * @return void
124
	 */
125
	public function revert( $postId )
126
	{
127
		if( get_post_field( 'post_type', $postId ) != Application::POST_TYPE )return;
128
		delete_post_meta( $postId, '_edit_last' );
129
		$result = wp_update_post([
130
			'ID' => $postId,
131
			'post_content' => get_post_meta( $postId, 'content', true ),
132
			'post_date' => get_post_meta( $postId, 'date', true ),
133
			'post_title' => get_post_meta( $postId, 'title', true ),
134
		]);
135
		if( is_wp_error( $result )) {
136
			glsr_log()->error( $result->get_error_message() );
137
		}
138
	}
139
140
	/**
141
	 * @return Review
142
	 */
143 1
	public function single( WP_Post $post )
144
	{
145 1
		if( $post->post_type != Application::POST_TYPE )return;
146 1
		$review = new Review( $post );
147 1
		return apply_filters( 'site-reviews/get/review', $review, $post );
148
	}
149
150
	/**
151
	 * @param bool $isBlacklisted
152
	 * @return string
153
	 */
154 1
	protected function getNewPostStatus( array $review, $isBlacklisted )
155
	{
156 1
		$requireApprovalOption = glsr( OptionManager::class )->get( 'settings.general.require.approval' );
157 1
		return $review['review_type'] == 'local' && ( $requireApprovalOption == 'yes' || $isBlacklisted )
158
			? 'pending'
159 1
			: 'publish';
160
	}
161
162
	/**
163
	 * @param int $postId
164
	 * @param string $termIds
165
	 * @return void
166
	 */
167 1
	protected function setTerms( $postId, $termIds )
168
	{
169 1
		$terms = $this->normalizeTerms( $termIds );
170 1
		if( empty( $terms ))return;
171
		$result = wp_set_object_terms( $postId, $terms, Application::TAXONOMY );
172
		if( is_wp_error( $result )) {
173
			glsr_log()->error( $result->get_error_message() );
174
		}
175
	}
176
}
177