1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Controllers\ListTableController\Columns; |
7
|
|
|
use GeminiLabs\SiteReviews\Database; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
11
|
|
|
use WP_Post; |
12
|
|
|
use WP_Query; |
13
|
|
|
use WP_Screen; |
14
|
|
|
|
15
|
|
|
class ListTableController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return void |
19
|
|
|
* @action admin_action_approve |
20
|
|
|
*/ |
21
|
|
|
public function approve() |
22
|
|
|
{ |
23
|
|
|
if (Application::ID != filter_input(INPUT_GET, 'plugin')) { |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
check_admin_referer('approve-review_'.($postId = $this->getPostId())); |
27
|
|
|
wp_update_post([ |
28
|
|
|
'ID' => $postId, |
29
|
|
|
'post_status' => 'publish', |
30
|
|
|
]); |
31
|
|
|
wp_safe_redirect(wp_get_referer()); |
32
|
|
|
exit; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $columns |
37
|
|
|
* @return array |
38
|
|
|
* @filter manage_.Application::POST_TYPE._posts_columns |
39
|
|
|
*/ |
40
|
|
|
public function filterColumnsForPostType($columns) |
41
|
|
|
{ |
42
|
|
|
$columns = Arr::consolidateArray($columns); |
43
|
|
|
$postTypeColumns = glsr()->postTypeColumns[Application::POST_TYPE]; |
44
|
|
|
foreach ($postTypeColumns as $key => &$value) { |
45
|
|
|
if (!array_key_exists($key, $columns) || !empty($value)) { |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
$value = $columns[$key]; |
49
|
|
|
} |
50
|
|
|
if (count(glsr(Database::class)->getReviewsMeta('review_type')) < 2) { |
51
|
|
|
unset($postTypeColumns['review_type']); |
52
|
|
|
} |
53
|
|
|
return array_filter($postTypeColumns, 'strlen'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $status |
58
|
|
|
* @param WP_Post $post |
59
|
|
|
* @return string |
60
|
|
|
* @filter post_date_column_status |
61
|
|
|
*/ |
62
|
|
|
public function filterDateColumnStatus($status, $post) |
63
|
|
|
{ |
64
|
|
|
if (Application::POST_TYPE == Arr::get($post, 'post_type')) { |
65
|
|
|
$status = __('Submitted', 'site-reviews'); |
66
|
|
|
} |
67
|
|
|
return $status; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $hidden |
72
|
|
|
* @param WP_Screen $post |
73
|
|
|
* @return array |
74
|
|
|
* @filter default_hidden_columns |
75
|
|
|
*/ |
76
|
|
|
public function filterDefaultHiddenColumns($hidden, $screen) |
77
|
|
|
{ |
78
|
|
|
if (Arr::get($screen, 'id') == 'edit-'.Application::POST_TYPE) { |
79
|
|
|
$hidden = Arr::consolidateArray($hidden); |
80
|
|
|
$hidden = array_unique(array_merge($hidden, [ |
81
|
|
|
'email', 'ip_address', 'response', 'reviewer', |
82
|
|
|
])); |
83
|
|
|
} |
84
|
|
|
return $hidden; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $actions |
89
|
|
|
* @param WP_Post $post |
90
|
|
|
* @return array |
91
|
|
|
* @filter post_row_actions |
92
|
|
|
*/ |
93
|
|
|
public function filterRowActions($actions, $post) |
94
|
|
|
{ |
95
|
|
|
if (Application::POST_TYPE != Arr::get($post, 'post_type') || 'trash' == $post->post_status) { |
96
|
|
|
return $actions; |
97
|
|
|
} |
98
|
|
|
unset($actions['inline hide-if-no-js']); //Remove Quick-edit |
99
|
|
|
$rowActions = [ |
100
|
|
|
'approve' => esc_attr__('Approve', 'site-reviews'), |
101
|
|
|
'unapprove' => esc_attr__('Unapprove', 'site-reviews'), |
102
|
|
|
]; |
103
|
|
|
$newActions = []; |
104
|
|
|
foreach ($rowActions as $key => $text) { |
105
|
|
|
$newActions[$key] = glsr(Builder::class)->a($text, [ |
106
|
|
|
'aria-label' => sprintf(esc_attr_x('%s this review', 'Approve the review', 'site-reviews'), $text), |
107
|
|
|
'class' => 'glsr-change-status', |
108
|
|
|
'href' => wp_nonce_url( |
109
|
|
|
admin_url('post.php?post='.$post->ID.'&action='.$key.'&plugin='.Application::ID), |
110
|
|
|
$key.'-review_'.$post->ID |
111
|
|
|
), |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
return $newActions + Arr::consolidateArray($actions); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array $columns |
119
|
|
|
* @return array |
120
|
|
|
* @filter manage_edit-.Application::POST_TYPE._sortable_columns |
121
|
|
|
*/ |
122
|
|
|
public function filterSortableColumns($columns) |
123
|
|
|
{ |
124
|
|
|
$columns = Arr::consolidateArray($columns); |
125
|
|
|
$postTypeColumns = glsr()->postTypeColumns[Application::POST_TYPE]; |
126
|
|
|
unset($postTypeColumns['cb']); |
127
|
|
|
foreach ($postTypeColumns as $key => $value) { |
128
|
|
|
if (Str::startsWith('taxonomy', $key)) { |
129
|
|
|
continue; |
130
|
|
|
} |
131
|
|
|
$columns[$key] = $key; |
132
|
|
|
} |
133
|
|
|
return $columns; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $columnName |
138
|
|
|
* @param string $postType |
139
|
|
|
* @return void |
140
|
|
|
* @action bulk_edit_custom_box |
141
|
|
|
*/ |
142
|
|
|
public function renderBulkEditFields($columnName, $postType) |
143
|
|
|
{ |
144
|
|
|
if ('assigned_to' == $columnName && Application::POST_TYPE == $postType) { |
145
|
|
|
glsr()->render('partials/editor/bulk-edit-assigned-to'); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $postType |
151
|
|
|
* @return void |
152
|
|
|
* @action restrict_manage_posts |
153
|
|
|
*/ |
154
|
|
|
public function renderColumnFilters($postType) |
155
|
|
|
{ |
156
|
|
|
glsr(Columns::class)->renderFilters($postType); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $column |
161
|
|
|
* @param string $postId |
162
|
|
|
* @return void |
163
|
|
|
* @action manage_posts_custom_column |
164
|
|
|
*/ |
165
|
|
|
public function renderColumnValues($column, $postId) |
166
|
|
|
{ |
167
|
|
|
glsr(Columns::class)->renderValues($column, $postId); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param int $postId |
172
|
|
|
* @return void |
173
|
|
|
* @action save_post_.Application::POST_TYPE |
174
|
|
|
*/ |
175
|
1 |
|
public function saveBulkEditFields($postId) |
176
|
|
|
{ |
177
|
1 |
|
if (!current_user_can('edit_posts')) { |
178
|
1 |
|
return; |
179
|
|
|
} |
180
|
|
|
$assignedTo = filter_input(INPUT_GET, 'assigned_to'); |
181
|
|
|
if ($assignedTo && get_post($assignedTo)) { |
182
|
|
|
glsr(Database::class)->update($postId, 'assigned_to', $assignedTo); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return void |
188
|
|
|
* @action pre_get_posts |
189
|
|
|
*/ |
190
|
1 |
|
public function setQueryForColumn(WP_Query $query) |
191
|
|
|
{ |
192
|
1 |
|
if (!$this->hasPermission($query)) { |
193
|
1 |
|
return; |
194
|
|
|
} |
195
|
|
|
$this->setMetaQuery($query, [ |
196
|
|
|
'rating', 'review_type', |
197
|
|
|
]); |
198
|
|
|
$this->setOrderby($query); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return void |
203
|
|
|
* @action admin_action_unapprove |
204
|
|
|
*/ |
205
|
|
|
public function unapprove() |
206
|
|
|
{ |
207
|
|
|
if (Application::ID != filter_input(INPUT_GET, 'plugin')) { |
208
|
|
|
return; |
209
|
|
|
} |
210
|
|
|
check_admin_referer('unapprove-review_'.($postId = $this->getPostId())); |
211
|
|
|
wp_update_post([ |
212
|
|
|
'ID' => $postId, |
213
|
|
|
'post_status' => 'pending', |
214
|
|
|
]); |
215
|
|
|
wp_safe_redirect(wp_get_referer()); |
216
|
|
|
exit; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Check if the translation string can be modified. |
221
|
|
|
* @param string $domain |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
protected function canModifyTranslation($domain = 'default') |
225
|
|
|
{ |
226
|
|
|
$screen = glsr_current_screen(); |
227
|
|
|
return 'default' == $domain |
228
|
|
|
&& 'edit' == $screen->base |
229
|
|
|
&& Application::POST_TYPE == $screen->post_type; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return bool |
234
|
|
|
*/ |
235
|
1 |
|
protected function hasPermission(WP_Query $query) |
236
|
|
|
{ |
237
|
1 |
|
global $pagenow; |
238
|
1 |
|
return is_admin() |
239
|
1 |
|
&& $query->is_main_query() |
240
|
1 |
|
&& Application::POST_TYPE == $query->get('post_type') |
241
|
1 |
|
&& 'edit.php' == $pagenow; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return void |
246
|
|
|
*/ |
247
|
|
|
protected function setMetaQuery(WP_Query $query, array $metaKeys) |
248
|
|
|
{ |
249
|
|
|
foreach ($metaKeys as $key) { |
250
|
|
|
$value = (string) filter_input(INPUT_GET, $key); |
251
|
|
|
if ('' === $value) { |
252
|
|
|
continue; |
253
|
|
|
} |
254
|
|
|
$metaQuery = (array) $query->get('meta_query'); |
255
|
|
|
$metaQuery[] = [ |
256
|
|
|
'key' => Str::prefix('_', $key, '_'), |
257
|
|
|
'value' => $value, |
258
|
|
|
]; |
259
|
|
|
$query->set('meta_query', array_filter($metaQuery)); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return void |
265
|
|
|
*/ |
266
|
|
|
protected function setOrderby(WP_Query $query) |
267
|
|
|
{ |
268
|
|
|
$orderby = $query->get('orderby'); |
269
|
|
|
$columns = glsr()->postTypeColumns[Application::POST_TYPE]; |
270
|
|
|
unset($columns['cb'], $columns['title'], $columns['date']); |
271
|
|
|
if (in_array($orderby, array_keys($columns))) { |
272
|
|
|
if ('reviewer' == $orderby) { |
273
|
|
|
$orderby = 'author'; |
274
|
|
|
} |
275
|
|
|
$query->set('meta_key', Str::prefix('_', $orderby, '_')); |
276
|
|
|
$query->set('orderby', 'meta_value'); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|