Passed
Push — develop ( 7073e7...becc1e )
by Paul
13:56
created

Controller::filterPostIdCallback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.9666
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\WPBakery;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Database\ShortcodeOptionManager;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Helpers\Cast;
9
use GeminiLabs\SiteReviews\Modules\Html\Builder;
10
11
class Controller extends AbstractController
12
{
13
    /**
14
     * @action vc_frontend_editor_enqueue_js_css
15
     */
16
    public function enqueueInlineScripts(): void
17
    {
18
        $script = file_get_contents(glsr()->path('assets/scripts/integrations/wpbakery-inline.js'));
19
        wp_add_inline_script('vc-frontend-editor-min-js', $script);
20
    }
21
22
    /**
23
     * @action vc_backend_editor_enqueue_js_css
24
     * @action vc_frontend_editor_enqueue_js_css
25
     */
26
    public function enqueueInlineStyles(): void
27
    {
28
        $css = file_get_contents(glsr()->path('assets/styles/integrations/wpbakery-inline.css'));
29
        $keys = [
30
            'site_review' => 'review',
31
            'site_reviews' => 'reviews',
32
            'site_reviews_form' => 'form',
33
            'site_reviews_summary' => 'summary',
34
        ];
35
        foreach ($keys as $shortcode => $icon) {
36
            $css .= sprintf('#%s .vc_element-icon{background-image:url(%s);}',
37
                $shortcode,
38
                glsr()->url("assets/images/icons/wpbakery/icon-{$icon}.svg")
39
            );
40
        }
41
        wp_add_inline_style('js_composer', $css);
42
        wp_add_inline_style('vc_inline_css', $css);
43
    }
44
45
    /**
46
     * @filter vc_autocomplete_site_reviews_assigned_posts_callback
47
     * @filter vc_autocomplete_site_reviews_field_assigned_posts_callback
48
     * @filter vc_autocomplete_site_reviews_form_assigned_posts_callback
49
     * @filter vc_autocomplete_site_reviews_images_assigned_posts_callback
50
     * @filter vc_autocomplete_site_reviews_summary_assigned_posts_callback
51
     */
52
    public function filterAssignedPostsCallback(string $query, string $shortcodeTag): array
53
    {
54
        $args = is_numeric($query) ? ['include' => $query] : ['search' => $query];
55
        $options = glsr(ShortcodeOptionManager::class)->assigned_posts(wp_parse_args($args, [
56
            'shortcode' => stripslashes($shortcodeTag),
57
        ]));
58
        return array_map(
59
            fn ($value, $label) => [
60
                'label' => is_numeric($value) ? sprintf('ID: %s - %s', $value, $label) : $label,
61
                'value' => $value,
62
            ],
63
            array_keys($options),
64
            array_values($options)
65
        );
66
    }
67
68
    /**
69
     * @filter vc_autocomplete_site_reviews_assigned_posts_render
70
     * @filter vc_autocomplete_site_reviews_field_assigned_posts_render
71
     * @filter vc_autocomplete_site_reviews_form_assigned_posts_render
72
     * @filter vc_autocomplete_site_reviews_images_assigned_posts_render
73
     * @filter vc_autocomplete_site_reviews_summary_assigned_posts_render
74
     */
75
    public function filterAssignedPostsRender(array $query, array $settings, string $shortcodeTag): ?array
76
    {
77
        $value = $query['value'] ?? '';
78
        if (empty($value)) {
79
            return null;
80
        }
81
        $options = glsr(ShortcodeOptionManager::class)->assigned_posts([
82
            'per_page' => 1,
83
            'include' => $value,
84
            'shortcode' => stripslashes($shortcodeTag),
85
        ]);
86
        if (!isset($options[$value])) {
87
            return $query;
88
        }
89
        return [
90
            'label' => is_numeric($value) ? sprintf('ID: %s - %s', $value, $options[$value]) : $options[$value],
91
            'value' => $value,
92
        ];
93
    }
94
95
    /**
96
     * @filter vc_autocomplete_site_reviews_assigned_term_callback
97
     * @filter vc_autocomplete_site_reviews_field_assigned_term_callback
98
     * @filter vc_autocomplete_site_reviews_form_assigned_term_callback
99
     * @filter vc_autocomplete_site_reviews_images_assigned_term_callback
100
     * @filter vc_autocomplete_site_reviews_summary_assigned_term_callback
101
     */
102
    public function filterAssignedTermsCallback(string $query, string $shortcodeTag): array
103
    {
104
        $options = glsr(ShortcodeOptionManager::class)->assigned_terms([
105
            'search' => $query,
106
            'shortcode' => stripslashes($shortcodeTag),
107
        ]);
108
        return array_map(
109
            fn ($value, $label) => [
110
                'label' => sprintf('ID: %s - %s', $value, $label),
111
                'value' => $value,
112
            ],
113
            array_keys($options),
114
            array_values($options)
115
        );
116
    }
117
118
    /**
119
     * @filter vc_autocomplete_site_reviews_assigned_term_render
120
     * @filter vc_autocomplete_site_reviews_field_assigned_term_render
121
     * @filter vc_autocomplete_site_reviews_form_assigned_term_render
122
     * @filter vc_autocomplete_site_reviews_images_assigned_term_render
123
     * @filter vc_autocomplete_site_reviews_summary_assigned_term_render
124
     */
125
    public function filterAssignedTermsRender(array $query, array $settings, string $shortcodeTag): ?array
126
    {
127
        $value = $query['value'] ?? '';
128
        if (empty($value)) {
129
            return null;
130
        }
131
        $options = glsr(ShortcodeOptionManager::class)->assigned_terms([
132
            'per_page' => 1,
133
            'search' => $value,
134
            'shortcode' => stripslashes($shortcodeTag),
135
        ]);
136
        if (!isset($options[$value])) {
137
            return $query;
138
        }
139
        return [
140
            'label' => sprintf('ID: %s - %s', $value, $options[$value]),
141
            'value' => $value,
142
        ];
143
    }
144
145
    /**
146
     * @filter vc_autocomplete_site_reviews_assigned_users_callback
147
     * @filter vc_autocomplete_site_reviews_form_assigned_users_callback
148
     * @filter vc_autocomplete_site_reviews_summary_assigned_users_callback
149
     */
150
    public function filterAssignedUsersCallback(string $query, string $shortcodeTag): array
151
    {
152
        $options = glsr(ShortcodeOptionManager::class)->assigned_users([
153
            'search' => $query,
154
            'shortcode' => stripslashes($shortcodeTag),
155
        ]);
156
        return array_map(
157
            fn ($value, $label) => [
158
                'label' => is_numeric($value) ? sprintf('ID: %s - %s', $value, $label) : $label,
159
                'value' => $value,
160
            ],
161
            array_keys($options),
162
            array_values($options)
163
        );
164
    }
165
166
    /**
167
     * @filter vc_autocomplete_site_reviews_assigned_users_render
168
     * @filter vc_autocomplete_site_reviews_field_assigned_users_render
169
     * @filter vc_autocomplete_site_reviews_form_assigned_users_render
170
     * @filter vc_autocomplete_site_reviews_images_assigned_users_render
171
     * @filter vc_autocomplete_site_reviews_summary_assigned_users_render
172
     */
173
    public function filterAssignedUsersRender(array $query, array $settings, string $shortcodeTag): ?array
174
    {
175
        $value = $query['value'] ?? '';
176
        if (empty($value)) {
177
            return null;
178
        }
179
        $options = glsr(ShortcodeOptionManager::class)->assigned_users([
180
            'per_page' => 1,
181
            'include' => $value,
182
            'shortcode' => stripslashes($shortcodeTag),
183
        ]);
184
        if (!isset($options[$value])) {
185
            return $query;
186
        }
187
        return [
188
            'label' => is_numeric($value) ? sprintf('ID: %s - %s', $value, $options[$value]) : $options[$value],
189
            'value' => $value,
190
        ];
191
    }
192
193
    /**
194
     * @filter vc_autocomplete_site_reviews_assigned_users_callback
195
     * @filter vc_autocomplete_site_reviews_form_assigned_users_callback
196
     * @filter vc_autocomplete_site_reviews_summary_assigned_users_callback
197
     */
198
    public function filterAuthorCallback(string $query, string $shortcodeTag): array
199
    {
200
        $options = glsr(ShortcodeOptionManager::class)->author([
201
            'search' => $query,
202
            'shortcode' => stripslashes($shortcodeTag),
203
        ]);
204
        return array_map(
205
            fn ($value, $label) => [
206
                'label' => is_numeric($value) ? sprintf('ID: %s - %s', $value, $label) : $label,
207
                'value' => $value,
208
            ],
209
            array_keys($options),
210
            array_values($options)
211
        );
212
    }
213
214
    /**
215
     * @filter vc_autocomplete_site_reviews_author_render
216
     * @filter vc_autocomplete_site_reviews_field_author_render
217
     * @filter vc_autocomplete_site_reviews_form_author_render
218
     * @filter vc_autocomplete_site_reviews_images_author_render
219
     * @filter vc_autocomplete_site_reviews_summary_author_render
220
     */
221
    public function filterAuthorRender(array $query, array $settings, string $shortcodeTag): ?array
222
    {
223
        $value = $query['value'] ?? '';
224
        if (empty($value)) {
225
            return null;
226
        }
227
        $options = glsr(ShortcodeOptionManager::class)->author([
228
            'per_page' => 1,
229
            'include' => $value,
230
            'shortcode' => stripslashes($shortcodeTag),
231
        ]);
232
        if (!isset($options[$value])) {
233
            return $query;
234
        }
235
        return [
236
            'label' => is_numeric($value) ? sprintf('ID: %s - %s', $value, $options[$value]) : $options[$value],
237
            'value' => $value,
238
        ];
239
    }
240
241
    /**
242
     * @filter site-reviews/modal_wrapped_by
243
     */
244
    public function filterModalWrappedBy(array $builders): array
245
    {
246
        $builders[] = 'wpbakery';
247
        return $builders;
248
    }
249
250
    /**
251
     * @filter vc_autocomplete_site_review_post_id_callback
252
     */
253
    public function filterPostIdCallback(string $query, string $shortcodeTag): array
254
    {
255
        $args = is_numeric($query) ? ['include' => $query] : ['search' => $query];
256
        $options = glsr(ShortcodeOptionManager::class)->post_id(wp_parse_args($args, [
257
            'shortcode' => stripslashes($shortcodeTag),
258
        ]));
259
        return array_map(
260
            fn ($value, $label) => [
261
                'label' => sprintf('ID: %s - %s', $value, $label),
262
                'value' => $value,
263
            ],
264
            array_keys($options),
265
            array_values($options)
266
        );
267
    }
268
269
    /**
270
     * @filter vc_autocomplete_site_review_post_id_render
271
     */
272
    public function filterPostIdRender(array $query, array $settings, string $shortcodeTag): ?array
273
    {
274
        $value = $query['value'] ?? '';
275
        if (empty($value)) {
276
            return null;
277
        }
278
        $options = glsr(ShortcodeOptionManager::class)->post_id([
279
            'per_page' => 1,
280
            'include' => $value,
281
            'shortcode' => stripslashes($shortcodeTag),
282
        ]);
283
        if (!isset($options[$value])) {
284
            return $query;
285
        }
286
        return [
287
            'label' => sprintf('ID: %s - %s', $value, $options[$value]),
288
            'value' => $value,
289
        ];
290
    }
291
292
    /**
293
     * @action vc_before_init
294
     */
295
    public function registerShortcodes(): void
296
    {
297
        Shortcodes\VcSiteReview::vcRegister();
298
        Shortcodes\VcSiteReviews::vcRegister();
299
        Shortcodes\VcSiteReviewsForm::vcRegister();
300
        Shortcodes\VcSiteReviewsSummary::vcRegister();
301
    }
302
303
    /**
304
     * @action vc_load_default_params
305
     */
306
    public function registerParameters(): void
307
    {
308
        vc_add_shortcode_param('glsr_type_range', [$this, 'renderFieldRange'], glsr()->url('assets/scripts/integrations/wpbakery-editor.js'));
309
    }
310
311
    /**
312
     * @param mixed  $settings
313
     * @param string $value
314
     *
315
     * @callback vc_add_shortcode_param
316
     */
317
    public function renderFieldRange($settings, $value): string
318
    {
319
        $max = Arr::get($settings, 'max');
320
        $min = Arr::get($settings, 'min');
321
        $step = Arr::get($settings, 'step');
322
        if ('' !== $max) {
323
            $max = Cast::toInt($max);
324
        }
325
        if ('' !== $min) {
326
            $min = Cast::toInt($min);
327
        }
328
        if ('' !== $step) {
329
            $step = Cast::toInt($step);
330
        }
331
        $input = glsr(Builder::class)->input([
332
            'class' => "wpb_vc_param_value wpb-textinput",
333
            'max' => $max,
334
            'min' => $min,
335
            'name' => $settings['param_name'],
336
            'step' => $step,
337
            'type' => 'range',
338
            'value' => sanitize_text_field(Cast::toString($value)),
339
        ]);
340
        $info = glsr(Builder::class)->input([
341
            'class' => 'wpb_vc_param_infobox',
342
            'max' => $max,
343
            'min' => $min,
344
            'step' => $step,
345
            'type' => 'number',
346
            'value' => sanitize_text_field(Cast::toString($value)),
347
        ]);
348
        return glsr(Builder::class)->div([
349
            'class' => "{$settings['type']}-wrapper",
350
            'text' => $info.$input,
351
        ]);
352
    }
353
}
354