Passed
Push — master ( f510e9...9822a2 )
by Paul
10:29
created

Shortcode::debug()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Shortcodes;
4
5
use GeminiLabs\SiteReviews\Arguments;
6
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Helpers\Cast;
9
use GeminiLabs\SiteReviews\Helpers\Str;
10
use GeminiLabs\SiteReviews\Modules\Html\Builder;
11
use GeminiLabs\SiteReviews\Modules\Multilingual;
12
use GeminiLabs\SiteReviews\Modules\Rating;
13
use GeminiLabs\SiteReviews\Modules\Sanitizer;
14
use GeminiLabs\SiteReviews\Modules\Style;
15
use ReflectionClass;
16
17
abstract class Shortcode implements ShortcodeContract
18
{
19
    /**
20
     * @var array
21
     */
22
    public $args;
23
24
    /**
25
     * @var array
26
     */
27
    public $dataAttributes;
28
29
    /**
30
     * @var string
31
     */
32
    public $debug;
33
34
    /**
35
     * @var string
36
     */
37
    public $shortcode;
38
39
    /**
40
     * @var string
41
     */
42
    public $type;
43
44 7
    public function __construct()
45
    {
46 7
        $this->shortcode = Str::snakeCase($this->getShortClassName());
47 7
    }
48
49
    public function __get($parameter)
50
    {
51
        // @compat provides backwards compatibility for unsupported add-ons
52
    }
53
54
    /**
55
     * @param string|array $atts
56
     * @param string $type
57
     * @return string
58
     */
59
    public function build($atts, array $args = [], $type = 'shortcode')
60
    {
61
        $this->type = $type;
62
        $args = $this->normalizeArgs($args, $type);
63
        $atts = $this->normalizeAtts($atts, $type);
64
        $template = $this->buildTemplate($atts->toArray());
65
        if (!empty($atts->title)) {
66
            $title = $args->before_title.$atts->title.$args->after_title;
67
            $atts->title = $title;
68
        }
69
        $attributes = wp_parse_args($this->dataAttributes, [
70
            'class' => glsr(Style::class)->styleClasses(),
71
            'id' => $atts->id,
72
            'text' => $template,
73
        ]);
74
        $attributes = glsr()->filterArray('shortcode/'.$this->shortcode.'/attributes', $attributes, $this);
75
        $html = glsr(Builder::class)->div($attributes);
76
        return sprintf('%s%s%s%s%s',
77
            $args->before_widget,
78
            $atts->title,
79
            $this->debug,
80
            $html,
81
            $args->after_widget
82
        );
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function buildBlock($atts = [])
89
    {
90
        return $this->build($atts, [], 'block');
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function buildShortcode($atts = [])
97
    {
98
        return $this->build($atts, [], 'shortcode');
99
    }
100
101
    /**
102
     * @return string|void
103
     */
104
    public function buildTemplate(array $args = [])
105
    {
106
        return; // @todo make this abstract in v6.0
107
    }
108
109
    /**
110
     * @return array
111
     */
112 7
    public function getHideOptions()
113
    {
114 7
        $options = $this->hideOptions();
115 7
        return glsr()->filterArray('shortcode/hide-options', $options, $this->shortcode, $this);
116
    }
117
118
    /**
119
     * @return string
120
     */
121 7
    public function getShortClassName($replace = '', $search = 'Shortcode')
122
    {
123 7
        return str_replace($search, $replace, (new ReflectionClass($this))->getShortName());
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getShortcodeDefaultsClassName()
130
    {
131
        $classname = str_replace('Shortcodes\\', 'Defaults\\', get_class($this));
132
        return str_replace('Shortcode', 'Defaults', $classname);
133
    }
134
135
    /**
136
     * @param array|string $args
137
     * @param string $type
138
     * @return Arguments
139
     */
140
    public function normalizeArgs($args, $type = 'shortcode')
141
    {
142
        $args = wp_parse_args($args, [
143
            'before_widget' => '',
144
            'after_widget' => '',
145
            'before_title' => '<h2 class="glsr-title">',
146
            'after_title' => '</h2>',
147
        ]);
148
        $args = glsr()->filterArray('shortcode/args', $args, $type, $this->shortcode);
149
        return glsr()->args($args);
150
    }
151
152
    /**
153
     * @param array|string $atts
154
     * @param string $type
155
     * @return Arguments
156
     */
157
    public function normalizeAtts($atts, $type = 'shortcode')
158
    {
159
        $atts = wp_parse_args($atts);
160
        $atts = glsr()->filterArray('shortcode/atts', $atts, $type, $this->shortcode);
161
        $atts = glsr($this->getShortcodeDefaultsClassName())->unguardedRestrict($atts);
162
        $atts = glsr()->args($atts);
163
        foreach ($atts as $key => &$value) {
164
            $method = Helper::buildMethodName($key, 'normalize');
165
            if (method_exists($this, $method)) {
166
                $value = call_user_func([$this, $method], $value, $atts);
167
            }
168
        }
169
        $this->setDataAttributes($atts, $type);
170
        return $atts;
171
    }
172
173
    /**
174
     * @return void
175
     */
176
    protected function debug(array $data = [])
177
    {
178
        if (empty($this->args['debug']) || 'shortcode' !== $this->type) {
179
            return;
180
        }
181
        $data = wp_parse_args($data, [
182
            'args' => $this->args,
183
            'shortcode' => $this->shortcode,
184
        ]);
185
        ksort($data);
186
        ob_start();
187
        glsr_debug($data);
188
        $this->debug = ob_get_clean();
189
    }
190
191
    /**
192
     * @return array
193
     */
194
    abstract protected function hideOptions();
195
196
    /**
197
     * @param string $postIds
198
     * @return string
199
     */
200
    protected function normalizeAssignedPosts($postIds, Arguments $atts)
201
    {
202
        $postIds = glsr(Sanitizer::class)->sanitizePostIds($postIds);
203
        $postIds = glsr(Multilingual::class)->getPostIds($postIds);
204
        return implode(',', $postIds);
205
    }
206
207
    /**
208
     * @param string $termIds
209
     * @return string
210
     */
211
    protected function normalizeAssignedTerms($termIds)
212
    {
213
        return implode(',', glsr(Sanitizer::class)->sanitizeTermIds($termIds));
214
    }
215
216
    /**
217
     * @param string $userIds
218
     * @return string
219
     */
220
    protected function normalizeAssignedUsers($userIds)
221
    {
222
        return implode(',', glsr(Sanitizer::class)->sanitizeUserIds($userIds));
223
    }
224
225
    /**
226
     * @param string|array $hide
227
     * @return array
228
     */
229
    protected function normalizeHide($hide)
230
    {
231
        $hideKeys = array_keys($this->getHideOptions());
232
        return array_filter(Cast::toArray($hide), function ($value) use ($hideKeys) {
233
            return in_array($value, $hideKeys);
234
        });
235
    }
236
237
    /**
238
     * @param string $labels
239
     * @return array
240
     */
241
    protected function normalizeLabels($labels)
242
    {
243
        $defaults = [
244
            __('Excellent', 'site-reviews'),
245
            __('Very good', 'site-reviews'),
246
            __('Average', 'site-reviews'),
247
            __('Poor', 'site-reviews'),
248
            __('Terrible', 'site-reviews'),
249
        ];
250
        $maxRating = (int) glsr()->constant('MAX_RATING', Rating::class);
251
        $defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, '');
252
        $labels = array_map('trim', explode(',', $labels));
253
        foreach ($defaults as $i => $label) {
254
            if (!empty($labels[$i])) {
255
                $defaults[$i] = $labels[$i];
256
            }
257
        }
258
        return array_combine(range($maxRating, 1), $defaults);
259
    }
260
261
    /**
262
     * @param string $type
263
     * @return void
264
     */
265
    protected function setDataAttributes(Arguments $atts, $type)
266
    {
267
        $this->dataAttributes = wp_parse_args(
268
            glsr($this->getShortcodeDefaultsClassName())->dataAttributes($atts->toArray()),
269
            ["data-{$type}" => '']
270
        );
271
    }
272
}
273