Passed
Push — develop ( afff43...70b7ee )
by Paul
14:29
created

SiteReviewsSummaryShortcode   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Test Coverage

Coverage 7.64%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 15
eloc 129
dl 0
loc 193
ccs 12
cts 157
cp 0.0764
rs 10
c 5
b 1
f 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A buildTemplate() 0 17 2
A buildTemplateTag() 0 9 2
A description() 0 3 1
A name() 0 3 1
A generateSchema() 0 5 2
B config() 0 105 1
A normalizeLabels() 0 10 3
A isEmpty() 0 3 2
A hideOptions() 0 8 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Shortcodes;
4
5
use GeminiLabs\SiteReviews\Database\RatingManager;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Modules\Rating;
10
use GeminiLabs\SiteReviews\Modules\Schema;
11
12
class SiteReviewsSummaryShortcode extends Shortcode
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $ratings;
18
19
    public function buildTemplate(): string
20
    {
21
        $this->ratings = glsr(RatingManager::class)->ratings($this->args);
22
        $this->debug(['ratings' => $this->ratings]);
23
        if ($this->isEmpty()) {
24
            return glsr()->filterString('summary/if_empty', '');
25
        }
26
        $this->generateSchema();
27
        return glsr(Template::class)->build('templates/reviews-summary', [
28
            'args' => $this->args,
29
            'context' => [
30
                'class' => 'glsr-summary',
31
                'id' => '', // @deprecated_5
32
                'percentages' => $this->buildTemplateTag('percentages'),
33
                'rating' => $this->buildTemplateTag('rating'),
34
                'stars' => $this->buildTemplateTag('stars'),
35
                'text' => $this->buildTemplateTag('text'),
36
            ],
37
        ]);
38
    }
39
40 8
    public function description(): string
41
    {
42 8
        return esc_html_x('Display a rating summary', 'admin-text', 'site-reviews');
43
    }
44
45 8
    public function name(): string
46
    {
47 8
        return esc_html_x('Rating Summary', 'admin-text', 'site-reviews');
48
    }
49
50
    protected function buildTemplateTag(string $tag): string
51
    {
52
        $args = $this->args;
53
        $className = Helper::buildClassName(['summary', $tag, 'tag'], 'Modules\Html\Tags');
54
        $className = glsr()->filterString("summary/tag/{$tag}", $className, $this);
55
        $field = class_exists($className)
56
            ? glsr($className, compact('tag', 'args'))->handleFor('summary', null, $this->ratings)
57
            : '';
58
        return glsr()->filterString("summary/build/{$tag}", $field, $this->ratings, $this);
59
    }
60
61
    protected function config(): array
