Passed
Push — develop ( 39ad60...0ca1de )
by Paul
14:09
created

Controller::filterThemeStyleControlGroups()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
ccs 0
cts 15
cp 0
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Bricks;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Helpers\Str;
9
use GeminiLabs\SiteReviews\Helpers\Svg;
10
use GeminiLabs\SiteReviews\Integrations\Bricks\Commands\SearchAssignedPosts;
11
use GeminiLabs\SiteReviews\Integrations\Bricks\Commands\SearchAssignedTerms;
12
use GeminiLabs\SiteReviews\Integrations\Bricks\Commands\SearchAssignedUsers;
13
use GeminiLabs\SiteReviews\Integrations\Bricks\Commands\SearchAuthor;
14
use GeminiLabs\SiteReviews\Integrations\Bricks\Commands\SearchPostId;
15
use GeminiLabs\SiteReviews\Modules\Sanitizer;
16
17
class Controller extends AbstractController
18
{
19
    /**
20
     * @filter bricks/builder/i18n
21
     */
22
    public function filterBuilderI18n(array $i18n): array
23
    {
24
        $i18n = Arr::consolidate($i18n);
25
        $i18n[glsr()->id] = glsr()->name;
26
        return $i18n;
27
    }
28
29
    /**
30
     * @filter site-reviews/bricks/element/controls:50
31
     */
32
    public function filterControls(array $controls): array
33
    {
34
        $sectionLabels = [
35
            'display' => esc_html_x('Display', 'admin-text', 'site-reviews'),
36
            'hide' => esc_html_x('Hide', 'admin-text', 'site-reviews'),
37
            'schema' => esc_html_x('Schema', 'admin-text', 'site-reviews'),
38
            'text' => esc_html_x('Text', 'admin-text', 'site-reviews'),
39
        ];
40
        $result = [];
41
        $separatorsAdded = [];
42
        foreach ($controls as $key => $control) {
43
            $group = $control['group'] ?? 'general';
44
            if (!isset($sectionLabels[$group])) {
45
                $result[$key] = $control;
46
                continue;
47
            }
48
            if (!isset($separatorsAdded[$group])) {
49
                $result["separator_{$group}"] = [
50
                    'group' => 'general',
51
                    'label' => $sectionLabels[$group],
52
                    'tab' => 'content',
53
                    'type' => 'separator',
54
                ];
55
                $separatorsAdded[$group] = true;
56
            }
57
            $control['group'] = 'general';
58
            $result[$key] = $control;
59
        }
60
        return $result;
61
    }
62
63
    /**
64
     * Add style classes.
65
     *
66
     * @filter bricks/element/settings
67
     */
68
    public function filterSettingsClass(array $settings, \Bricks\Element $element): array
69
    {
70
        if (!$element instanceof BricksElement) {
71
            return $settings;
72
        }
73
        $classes = $settings['class'] ?? '';
74
        $classes = explode(' ', $classes);
75
        if ($align = Cast::toString($element->styledSetting('style_align'))) {
76
            $align = str_replace('flex-', '', $align);
77
            $align = ['start' => 'left', 'end' => 'right'][$align] ?? $align;
78
            $classes[] = "items-justified-{$align}";
79
        }
80
        if ($textAlign = Cast::toString($element->styledSetting('style_text_align'))) {
81
            $classes[] = "has-text-align-{$textAlign}";
82
        }
83
        $classes = implode(' ', $classes);
84
        $settings['class'] = glsr(Sanitizer::class)->sanitizeAttrClass($classes);
85
        return $settings;
86
    }
87
88
    /**
89
     * Consolidate multi-checkbox values because Bricks does not allow them.
90
     *
91
     * @filter bricks/element/settings
92
     */
93
    public function filterSettingsMultiCheckbox(array $settings, \Bricks\Element $element): array
94
    {
95
        if (!$element instanceof BricksElement) {
96
            return $settings;
97
        }
98
        foreach ($element->elementConfig() as $key => $control) {
99
            $type = $control['type'] ?? '';
100
            $options = Arr::getAs('array', $control, 'options');
101
            if ('checkbox' !== $type || empty($options)) {
102
                continue;
103
            }
104
            $values = array_filter(
105
                array_keys($settings),
106
                fn ($k) => !empty($settings[$k]) && str_starts_with($k, "{$key}_")
107
            );
108
            $settings[$key] = array_map(fn ($k) => Str::removePrefix($k, "{$key}_"), $values);
109
        }
110
        return $settings;
111
    }
112
113
    /**
114
     * Remove the "id::" prefix used to maintain javascript sorting.
115
     *
116
     * @filter bricks/element/settings
117
     */
118
    public function filterSettingsPrefixedId(array $settings, \Bricks\Element $element): array
119
    {
120
        if (!$element instanceof BricksElement) {
121
            return $settings;
122
        }
123
        foreach ($settings as $key => $value) {
124
            if (is_string($value) && str_starts_with($value, 'id::')) {
125
                $settings[$key] = Str::removePrefix($value, 'id::');
126
                continue;
127
            }
128
            if (is_array($value) && wp_is_numeric_array($value)) {
129
                $settings[$key] = array_map(
130
                    fn ($val) => is_string($val) ? Str::removePrefix($val, 'id::') : '',
131
                    $value
132
                );
133
            }
134
        }
135
        return $settings;
136
    }
137
138
    /**
139
     * Use this hook in addons to remove the group if unsupported.
140
     *
141
     * @filter bricks/theme_styles/control_groups
142
     */
143
    public function filterThemeStyleControlGroups(array $groups): array
144
    {
145
        if (!class_exists(\Bricks\Elements::class)) {
146
            return $groups;
147
        }
148
        $groups[glsr()->id] = [
149
            'isParent' => true,
150
            'title' => glsr()->name,
151
        ];
152
        foreach (\Bricks\Elements::$elements as $tag => $element) {
153
            if (!str_starts_with($tag, 'site_review')) {
154
                continue;
155
            }
156
            $groups[$tag] = [
157
                'title' => $element['label'] ?? $tag,
158
                'parent' => glsr()->id,
159
            ];
160
        }
161
        return $groups;
162
    }
163
164
    /**
165
     * Use this hook in addons to remove the contolrs group if unsupported.
166
     *
167
     * @filter bricks/theme_styles/controls
168
     */
169
    public function filterThemeStyleControls(array $controls): array
170
    {
171
        if (!class_exists(\Bricks\Elements::class)) {
172
            return $controls;
173
        }
174
        foreach (\Bricks\Elements::$elements as $tag => $element) {
175
            if (!str_starts_with($tag, 'site_review')) {
176
                continue;
177
            }
178
            $instance = new $element['class']();
179
            $instance->set_controls();
180
            $instance->controls = apply_filters("bricks/elements/{$tag}/controls", $instance->controls);
181
            $instance->controls = glsr()->filterArray('bricks/element/controls', $instance->controls, $instance);
182
            $themeStyleControls = array_filter($instance->controls,
183
                fn ($control) => !empty($control['themeStyle'])
184
            );
185
            array_walk($themeStyleControls, function (&$control) use ($tag) {
186
                $control['group'] = $tag;
187
                unset($control['required']);
188
            });
189
            if ($themeStyleControls) {
190
                $controls[$tag] = $themeStyleControls;
191
            }
192
        }
193
        return $controls;
194
    }
195
196
    /**
197
     * @action wp_enqueue_scripts:20
198
     */
199
    public function printInlineStyles(): void
200
    {
201
        if (!function_exists('bricks_is_builder') || !bricks_is_builder()) {
202
            return;
203
        }
204
        $icons = [
205
            'ti-site_reviews_form' => Svg::encoded('assets/images/icons/bricks/icon-form.svg'),
206
            'ti-site_review' => Svg::encoded('assets/images/icons/bricks/icon-review.svg'),
207
            'ti-site_reviews' => Svg::encoded('assets/images/icons/bricks/icon-reviews.svg'),
208
            'ti-site_reviews_summary' => Svg::encoded('assets/images/icons/bricks/icon-summary.svg'),
209
        ];
210
        $maskRules = '';
211
        foreach ($icons as $class => $url) {
212
            $maskRules .= "i.{$class}::before { mask-image: url(\"{$url}\"); }\n";
213
        }
214
        $css = <<<CSS
215
            i[class^="ti-site_review"]::before {
216
                background-color: currentColor;
217
                content: '';
218
                display: inline-block;
219
                height: 1em;
220
                width: 1em;
221
                mask-position: center;
222
                mask-repeat: no-repeat;
223
                mask-size: 100%;
224
            }
225
            {$maskRules}
226
            .glsr :is(a,button,input,textarea,select,.dz-clickable) {
227
                pointer-events: none !important;
228
            }
229
        CSS;
230
        wp_add_inline_style('bricks-builder', $css);
231
    }
232
233
    /**
234
     * @action init:11
235
     */
236
    public function registerElements(): void
237
    {
238
        if (class_exists('Bricks\Element')) {
239
            Elements\BricksSiteReview::registerElement();
240
            Elements\BricksSiteReviews::registerElement();
241
            Elements\BricksSiteReviewsForm::registerElement();
242
            Elements\BricksSiteReviewsSummary::registerElement();
243
        }
244
    }
245
246
    /**
247
     * @action wp_ajax_bricks_glsr_assigned_posts
248
     */
249
    public function searchAssignedPosts(): void
250
    {
251
        $this->execute(new SearchAssignedPosts())->sendJsonResponse();
252
    }
253
254
    /**
255
     * @action wp_ajax_bricks_glsr_assigned_terms
256
     */
257
    public function searchAssignedTerms(): void
258
    {
259
        $this->execute(new SearchAssignedTerms())->sendJsonResponse();
260
    }
261
262
    /**
263
     * @action wp_ajax_bricks_glsr_assigned_users
264
     */
265
    public function searchAssignedUsers(): void
266
    {
267
        $this->execute(new SearchAssignedUsers())->sendJsonResponse();
268
    }
269
270
    /**
271
     * @action wp_ajax_bricks_glsr_author
272
     */
273
    public function searchAuthor(): void
274
    {
275
        $this->execute(new SearchAuthor())->sendJsonResponse();
276
    }
277
278
    /**
279
     * @action wp_ajax_bricks_glsr_post_id
280
     */
281
    public function searchPostId(): void
282
    {
283
        $this->execute(new SearchPostId())->sendJsonResponse();
284
    }
285
}
286