Test Failed
Push — develop ( 7aea1a...9fd874 )
by Paul
15:27
created

SiteReviewsSummaryShortcode::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 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
            'rating' => [
107
                'default' => (string) Rating::min(),
108
                'group' => 'display',
109
                'label' => esc_html_x('Minimum Rating', 'admin-text', 'site-reviews'),
110
                'max' => Rating::max(),
111
                'min' => Rating::min(),
112
                'placeholder' => (string) Rating::min(),
113
                'type' => 'number',
114
            ],
115
            'rating_field' => [
116
                'description' => sprintf(_x('Use the %s addon to add custom rating fields.', 'admin-text', 'site-reviews'),
117
                    glsr_premium_link('site-reviews-forms')
118
                ),
119
                'group' => 'display',
120
                'label' => esc_html_x('Custom Rating Field Name', 'admin-text', 'site-reviews'),
121
                'type' => 'text',
122
            ],
123
            'schema' => [
124
                'description' => esc_html_x('The schema should only be enabled once on your page.', 'admin-text', 'site-reviews'),
125
                'group' => 'schema',
126
                'label' => esc_html_x('Enable the schema?', 'admin-text', 'site-reviews'),
127
                'type' => 'checkbox',
128
            ],
129
            'hide' => [
130
                'group' => 'hide',
131
                'options' => $this->options('hide'),
132
                'type' => 'checkbox',
133
            ],
134
            'text' => [
135
                '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'),
136
                'group' => 'text',
137
                'label' => _x('Summary Text', 'admin-text', 'site-reviews'),
138
                'placeholder' => _x('{rating} out of {max} stars (based on {num} reviews)', 'admin-text', 'site-reviews'),
139
                'type' => 'text',
140
            ],
141
            'labels' => [
142
                'description' => _x('Enter custom labels for the percentage bar levels (from high to low) and separate them with a comma.', 'admin-text', 'site-reviews'),
143
                'group' => 'text',
144
                'label' => _x('Summary Labels', 'admin-text', 'site-reviews'),
145
                'placeholder' => _x('Excellent, Very good, Average, Poor, Terrible', 'admin-text', 'site-reviews'),
146
                'type' => 'text',
147
            ],
148
            'id' => [
149
                'description' => esc_html_x('This should be a unique value.', 'admin-text', 'site-reviews'),
150
                'group' => 'advanced',
151
                'label' => esc_html_x('Custom ID', 'admin-text', 'site-reviews'),
152
                'type' => 'text',
153
            ],
154
            'class' => [
155
                'description' => esc_html_x('Separate multiple classes with spaces.', 'admin-text', 'site-reviews'),
156
                'group' => 'advanced',
157
                'label' => esc_html_x('Additional CSS classes', 'admin-text', 'site-reviews'),
158
                'type' => 'text',
159
            ],
160
        ];
161
    }
162
163
    protected function generateSchema(): void
164
    {
165
        if (Cast::toBool($this->args['schema'])) {
166
            glsr(Schema::class)->store(
167
                glsr(Schema::class)->buildSummary($this->args, $this->ratings)
168
            );
169
        }
170
    }
171
172 8
    protected function hideOptions(): array
173
    {
174 8
        return [
175 8
            'rating' => _x('Hide the rating', 'admin-text', 'site-reviews'),
176 8
            'stars' => _x('Hide the stars', 'admin-text', 'site-reviews'),
177 8
            'summary' => _x('Hide the summary', 'admin-text', 'site-reviews'),
178 8
            'bars' => _x('Hide the percentage bars', 'admin-text', 'site-reviews'),
179 8
            'if_empty' => _x('Hide if no reviews are found', 'admin-text', 'site-reviews'),
180 8
        ];
181
    }
182
183
    protected function isEmpty(): bool
184
    {
185
        return !array_sum($this->ratings) && in_array('if_empty', $this->args['hide']);
186
    }
187
188
    /**
189
     * @param string $value
190
     */
191
    protected function normalizeLabels($value): array
192
    {
193
        $defaults = [
194
            __('Excellent', 'site-reviews'),
195
            __('Very good', 'site-reviews'),
196
            __('Average', 'site-reviews'),
197
            __('Poor', 'site-reviews'),
198
            __('Terrible', 'site-reviews'),
199
        ];
200
        $maxRating = Rating::max();
201
        $defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, '');
202
        $labels = array_map('trim', explode(',', $value));
203
        foreach ($defaults as $i => $label) {
204
            if (!empty($labels[$i])) {
205
                $defaults[$i] = $labels[$i];
206
            }
207
        }
208
        return array_combine(range($maxRating, 1), $defaults);
209
    }
210
}
211