1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Controllers\Controller; |
7
|
|
|
use GeminiLabs\SiteReviews\Controllers\ListTableController\Columns; |
8
|
|
|
use GeminiLabs\SiteReviews\Database; |
9
|
|
|
use GeminiLabs\SiteReviews\Helper; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Html; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
12
|
|
|
use WP_Post; |
13
|
|
|
use WP_Query; |
14
|
|
|
use WP_Screen; |
15
|
|
|
|
16
|
|
|
class ListTableController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return void |
20
|
|
|
* @action admin_action_approve |
21
|
|
|
*/ |
22
|
|
|
public function approve() |
23
|
|
|
{ |
24
|
|
|
if( filter_input( INPUT_GET, 'plugin' ) != Application::ID )return; |
25
|
|
|
check_admin_referer( 'approve-review_'.( $postId = $this->getPostId() )); |
26
|
|
|
wp_update_post([ |
27
|
|
|
'ID' => $postId, |
28
|
|
|
'post_status' => 'publish', |
29
|
|
|
]); |
30
|
|
|
wp_safe_redirect( wp_get_referer() ); |
31
|
|
|
exit; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $messages |
36
|
|
|
* @return array |
37
|
|
|
* @filter bulk_post_updated_messages |
38
|
|
|
*/ |
39
|
|
|
public function filterBulkUpdateMessages( $messages, array $counts ) |
40
|
|
|
{ |
41
|
|
|
$messages = glsr( Helper::class )->consolidateArray( $messages ); |
42
|
|
|
$messages[Application::POST_TYPE] = [ |
43
|
|
|
'updated' => _n( '%s review updated.', '%s reviews updated.', $counts['updated'], 'site-reviews' ), |
44
|
|
|
'locked' => _n( '%s review not updated, somebody is editing it.', '%s reviews not updated, somebody is editing them.', $counts['locked'], 'site-reviews' ), |
45
|
|
|
'deleted' => _n( '%s review permanently deleted.', '%s reviews permanently deleted.', $counts['deleted'], 'site-reviews' ), |
46
|
|
|
'trashed' => _n( '%s review moved to the Trash.', '%s reviews moved to the Trash.', $counts['trashed'], 'site-reviews' ), |
47
|
|
|
'untrashed' => _n( '%s review restored from the Trash.', '%s reviews restored from the Trash.', $counts['untrashed'], 'site-reviews' ), |
48
|
|
|
]; |
49
|
|
|
return $messages; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $columns |
54
|
|
|
* @return array |
55
|
|
|
* @filter manage_.Application::POST_TYPE._posts_columns |
56
|
|
|
*/ |
57
|
|
|
public function filterColumnsForPostType( $columns ) |
58
|
|
|
{ |
59
|
|
|
$columns = glsr( Helper::class )->consolidateArray( $columns ); |
60
|
|
|
$postTypeColumns = glsr()->postTypeColumns[Application::POST_TYPE]; |
61
|
|
|
foreach( $postTypeColumns as $key => &$value ) { |
62
|
|
|
if( !array_key_exists( $key, $columns ) || !empty( $value ))continue; |
63
|
|
|
$value = $columns[$key]; |
64
|
|
|
} |
65
|
|
|
if( count( glsr( Database::class )->getReviewsMeta( 'review_type' )) < 2 ) { |
66
|
|
|
unset( $postTypeColumns['review_type'] ); |
67
|
|
|
} |
68
|
|
|
return array_filter( $postTypeColumns, 'strlen' ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $status |
73
|
|
|
* @return string |
74
|
|
|
* @filter post_date_column_status |
75
|
|
|
*/ |
76
|
|
|
public function filterDateColumnStatus( $status, WP_Post $post ) |
77
|
|
|
{ |
78
|
|
|
if( $post->post_type == Application::POST_TYPE ) { |
79
|
|
|
$status = __( 'Submitted', 'site-reviews' ); |
80
|
|
|
} |
81
|
|
|
return $status; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $hidden |
86
|
|
|
* @return array |
87
|
|
|
* @filter default_hidden_columns |
88
|
|
|
*/ |
89
|
|
|
public function filterDefaultHiddenColumns( $hidden, WP_Screen $screen ) |
90
|
|
|
{ |
91
|
|
|
if( $screen->id == 'edit-'.Application::POST_TYPE ) { |
92
|
|
|
$hidden = glsr( Helper::class )->consolidateArray( $hidden ); |
93
|
|
|
$hidden = ['reviewer']; |
94
|
|
|
} |
95
|
|
|
return $hidden; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $postStates |
100
|
|
|
* @return array |
101
|
|
|
* @filter display_post_states |
102
|
|
|
*/ |
103
|
|
|
public function filterPostStates( $postStates, WP_Post $post ) { |
104
|
|
|
$postStates = glsr( Helper::class )->consolidateArray( $postStates ); |
105
|
|
|
if( $post->post_type == Application::POST_TYPE && array_key_exists( 'pending', $postStates )) { |
106
|
|
|
$postStates['pending'] = __( 'Unapproved', 'site-reviews' ); |
107
|
|
|
} |
108
|
|
|
return $postStates; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $actions |
113
|
|
|
* @return array |
114
|
|
|
* @filter post_row_actions |
115
|
|
|
*/ |
116
|
|
|
public function filterRowActions( $actions, WP_Post $post ) |
117
|
|
|
{ |
118
|
|
|
if( $post->post_type != Application::POST_TYPE || $post->post_status == 'trash' ) { |
119
|
|
|
return $actions; |
120
|
|
|
} |
121
|
|
|
unset( $actions['inline hide-if-no-js'] ); //Remove Quick-edit |
122
|
|
|
$rowActions = [ |
123
|
|
|
'approve' => esc_attr__( 'Approve', 'site-reviews' ), |
124
|
|
|
'unapprove' => esc_attr__( 'Unapprove', 'site-reviews' ), |
125
|
|
|
]; |
126
|
|
|
$newActions = []; |
127
|
|
|
foreach( $rowActions as $key => $text ) { |
128
|
|
|
$newActions[$key] = glsr( Builder::class )->a( $text, [ |
129
|
|
|
'aria-label' => sprintf( esc_attr_x( '%s this review', 'Approve the review', 'site-reviews' ), $text ), |
130
|
|
|
'class' => 'glsr-change-status', |
131
|
|
|
'href' => wp_nonce_url( |
132
|
|
|
admin_url( 'post.php?post='.$post->ID.'&action='.$key.'&plugin='.Application::ID ), |
133
|
|
|
$key.'-review_'.$post->ID |
134
|
|
|
), |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
return $newActions + glsr( Helper::class )->consolidateArray( $actions ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $columns |
142
|
|
|
* @return array |
143
|
|
|
* @filter manage_edit-.Application::POST_TYPE._sortable_columns |
144
|
|
|
*/ |
145
|
|
|
public function filterSortableColumns( $columns ) |
146
|
|
|
{ |
147
|
|
|
$columns = glsr( Helper::class )->consolidateArray( $columns ); |
148
|
|
|
$postTypeColumns = glsr()->postTypeColumns[Application::POST_TYPE]; |
149
|
|
|
unset( $postTypeColumns['cb'] ); |
150
|
|
|
foreach( $postTypeColumns as $key => $value ) { |
151
|
|
|
if( glsr( Helper::class )->startsWith( 'taxonomy', $key ))continue; |
152
|
|
|
$columns[$key] = $key; |
153
|
|
|
} |
154
|
|
|
return $columns; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Customize the post_type status text |
159
|
|
|
* @param string $translation |
160
|
|
|
* @param string $single |
161
|
|
|
* @param string $plural |
162
|
|
|
* @param int $number |
163
|
|
|
* @param string $domain |
164
|
|
|
* @return string |
165
|
|
|
* @filter ngettext |
166
|
|
|
*/ |
167
|
1 |
|
public function filterStatusText( $translation, $single, $plural, $number, $domain ) |
168
|
|
|
{ |
169
|
1 |
|
if( $this->canModifyTranslation( $domain )) { |
170
|
|
|
$strings = [ |
171
|
|
|
'Published' => __( 'Approved', 'site-reviews' ), |
172
|
|
|
'Pending' => __( 'Unapproved', 'site-reviews' ), |
173
|
|
|
]; |
174
|
|
|
foreach( $strings as $search => $replace ) { |
175
|
|
|
if( strpos( $single, $search ) === false )continue; |
176
|
|
|
$translation = $this->getTranslation([ |
177
|
|
|
'number' => $number, |
178
|
|
|
'plural' => str_replace( $search, $replace, $plural ), |
179
|
|
|
'single' => str_replace( $search, $replace, $single ), |
180
|
|
|
]); |
181
|
|
|
} |
182
|
|
|
} |
183
|
1 |
|
return $translation; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $columnName |
188
|
|
|
* @param string $postType |
189
|
|
|
* @return void |
190
|
|
|
* @action bulk_edit_custom_box |
191
|
|
|
*/ |
192
|
|
|
public function renderBulkEditFields( $columnName, $postType ) |
193
|
|
|
{ |
194
|
|
|
if( $columnName == 'assigned_to' && $postType == Application::POST_TYPE ) { |
195
|
|
|
glsr()->render( 'partials/editor/bulk-edit-assigned-to' ); |
196
|
|
|
}; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $postType |
201
|
|
|
* @return void |
202
|
|
|
* @action restrict_manage_posts |
203
|
|
|
*/ |
204
|
|
|
public function renderColumnFilters( $postType ) |
205
|
|
|
{ |
206
|
|
|
glsr( Columns::class )->renderFilters( $postType ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $column |
211
|
|
|
* @param string $postId |
212
|
|
|
* @return void |
213
|
|
|
* @action manage_posts_custom_column |
214
|
|
|
*/ |
215
|
|
|
public function renderColumnValues( $column, $postId ) |
216
|
|
|
{ |
217
|
|
|
glsr( Columns::class )->renderValues( $column, $postId ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param int $postId |
222
|
|
|
* @return void |
223
|
|
|
* @action save_post_.Application::POST_TYPE |
224
|
|
|
*/ |
225
|
1 |
|
public function saveBulkEditFields( $postId ) |
226
|
|
|
{ |
227
|
1 |
|
if( !current_user_can( 'edit_posts' ))return; |
228
|
|
|
$assignedTo = filter_input( INPUT_GET, 'assigned_to' ); |
229
|
|
|
if( $assignedTo && get_post( $assignedTo )) { |
230
|
|
|
update_post_meta( $postId, 'assigned_to', $assignedTo ); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return void |
236
|
|
|
* @action pre_get_posts |
237
|
|
|
*/ |
238
|
1 |
|
public function setQueryForColumn( WP_Query $query ) |
239
|
|
|
{ |
240
|
1 |
|
if( !$this->hasPermission( $query ))return; |
241
|
|
|
$this->setMetaQuery( $query, [ |
242
|
|
|
'rating', 'review_type', |
243
|
|
|
]); |
244
|
|
|
$this->setOrderby( $query ); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return void |
249
|
|
|
* @action admin_action_unapprove |
250
|
|
|
*/ |
251
|
|
|
public function unapprove() |
252
|
|
|
{ |
253
|
|
|
if( filter_input( INPUT_GET, 'plugin' ) != Application::ID )return; |
254
|
|
|
check_admin_referer( 'unapprove-review_'.( $postId = $this->getPostId() )); |
255
|
|
|
wp_update_post([ |
256
|
|
|
'ID' => $postId, |
257
|
|
|
'post_status' => 'pending', |
258
|
|
|
]); |
259
|
|
|
wp_safe_redirect( wp_get_referer() ); |
260
|
|
|
exit; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Check if the translation string can be modified |
265
|
|
|
* @param string $domain |
266
|
|
|
* @return bool |
267
|
|
|
*/ |
268
|
1 |
|
protected function canModifyTranslation( $domain = 'default' ) |
269
|
|
|
{ |
270
|
1 |
|
$screen = glsr_current_screen(); |
271
|
1 |
|
return $domain == 'default' |
272
|
1 |
|
&& $screen->base == 'edit' |
273
|
1 |
|
&& $screen->post_type == Application::POST_TYPE; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Get the modified translation string |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
|
protected function getTranslation( array $args ) |
281
|
|
|
{ |
282
|
|
|
$defaults = [ |
283
|
|
|
'number' => 0, |
284
|
|
|
'plural' => '', |
285
|
|
|
'single' => '', |
286
|
|
|
'text' => '', |
287
|
|
|
]; |
288
|
|
|
$args = (object) wp_parse_args( $args, $defaults ); |
289
|
|
|
$translations = get_translations_for_domain( Application::ID ); |
290
|
|
|
return $args->text |
291
|
|
|
? $translations->translate( $args->text ) |
292
|
|
|
: $translations->translate_plural( $args->single, $args->plural, $args->number ); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @return bool |
297
|
|
|
*/ |
298
|
1 |
|
protected function hasPermission( WP_Query $query ) |
299
|
|
|
{ |
300
|
1 |
|
global $pagenow; |
301
|
1 |
|
return is_admin() |
302
|
1 |
|
&& $query->is_main_query() |
303
|
1 |
|
&& $query->get( 'post_type' ) == Application::POST_TYPE |
304
|
1 |
|
&& $pagenow == 'edit.php'; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return void |
309
|
|
|
*/ |
310
|
|
|
protected function setMetaQuery( WP_Query $query, array $metaKeys ) |
311
|
|
|
{ |
312
|
|
|
foreach( $metaKeys as $key ) { |
313
|
|
|
if( !( $value = filter_input( INPUT_GET, $key )))continue; |
314
|
|
|
$metaQuery = (array)$query->get( 'meta_query' ); |
315
|
|
|
$metaQuery[] = [ |
316
|
|
|
'key' => $key, |
317
|
|
|
'value' => $value, |
318
|
|
|
]; |
319
|
|
|
$query->set( 'meta_query', $metaQuery ); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @return void |
325
|
|
|
*/ |
326
|
|
|
protected function setOrderby( WP_Query $query ) |
327
|
|
|
{ |
328
|
|
|
$orderby = $query->get( 'orderby' ); |
329
|
|
|
$columns = glsr()->postTypeColumns[Application::POST_TYPE]; |
330
|
|
|
unset( $columns['cb'], $columns['title'], $columns['date'] ); |
331
|
|
|
if( in_array( $orderby, array_keys( $columns ))) { |
332
|
|
|
if( $orderby == 'reviewer' ) { |
333
|
|
|
$orderby = 'author'; |
334
|
|
|
} |
335
|
|
|
$query->set( 'meta_key', $orderby ); |
336
|
|
|
$query->set( 'orderby', 'meta_value' ); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|