Passed
Push — master ( 2009cc...a406f9 )
by Paul
04:50 queued 21s
created

Columns   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 61
dl 0
loc 172
ccs 0
cts 109
cp 0
rs 10
c 1
b 0
f 0
wmc 25

12 Methods

Rating   Name   Duplication   Size   Complexity  
A buildColumnAssignedTo() 0 6 3
A buildColumnEmail() 0 4 2
A buildColumnReviewType() 0 6 2
A renderFilterRatings() 0 18 2
A buildColumnReviewer() 0 3 1
A renderFilterTypes() 0 13 2
A buildColumnPinned() 0 8 2
A renderValues() 0 10 3
A buildColumnRating() 0 3 1
A buildColumnResponse() 0 5 2
A renderFilters() 0 12 3
A buildColumnIpAddress() 0 4 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers\ListTableController;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Modules\Html\Builder;
9
use WP_Post;
10
11
class Columns
12
{
13
    /**
14
     * @param int $postId
15
     * @return void|string
16
     */
17
    public function buildColumnAssignedTo($postId)
18
    {
19
        $assignedPost = glsr(Database::class)->getAssignedToPost($postId);
20
        if ($assignedPost instanceof WP_Post && 'publish' == $assignedPost->post_status) {
21
            return glsr(Builder::class)->a(get_the_title($assignedPost->ID), [
22
                'href' => (string) get_the_permalink($assignedPost->ID),
23
            ]);
24
        }
25
    }
26
27
    /**
28
     * @param int $postId
29
     * @return void|string
30
     */
31
    public function buildColumnEmail($postId)
32
    {
33
        if ($email = glsr(Database::class)->get($postId, 'email')) {
34
            return $email;
35
        }
36
    }
37
38
    /**
39
     * @param int $postId
40
     * @return void|string
41
     */
42
    public function buildColumnIpAddress($postId)
43
    {
44
        if ($ipAddress = glsr(Database::class)->get($postId, 'ip_address')) {
45
            return $ipAddress;
46
        }
47
    }
48
49
    /**
50
     * @param int $postId
51
     * @return string
52
     */
53
    public function buildColumnPinned($postId)
54
    {
55
        $pinned = glsr(Database::class)->get($postId, 'pinned')
56
            ? 'pinned '
57
            : '';
58
        return glsr(Builder::class)->i([
59
            'class' => $pinned.'dashicons dashicons-sticky',
60
            'data-id' => $postId,
61
        ]);
62
    }
63
64
    /**
65
     * @param int $postId
66
     * @return string
67
     */
68
    public function buildColumnResponse($postId)
69
    {
70
        return glsr(Database::class)->get($postId, 'response')
71
            ? __('Yes', 'site-reviews')
72
            : __('No', 'site-reviews');
73
    }
74
75
    /**
76
     * @param int $postId
77
     * @return string
78
     */
79
    public function buildColumnReviewer($postId)
80
    {
81
        return strval(glsr(Database::class)->get($postId, 'author'));
82
    }
83
84
    /**
85
     * @param int $postId
86
     * @param int|null $rating
87
     * @return string
88
     */
89
    public function buildColumnRating($postId)
90
    {
91
        return glsr_star_rating(intval(glsr(Database::class)->get($postId, 'rating')));
92
    }
93
94
    /**
95
     * @param int $postId
96
     * @return string
97
     */
98
    public function buildColumnReviewType($postId)
99
    {
100
        $type = glsr(Database::class)->get($postId, 'review_type');
101
        return array_key_exists($type, glsr()->reviewTypes)
102
            ? glsr()->reviewTypes[$type]
103
            : __('Unsupported Type', 'site-reviews');
104
    }
105
106
    /**
107
     * @param string $postType
108
     * @return void
109
     */
110
    public function renderFilters($postType)
111
    {
112
        if (Application::POST_TYPE !== $postType) {
113
            return;
114
        }
115
        if (!($status = filter_input(INPUT_GET, 'post_status'))) {
116
            $status = 'publish';
117
        }
118
        $ratings = glsr(Database::class)->getReviewsMeta('rating', $status);
119
        $types = glsr(Database::class)->getReviewsMeta('review_type', $status);
120
        $this->renderFilterRatings($ratings);
121
        $this->renderFilterTypes($types);
122
    }
123
124
    /**
125
     * @param string $column
126
     * @param int $postId
127
     * @return void
128
     */
129
    public function renderValues($column, $postId)
130
    {
131
        $method = Helper::buildMethodName($column, 'buildColumn');
132
        $value = method_exists($this, $method)
133
            ? call_user_func([$this, $method], $postId)
134
            : apply_filters('site-reviews/columns/'.$column, '', $postId);
135
        if (empty($value)) {
136
            $value = '&mdash;';
137
        }
138
        echo $value;
139
    }
140
141
    /**
142
     * @param array $ratings
143
     * @return void
144
     */
145
    protected function renderFilterRatings($ratings)
146
    {
147
        if (empty($ratings)) {
148
            return;
149
        }
150
        $ratings = array_flip(array_reverse($ratings));
151
        array_walk($ratings, function (&$value, $key) {
152
            $label = _n('%s star', '%s stars', $key, 'site-reviews');
153
            $value = sprintf($label, $key);
154
        });
155
        echo glsr(Builder::class)->label(__('Filter by rating', 'site-reviews'), [
156
            'class' => 'screen-reader-text',
157
            'for' => 'rating',
158
        ]);
159
        echo glsr(Builder::class)->select([
160
            'name' => 'rating',
161
            'options' => ['' => __('All ratings', 'site-reviews')] + $ratings,
162
            'value' => filter_input(INPUT_GET, 'rating'),
163
        ]);
164
    }
165
166
    /**
167
     * @param array $types
168
     * @return void
169
     */
170
    protected function renderFilterTypes($types)
171
    {
172
        if (count(glsr()->reviewTypes) < 2) {
173
            return;
174
        }
175
        echo glsr(Builder::class)->label(__('Filter by type', 'site-reviews'), [
176
            'class' => 'screen-reader-text',
177
            'for' => 'review_type',
178
        ]);
179
        echo glsr(Builder::class)->select([
180
            'name' => 'review_type',
181
            'options' => ['' => __('All types', 'site-reviews')] + glsr()->reviewTypes,
182
            'value' => filter_input(INPUT_GET, 'review_type'),
183
        ]);
184
    }
185
}
186