62
    {
63
        return [ // order is intentional
64
            'assigned_posts' => [
65
                'group' => 'general',
66
                'label' => esc_html_x('Limit Reviews by Assigned Pages', 'admin-text', 'site-reviews'),
67
                'multiple' => true,
68
                'placeholder' => esc_html_x('Select a Page...', 'admin-text', 'site-reviews'),
69
                'type' => 'select',
70
            ],
71
            'assigned_terms' => [
72
                'group' => 'general',
73
                'label' => esc_html_x('Limit Reviews by Assigned Categories', 'admin-text', 'site-reviews'),
74
                'multiple' => true,
75
                'placeholder' => esc_html_x('Select a Category...', 'admin-text', 'site-reviews'),
76
                'type' => 'select',
77
            ],
78
            'assigned_users' => [
79
                'group' => 'general',
80
                'label' => esc_html_x('Limit Reviews by Assigned Users', 'admin-text', 'site-reviews'),
81
                'multiple' => true,
82
                'placeholder' => esc_html_x('Select a User...', 'admin-text', 'site-reviews'),
83
                'type' => 'select',
84
            ],
85
            'author' => [
86
                'group' => 'general',
87
                'label' => esc_html_x('Limit Reviews by Review Author', 'admin-text', 'site-reviews'),
88
                'multiple' => false,
89
                'placeholder' => esc_html_x('Select a User...', 'admin-text', 'site-reviews'),
90
                'type' => 'select',
91
            ],
92
            'terms' => [
93
                'group' => 'general',
94
                'label' => esc_html_x('Limit Reviews by Accepted Terms', 'admin-text', 'site-reviews'),
95
                'options' => $this->options('terms'),
96
                'placeholder' => esc_html_x('Select Review Terms...', 'admin-text', 'site-reviews'),
97
                'type' => 'select',
98
            ],
99
            'type' => [
100
                'group' => 'general',
101
                'label' => esc_html_x('Limit Reviews by Type', 'admin-text', 'site-reviews'),
102
                'options' => $this->options('type'),
103
                'placeholder' => esc_html_x('Select a Review Type...', 'admin-text', 'site-reviews'),
104
                'type' => 'select',
105
            ],
106
            'verified' => [
107
                'group' => 'general',
108
                'label' => esc_html_x('Limit Reviews by Verified Status', 'admin-text', 'site-reviews'),
109
                'options' => $this->options('verified'),
110
                'placeholder' => esc_html_x('Select Verified Status...', 'admin-text', 'site-reviews'),
111
                'type' => 'select',
112
            ],
113
            'rating' => [
114
                'default' => (string) Rating::min(),
115
                'group' => 'display',
116
                'label' => esc_html_x('Minimum Rating', 'admin-text', 'site-reviews'),
117
                'max' => Rating::max(),
118
                'min' => Rating::min(),
119
                'placeholder' => (string) Rating::min(),
120
                'type' => 'number',
121
            ],
122
            'rating_field' => [
123
                'description' => sprintf(_x('Use the %s addon to add custom rating fields.', 'admin-text', 'site-reviews'),
124
                    glsr_premium_link('site-reviews-forms')
125
                ),
126
                'group' => 'display',
127
                'label' => esc_html_x('Custom Rating Field Name', 'admin-text', 'site-reviews'),
128
                'type' => 'text',
129
            ],
130
            'schema' => [
131
                'description' => esc_html_x('The schema should only be enabled once on your page.', 'admin-text', 'site-reviews'),
132
                'group' => 'schema',
133
                'label' => esc_html_x('Enable the schema?', 'admin-text', 'site-reviews'),
134
                'type' => 'checkbox',
135
            ],
136
            'hide' => [
137
                'group' => 'hide',
138
                'options' => $this->options('hide'),
139
                'type' => 'checkbox',
140
            ],
141
            'text' => [
142
                'description' => _x('Use {num} to display the number of reviews, {rating} to display the average rating, and {max} to display the maximum rating value.', 'admin-text', 'site-reviews'),
143
                'group' => 'text',
144
                'label' => _x('Summary Text', 'admin-text', 'site-reviews'),
145
                'placeholder' => _x('{rating} out of {max} stars (based on {num} reviews)', 'admin-text', 'site-reviews'),
146
                'type' => 'text',
147
            ],
148
            'labels' => [
149
                'description' => _x('Enter custom labels for the percentage bar levels (from high to low) and separate them with a comma.', 'admin-text', 'site-reviews'),
150
                'group' => 'text',
151
                'label' => _x('Summary Labels', 'admin-text', 'site-reviews'),
152
                'placeholder' => implode(', ', Rating::labels()),
153
                'type' => 'text',
154
            ],
155
            'id' => [
156
                'description' => esc_html_x('This should be a unique value.', 'admin-text', 'site-reviews'),
157
                'group' => 'advanced',
158
                'label' => esc_html_x('Custom ID', 'admin-text', 'site-reviews'),
159
                'type' => 'text',
160
            ],
161
            'class' => [
162
                'description' => esc_html_x('Separate multiple classes with spaces.', 'admin-text', 'site-reviews'),
163
                'group' => 'advanced',
164
                'label' => esc_html_x('Additional CSS classes', 'admin-text', 'site-reviews'),
165
                'type' => 'text',
166
            ],
167
        ];
168
    }
169
170
    protected function generateSchema(): void
171
    {
172
        if (Cast::toBool($this->args['schema'])) {
173
            glsr(Schema::class)->store(
174
                glsr(Schema::class)->buildSummary($this->args, $this->ratings)
175
            );
176
        }
177
    }
178
179 8
    protected function hideOptions(): array
180
    {
181 8
        return [
182 8
            'rating' => _x('Hide the rating', 'admin-text', 'site-reviews'),
183 8
            'stars' => _x('Hide the stars', 'admin-text', 'site-reviews'),
184 8
            'summary' => _x('Hide the summary', 'admin-text', 'site-reviews'),
185 8
            'bars' => _x('Hide the percentage bars', 'admin-text', 'site-reviews'),
186 8
            'if_empty' => _x('Hide if no reviews are found', 'admin-text', 'site-reviews'),
187 8
        ];
188
    }
189
190
    protected function isEmpty(): bool
191
    {
192
        return !array_sum($this->ratings) && in_array('if_empty', $this->args['hide']);
193
    }
194
195
    protected function normalizeLabels(string $value): array
196
    {
197
        $labels = Rating::labels(); // indexed in DESC order
198
        $customLabels  = array_map('trim', explode(',', $value));
199
        foreach ($labels as $index => $label) {
200
            if (!empty($customLabels[$index])) { // don't allow 0
201
                $labels[$index] = $customLabels[$index];
202
            }
203
        }
204
        return $labels;
205
    }
206
}
207