Passed
Push — develop ( 667a57...839f1b )
by Paul
15:02
created

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