Passed
Push — master ( ccb079...7906b4 )
by Paul
04:39
created

Shortcode::buildBlock()   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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Shortcodes;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Helpers\Str;
9
use GeminiLabs\SiteReviews\Modules\Html\Builder;
10
use GeminiLabs\SiteReviews\Modules\Html\Partial;
11
use GeminiLabs\SiteReviews\Modules\Rating;
12
use GeminiLabs\SiteReviews\Modules\Style;
13
use ReflectionClass;
14
15
abstract class Shortcode implements ShortcodeContract
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $partialName;
21
22
    /**
23
     * @var string
24
     */
25
    protected $shortcodeName;
26
27 1
    public function __construct()
28
    {
29 1
        $this->partialName = $this->getShortcodePartialName();
30 1
        $this->shortcodeName = $this->getShortcodeName();
31 1
    }
32
33
    /**
34
     * @param string|array $atts
35
     * @param string $type
36
     * @return string
37
     */
38
    public function build($atts, array $args = [], $type = 'shortcode')
39
    {
40
        $args = $this->normalizeArgs($args, $type);
41
        $atts = $this->normalizeAtts($atts, $type);
42
        $partial = glsr(Partial::class)->build($this->partialName, $atts);
43
        if (!empty($atts['title'])) {
44
            $atts = Arr::set($atts, 'title', $args['before_title'].$atts['title'].$args['after_title']);
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Shortcodes\Arr was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
        }
46
        $html = glsr(Builder::class)->div((string) $partial, [
47
            'class' => 'glsr glsr-'.glsr(Style::class)->get(),
48
            'data-shortcode' => Str::snakeCase($this->partialName),
49
            'data-type' => $type,
50
            'data-atts' => $atts['json'], // I prefer to have this attribute displayed last
51
        ]);
52
        return $args['before_widget'].$atts['title'].$html.$args['after_widget'];
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function buildBlock($atts = [])
59
    {
60
        return $this->build($atts, [], 'block');
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function buildShortcode($atts = [])
67
    {
68
        return $this->build($atts, [], 'shortcode');
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function getDefaults($atts)
75
    {
76
        return glsr($this->getShortcodeDefaultsClassName())->restrict(wp_parse_args($atts));
77
    }
78
79
    /**
80
     * @return array
81
     */
82 1
    public function getHideOptions()
83
    {
84 1
        $options = $this->hideOptions();
85 1
        return apply_filters('site-reviews/shortcode/hide-options', $options, $this->shortcodeName);
86
    }
87
88
    /**
89
     * @return string
90
     */
91 1
    public function getShortClassName($replace = '', $search = 'Shortcode')
92
    {
93 1
        return str_replace($search, $replace, (new ReflectionClass($this))->getShortName());
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getShortcodeDefaultsClassName()
100
    {
101
        $className = Str::replaceLast('Shortcode', 'Defaults', get_class($this));
102
        return str_replace('Shortcodes', 'Defaults', $className);
103
    }
104
105
    /**
106
     * @return string
107
     */
108 1
    public function getShortcodeName()
109
    {
110 1
        return Str::snakeCase($this->getShortClassName());
111
    }
112
113
    /**
114
     * @return string
115
     */
116 1
    public function getShortcodePartialName()
117
    {
118 1
        return Str::dashCase($this->getShortClassName());
119
    }
120
121
    /**
122
     * @param array|string $args
123
     * @param string $type
124
     * @return array
125
     */
126
    public function normalizeArgs($args, $type = 'shortcode')
127
    {
128
        $args = wp_parse_args($args, [
129
            'before_widget' => '',
130
            'after_widget' => '',
131
            'before_title' => '<h2 class="glsr-title">',
132
            'after_title' => '</h2>',
133
        ]);
134
        return apply_filters('site-reviews/shortcode/args', $args, $type, $this->partialName);
135
    }
136
137
    /**
138
     * @param array|string $atts
139
     * @param string $type
140
     * @return array
141
     */
142
    public function normalizeAtts($atts, $type = 'shortcode')
143
    {
144
        $atts = apply_filters('site-reviews/shortcode/atts', $atts, $type, $this->partialName);
145
        $atts = $this->getDefaults($atts);
146
        array_walk($atts, function (&$value, $key) {
147
            $methodName = Helper::buildMethodName($key, 'normalize');
148
            if (!method_exists($this, $methodName)) {
149
                return;
150
            }
151
            $value = $this->$methodName($value);
152
        });
153
        $this->setId($atts);
154
        return $atts;
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    abstract protected function hideOptions();
161
162
    /**
163
     * @param string $postId
164
     * @return int|string
165
     */
166
    protected function normalizeAssignedTo($postId)
167
    {
168
        if ('parent_id' == $postId) {
169
            $postId = intval(wp_get_post_parent_id(intval(get_the_ID())));
170
        } elseif ('post_id' == $postId) {
171
            $postId = intval(get_the_ID());
172
        }
173
        return $postId;
174
    }
175
176
    /**
177
     * @param string $postId
178
     * @return int|string
179
     */
180
    protected function normalizeAssignTo($postId)
181
    {
182
        return $this->normalizeAssignedTo($postId);
183
    }
184
185
    /**
186
     * @param string|array $hide
187
     * @return array
188
     */
189
    protected function normalizeHide($hide)
190
    {
191
        if (is_string($hide)) {
192
            $hide = explode(',', $hide);
193
        }
194
        $hideKeys = array_keys($this->getHideOptions());
195
        return array_filter(array_map('trim', $hide), function ($value) use ($hideKeys) {
196
            return in_array($value, $hideKeys);
197
        });
198
    }
199
200
    /**
201
     * @param string $id
202
     * @return string
203
     */
204
    protected function normalizeId($id)
205
    {
206
        return sanitize_title($id);
207
    }
208
209
    /**
210
     * @param string $labels
211
     * @return array
212
     */
213
    protected function normalizeLabels($labels)
214
    {
215
        $defaults = [
216
            __('Excellent', 'site-reviews'),
217
            __('Very good', 'site-reviews'),
218
            __('Average', 'site-reviews'),
219
            __('Poor', 'site-reviews'),
220
            __('Terrible', 'site-reviews'),
221
        ];
222
        $maxRating = (int) glsr()->constant('MAX_RATING', Rating::class);
223
        $defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, '');
224
        $labels = array_map('trim', explode(',', $labels));
225
        foreach ($defaults as $i => $label) {
226
            if (empty($labels[$i])) {
227
                continue;
228
            }
229
            $defaults[$i] = $labels[$i];
230
        }
231
        return array_combine(range($maxRating, 1), $defaults);
232
    }
233
234
    /**
235
     * @param string $schema
236
     * @return bool
237
     */
238
    protected function normalizeSchema($schema)
239
    {
240
        return wp_validate_boolean($schema);
241
    }
242
243
    /**
244
     * @param string $text
245
     * @return string
246
     */
247
    protected function normalizeText($text)
248
    {
249
        return trim($text);
250
    }
251
252
    /**
253
     * @return void
254
     */
255
    protected function setId(array &$atts)
256
    {
257
        if (empty($atts['id'])) {
258
            $atts['id'] = Application::PREFIX.substr(md5(serialize($atts)), 0, 8);
259
        }
260
    }
261
}
262