Passed
Push — master ( ece31d...41b8a6 )
by Paul
10:20 queued 04:17
created

TinymcePopupGenerator::normalizeTextbox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 15
ccs 3
cts 3
cp 1
crap 1
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Shortcodes;
4
5
use GeminiLabs\SiteReviews\Database;
6
7
abstract class TinymcePopupGenerator
8
{
9
    /**
10
     * @var array
11
     */
12
    public $properties;
13
14
    /**
15
     * @var string
16
     */
17
    public $tag;
18
19
    /**
20
     * @var array
21
     */
22
    protected $errors = [];
23
24
    /**
25
     * @var array
26
     */
27
    protected $required = [];
28
29
    /**
30
     * @return array
31
     */
32
    abstract public function fields();
33
34
    /**
35
     * @param string $tag
36
     * @return static
37
     */
38 1
    public function register($tag, array $args)
39
    {
40 1
        $this->tag = $tag;
41 1
        $this->properties = wp_parse_args($args, [
42 1
            'btn_close' => esc_html__('Close', 'site-reviews'),
43 1
            'btn_okay' => esc_html__('Insert Shortcode', 'site-reviews'),
44 1
            'errors' => $this->errors,
45 1
            'fields' => $this->getFields(),
46 1
            'label' => '['.$tag.']',
47 1
            'required' => $this->required,
48 1
            'title' => esc_html__('Shortcode', 'site-reviews'),
49
        ]);
50 1
        return $this;
51
    }
52
53
    /**
54
     * @return array
55
     */
56
    protected function generateFields(array $fields)
57
    {
58 1
        $generatedFields = array_map(function ($field) {
59 1
            if (empty($field)) {
60 1
                return;
61
            }
62 1
            $field = $this->normalize($field);
63 1
            if (!method_exists($this, $method = 'normalize'.ucfirst($field['type']))) {
64
                return;
65
            }
66 1
            return $this->$method($field);
67 1
        }, $fields);
68 1
        return array_values(array_filter($generatedFields));
69
    }
70
71
    /**
72
     * @param string $tooltip
73
     * @return array
74
     */
75 1
    protected function getCategories($tooltip = '')
76
    {
77 1
        $terms = glsr(Database::class)->getTerms();
78 1
        if (empty($terms)) {
79 1
            return [];
80
        }
81
        return [
82
            'label' => esc_html__('Category', 'site-reviews'),
83
            'name' => 'category',
84
            'options' => $terms,
85
            'tooltip' => $tooltip,
86
            'type' => 'listbox',
87
        ];
88
    }
89
90
    /**
91
     * @return array
92
     */
93 1
    protected function getFields()
94
    {
95 1
        $fields = $this->generateFields($this->fields());
96 1
        if (!empty($this->errors)) {
97
            $errors = [];
98
            foreach ($this->required as $name => $alert) {
99
                if (false !== array_search($name, glsr_array_column($fields, 'name'))) {
100
                    continue;
101
                }
102
                $errors[] = $this->errors[$name];
103
            }
104
            $this->errors = $errors;
105
        }
106 1
        return empty($this->errors)
107 1
            ? $fields
108 1
            : $this->errors;
109
    }
110
111
    /**
112
     * @return array
113
     */
114 1
    protected function getHideOptions()
115
    {
116 1
        $classname = str_replace('Popup', 'Shortcode', get_class($this));
117 1
        $hideOptions = glsr($classname)->getHideOptions();
118 1
        $options = [];
119 1
        foreach ($hideOptions as $name => $tooltip) {
120 1
            $options[] = [
121 1
                'name' => 'hide_'.$name,
122 1
                'text' => $name,
123 1
                'tooltip' => $tooltip,
124 1
                'type' => 'checkbox',
125
            ];
126
        }
127 1
        return $options;
128
    }
129
130
    /**
131
     * @param string $tooltip
132
     * @return array
133
     */
134 1
    protected function getTypes($tooltip = '')
135
    {
136 1
        if (count(glsr()->reviewTypes) < 2) {
137 1
            return [];
138
        }
139
        return [
140
            'label' => esc_html__('Type', 'site-reviews'),
141
            'name' => 'type',
142
            'options' => glsr()->reviewTypes,
143
            'tooltip' => $tooltip,
144
            'type' => 'listbox',
145
        ];
146
    }
147
148
    /**
149
     * @return array
150
     */
151 1
    protected function normalize(array $field)
152
    {
153 1
        return wp_parse_args($field, [
154 1
            'items' => [],
155
            'type' => '',
156
        ]);
157
    }
158
159
    /**
160
     * @return void|array
161
     */
162 1
    protected function normalizeCheckbox(array $field)
163
    {
164 1
        return $this->normalizeField($field, [
165 1
            'checked' => false,
166
            'label' => '',
167
            'minHeight' => '',
168
            'minWidth' => '',
169
            'name' => false,
170
            'text' => '',
171
            'tooltip' => '',
172
            'type' => '',
173
            'value' => '',
174
        ]);
175
    }
176
177
    /**
178
     * @return void|array
179
     */
180 1
    protected function normalizeContainer(array $field)
181
    {
182 1
        if (!array_key_exists('html', $field) && !array_key_exists('items', $field)) {
183
            return;
184
        }
185 1
        $field['items'] = $this->generateFields($field['items']);
186 1
        return $field;
187
    }
188
189
    /**
190
     * @return void|array
191
     */
192 1
    protected function normalizeField(array $field, array $defaults)
193
    {
194 1
        if (!$this->validate($field)) {
195
            return;
196
        }
197 1
        return array_filter(shortcode_atts($defaults, $field), function ($value) {
198 1
            return '' !== $value;
199 1
        });
200
    }
201
202
    /**
203
     * @return void|array
204
     */
205 1
    protected function normalizeListbox(array $field)
206
    {
207 1
        $listbox = $this->normalizeField($field, [
208 1
            'label' => '',
209 1
            'minWidth' => '',
210
            'name' => false,
211
            'options' => [],
212 1
            'placeholder' => esc_attr__('- Select -', 'site-reviews'),
213 1
            'tooltip' => '',
214 1
            'type' => '',
215 1
            'value' => '',
216
        ]);
217 1
        if (!is_array($listbox)) {
218
            return;
219
        }
220 1
        if (!array_key_exists('', $listbox['options'])) {
221 1
            $listbox['options'] = ['' => $listbox['placeholder']] + $listbox['options'];
222
        }
223 1
        foreach ($listbox['options'] as $value => $text) {
224 1
            $listbox['values'][] = [
225 1
                'text' => $text,
226 1
                'value' => $value,
227
            ];
228
        }
229 1
        return $listbox;
230
    }
231
232
    /**
233
     * @return void|array
234
     */
235
    protected function normalizePost(array $field)
236
    {
237
        if (!is_array($field['query_args'])) {
238
            $field['query_args'] = [];
239
        }
240
        $posts = get_posts(wp_parse_args($field['query_args'], [
241
            'order' => 'ASC',
242
            'orderby' => 'title',
243
            'post_type' => 'post',
244
            'posts_per_page' => 30,
245
        ]));
246
        if (!empty($posts)) {
247
            $options = [];
248
            foreach ($posts as $post) {
249
                $options[$post->ID] = esc_html($post->post_title);
250
            }
251
            $field['options'] = $options;
252
            $field['type'] = 'listbox';
253
            return $this->normalizeListbox($field);
254
        }
255
        $this->validate($field);
256
    }
257
258
    /**
259
     * @return void|array
260
     */
261 1
    protected function normalizeTextbox(array $field)
262
    {
263 1
        return $this->normalizeField($field, [
264 1
            'hidden' => false,
265
            'label' => '',
266
            'maxLength' => '',
267
            'minHeight' => '',
268
            'minWidth' => '',
269
            'multiline' => false,
270
            'name' => false,
271
            'size' => '',
272
            'text' => '',
273
            'tooltip' => '',
274
            'type' => '',
275
            'value' => '',
276
        ]);
277
    }
278
279
    /**
280
     * @return bool
281
     */
282 1
    protected function validate(array $field)
283
    {
284 1
        $args = shortcode_atts([
285 1
            'label' => '',
286
            'name' => false,
287
            'required' => false,
288 1
        ], $field);
289 1
        if (!$args['name']) {
290
            return false;
291
        }
292 1
        return $this->validateErrors($args) && $this->validateRequired($args);
293
    }
294
295
    /**
296
     * @return bool
297
     */
298 1
    protected function validateErrors(array $args)
299
    {
300 1
        if (!isset($args['required']['error'])) {
301 1
            return true;
302
        }
303
        $this->errors[$args['name']] = $this->normalizeContainer([
304
            'html' => $args['required']['error'],
305
            'type' => 'container',
306
        ]);
307
        return false;
308
    }
309
310
    /**
311
     * @return bool
312
     */
313 1
    protected function validateRequired(array $args)
314
    {
315 1
        if (false == $args['required']) {
316 1
            return true;
317
        }
318
        $alert = esc_html__('Some of the shortcode options are required.', 'site-reviews');
319
        if (isset($args['required']['alert'])) {
320
            $alert = $args['required']['alert'];
321
        } elseif (!empty($args['label'])) {
322
            $alert = sprintf(
323
                esc_html_x('The "%s" option is required.', 'the option label', 'site-reviews'),
324
                str_replace(':', '', $args['label'])
325
            );
326
        }
327
        $this->required[$args['name']] = $alert;
328
        return false;
329
    }
330
}
331