Test Failed
Push — develop ( 396f05...ae48f7 )
by Paul
15:45
created

Module::module_classnames()   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 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Divi\Modules\SiteReviewsForm;
4
5
use ET\Builder\FrontEnd\Module\Style;
6
use ET\Builder\Packages\Module\Options\Font\FontStyle;
7
use ET\Builder\Packages\StyleLibrary\Utils\StyleDeclarations;
8
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
9
use GeminiLabs\SiteReviews\Integrations\Divi\Defaults\ModuleClassnamesDefaults;
10
use GeminiLabs\SiteReviews\Integrations\Divi\Defaults\ModuleStylesDefaults;
11
use GeminiLabs\SiteReviews\Integrations\Divi\Modules\DiviModule;
12
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsFormShortcode;
13
14
class Module extends DiviModule
15
{
16
    public static function blockName(): string
17
    {
18
        return 'glsr-divi/form';
19
    }
20
21
    /**
22
     * This method is equivalent to "module-classnames.ts".
23
     */
24
    public static function module_classnames(array $args): void
25
    {
26
        $args = glsr(ModuleClassnamesDefaults::class)->merge($args);
27
        $ratingColor = $args['attrs']['design']['decoration']['ratingColor']['desktop']['value'] ?? '';
28
        $args['classnamesInstance']->add('has-custom-color', !empty($ratingColor));
29
        parent::module_classnames($args);
30
    }
31
32
    /**
33
     * This method is equivalent to "module-styles.tsx".
34
     */
35
    public static function module_styles(array $args): void
36
    {
37
        $args = glsr(ModuleStylesDefaults::class)->merge($args);
38
        $attrs = $args['attrs'];
39
        $baseSelector = '.et-db #page-container .et_pb_section';
40
        $elements = $args['elements'];
41
        $orderClass = $args['orderClass'];
42
        $settings = $args['settings'];
43
        Style::add([
44
            'id' => $args['id'],
45
            'name' => $args['name'],
46
            'orderIndex' => $args['orderIndex'],
47
            'storeInstance' => $args['storeInstance'],
48
            'styles' => [
49
                $elements->style([
50
                    'attrName' => 'module',
51
                    'styleProps' => [
52
                        'advancedStyles' => [
53
                            [
54
                                'componentName' => 'divi/common',
55
                                'props' => [
56
                                    'attr' => $attrs['module']['advanced']['text']['text'] ?? [],
57
                                    'declarationFunction' => static::orientationStyleDeclaration(),
58
                                    'selector' => implode(',', [
59
                                        "{$baseSelector} {$orderClass} .glsr-field:not(.glsr-layout-inline) .glsr-field-subgroup > *",
60
                                        "{$baseSelector} {$orderClass} .glsr-layout-inline .glsr-field-subgroup",
61
                                        "{$baseSelector} {$orderClass} .glsr-range-options input:checked + label",
62
                                        "{$baseSelector} {$orderClass} .glsr-range-options:not(:has(input:checked))::after",
63
                                        "{$baseSelector} {$orderClass} .glsr-star-rating",
64
                                    ]),
65
                                ],
66
                            ],
67
                            [
68
                                'componentName' => 'divi/text',
69
                                'props' => [
70
                                    'attr' => $attrs['module']['advanced']['text'] ?? [],
71
                                    'propertySelectors' => [
72
                                        'textShadow' => [
73
                                            'desktop' => [
74
                                                'value' => [
75
                                                    'text-shadow' => implode(',', [
76
                                                        "{$baseSelector} {$orderClass} .glsr-field",
77
                                                        "{$baseSelector} {$orderClass} .glsr-input",
78
                                                        "{$baseSelector} {$orderClass} .glsr-select",
79
                                                        "{$baseSelector} {$orderClass} .glsr-textarea",
80
                                                    ]),
81
                                                ],
82
                                            ],
83
                                        ],
84
                                    ],
85
                                ],
86
                            ],
87
                        ],
88
                        'border' => [
89
                            'propertySelectors' => [
90
                                'desktop' => [
91
                                    'value' => [
92
                                        'border-radius' => implode(',', [
93
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-dropzone",
94
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input",
95
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input-checkbox",
96
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-select",
97
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-textarea",
98
                                        ]),
99
                                        'border-style' => implode(',', [
100
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-dropzone",
101
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input",
102
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input-checkbox",
103
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input-radio",
104
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input-range",
105
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-select",
106
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-textarea",
107
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-toggle-track::before",
108
                                        ]),
109
                                    ],
110
                                ],
111
                            ],
112
                        ],
113
                        'boxShadow' => [
114
                            'selector' => implode(',', [
115
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-dropzone",
116
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-input",
117
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-input-checkbox",
118
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-input-radio",
119
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-input-range",
120
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-select",
121
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-textarea",
122
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-toggle-track::before",
123
                            ]),
124
                        ],
125
                        'disabledOn' => [
126
                            'disabledModuleVisibility' => $settings['disabledModuleVisibility'] ?? null,
127
                        ],
128
                    ],
129
                ]),
130
                $elements->style([
131
                    'attrName' => 'button',
132
                ]),
133
                $elements->style([
134
                    'styleProps' => [
135
                        'advancedStyles' => [
136
                            [
137
                                // Rating Color
138
                                'componentName' => 'divi/common',
139
                                'props' => [
140
                                    'attr' => $attrs['design']['decoration']['ratingColor'] ?? [],
141
                                    'property' => '--glsr-form-star-bg',
142
                                    'selector' => "{$orderClass} .glsr-form",
143
                                ],
144
                            ],
145
                            [
146
                                // Rating Size
147
                                'componentName' => 'divi/common',
148
                                'props' => [
149
                                    'attr' => $attrs['design']['decoration']['ratingSize'] ?? [],
150
                                    'property' => '--glsr-form-star',
151
                                    'selector' => "{$orderClass} .glsr-form",
152
                                ],
153
                            ],
154
                            [
155
                                // Form Column Gap
156
                                'componentName' => 'divi/common',
157
                                'props' => [
158
                                    'attr' => $attrs['design']['decoration']['columnGap'] ?? [],
159
                                    'property' => '--glsr-form-col-gap',
160
                                    'selector' => "{$orderClass} .glsr-form",
161
                                ],
162
                            ],
163
                            [
164
                                // Form Row Gap
165
                                'componentName' => 'divi/common',
166
                                'props' => [
167
                                    'attr' => $attrs['design']['decoration']['rowGap'] ?? [],
168
                                    'property' => '--glsr-form-row-gap',
169
                                    'selector' => "{$orderClass} .glsr-form",
170
                                ],
171
                            ],
172
                            [
173
                                // Field Padding
174
                                'componentName' => 'divi/common',
175
                                'props' => [
176
                                    'attr' => $attrs['design']['decoration']['spacing'] ?? [],
177
                                    'declarationFunction' => static::spacingStyleDeclaration(),
178
                                    'selector' => "{$orderClass} .glsr-form",
179
                                ],
180
                            ],
181
                            [
182
                                // Field Label Text Color
183
                                'componentName' => 'divi/common',
184
                                'props' => [
185
                                    'attr' => $attrs['design']['decoration']['fieldLabelGroup']['textColor'] ?? [],
186
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-label-color']),
187
                                    'selector' => "{$orderClass} .glsr-form",
188
                                ],
189
                            ],
190
                            [
191
                                // Field Background Color
192
                                'componentName' => 'divi/common',
193
                                'props' => [
194
                                    'attr' => $attrs['design']['decoration']['fieldElementGroup']['background'] ?? [],
195
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-input-bg', '--glsr-toggle-bg-0']),
196
                                    'selector' => "{$orderClass} .glsr-form",
197
                                ],
198
                            ],
199
                            [
200
                                // Field Text Color
201
                                'componentName' => 'divi/common',
202
                                'props' => [
203
                                    'attr' => $attrs['design']['decoration']['fieldElementGroup']['textColor'] ?? [],
204
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-input-color']),
205
                                    'selector' => "{$orderClass} .glsr-form",
206
                                ],
207
                            ],
208
                            [
209
                                // Field Checked Color
210
                                'componentName' => 'divi/common',
211
                                'props' => [
212
                                    'attr' => $attrs['design']['advanced']['state']['checkedColor'] ?? [],
213
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-choice-color']),
214
                                    'selector' => implode(',', [
215
                                        "{$orderClass} .glsr-input-checkbox:checked",
216
                                        "{$orderClass} .glsr-input-radio:checked",
217
                                        "{$orderClass} .glsr-input-range:checked",
218
                                        "{$orderClass} .glsr-toggle-switch:has(:checked)",
219
                                    ]),
220
                                ],
221
                            ],
222
                            [
223
                                // Field Focused Background Color
224
                                'componentName' => 'divi/common',
225
                                'props' => [
226
                                    'attr' => $attrs['design']['advanced']['state']['focusBackground'] ?? [],
227
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-input-bg', '--glsr-toggle-bg-0']),
228
                                    'selector' => implode(',', [
229
                                        "{$orderClass} .glsr-input:focus",
230
                                        "{$orderClass} .glsr-select:focus",
231
                                        "{$orderClass} .glsr-textarea:focus",
232
                                    ]),
233
                                ],
234
                            ],
235
                            [
236
                                // Field Focused Text Color
237
                                'componentName' => 'divi/common',
238
                                'props' => [
239
                                    'attr' => $attrs['design']['advanced']['state']['focusColor'] ?? [],
240
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-input-color']),
241
                                    'selector' => implode(',', [
242
                                        "{$orderClass} .glsr-input:focus",
243
                                        "{$orderClass} .glsr-select:focus",
244
                                        "{$orderClass} .glsr-textarea:focus",
245
                                    ]),
246
                                ],
247
                            ],
248
                            [
249
                                // Field Invalid Text Color
250
                                'componentName' => 'divi/common',
251
                                'props' => [
252
                                    'attr' => $attrs['design']['advanced']['state']['invalidColor'] ?? [],
253
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-form-error']),
254
                                    'selector' => "{$orderClass} .glsr-form",
255
                                ],
256
                            ],
257
                        ],
258
                    ],
259
                ]),
260
                FontStyle::style([
261
                    // Field Label Text
262
                    'attr' => $attrs['design']['decoration']['fieldLabelGroup'] ?? [],
263
                    'orderClass' => $orderClass,
264
                    'selector' => "{$baseSelector} {$orderClass} .glsr-field .glsr-label",
265
                ]),
266
                FontStyle::style([
267
                    // Field Text
268
                    'attr' => $attrs['design']['decoration']['fieldElementGroup'] ?? [],
269
                    'orderClass' => $orderClass,
270
                    'selector' => implode(', ', [
271
                        "{$baseSelector} {$orderClass} .glsr-field .glsr-dz-message",
272
                        "{$baseSelector} {$orderClass} .glsr-field .glsr-field-subgroup",
273
                        "{$baseSelector} {$orderClass} .glsr-field .glsr-input",
274
                        "{$baseSelector} {$orderClass} .glsr-field .glsr-range-labels",
275
                        "{$baseSelector} {$orderClass} .glsr-field .glsr-select",
276
                        "{$baseSelector} {$orderClass} .glsr-field .glsr-textarea",
277
                    ]),
278
                    'propertySelectors' => [
279
                        'font' => [
280
                            'desktop' => [
281
                                'value' => [
282
                                    'font-size' => implode(', ', [
283
                                        "{$baseSelector} {$orderClass} .glsr-field",
284
                                        "{$baseSelector} {$orderClass} .glsr-dropzone",
285
                                    ]),
286
                                    'text-align' => implode(', ', [
287
                                        "{$baseSelector} {$orderClass} .glsr-field .glsr-input",
288
                                        "{$baseSelector} {$orderClass} .glsr-field .glsr-select",
289
                                        "{$baseSelector} {$orderClass} .glsr-field .glsr-textarea",
290
                                    ]),
291
                                ],
292
                            ],
293
                        ],
294
                    ],
295
                ]),
296
            ],
297
        ]);
298
    }
299
300
    public static function shortcodeInstance(): ShortcodeContract
301
    {
302
        static $shortcode;
303
        if (empty($shortcode)) {
304
            $shortcode = glsr(SiteReviewsFormShortcode::class);
305
        }
306
        return $shortcode;
307
    }
308
309
    protected static function colorStyleDeclaration(array $cssProperties): callable
310
    {
311
        return static function (array $args) use ($cssProperties): string {
312
            $color = $args['attrValue']['color'] ?? null;
313
            $declarations = new StyleDeclarations([
314
                'important' => false,
315
                'returnType' => 'string',
316
            ]);
317
            if ($color) {
318
                foreach ($cssProperties as $prop) {
319
                    $declarations->add($prop, $color);
320
                }
321
            }
322
            return $declarations->value();
323
        };
324
    }
325
326
    protected static function orientationStyleDeclaration(): callable
327
    {
328
        return static function (array $args): string {
329
            $padding = $args['attrValue']['orientation'] ?? null;
0 ignored issues
show
Unused Code introduced by
The assignment to $padding is dead and can be removed.
Loading history...
330
            $declarations = new StyleDeclarations([
331
                'important' => true,
332
                'returnType' => 'string',
333
            ]);
334
            if ($orientation) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $orientation seems to be never defined.
Loading history...
335
                $declarations->add('display', 'flex');
336
                $declarations->add('justify-content', $orientation);
337
            }
338
            return $declarations->value();
339
        };
340
    }
341
342
    protected static function spacingStyleDeclaration(): callable
343
    {
344
        return static function (array $args): string {
345
            $padding = $args['attrValue']['padding'] ?? null;
346
            $declarations = new StyleDeclarations([
347
                'important' => false,
348
                'returnType' => 'string',
349
            ]);
350
            if (isset($padding['top'])) {
351
                $declarations->add('--glsr-input-pt', $padding['top']);
352
            }
353
            if (isset($padding['right'])) {
354
                $declarations->add('--glsr-input-pr', $padding['right']);
355
            }
356
            if (isset($padding['bottom'])) {
357
                $declarations->add('--glsr-input-pb', $padding['bottom']);
358
            }
359
            if (isset($padding['left'])) {
360
                $declarations->add('--glsr-input-pl', $padding['left']);
361
            }
362
            return $declarations->value();
363
        };
364
    }
365
}
366