Test Failed
Push — develop ( 6c74c3...4ba53a )
by Paul
08:25
created

Controller::interceptGetPostsQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 20
rs 9.7666
cc 3
nc 3
nop 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Flatsome;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Database\ShortcodeOptionManager;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Helpers\Str;
9
10
class Controller extends AbstractController
11
{
12
    /**
13
     * @action wp_ajax_ux_builder_get_posts:1
14
     */
15
    public function interceptGetPostsQuery(): void
16
    {
17
        $option = filter_input(INPUT_GET, 'option');
18
        if (!str_starts_with($option, glsr()->prefix)) {
19
            return;
20
        }
21
        $option = Str::removePrefix($option, glsr()->prefix);
22
        $postId = (string) filter_input(INPUT_GET, 'id');
23
        check_ajax_referer("ux-builder-{$postId}", 'security');
24
        $values = filter_input(INPUT_GET, 'values', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
25
        if (!is_array($values)) {
26
            $values = [filter_input(INPUT_GET, 'values')];
27
        }
28
        $items = call_user_func([glsr(ShortcodeOptionManager::class), $option], [
29
            'include' => array_filter($values),
30
        ]);
31
        $items = array_filter($items, fn ($id) => in_array($id, $values), \ARRAY_FILTER_USE_KEY);
32
        $callback = fn ($id, $title) => compact('id', 'title');
33
        $results = array_map($callback, array_keys($items), array_values($items));
34
        wp_send_json_success($results);
35
    }
36
37
    /**
38
     * @action wp_ajax_ux_builder_search_posts:1
39
     */
40
    public function interceptSearchPostsQuery(): void
41
    {
42
        $option = filter_input(INPUT_GET, 'option');
43
        if (!str_starts_with($option, glsr()->prefix)) {
44
            return;
45
        }
46
        $option = Str::removePrefix($option, glsr()->prefix);
47
        $postId = (string) filter_input(INPUT_GET, 'id');
48
        check_ajax_referer("ux-builder-{$postId}", 'security');
49
        $query = filter_input(INPUT_GET, 'query');
50
        $items = call_user_func([glsr(ShortcodeOptionManager::class), $option], [
51
            'search' => $query,
52
        ]);
53
        $callback = fn ($id, $title) => compact('id', 'title');
54
        $results = array_map($callback, array_keys($items), array_values($items));
55
        wp_send_json_success($results);
56
    }
57
58
    /**
59
     * @action ux_builder_enqueue_scripts
60
     */
61
    public function printInlineScripts(string $page = ''): void
62
    {
63
        if ('editor' === $page) {
64
            $script = file_get_contents(glsr()->path('assets/scripts/integrations/flatsome-inline.js'));
65
            wp_add_inline_script('ux-builder-core', $script);
66
        }
67
    }
68
69
    /**
70
     * @action ux_builder_enqueue_scripts
71
     */
72
    public function printInlineStyles(string $page = ''): void
73
    {
74
        if ('editor' === $page) {
75
            $css = file_get_contents(glsr()->path('assets/styles/integrations/flatsome-inline.css'));
76
            wp_add_inline_style('ux-builder-core', $css);
77
        } else {
78
            // This fixes JS errors on mouseover events
79
            wp_add_inline_style('ux-builder-core', '.uxb-draggable>.glsr{pointer-events:none;}');
80
        }
81
    }
82
83
    /**
84
     * @action init
85
     */
86
    public function registerShortcodes(): void
87
    {
88
        glsr(FlatsomeSiteReview::class)->register();
89
        glsr(FlatsomeSiteReviews::class)->register();
90
        glsr(FlatsomeSiteReviewsForm::class)->register();
91
        glsr(FlatsomeSiteReviewsSummary::class)->register();
92
    }
93
}
94