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