Test Failed
Push — tmp ( 15f615...89cc97 )
by Paul
10:31 queued 04:40
created

ListTableController::modifyClauseOrderby()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 3
dl 0
loc 17
ccs 0
cts 8
cp 0
crap 6
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Controllers\ListTableColumns\ColumnFilterRating;
7
use GeminiLabs\SiteReviews\Controllers\ListTableColumns\ColumnFilterReviewType;
8
use GeminiLabs\SiteReviews\Database\Query;
9
use GeminiLabs\SiteReviews\Helper;
10
use GeminiLabs\SiteReviews\Helpers\Arr;
11
use GeminiLabs\SiteReviews\Helpers\Str;
12
use GeminiLabs\SiteReviews\Modules\Html\Builder;
13
use GeminiLabs\SiteReviews\Modules\Migrate;
14
use WP_Post;
15
use WP_Query;
16
use WP_Screen;
17
18
class ListTableController extends Controller
19
{
20
    /**
21
     * @param array $columns
22
     * @return array
23
     * @filter manage_.Application::POST_TYPE._posts_columns
24
     */
25
    public function filterColumnsForPostType($columns)
26
    {
27
        $columns = Arr::consolidate($columns);
28
        $postTypeColumns = glsr()->retrieve('columns.'.glsr()->post_type, []);
29
        foreach ($postTypeColumns as $key => &$value) {
30
            if (array_key_exists($key, $columns) && empty($value)) {
31
                $value = $columns[$key];
32
            }
33
        }
34
        return array_filter($postTypeColumns, 'strlen');
35
    }
36
37
    /**
38
     * @param string $status
39
     * @param WP_Post $post
40
     * @return string
41
     * @filter post_date_column_status
42
     */
43
    public function filterDateColumnStatus($status, $post)
44
    {
45
        if (glsr()->post_type == Arr::get($post, 'post_type')) {
46
            $status = _x('Submitted', 'admin-text', 'site-reviews');
47
        }
48
        return $status;
49
    }
50
51
    /**
52
     * @param array $hidden
53
     * @param WP_Screen $post
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
                'email', 'ip_address', 'response', 'reviewer',
63
            ]));
64
        }
65
        return $hidden;
66
    }
67
68
    /**
69
     * @return void
70
     * @filter posts_clauses
71
     */
72
    public function filterPostClauses(array $clauses, WP_Query $query)
73
    {
74
        if (!$this->hasPermission($query)) {
75
            return $clauses;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $clauses returns the type array which is incompatible with the documented return type void.
Loading history...
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 $clauses;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $clauses returns the type array which is incompatible with the documented return type void.
Loading history...
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-.Application::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 $columnName
139
     * @param string $postType
140
     * @return void
141
     * @action bulk_edit_custom_box
142
     */
143
    public function renderBulkEditFields($columnName, $postType)
144
    {
145
        if ('assigned_to' == $columnName && glsr()->post_type == $postType) {
146
            glsr()->render('partials/editor/bulk-edit-assigned-to');
147
        }
148
    }
149
150
    /**
151
     * @param string $postType
152
     * @return void
153
     * @action restrict_manage_posts
154
     */
155
    public function renderColumnFilters($postType)
156
    {
157
        if (glsr()->post_type !== $postType) {
158
            return;
159
        }
160
        if ($filter = glsr()->runIf(ColumnFilterRating::class)) {
161
            echo $filter;
162
        }
163
        if ($filter = glsr()->runIf(ColumnFilterReviewType::class)) {
164
            echo $filter;
165
        }
166
    }
167
168
    /**
169
     * @param string $column
170
     * @param int $postId
171
     * @return void
172
     * @action manage_posts_custom_column
173
     */
174
    public function renderColumnValues($column, $postId)
175
    {
176
        $review = glsr(Query::class)->review($postId);
177
        if (!$review->isValid()) {
178
            glsr(Migrate::class)->reset(); // looks like a migration is needed!
179
            return;
180
        }
181
        $className = Helper::buildClassName('ColumnValue'.$column, 'Controllers\ListTableColumns');
182
        $value = glsr()->runIf($className, $review);
183
        $value = glsr()->filterString('columns/'.$column, $value, $postId);
184
        echo Helper::ifEmpty($value, '&mdash;');
185
    }
186
187
    /**
188
     * @param int $postId
189
     * @return void
190
     * @action save_post_.Application::POST_TYPE
191
     */
192
    public function saveBulkEditFields($postId)
193
    {
194
        if (glsr()->can('edit_posts')) {
195
            $review = glsr(Query::class)->review($reviewId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $reviewId does not exist. Did you maybe mean $review?
Loading history...
196
            $assignedPostIds = Arr::consolidate(filter_input(INPUT_GET, 'assigned_to'));
197
            $assignedUserIds = Arr::consolidate(filter_input(INPUT_GET, 'user_ids'));
198
            glsr()->action('review/updated/post_ids', $review, $assignedPostIds);
199
            glsr()->action('review/updated/user_ids', $review, $assignedUserIds);
200
        }
201
    }
202
203
    /**
204
     * @return void
205
     * @action pre_get_posts
206
     */
207
    public function setQueryForColumn(WP_Query $query)
208
    {
209
        if (!$this->hasPermission($query)) {
210
            return;
211
        }
212
        $orderby = $query->get('orderby');
213
        if ('response' === $orderby) {
214
            $query->set('meta_key', Str::prefix('_', $orderby));
215
            $query->set('orderby', 'meta_value');
216
        }
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
            && glsr()->post_type == $screen->post_type;
230
    }
231
232
    /**
233
     * @return bool
234
     */
235
    protected function hasPermission(WP_Query $query)
236
    {
237
        global $pagenow;
238
        return is_admin()
239
            && $query->is_main_query()
240
            && glsr()->post_type == $query->get('post_type')
241
            && 'edit.php' == $pagenow;
242
    }
243
244
    /**
245
     * @param string $join
246
     * @return string
247
     */
248
    protected function modifyClauseJoin($join, $table, WP_Query $query)
249
    {
250
        global $wpdb;
251
        $join .= " INNER JOIN {$table} ON {$table}.review_id = {$wpdb->posts}.ID ";
252
        return $join;
253
    }
254
255
    /**
256
     * @param string $orderby
257
     * @return string
258
     */
259
    protected function modifyClauseOrderby($orderby, $table, WP_Query $query)
260
    {
261
        $order = $query->get('order');
262
        $orderby = $query->get('orderby');
263
        $columns = [
264
            'email' => 'email',
265
            'ip_address' => 'ip_address',
266
            'pinned' => 'is_pinned',
267
            'rating' => 'rating',
268
            'review_type' => 'type',
269
            'reviewer' => 'name',
270
        ];
271
        if (array_key_exists($orderby, $columns)) {
272
            $column = "{$table}.{$columns[$orderby]}";
273
            $orderby = "NULLIF({$column}, '') IS NULL, {$column} {$order}";
274
        }
275
        return $orderby;
276
    }
277
278
    /**
279
     * @param string $where
280
     * @return string
281
     */
282
    protected function modifyClauseWhere($where, $table, WP_Query $query)
283
    {
284
        $filters = Arr::removeEmptyValues([
285
            'rating' => filter_input(INPUT_GET, 'rating'),
286
            'type' => filter_input(INPUT_GET, 'review_type'),
287
        ]);
288
        foreach ($filters as $key => $value) {
289
            $where .= " (AND {$table}.{$key} = '{$value}') ";
290
        }
291
        return $where;
292
    }
293
}
294