Test Failed
Push — develop ( c0d6f9...325b33 )
by Paul
08:37
created

Controller   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 35
eloc 123
c 1
b 0
f 0
dl 0
loc 258
rs 9.6

12 Methods

Rating   Name   Duplication   Size   Complexity  
A filterSettingsPrefixedId() 0 19 6
A verifyAjaxRequest() 0 6 3
A includedIds() 0 7 1
A searchAssignedPosts() 0 27 4
A searchPostId() 0 23 3
A filterSettingsMultiCheckbox() 0 19 6
A searchAssignedUsers() 0 22 3
A registerElements() 0 7 2
A searchAssignedTerms() 0 15 2
A printInlineStyles() 0 40 3
A filterBuilderI18n() 0 5 1
A prefixedResultIds() 0 5 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Bricks;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Helpers\Arr;
9
use GeminiLabs\SiteReviews\Helpers\Str;
10
11
class Controller extends AbstractController
12
{
13
    /**
14
     * @param array $i18n
15
     *
16
     * @filter bricks/builder/i18n
17
     */
18
    public function filterBuilderI18n($i18n): array
19
    {
20
        $i18n = Arr::consolidate($i18n);
21
        $i18n[glsr()->id] = glsr()->name;
22
        return $i18n;
23
    }
24
25
    /**
26
     * Consolidate multi-checkbox values because Bricks does not allow them.
27
     *
28
     * @param \Bricks\Element $element
0 ignored issues
show
Bug introduced by
The type Bricks\Element was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
     *
30
     * @filter bricks/element/settings
31
     */
32
    public function filterSettingsMultiCheckbox($settings, $element): array
33
    {
34
        $settings = Arr::consolidate($settings);
35
        if (!is_a($element, BricksElement::class)) {
36
            return $settings;
37
        }
38
        foreach ($element->elementConfig() as $key => $control) {
39
            $type = $control['type'] ?? '';
40
            $options = Arr::getAs('array', $control, 'options');
41
            if ('checkbox' !== $type || empty($options)) {
42
                continue;
43
            }
44
            $values = array_filter(
45
                array_keys($settings),
46
                fn ($k) => !empty($settings[$k]) && str_starts_with($k, "{$key}_")
47
            );
48
            $settings[$key] = array_map(fn ($k) => Str::removePrefix($k, "{$key}_"), $values);
49
        }
50
        return $settings;
51
    }
52
53
    /**
54
     * Remove the "id::" prefix used to maintain javascript sorting.
55
     *
56
     * @param \Bricks\Element $element
57
     *
58
     * @filter bricks/element/settings
59
     */
60
    public function filterSettingsPrefixedId($settings, $element): array
61
    {
62
        $settings = Arr::consolidate($settings);
63
        if (!is_a($element, BricksElement::class)) {
64
            return $settings;
65
        }
66
        foreach ($settings as $key => $value) {
67
            if (is_string($value) && str_starts_with($value, 'id::')) {
68
                $settings[$key] = Str::removePrefix($value, 'id::');
69
                continue;
70
            }
71
            if (is_array($value)) {
72
                $settings[$key] = array_map(
73
                    fn ($val) => Str::removePrefix((string) $val, 'id::'),
74
                    $value
75
                );
76
            }
77
        }
78
        return $settings;
79
    }
80
81
    /**
82
     * @action wp_enqueue_scripts
83
     */
84
    public function printInlineStyles(): void
85
    {
86
        if (!function_exists('bricks_is_builder')) {
87
            return;
88
        }
89
        if (!bricks_is_builder()) {
90
            return;
91
        }
92
        $iconForm = Helper::svg('assets/images/icons/bricks/icon-form.svg', true);
93
        $iconReview = Helper::svg('assets/images/icons/bricks/icon-review.svg', true);
94
        $iconReviews = Helper::svg('assets/images/icons/bricks/icon-reviews.svg', true);
95
        $iconSummary = Helper::svg('assets/images/icons/bricks/icon-summary.svg', true);
96
        $css = "
97
            i[class^='ti-site_review']::before {
98
                background-color: currentColor;
99
                content: '';
100
                display: inline-block;
101
                height: 1em;
102
                mask-position: center;
103
                mask-repeat: no-repeat;
104
                mask-size: 100%;
105
                width: 1em;
106
            }
107
            i.ti-site_reviews_form::before {
108
                mask-image: url(\"{$iconForm}\");
109
            }
110
            i.ti-site_review::before {
111
                mask-image: url(\"{$iconReview}\");
112
            }
113
            i.ti-site_reviews::before {
114
                mask-image: url(\"{$iconReviews}\");
115
            }
116
            i.ti-site_reviews_summary::before {
117
                mask-image: url(\"{$iconSummary}\");
118
            }
119
            .glsr :is(a,button,input,textarea,select,.dz-clickable) {
120
                pointer-events: none !important;
121
            }
122
        ";
123
        wp_add_inline_style('bricks-builder', $css);
124
    }
125
126
    /**
127
     * @action init
128
     */
129
    public function registerElements(): void
130
    {
131
        if (class_exists('Bricks\Element')) {
132
            BricksSiteReview::registerElement();
133
            BricksSiteReviews::registerElement();
134
            BricksSiteReviewsForm::registerElement();
135
            BricksSiteReviewsSummary::registerElement();
136
        }
137
    }
138
139
    /**
140
     * @action wp_ajax_bricks_glsr_assigned_posts
141
     */
142
    public function searchAssignedPosts(): void
143
    {
144
        $this->verifyAjaxRequest();
145
        $query = stripslashes_deep(sanitize_text_field((string) filter_input(INPUT_GET, 'search')));
146
        $args = [
147
            'post__in' => [],
148
            'posts_per_page' => 25,
149
        ];
150
        if (is_numeric($query)) {
151
            $args['post__in'][] = (int) $query;
152
        } else {
153
            $args['s'] = $query;
154
        }
155
        $posts = glsr(Database::class)->posts($args);
156
        if ($include = $this->includedIds($posts)) {
157
            $posts += glsr(Database::class)->posts([
158
                'post__in' => $include,
159
            ]);
160
        }
161
        $results = $this->prefixedResultIds($posts);
162
        if (empty($query)) {
163
            $results = [
164
                'post_id' => esc_html_x('The Current Page', 'admin-text', 'site-reviews').' (post_id)',
165
                'parent_id' => esc_html_x('The Parent Page', 'admin-text', 'site-reviews').' (parent_id)',
166
            ] + $results;
167
        }
168
        wp_send_json_success($results);
169
    }
170
171
    /**
172
     * @action wp_ajax_bricks_glsr_assigned_terms
173
     */
174
    public function searchAssignedTerms(): void
175
    {
176
        $this->verifyAjaxRequest();
177
        $query = stripslashes_deep(sanitize_text_field((string) filter_input(INPUT_GET, 'search')));
178
        $terms = glsr(Database::class)->terms([
179
            'number' => 25,
180
            'search' => $query,
181
        ]);
182
        if ($include = $this->includedIds($terms)) {
183
            $terms += glsr(Database::class)->terms([
184
                'term_taxonomy_id' => $include,
185
            ]);
186
        }
187
        $results = $this->prefixedResultIds($terms);
188
        wp_send_json_success($results);
189
    }
190
191
    /**
192
     * @action wp_ajax_bricks_glsr_assigned_users
193
     */
194
    public function searchAssignedUsers(): void
195
    {
196
        $this->verifyAjaxRequest();
197
        $query = stripslashes_deep(sanitize_text_field((string) filter_input(INPUT_GET, 'search')));
198
        $users = glsr(Database::class)->users([
199
            'number' => 25,
200
            'search_wild' => $query,
201
        ]);
202
        if ($include = $this->includedIds($users)) {
203
            $users += glsr(Database::class)->users([
204
                'include' => $include,
205
            ]);
206
        }
207
        $results = $this->prefixedResultIds($users);
208
        if (empty($query)) {
209
            $results = [
210
                'user_id' => esc_html_x('The Logged In User', 'admin-text', 'site-reviews').' (user_id)',
211
                'author_id' => esc_html_x('The Page Author', 'admin-text', 'site-reviews').' (author_id)',
212
                'profile_id' => esc_html_x('The Profile User', 'admin-text', 'site-reviews').' (profile_id)',
213
            ] + $results;
214
        }
215
        wp_send_json_success($results);
216
    }
217
218
    /**
219
     * @action wp_ajax_bricks_glsr_post_id
220
     */
221
    public function searchPostId(): void
222
    {
223
        $this->verifyAjaxRequest();
224
        $query = stripslashes_deep(sanitize_text_field((string) filter_input(INPUT_GET, 'search')));
225
        $args = [
226
            'post__in' => [],
227
            'post_type' => glsr()->post_type,
228
            'posts_per_page' => 25,
229
        ];
230
        if (is_numeric($query)) {
231
            $args['post__in'][] = (int) $query;
232
        } else {
233
            $args['s'] = (string) $query;
234
        }
235
        $posts = glsr(Database::class)->posts($args);
236
        if ($include = $this->includedIds($posts)) {
237
            $posts += glsr(Database::class)->posts([
238
                'post__in' => $include,
239
                'post_type' => glsr()->post_type,
240
            ]);
241
        }
242
        $results = $this->prefixedResultIds($posts);
243
        wp_send_json_success($results);
244
    }
245
246
    protected function includedIds(array $results): array
247
    {
248
        $ids = filter_input(INPUT_GET, 'include', FILTER_DEFAULT, FILTER_FORCE_ARRAY) ?? [];
249
        $ids = array_map(fn ($id) => Str::removePrefix((string) $id, 'id::'), $ids);
250
        $ids = Arr::uniqueInt($ids);
251
        $ids = array_filter($ids, fn ($id) => !array_key_exists($id, $results));
252
        return $ids;
253
    }
254
255
    protected function prefixedResultIds(array $results): array
256
    {
257
        return array_combine(
258
            array_map(fn ($key) => "id::{$key}", array_keys($results)),
259
            $results
260
        );
261
    }
262
263
    protected function verifyAjaxRequest(): void
264
    {
265
        if (method_exists('Bricks\Ajax', 'verify_request')) { // @phpstan-ignore-line
266
            \Bricks\Ajax::verify_request('bricks-nonce-builder');
0 ignored issues
show
Bug introduced by
The type Bricks\Ajax was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
267
        } elseif (!check_ajax_referer('bricks-nonce-builder', 'nonce', false)) {
268
            wp_send_json_error('verify_nonce: "bricks-nonce-builder" is invalid.');
269
        }
270
    }
271
}
272