|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Controllers\ListTableColumns\ColumnFilterRating; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Controllers\ListTableColumns\ColumnFilterType; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Database\Query; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Defaults\ColumnFilterbyDefaults; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Defaults\ColumnOrderbyDefaults; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
12
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
13
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
14
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
|
15
|
|
|
use GeminiLabs\SiteReviews\Modules\Migrate; |
|
16
|
|
|
use WP_Post; |
|
17
|
|
|
use WP_Query; |
|
18
|
|
|
use WP_Screen; |
|
19
|
|
|
|
|
20
|
|
|
class ListTableController extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param array $columns |
|
24
|
|
|
* @return array |
|
25
|
|
|
* @filter manage_{glsr()->post_type}_posts_columns |
|
26
|
|
|
*/ |
|
27
|
|
|
public function filterColumnsForPostType($columns) |
|
28
|
|
|
{ |
|
29
|
|
|
$columns = Arr::consolidate($columns); |
|
30
|
|
|
$postTypeColumns = glsr()->retrieve('columns.'.glsr()->post_type, []); |
|
31
|
|
|
foreach ($postTypeColumns as $key => &$value) { |
|
32
|
|
|
if (array_key_exists($key, $columns) && empty($value)) { |
|
33
|
|
|
$value = $columns[$key]; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
return array_filter($postTypeColumns, 'strlen'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $status |
|
41
|
|
|
* @param WP_Post $post |
|
42
|
|
|
* @return string |
|
43
|
|
|
* @filter post_date_column_status |
|
44
|
|
|
*/ |
|
45
|
|
|
public function filterDateColumnStatus($status, $post) |
|
46
|
|
|
{ |
|
47
|
|
|
$isReview = glsr()->post_type === Arr::get($post, 'post_type'); |
|
48
|
|
|
return Helper::ifTrue(!$isReview, $status, _x('Submitted', 'admin-text', 'site-reviews')); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param array $hidden |
|
53
|
|
|
* @param WP_Screen $screen |
|
54
|
|
|
* @return array |
|
55
|
|
|
* @filter default_hidden_columns |
|
56
|
|
|
*/ |
|
57
|
|
|
public function filterDefaultHiddenColumns($hidden, $screen) |
|
58
|
|
|
{ |
|
59
|
|
|
if (Arr::get($screen, 'id') === 'edit-'.glsr()->post_type) { |
|
60
|
|
|
$hidden = Arr::consolidate($hidden); |
|
61
|
|
|
$hidden = array_unique(array_merge($hidden, [ |
|
62
|
|
|
'assigned_users', 'author_name', 'author_email', 'ip_address', 'response', |
|
63
|
|
|
])); |
|
64
|
|
|
} |
|
65
|
|
|
return $hidden; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return array |
|
70
|
|
|
* @filter posts_clauses |
|
71
|
|
|
*/ |
|
72
|
|
|
public function filterPostClauses(array $clauses, WP_Query $query) |
|
73
|
|
|
{ |
|
74
|
|
|
if (!$this->hasQueryPermission($query) || (!$this->isListFiltered() && !$this->isListOrdered())) { |
|
75
|
|
|
return $clauses; |
|
76
|
|
|
} |
|
77
|
|
|
$table = glsr(Query::class)->table('ratings'); |
|
78
|
|
|
foreach ($clauses as $key => &$clause) { |
|
79
|
|
|
$method = Helper::buildMethodName($key, 'modifyClause'); |
|
80
|
|
|
if (method_exists($this, $method)) { |
|
81
|
|
|
$clause = call_user_func([$this, $method], $clause, $table, $query); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
return glsr()->filterArray('review-table/clauses', $clauses, $table, $query); |
|
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 (glsr()->post_type !== Arr::get($post, 'post_type') |
|
96
|
|
|
|| 'trash' == $post->post_status |
|
97
|
|
|
|| !user_can(get_current_user_id(), 'edit_post', $post->ID)) { |
|
98
|
|
|
return $actions; |
|
99
|
|
|
} |
|
100
|
|
|
unset($actions['inline hide-if-no-js']); //Remove Quick-edit |
|
101
|
|
|
$rowActions = [ |
|
102
|
|
|
'approve' => _x('Approve', 'admin-text', 'site-reviews'), |
|
103
|
|
|
'unapprove' => _x('Unapprove', 'admin-text', 'site-reviews'), |
|
104
|
|
|
]; |
|
105
|
|
|
$newActions = []; |
|
106
|
|
|
foreach ($rowActions as $key => $text) { |
|
107
|
|
|
$newActions[$key] = glsr(Builder::class)->a($text, [ |
|
108
|
|
|
'aria-label' => esc_attr(sprintf(_x('%s this review', 'Approve the review (admin-text)', 'site-reviews'), $text)), |
|
109
|
|
|
'class' => 'glsr-toggle-status', |
|
110
|
|
|
'href' => wp_nonce_url( |
|
111
|
|
|
admin_url('post.php?post='.$post->ID.'&action='.$key.'&plugin='.glsr()->id), |
|
112
|
|
|
$key.'-review_'.$post->ID |
|
113
|
|
|
), |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
return $newActions + Arr::consolidate($actions); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param array $columns |
|
121
|
|
|
* @return array |
|
122
|
|
|
* @filter manage_edit-{glsr()->post_type}_sortable_columns |
|
123
|
|
|
*/ |
|
124
|
|
|
public function filterSortableColumns($columns) |
|
125
|
|
|
{ |
|
126
|
|
|
$columns = Arr::consolidate($columns); |
|
127
|
|
|
$postTypeColumns = glsr()->retrieve('columns.'.glsr()->post_type, []); |
|
128
|
|
|
unset($postTypeColumns['cb']); |
|
129
|
|
|
foreach ($postTypeColumns as $key => $value) { |
|
130
|
|
|
if (!Str::startsWith('assigned', $key) && !Str::startsWith('taxonomy', $key)) { |
|
131
|
|
|
$columns[$key] = $key; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
return $columns; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param string $postType |
|
139
|
|
|
* @return void |
|
140
|
|
|
* @action restrict_manage_posts |
|
141
|
|
|
*/ |
|
142
|
|
|
public function renderColumnFilters($postType) |
|
143
|
|
|
{ |
|
144
|
|
|
if (glsr()->post_type === $postType) { |
|
145
|
|
|
echo Cast::toString(glsr()->runIf(ColumnFilterRating::class)); |
|
146
|
|
|
echo Cast::toString(glsr()->runIf(ColumnFilterType::class)); |
|
147
|
|
|
echo Cast::toString(glsr()->runIf(glsr()->filterString('review-table/filter', ''))); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $column |
|
153
|
|
|
* @param int $postId |
|
154
|
|
|
* @return void |
|
155
|
|
|
* @action manage_{glsr()->post_type}_posts_custom_column |
|
156
|
|
|
*/ |
|
157
|
|
|
public function renderColumnValues($column, $postId) |
|
158
|
|
|
{ |
|
159
|
|
|
$review = glsr(Query::class)->review($postId); |
|
160
|
|
|
if (!$review->isValid()) { |
|
161
|
|
|
glsr(Migrate::class)->reset(); // looks like a migration is needed! |
|
162
|
|
|
return; |
|
163
|
|
|
} |
|
164
|
|
|
$className = Helper::buildClassName('column-value-'.$column, 'Controllers\ListTableColumns'); |
|
165
|
|
|
$className = glsr()->filterString('column/'.$column, $className); |
|
166
|
|
|
$value = glsr()->runIf($className, $review); |
|
167
|
|
|
$value = glsr()->filterString('columns/'.$column, $value, $postId); |
|
168
|
|
|
echo Helper::ifEmpty($value, '—'); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return void |
|
173
|
|
|
* @action pre_get_posts |
|
174
|
|
|
*/ |
|
175
|
15 |
|
public function setQueryForColumn(WP_Query $query) |
|
176
|
|
|
{ |
|
177
|
15 |
|
if (!$this->hasQueryPermission($query)) { |
|
178
|
15 |
|
return; |
|
179
|
|
|
} |
|
180
|
|
|
$orderby = $query->get('orderby'); |
|
181
|
|
|
if ('response' === $orderby) { |
|
182
|
|
|
$query->set('meta_key', Str::prefix($orderby, '_')); |
|
183
|
|
|
$query->set('orderby', 'meta_value'); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @return bool |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function isListFiltered() |
|
191
|
|
|
{ |
|
192
|
|
|
$filterBy = glsr(ColumnFilterbyDefaults::class)->defaults(); |
|
193
|
|
|
$filterBy = filter_input_array(INPUT_GET, $filterBy); |
|
194
|
|
|
$filterBy = Arr::removeEmptyValues($filterBy); |
|
195
|
|
|
return !empty($filterBy); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @return bool |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function isListOrdered() |
|
202
|
|
|
{ |
|
203
|
|
|
$columns = glsr(ColumnOrderbyDefaults::class)->defaults(); |
|
204
|
|
|
return array_key_exists(get_query_var('orderby'), $columns); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return bool |
|
209
|
|
|
*/ |
|
210
|
|
|
protected function isOrderbyWithIsNull($column) |
|
211
|
|
|
{ |
|
212
|
|
|
$columns = [ |
|
213
|
|
|
'email', 'name', 'ip_address', 'type', |
|
214
|
|
|
]; |
|
215
|
|
|
$columns = glsr()->filterArray('columns/orderby-is-null', $columns); |
|
216
|
|
|
return in_array($column, $columns); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param string $join |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
|
|
protected function modifyClauseJoin($join, $table, WP_Query $query) |
|
224
|
|
|
{ |
|
225
|
|
|
global $wpdb; |
|
226
|
|
|
$join .= " INNER JOIN {$table} ON {$table}.review_id = {$wpdb->posts}.ID "; |
|
227
|
|
|
return $join; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param string $orderby |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
|
|
protected function modifyClauseOrderby($orderby, $table, WP_Query $query) |
|
235
|
|
|
{ |
|
236
|
|
|
$columns = glsr(ColumnOrderbyDefaults::class)->defaults(); |
|
237
|
|
|
if ($column = Arr::get($columns, $query->get('orderby'))) { |
|
238
|
|
|
$order = $query->get('order'); |
|
239
|
|
|
$orderby = "{$table}.{$column} {$order}"; |
|
240
|
|
|
if ($this->isOrderbyWithIsNull($column)) { |
|
241
|
|
|
$orderby = "NULLIF({$table}.{$column}, '') IS NULL, {$orderby}"; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
return $orderby; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @param string $where |
|
249
|
|
|
* @return string |
|
250
|
|
|
*/ |
|
251
|
|
|
protected function modifyClauseWhere($where, $table, WP_Query $query) |
|
252
|
|
|
{ |
|
253
|
|
|
$filterBy = glsr(ColumnFilterbyDefaults::class)->defaults(); |
|
254
|
|
|
$filterBy = filter_input_array(INPUT_GET, $filterBy); |
|
255
|
|
|
$filterBy = Arr::removeEmptyValues($filterBy); |
|
256
|
|
|
foreach ($filterBy as $key => $value) { |
|
257
|
|
|
$where .= " AND {$table}.{$key} = '{$value}' "; |
|
258
|
|
|
} |
|
259
|
|
|
return $where; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|