Passed
Push — develop ( 49b51d...3839fb )
by Paul
26:28
created

Controller::queryAjaxControlOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Elementor;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Database\ShortcodeOptionManager;
7
use GeminiLabs\SiteReviews\Helpers\Svg;
8
use GeminiLabs\SiteReviews\Integrations\Elementor\Controls\MultiSwitcher;
9
use GeminiLabs\SiteReviews\Integrations\Elementor\Controls\Select2Ajax;
10
use GeminiLabs\SiteReviews\Integrations\Elementor\Defaults\QueryAjaxDefaults;
11
use GeminiLabs\SiteReviews\Integrations\Elementor\Widgets\ElementorSiteReview;
12
use GeminiLabs\SiteReviews\Integrations\Elementor\Widgets\ElementorSiteReviews;
13
use GeminiLabs\SiteReviews\Integrations\Elementor\Widgets\ElementorSiteReviewsForm;
14
use GeminiLabs\SiteReviews\Integrations\Elementor\Widgets\ElementorSiteReviewsSummary;
15
16
class Controller extends AbstractController
17
{
18
    /**
19
     * @filter site-reviews/schema/generate
20
     */
21
    public function filterGeneratedSchema(array $schema): array
22
    {
23
        return empty($schema)
24
            ? glsr(SchemaParser::class)->generate()
25
            : $schema;
26
    }
27
28
    /**
29
     * Fix Star Rating control when review form is used inside an Elementor Pro Popup.
30
     *
31
     * @filter site-reviews/enqueue/public/inline-script/after
32
     */
33
    public function filterPublicInlineScript(string $script): string
34
    {
35
        if (!defined('ELEMENTOR_VERSION')) {
36
            return $script;
37
        }
38
        $inlineScript = (string) file_get_contents(glsr()->path('assets/scripts/integrations/elementor-frontend.js'));
39
        return $script.$inlineScript;
40
    }
41
42
    /**
43
     * Fix Star Rating CSS class prefix in the Elementor editor.
44
     *
45
     * @filter site-reviews/defaults/star-rating/defaults
46
     */
47 8
    public function filterStarRatingDefaults(array $defaults): array
48
    {
49 8
        if ('elementor' === filter_input(INPUT_GET, 'action')) {
50
            $defaults['prefix'] = 'glsr-';
51
        }
52 8
        return $defaults;
53
    }
54
55
    /**
56
     * @see static::registerAjaxActions()
57
     */
58
    public function queryAjaxControlOptions($data): array
59
    {
60
        $args = glsr(QueryAjaxDefaults::class)->restrict($data);
61
        $options = call_user_func([glsr(ShortcodeOptionManager::class), $args['option']], $args);
62
        if (!is_array($options)) {
63
            return [];
64
        }
65
        $callback = fn ($id, $text) => compact('id', 'text');
66
        $results = array_map($callback, array_keys($options), array_values($options));
67
        return compact('results');
68
    }
69
70
    /**
71
     * @see static::registerAjaxActions()
72
     */
73
    public function queryAjaxControlSelected($data): array
74
    {
75
        $args = glsr(QueryAjaxDefaults::class)->restrict($data);
76
        $results = call_user_func([glsr(ShortcodeOptionManager::class), $args['option']], $args);
77
        if (!is_array($results)) {
78
            return [];
79
        }
80
        if (!array_key_exists($args['include'], $results)) {
81
            return [];
82
        }
83
        return [
84
            $args['include'] => $results[$args['include']],
85
        ];
86
    }
87
88
    /**
89
     * @param \Elementor\Core\Common\Modules\Ajax\Module $manager
90
     *
91
     * @action elementor/ajax/register_actions
92
     */
93
    public function registerAjaxActions($manager): void
94
    {
95
        $manager->register_ajax_action(glsr()->prefix.'elementor_ajax_query', [$this, 'queryAjaxControlOptions']);
96
        $manager->register_ajax_action(glsr()->prefix.'elementor_ajax_query_selected', [$this, 'queryAjaxControlSelected']);
97
    }
98
99
    /**
100
     * @param \Elementor\Elements_Manager $manager
101
     *
102
     * @action elementor/elements/categories_registered
103
     */
104
    public function registerCategory($manager): void
105
    {
106
        $manager->add_category(glsr()->id, [
107
            'hideIfEmpty' => true,
108
            'title' => glsr()->name,
109
        ]);
110
    }
111
112
    /**
113
     * @param \Elementor\Controls_Manager $manager
114
     *
115
     * @action elementor/controls/register
116
     */
117
    public function registerControls($manager): void
118
    {
119
        $manager->register(new MultiSwitcher());
120
        $manager->register(new Select2Ajax());
121
    }
122
123
    /**
124
     * @action admin_enqueue_scripts
125
     * @action elementor/editor/after_enqueue_styles
126
     * @action elementor/preview/enqueue_styles
127
     */
128
    public function registerInlineStyles(): void
129
    {
130
        $icons = [
131
            'eicon-glsr-form' => Svg::encoded('assets/images/icons/elementor/icon-form.svg'),
132
            'eicon-glsr-review' => Svg::encoded('assets/images/icons/elementor/icon-review.svg'),
133
            'eicon-glsr-reviews' => Svg::encoded('assets/images/icons/elementor/icon-reviews.svg'),
134
            'eicon-glsr-summary' => Svg::encoded('assets/images/icons/elementor/icon-summary.svg'),
135
        ];
136
        $maskRules = '';
137
        foreach ($icons as $class => $url) {
138
            $maskRules .= "i.{$class}::before { mask-image: url(\"{$url}\"); }";
139
        }
140
        $css = <<<CSS
141
            i[class^="eicon-glsr-"]::before {
142
                background-color: currentColor;
143
                content: '.';
144
                display: block;
145
                mask-repeat: no-repeat;
146
                mask-size: contain;
147
                width: 1em;
148
            }
149
            {$maskRules}
150
            .elementor-nerd-box-icon[src$="assets/images/premium.svg"] {
151
                width: 240px;
152
            }
153
        CSS;
154
        $css = preg_replace('/\s+/', ' ', $css);
155
        wp_add_inline_style('elementor-admin', $css);
156
        wp_add_inline_style('elementor-editor', $css);
157
        wp_add_inline_style('elementor-frontend',
158
            $css."[class*=\"eicon-glsr-\"]::before{font-size:28px;margin:0 auto}"
159
        );
160
    }
161
162
    /**
163
     * @param \Elementor\Widgets_Manager $manager
164
     *
165
     * @action elementor/widgets/register
166
     */
167
    public function registerWidgets($manager): void
168
    {
169
        $manager->register(new ElementorSiteReview());
170
        $manager->register(new ElementorSiteReviews());
171
        $manager->register(new ElementorSiteReviewsForm());
172
        $manager->register(new ElementorSiteReviewsSummary());
173
    }
174
}
175