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