Passed
Push — master ( 6b35fa...ccb079 )
by Paul
10:24 queued 05:13
created

Shortcode::getShortcodeClassName()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
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\Partial;
10
use GeminiLabs\SiteReviews\Modules\Rating;
11
use ReflectionClass;
12
13
abstract class Shortcode implements ShortcodeContract
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $partialName;
19
20
    /**
21
     * @var string
22
     */
23
    protected $shortcodeName;
24
25 1
    public function __construct()
26
    {
27 1
        $this->partialName = $this->getShortcodePartialName();
28 1
        $this->shortcodeName = $this->getShortcodeName();
29 1
    }
30
31
    /**
32
     * @param string|array $atts
33
     * @param string $type
34
     * @return string
35
     */
36
    public function build($atts, array $args = [], $type = 'shortcode')
37
    {
38
        $args = $this->normalizeArgs($args, $type);
39
        $atts = $this->normalizeAtts($atts, $type);
40
        $partial = glsr(Partial::class)->build($this->partialName, $atts);
41
        $title = !empty($atts['title'])
42
            ? $args['before_title'].$atts['title'].$args['after_title']
43
            : '';
44
        $debug = sprintf('<glsr-%1$s hidden data-atts=\'%2$s\'></glsr-%1$s>', $type, $atts['json']);
45
        return $args['before_widget'].$title.$partial.$debug.$args['after_widget'];
46
    }
47
48
    /**
49
     * @param string|array $atts
50
     * @return string
51
     */
52
    public function buildShortcode($atts = [])
53
    {
54
        return $this->build($atts);
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function getDefaults($atts)
61
    {
62
        return glsr($this->getShortcodeDefaultsClassName())->restrict(wp_parse_args($atts));
63
    }
64
65
    /**
66
     * @return array
67
     */
68 1
    public function getHideOptions()
69
    {
70 1
        $options = $this->hideOptions();
71 1
        return apply_filters('site-reviews/shortcode/hide-options', $options, $this->shortcodeName);
72
    }
73
74
    /**
75
     * @return string
76
     */
77 1
    public function getShortcodeClassName($replace = '', $search = 'Shortcode')
78
    {
79 1
        return str_replace($search, $replace, (new ReflectionClass($this))->getShortName());
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getShortcodeDefaultsClassName()
86
    {
87
        return Helper::buildClassName(
88
            $this->getShortcodeClassName('Defaults'),
89
            'Defaults'
90
        );
91
    }
92
93
    /**
94
     * @return string
95
     */
96 1
    public function getShortcodeName()
97
    {
98 1
        return Str::snakeCase($this->getShortcodeClassName());
99
    }
100
101
    /**
102
     * @return string
103
     */
104 1
    public function getShortcodePartialName()
105
    {
106 1
        return Str::dashCase($this->getShortcodeClassName());
107
    }
108
109
    /**
110
     * @param array|string $args
111
     * @param string $type
112
     * @return array
113
     */
114
    public function normalizeArgs($args, $type = 'shortcode')
115
    {
116
        $args = wp_parse_args($args, [
117
            'before_widget' => '<div class="glsr-'.$type.' '.$type.'-'.$this->partialName.'">',
118
            'after_widget' => '</div>',
119
            'before_title' => '<h3 class="glsr-'.$type.'-title">',
120
            'after_title' => '</h3>',
121
        ]);
122
        return apply_filters('site-reviews/shortcode/args', $args, $type, $this->partialName);
123
    }
124
125
    /**
126
     * @param array|string $atts
127
     * @param string $type
128
     * @return array
129
     */
130
    public function normalizeAtts($atts, $type = 'shortcode')
131
    {
132
        $atts = apply_filters('site-reviews/shortcode/atts', $atts, $type, $this->partialName);
133
        $atts = $this->getDefaults($atts);
134
        array_walk($atts, function (&$value, $key) {
135
            $methodName = Helper::buildMethodName($key, 'normalize');
136
            if (!method_exists($this, $methodName)) {
137
                return;
138
            }
139
            $value = $this->$methodName($value);
140
        });
141
        $this->setId($atts);
142
        return $atts;
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    abstract protected function hideOptions();
149
150
    /**
151
     * @param string $postId
152
     * @return int|string
153
     */
154
    protected function normalizeAssignedTo($postId)
155
    {
156
        if ('parent_id' == $postId) {
157
            $postId = intval(wp_get_post_parent_id(intval(get_the_ID())));
158
        } elseif ('post_id' == $postId) {
159
            $postId = intval(get_the_ID());
160
        }
161
        return $postId;
162
    }
163
164
    /**
165
     * @param string $postId
166
     * @return int|string
167
     */
168
    protected function normalizeAssignTo($postId)
169
    {
170
        return $this->normalizeAssignedTo($postId);
171
    }
172
173
    /**
174
     * @param string|array $hide
175
     * @return array
176
     */
177
    protected function normalizeHide($hide)
178
    {
179
        if (is_string($hide)) {
180
            $hide = explode(',', $hide);
181
        }
182
        $hideKeys = array_keys($this->getHideOptions());
183
        return array_filter(array_map('trim', $hide), function ($value) use ($hideKeys) {
184
            return in_array($value, $hideKeys);
185
        });
186
    }
187
188
    /**
189
     * @param string $id
190
     * @return string
191
     */
192
    protected function normalizeId($id)
193
    {
194
        return sanitize_title($id);
195
    }
196
197
    /**
198
     * @param string $labels
199
     * @return array
200
     */
201
    protected function normalizeLabels($labels)
202
    {
203
        $defaults = [
204
            __('Excellent', 'site-reviews'),
205
            __('Very good', 'site-reviews'),
206
            __('Average', 'site-reviews'),
207
            __('Poor', 'site-reviews'),
208
            __('Terrible', 'site-reviews'),
209
        ];
210
        $maxRating = (int) glsr()->constant('MAX_RATING', Rating::class);
211
        $defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, '');
212
        $labels = array_map('trim', explode(',', $labels));
213
        foreach ($defaults as $i => $label) {
214
            if (empty($labels[$i])) {
215
                continue;
216
            }
217
            $defaults[$i] = $labels[$i];
218
        }
219
        return array_combine(range($maxRating, 1), $defaults);
220
    }
221
222
    /**
223
     * @param string $schema
224
     * @return bool
225
     */
226
    protected function normalizeSchema($schema)
227
    {
228
        return wp_validate_boolean($schema);
229
    }
230
231
    /**
232
     * @param string $text
233
     * @return string
234
     */
235
    protected function normalizeText($text)
236
    {
237
        return trim($text);
238
    }
239
240
    /**
241
     * @return void
242
     */
243
    protected function setId(array &$atts)
244
    {
245
        if (empty($atts['id'])) {
246
            $atts['id'] = Application::PREFIX.substr(md5(serialize($atts)), 0, 8);
247
        }
248
    }
249
}
250