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 GeminiLabs\SiteReviews\Reviews; |
15
|
|
|
use WP_Post; |
16
|
|
|
use WP_Query; |
17
|
|
|
|
18
|
|
|
class ReviewManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @return false|Review |
22
|
|
|
*/ |
23
|
1 |
|
public function create( CreateReview $command ) |
24
|
|
|
{ |
25
|
1 |
|
$reviewValues = glsr( CreateReviewDefaults::class )->restrict( (array)$command ); |
26
|
1 |
|
$reviewValues = apply_filters( 'site-reviews/create/review-values', $reviewValues, $command ); |
27
|
|
|
$postValues = [ |
28
|
1 |
|
'comment_status' => 'closed', |
29
|
1 |
|
'meta_input' => $reviewValues, |
30
|
1 |
|
'ping_status' => 'closed', |
31
|
1 |
|
'post_content' => $reviewValues['content'], |
32
|
1 |
|
'post_date' => $reviewValues['date'], |
33
|
1 |
|
'post_name' => $reviewValues['review_type'].'-'.$reviewValues['review_id'], |
34
|
1 |
|
'post_status' => $this->getNewPostStatus( $reviewValues, $command->blacklisted ), |
35
|
1 |
|
'post_title' => $reviewValues['title'], |
36
|
|
|
'post_type' => Application::POST_TYPE, |
37
|
|
|
]; |
38
|
1 |
|
$postId = wp_insert_post( $postValues, true ); |
39
|
1 |
|
if( is_wp_error( $postId )) { |
40
|
|
|
glsr_log()->error( $postId->get_error_message() )->debug( $postValues ); |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
1 |
|
$this->setTerms( $postId, $command->category ); |
44
|
1 |
|
$review = $this->single( get_post( $postId )); |
45
|
1 |
|
do_action( 'site-reviews/review/created', $review, $command ); |
46
|
1 |
|
return $review; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $metaReviewId |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function delete( $metaReviewId ) |
54
|
|
|
{ |
55
|
|
|
if( $postId = $this->getPostId( $metaReviewId )) { |
56
|
|
|
wp_delete_post( $postId, true ); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return object |
62
|
|
|
*/ |
63
|
|
|
public function get( array $args = [] ) |
64
|
|
|
{ |
65
|
|
|
$args = glsr( ReviewsDefaults::class )->restrict( $args ); |
66
|
|
|
$metaQuery = glsr( QueryBuilder::class )->buildQuery( |
67
|
|
|
['assigned_to', 'type', 'rating'], |
68
|
|
|
$args |
69
|
|
|
); |
70
|
|
|
$taxQuery = glsr( QueryBuilder::class )->buildQuery( |
71
|
|
|
['category'], |
72
|
|
|
['category' => $this->normalizeTermIds( $args['category'] )] |
73
|
|
|
); |
74
|
|
|
$paged = glsr( QueryBuilder::class )->getPaged( |
75
|
|
|
wp_validate_boolean( $args['pagination'] ) |
76
|
|
|
); |
77
|
|
|
$query = new WP_Query([ |
78
|
|
|
'meta_key' => 'pinned', |
79
|
|
|
'meta_query' => $metaQuery, |
80
|
|
|
'offset' => $args['offset'], |
81
|
|
|
'order' => $args['order'], |
82
|
|
|
'orderby' => 'meta_value '.$args['orderby'], |
83
|
|
|
'paged' => $paged, |
84
|
|
|
'post__in' => $args['post__in'], |
85
|
|
|
'post__not_in' => $args['post__not_in'], |
86
|
|
|
'post_status' => 'publish', |
87
|
|
|
'post_type' => Application::POST_TYPE, |
88
|
|
|
'posts_per_page' => $args['count'], |
89
|
|
|
'tax_query' => $taxQuery, |
90
|
|
|
]); |
91
|
|
|
$results = array_map( [$this, 'single'], $query->posts ); |
92
|
|
|
$reviews = new Reviews( $results, $query->max_num_pages, $args ); |
93
|
|
|
return apply_filters( 'site-reviews/get/reviews', $reviews, $query ); |
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 normalizeTermIds( $commaSeparatedTermIds ) |
110
|
|
|
{ |
111
|
1 |
|
$termIds = glsr_array_column( $this->normalizeTerms( $commaSeparatedTermIds ), 'term_id' ); |
112
|
1 |
|
return array_unique( array_map( 'intval', $termIds )); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $commaSeparatedTermIds |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
1 |
|
public function normalizeTerms( $commaSeparatedTermIds ) |
120
|
|
|
{ |
121
|
1 |
|
$terms = []; |
122
|
1 |
|
$termIds = glsr( Helper::class )->convertStringToArray( $commaSeparatedTermIds ); |
123
|
1 |
|
foreach( $termIds as $termId ) { |
124
|
|
|
if( is_numeric( $termId )) { |
125
|
|
|
$termId = intval( $termId ); |
126
|
|
|
} |
127
|
|
|
$term = term_exists( $termId, Application::TAXONOMY ); |
128
|
|
|
if( !isset( $term['term_id'] ))continue; |
129
|
|
|
$terms[] = $term; |
130
|
|
|
} |
131
|
1 |
|
return $terms; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param int $postId |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function revert( $postId ) |
139
|
|
|
{ |
140
|
|
|
if( get_post_field( 'post_type', $postId ) != Application::POST_TYPE )return; |
141
|
|
|
delete_post_meta( $postId, '_edit_last' ); |
142
|
|
|
$result = wp_update_post([ |
143
|
|
|
'ID' => $postId, |
144
|
|
|
'post_content' => get_post_meta( $postId, 'content', true ), |
145
|
|
|
'post_date' => get_post_meta( $postId, 'date', true ), |
146
|
|
|
'post_title' => get_post_meta( $postId, 'title', true ), |
147
|
|
|
]); |
148
|
|
|
if( is_wp_error( $result )) { |
149
|
|
|
glsr_log()->error( $result->get_error_message() ); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return Review |
155
|
|
|
*/ |
156
|
1 |
|
public function single( WP_Post $post ) |
157
|
|
|
{ |
158
|
1 |
|
if( $post->post_type != Application::POST_TYPE ) { |
159
|
|
|
$post = new WP_Post( (object)[] ); |
160
|
|
|
} |
161
|
1 |
|
$review = new Review( $post ); |
162
|
1 |
|
return apply_filters( 'site-reviews/get/review', $review, $post ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param bool $isBlacklisted |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
1 |
|
protected function getNewPostStatus( array $review, $isBlacklisted ) |
170
|
|
|
{ |
171
|
1 |
|
$requireApprovalOption = glsr( OptionManager::class )->get( 'settings.general.require.approval' ); |
172
|
1 |
|
return $review['review_type'] == 'local' && ( $requireApprovalOption == 'yes' || $isBlacklisted ) |
173
|
|
|
? 'pending' |
174
|
1 |
|
: 'publish'; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param int $postId |
179
|
|
|
* @param string $termIds |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
1 |
|
protected function setTerms( $postId, $termIds ) |
183
|
|
|
{ |
184
|
1 |
|
$termIds = $this->normalizeTermIds( $termIds ); |
185
|
1 |
|
if( empty( $termIds ))return; |
186
|
|
|
$termTaxonomyIds = wp_set_object_terms( $postId, $termIds, Application::TAXONOMY ); |
187
|
|
|
if( is_wp_error( $termTaxonomyIds )) { |
188
|
|
|
glsr_log()->error( $termTaxonomyIds->get_error_message() ); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|