|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Integrations\Elementor; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Controllers\AbstractController; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Request; |
|
9
|
|
|
|
|
10
|
|
|
class Controller extends AbstractController |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Fix Star Rating control when review form is used inside an Elementor Pro Popup. |
|
14
|
|
|
* |
|
15
|
|
|
* @filter site-reviews/enqueue/public/inline-script/after |
|
16
|
|
|
*/ |
|
17
|
|
|
public function filterElementorPublicInlineScript(string $script): string |
|
18
|
|
|
{ |
|
19
|
|
|
if (!defined('ELEMENTOR_VERSION')) { |
|
20
|
|
|
return $script; |
|
21
|
|
|
} |
|
22
|
|
|
$inlineScript = (string) file_get_contents(glsr()->path('assets/scripts/integrations/elementor-frontend.js')); |
|
23
|
|
|
return $script.$inlineScript; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Fix Star Rating CSS class prefix in the Elementor editor. |
|
28
|
|
|
* |
|
29
|
|
|
* @filter site-reviews/defaults/star-rating/defaults |
|
30
|
|
|
*/ |
|
31
|
|
|
public function filterElementorStarRatingDefaults(array $defaults): array |
|
32
|
|
|
{ |
|
33
|
|
|
if ('elementor' === filter_input(INPUT_GET, 'action')) { |
|
34
|
|
|
$defaults['prefix'] = 'glsr-'; |
|
35
|
|
|
} |
|
36
|
|
|
return $defaults; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @filter site-reviews/schema/generate |
|
41
|
|
|
*/ |
|
42
|
|
|
public function filterGeneratedSchema(array $schema): array |
|
43
|
|
|
{ |
|
44
|
|
|
return empty($schema) |
|
45
|
|
|
? glsr(SchemaParser::class)->generate() |
|
46
|
|
|
: $schema; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param \Elementor\Core\Files\CSS\Post $cssFile |
|
51
|
|
|
* @param \Elementor\Element_Base $element |
|
52
|
|
|
* |
|
53
|
|
|
* @action elementor/element/parse_css |
|
54
|
|
|
*/ |
|
55
|
|
|
public function parseElementCss($cssFile, $element): void |
|
56
|
|
|
{ |
|
57
|
|
|
$shortcode = $element->get_name(); |
|
58
|
|
|
$shortcodes = [ |
|
59
|
|
|
'site_review', |
|
60
|
|
|
'site_reviews', |
|
61
|
|
|
'site_reviews_form', |
|
62
|
|
|
'site_reviews_summary', |
|
63
|
|
|
]; |
|
64
|
|
|
if (!in_array($shortcode, $shortcodes)) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
$ratingColor = $element->get_settings('rating_color') ?: ($element->get_settings('__globals__')['rating_color'] ?? ''); |
|
68
|
|
|
if (empty($ratingColor)) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
$selector = "{$cssFile->get_element_unique_selector($element)} .glsr:not([data-theme])"; |
|
72
|
|
|
$stylesheet = $cssFile->get_stylesheet(); |
|
73
|
|
|
$fn = fn ($variable) => [ |
|
74
|
|
|
'mask-image' => $variable, |
|
75
|
|
|
'mask-size' => '100%', |
|
76
|
|
|
]; |
|
77
|
|
|
$stars = [ |
|
78
|
|
|
'empty' => 'var(--glsr-star-empty)', |
|
79
|
|
|
'error' => 'var(--glsr-star-error)', |
|
80
|
|
|
'full' => 'var(--glsr-star-full)', |
|
81
|
|
|
'half' => 'var(--glsr-star-half)', |
|
82
|
|
|
]; |
|
83
|
|
|
if (in_array($shortcode, ['site_review', 'site_reviews'])) { |
|
84
|
|
|
$stylesheet->add_rules("{$selector} .glsr-review .glsr-star-empty", $fn($stars['empty'])); |
|
85
|
|
|
$stylesheet->add_rules("{$selector} .glsr-review .glsr-star-full", $fn($stars['full'])); |
|
86
|
|
|
$stylesheet->add_rules("{$selector} .glsr-review .glsr-star-half", $fn($stars['half'])); |
|
87
|
|
|
} elseif ('site_reviews_form' === $shortcode) { |
|
88
|
|
|
$stylesheet->add_rules("{$selector} .glsr-field:not(.glsr-field-is-invalid) .glsr-star-rating--stars > span", $fn($stars['empty'])); |
|
89
|
|
|
$stylesheet->add_rules("{$selector} .glsr-field:not(.glsr-field-is-invalid) .glsr-star-rating--stars > span:is(.gl-active,.gl-selected)", $fn($stars['full'])); |
|
90
|
|
|
$stylesheet->add_rules("{$selector} .glsr-field-is-invalid .glsr-star-rating--stars > span.gl-active", $fn($stars['error'])); |
|
91
|
|
|
} elseif ('site_reviews_summary' === $shortcode) { |
|
92
|
|
|
$stylesheet->add_rules("{$selector} .glsr-star-empty", $fn($stars['empty'])); |
|
93
|
|
|
$stylesheet->add_rules("{$selector} .glsr-star-full", $fn($stars['full'])); |
|
94
|
|
|
$stylesheet->add_rules("{$selector} .glsr-star-half", $fn($stars['half'])); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @callback \Elementor\Core\Common\Modules\Ajax\Module::register_ajax_action |
|
100
|
|
|
*/ |
|
101
|
|
|
public function queryAssignedPosts(array $data): array |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
return [ |
|
104
|
|
|
'results' => [], |
|
105
|
|
|
]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @callback \Elementor\Core\Common\Modules\Ajax\Module::register_ajax_action |
|
110
|
|
|
*/ |
|
111
|
|
|
public function queryAssignedTerms(array $data): array |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
return [ |
|
114
|
|
|
'results' => [], |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @callback \Elementor\Core\Common\Modules\Ajax\Module::register_ajax_action |
|
120
|
|
|
*/ |
|
121
|
|
|
public function queryAssignedUsers(array $data): array |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
return [ |
|
124
|
|
|
'results' => [], |
|
125
|
|
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param \Elementor\Core\Common\Modules\Ajax\Module $manager |
|
130
|
|
|
* |
|
131
|
|
|
* @action elementor/ajax/register_actions |
|
132
|
|
|
*/ |
|
133
|
|
|
public function registerAjaxActions($manager): void |
|
134
|
|
|
{ |
|
135
|
|
|
$manager->register_ajax_action(glsr()->prefix.'query_assigned_posts', [$this, 'queryAssignedPosts']); |
|
136
|
|
|
$manager->register_ajax_action(glsr()->prefix.'query_assigned_terms', [$this, 'queryAssignedTerms']); |
|
137
|
|
|
$manager->register_ajax_action(glsr()->prefix.'query_assigned_users', [$this, 'queryAssignedUsers']); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param \Elementor\Elements_Manager $manager |
|
142
|
|
|
* |
|
143
|
|
|
* @action elementor/elements/categories_registered |
|
144
|
|
|
*/ |
|
145
|
|
|
public function registerElementorCategory($manager): void |
|
146
|
|
|
{ |
|
147
|
|
|
$manager->add_category(glsr()->id, [ |
|
148
|
|
|
'title' => glsr()->name, |
|
149
|
|
|
'icon' => 'eicon-star-o', // default icon |
|
150
|
|
|
]); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param \Elementor\Widgets_Manager $manager |
|
155
|
|
|
* |
|
156
|
|
|
* @action elementor/widgets/register |
|
157
|
|
|
*/ |
|
158
|
|
|
public function registerElementorWidgets($manager): void |
|
159
|
|
|
{ |
|
160
|
|
|
$manager->register(new ElementorFormWidget()); |
|
161
|
|
|
$manager->register(new ElementorReviewsWidget()); |
|
162
|
|
|
$manager->register(new ElementorReviewWidget()); |
|
163
|
|
|
$manager->register(new ElementorSummaryWidget()); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @action admin_enqueue_scripts |
|
168
|
|
|
* @action elementor/editor/after_enqueue_styles |
|
169
|
|
|
* @action elementor/preview/enqueue_styles |
|
170
|
|
|
*/ |
|
171
|
|
|
public function registerInlineStyles(): void |
|
172
|
|
|
{ |
|
173
|
|
|
$iconForm = Helper::svg('assets/images/icons/elementor/icon-form.svg', true); |
|
174
|
|
|
$iconReview = Helper::svg('assets/images/icons/elementor/icon-review.svg', true); |
|
175
|
|
|
$iconReviews = Helper::svg('assets/images/icons/elementor/icon-reviews.svg', true); |
|
176
|
|
|
$iconSummary = Helper::svg('assets/images/icons/elementor/icon-summary.svg', true); |
|
177
|
|
|
$css = " |
|
178
|
|
|
[class*=\"eicon-glsr-\"]::before { |
|
179
|
|
|
background-color: currentColor; |
|
180
|
|
|
content: '.'; |
|
181
|
|
|
display: block; |
|
182
|
|
|
mask-repeat: no-repeat; |
|
183
|
|
|
mask-size: contain; |
|
184
|
|
|
width: 1em; |
|
185
|
|
|
} |
|
186
|
|
|
.eicon-glsr-form::before { |
|
187
|
|
|
mask-image: url(\"{$iconForm}\"); |
|
188
|
|
|
} |
|
189
|
|
|
.eicon-glsr-review::before { |
|
190
|
|
|
mask-image: url(\"{$iconReview}\"); |
|
191
|
|
|
} |
|
192
|
|
|
.eicon-glsr-reviews::before { |
|
193
|
|
|
mask-image: url(\"{$iconReviews}\"); |
|
194
|
|
|
} |
|
195
|
|
|
.eicon-glsr-summary::before { |
|
196
|
|
|
mask-image: url(\"{$iconSummary}\"); |
|
197
|
|
|
} |
|
198
|
|
|
"; |
|
199
|
|
|
wp_add_inline_style('elementor-admin', $css); |
|
200
|
|
|
wp_add_inline_style('elementor-editor', $css); |
|
201
|
|
|
wp_add_inline_style('elementor-frontend', $css." |
|
202
|
|
|
[class*=\"eicon-glsr-\"]::before { |
|
203
|
|
|
font-size: 28px; |
|
204
|
|
|
margin: 0 auto; |
|
205
|
|
|
} |
|
206
|
|
|
"); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @action elementor/editor/after_enqueue_scripts |
|
211
|
|
|
*/ |
|
212
|
|
|
public function registerScripts(): void |
|
213
|
|
|
{ |
|
214
|
|
|
wp_enqueue_script( |
|
215
|
|
|
glsr()->id.'/elementor', |
|
216
|
|
|
glsr()->url('assets/scripts/integrations/elementor-editor.js'), |
|
217
|
|
|
[], |
|
218
|
|
|
glsr()->version, |
|
219
|
|
|
['strategy' => 'defer'] |
|
220
|
|
|
); |
|
221
|
|
|
wp_localize_script(glsr()->id.'/elementor', 'GLSR', [ |
|
222
|
|
|
'action' => glsr()->prefix.'admin_action', |
|
223
|
|
|
'nameprefix' => glsr()->id, |
|
224
|
|
|
'nonce' => [ |
|
225
|
|
|
'elementor-assigned_posts' => wp_create_nonce('elementor-assigned_posts'), |
|
226
|
|
|
'elementor-assigned_terms' => wp_create_nonce('elementor-assigned_terms'), |
|
227
|
|
|
'elementor-assigned_users' => wp_create_nonce('elementor-assigned_users'), |
|
228
|
|
|
], |
|
229
|
|
|
]); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @action site-reviews/route/ajax/elementor-assigned_terms |
|
234
|
|
|
*/ |
|
235
|
|
|
public function searchAssignedTerms(Request $request): void |
|
236
|
|
|
{ |
|
237
|
|
|
$search = $request->cast('search', 'string'); |
|
238
|
|
|
$include = $request->cast('include', 'array'); |
|
239
|
|
|
$query = stripslashes_deep(sanitize_text_field($search)); |
|
240
|
|
|
$terms = glsr(Database::class)->terms([ |
|
241
|
|
|
'number' => 25, |
|
242
|
|
|
'search' => $query, |
|
243
|
|
|
]); |
|
244
|
|
|
if (!empty($include)) { |
|
245
|
|
|
$terms += glsr(Database::class)->terms([ |
|
246
|
|
|
'term_taxonomy_id' => $include, |
|
247
|
|
|
]); |
|
248
|
|
|
} |
|
249
|
|
|
$results = []; |
|
250
|
|
|
foreach ($terms as $id => $text) { |
|
251
|
|
|
$results[] = compact('id', 'text'); |
|
252
|
|
|
} |
|
253
|
|
|
wp_send_json_success($results); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.