Passed
Push — develop ( f6f5f6...710bfb )
by Paul
14:13
created

SiteReviewsSummaryShortcode::enqueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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
    public function enqueue(): void
46
    {
47
        wp_enqueue_style('site-reviews-summary-style');
48
    }
49
50 8
    public function name(): string
51
    {
52 8
        return esc_html_x('Rating Summary', 'admin-text', 'site-reviews');
53
    }
54
55
    protected function buildTemplateTag(string $tag): string
56
    {
57
        $args = $this->args;
58
        $className = Helper::buildClassName(['summary', $tag, 'tag'], 'Modules\Html\Tags');
59
        $className = glsr()->filterString("summary/tag/{$tag}", $className, $this);
60
        $field = class_exists($className)
61
            ? glsr($className, compact('tag', 'args'))->handleFor('summary', null, $this->ratings)
62
            : '';
63
        return glsr()->filterString("summary/build/{$tag}", $field, $this->ratings, $this);
64
    }
65
66
    protected function config(): array
67
    {
68
        return [ // order is intentional
69
            'assigned_posts' => [
70
                'group' => 'general',
71
                'label' => esc_html_x('Limit Reviews by Assigned Pages', 'admin-text', 'site-reviews'),
72
                'multiple' => true,
73
                'placeholder' => esc_html_x('Select a Page...', 'admin-text', 'site-reviews'),
74
                'type' => 'select',
75
            ],
76
            'assigned_terms' => [
77
                'group' => 'general',
78
                'label' => esc_html_x('Limit Reviews by Assigned Categories', 'admin-text', 'site-reviews'),
79
                'multiple' => true,
80
                'placeholder' => esc_html_x('Select a Category...', 'admin-text', 'site-reviews'),
81
                'type' => 'select',
82
            ],
83
            'assigned_users' => [
84
                'group' => 'general',
85
                'label' => esc_html_x('Limit Reviews by Assigned Users', 'admin-text', 'site-reviews'),
86
                'multiple' => true,
87
                'placeholder' => esc_html_x('Select a User...', 'admin-text', 'site-reviews'),
88
                'type' => 'select',
89
            ],
90
            'author' => [
91
                'group' => 'general',
92
                'label' => esc_html_x('Limit Reviews by Review Author', 'admin-text', 'site-reviews'),
93
                'multiple' => false,
94
                'placeholder' => esc_html_x('Select a User...', 'admin-text', 'site-reviews'),
95
                'type' => 'select',
96
            ],
97
            'terms' => [
98
                'group' => 'general',
99
                'label' => esc_html_x('Limit Reviews by Accepted Terms', 'admin-text', 'site-reviews'),
100
                'options' => $this->options('terms'),
101
                'placeholder' => esc_html_x('Select Review Terms...', 'admin-text', 'site-reviews'),
102
                'type' => 'select',
103
            ],
104
            'type' => [
105
                'group' => 'general',
106
                'label' => esc_html_x('Limit Reviews by Type', 'admin-text', 'site-reviews'),
107
                'options' => $this->options('type'),
108
                'placeholder' => esc_html_x('Select a Review Type...', 'admin-text', 'site-reviews'),
109
                'type' => 'select',
110
            ],
111
            'verified' => [
112
                'group' => 'general',
113
                'label' => esc_html_x('Limit Reviews by Verified Status', 'admin-text', 'site-reviews'),
114
                'options' => $this->options('verified'),
115
                'placeholder' => esc_html_x('Select Verified Status...', 'admin-text', 'site-reviews'),
116
                'type' => 'select',
117
            ],
118
            'rating' => [
119
                'default' => (string) Rating::min(),
120
                'group' => 'display',
121
                'label' => esc_html_x('Minimum Rating', 'admin-text', 'site-reviews'),
122
                'max' => Rating::max(),
123
                'min' => Rating::min(),
124
                'placeholder' => (string) Rating::min(),
125
                'type' => 'number',
126
            ],
127
            'rating_field' => [
128
                'description' => sprintf(_x('Use the %s addon to add custom rating fields.', 'admin-text', 'site-reviews'),
129
                    glsr_premium_link('site-reviews-forms')
130
                ),
131
                'group' => 'display',
132
                'label' => esc_html_x('Custom Rating Field Name', 'admin-text', 'site-reviews'),
133
                'type' => 'text',
134
            ],
135
            'schema' => [
136
                'description' => esc_html_x('The schema should only be enabled once on your page.', 'admin-text', 'site-reviews'),
137
                'group' => 'schema',
138
                'label' => esc_html_x('Enable the schema?', 'admin-text', 'site-reviews'),
139
                'type' => 'checkbox',
140
            ],
141
            'hide' => [
142
                'group' => 'hide',
143
                'options' => $this->options('hide'),
144
                'type' => 'checkbox',
145
            ],
146
            'text' => [
147
                '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'),
148
                'group' => 'text',
149
                'label' => _x('Summary Text', 'admin-text', 'site-reviews'),
150
                'placeholder' => _x('{rating} out of {max} stars (based on {num} reviews)', 'admin-text', 'site-reviews'),
151
                'type' => 'text',
152
            ],
153
            'labels' => [
154
                'description' => _x('Enter custom labels for the percentage bar levels (from high to low) and separate them with a comma.', 'admin-text', 'site-reviews'),
155
                'group' => 'text',
156
                'label' => _x('Summary Labels', 'admin-text', 'site-reviews'),
157
                'placeholder' => implode(', ', Rating::labels()),
158
                'type' => 'text',
159
            ],
160
            'id' => [
161
                'description' => esc_html_x('This should be a unique value.', 'admin-text', 'site-reviews'),
162
                'group' => 'advanced',
163
                'label' => esc_html_x('Custom ID', 'admin-text', 'site-reviews'),
164
                'type' => 'text',
165
            ],
166
            'class' => [
167
                'description' => esc_html_x('Separate multiple classes with spaces.', 'admin-text', 'site-reviews'),
168
                'group' => 'advanced',
169
                'label' => esc_html_x('Additional CSS classes', 'admin-text', 'site-reviews'),
170
                'type' => 'text',
171
            ],
172
        ];
173
    }
174
175
    protected function generateSchema(): void
176
    {
177
        if (Cast::toBool($this->args['schema'])) {
178
            glsr(Schema::class)->store(
179
                glsr(Schema::class)->buildSummary($this->args, $this->ratings)
180
            );
181
        }
182
    }
183
184 8
    protected function hideOptions(): array
185
    {
186 8
        return [
187 8
            'rating' => _x('Hide the rating', 'admin-text', 'site-reviews'),
188 8
            'stars' => _x('Hide the stars', 'admin-text', 'site-reviews'),
189 8
            'summary' => _x('Hide the summary', 'admin-text', 'site-reviews'),
190 8
            'bars' => _x('Hide the percentage bars', 'admin-text', 'site-reviews'),
191 8
            'if_empty' => _x('Hide if no reviews are found', 'admin-text', 'site-reviews'),
192 8
        ];
193
    }
194
195
    protected function isEmpty(): bool
196
    {
197
        return !array_sum($this->ratings) && in_array('if_empty', $this->args['hide']);
198
    }
199
200
    protected function normalizeLabels(string $value): array
201
    {
202
        $labels = Rating::labels(); // indexed in DESC order
203
        $customLabels  = array_map('trim', explode(',', $value));
204
        foreach ($labels as $index => $label) {
205
            if (!empty($customLabels[$index])) { // don't allow 0
206
                $labels[$index] = $customLabels[$index];
207
            }
208
        }
209
        return $labels;
210
    }
211
}
212