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