Test Failed
Push — develop ( 1febff...e7b0b3 )
by Paul
09:43
created

SiteReviewsSummaryShortcode::getClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
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
    public function description(): string
41
    {
42
        return esc_html_x('Display a rating summary', 'admin-text', 'site-reviews');
43
    }
44
45
    public function name(): string
46
    {
47
        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 [
64
            'assigned_posts' => [
65
                'label' => esc_html_x('Limit Reviews by Assigned Pages', 'admin-text', 'site-reviews'),
66
                'multiple' => true,
67
                'placeholder' => esc_html_x('Select a Page...', 'admin-text', 'site-reviews'),
68 8
                'type' => 'select',
69
            ],
70 8
            'assigned_terms' => [
71 8
                'label' => esc_html_x('Limit Reviews by Assigned Categories', 'admin-text', 'site-reviews'),
72 8
                'multiple' => true,
73 8
                'placeholder' => esc_html_x('Select a Category...', 'admin-text', 'site-reviews'),
74 8
                'type' => 'select',
75 8
            ],
76 8
            'assigned_users' => [
77
                'label' => esc_html_x('Limit Reviews by Assigned Users', 'admin-text', 'site-reviews'),
78
                'multiple' => true,
79
                'placeholder' => esc_html_x('Select a User...', 'admin-text', 'site-reviews'),
80
                'type' => 'select',
81
            ],
82
            'terms' => [
83
                'label' => esc_html_x('Limit Reviews by terms accepted', 'admin-text', 'site-reviews'),
84
                'options' => $this->options('terms'),
85
                'placeholder' => esc_html_x('Select Review Terms...', 'admin-text', 'site-reviews'),
86
                'type' => 'select',
87
            ],
88
            'type' => [
89
                'label' => esc_html_x('Limit Reviews by Type', 'admin-text', 'site-reviews'),
90
                'options' => $this->options('type'),
91
                'placeholder' => esc_html_x('Select a Review Type...', 'admin-text', 'site-reviews'),
92
                'type' => 'select',
93
            ],
94
            'rating' => [
95
                'default' => (string) Rating::min(),
96
                'group' => 'display',
97
                'label' => esc_html_x('Minimum Rating', 'admin-text', 'site-reviews'),
98
                'max' => Rating::max(),
99
                'min' => Rating::min(),
100
                'placeholder' => (string) Rating::min(),
101
                'type' => 'number',
102
            ],
103
            'rating_field' => [
104
                'description' => sprintf(_x('Use the %sReview Forms%s addon to add custom rating fields.', 'admin-text', 'site-reviews'),
105
                    '<a href="https://niftyplugins.com/plugins/site-reviews-forms/" target="_blank">', '</a>'
106
                ),
107
                'label' => esc_html_x('Custom Rating Field Name', 'admin-text', 'site-reviews'),
108
                'group' => 'display',
109
                'type' => 'text',
110
            ],
111
            'schema' => [
112
                'description' => esc_html_x('The schema should only be enabled once on your page.', 'admin-text', 'site-reviews'),
113
                'group' => 'schema',
114
                'label' => esc_html_x('Enable the schema?', 'admin-text', 'site-reviews'),
115
                'type' => 'checkbox',
116
            ],
117
            'hide' => [
118
                'group' => 'hide',
119
                'options' => $this->options('hide'),
120
                'type' => 'checkbox',
121
            ],
122
            'id' => [
123
                'description' => esc_html_x('This should be a unique value.', 'admin-text', 'site-reviews'),
124
                'group' => 'advanced',
125
                'label' => esc_html_x('Custom ID', 'admin-text', 'site-reviews'),
126
                'type' => 'text',
127
            ],
128
            'class' => [
129
                'description' => esc_html_x('Separate multiple classes with spaces.', 'admin-text', 'site-reviews'),
130
                'group' => 'advanced',
131
                'label' => esc_html_x('Additional CSS classes', 'admin-text', 'site-reviews'),
132
                'type' => 'text',
133
            ],
134
        ];
135
    }
136
137
    protected function generateSchema(): void
138
    {
139
        if (Cast::toBool($this->args['schema'])) {
140
            glsr(Schema::class)->store(
141
                glsr(Schema::class)->buildSummary($this->args, $this->ratings)
142
            );
143
        }
144
    }
145
146
    protected function hideOptions(): array
147
    {
148
        return [
149
            'rating' => _x('Hide the rating', 'admin-text', 'site-reviews'),
150
            'stars' => _x('Hide the stars', 'admin-text', 'site-reviews'),
151
            'summary' => _x('Hide the summary', 'admin-text', 'site-reviews'),
152
            'bars' => _x('Hide the percentage bars', 'admin-text', 'site-reviews'),
153
            'if_empty' => _x('Hide if no reviews are found', 'admin-text', 'site-reviews'),
154
        ];
155
    }
156
157
    protected function isEmpty(): bool
158
    {
159
        return !array_sum($this->ratings) && in_array('if_empty', $this->args['hide']);
160
    }
161
}
162