PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.9.1
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.9.1
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / blocks / search / block.php

block.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.9.1, at includes/blocks/search/block.php

429 lines 15.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ABlocks\Blocks\Search;
3
4 use ABlocks\Classes\BlockBaseAbstract;
5 use ABlocks\Classes\CssGenerator;
6 use ABlocks\Controls\Icon;
7 use ABlocks\Controls\Alignment;
8 use ABlocks\Controls\Typography;
9 use ABlocks\Controls\TextShadow;
10 use ABlocks\Controls\TextStroke;
11 use ABlocks\Controls\Dimensions;
12 use ABlocks\Controls\Border;
13 use ABlocks\Controls\Range;
14 use WP_Query;
15
16 class Block extends BlockBaseAbstract {
17 protected $block_name = 'search';
18
19 public function build_css( $attributes ) {
20 $css_generator = new CssGenerator( $attributes, $this->block_name );
21
22 $css_generator->add_class_styles(
23 '{{WRAPPER}} .ablocks-block--search-form',
24 $this->get_SearchBar_css( $attributes ),
25 $this->get_SearchBar_css( $attributes, 'Tablet' ),
26 $this->get_SearchBar_css( $attributes, 'Mobile' )
27 );
28
29 $css_generator->add_class_styles(
30 '{{WRAPPER}} .ablocks-block--search-input',
31 $this->get_Input_css( $attributes ),
32 $this->get_Input_css( $attributes, 'Tablet' ),
33 $this->get_Input_css( $attributes, 'Mobile' )
34 );
35
36 $css_generator->add_class_styles(
37 '{{WRAPPER}} .ablocks-block--search-input::placeholder',
38 $this->get_Input_css( $attributes ),
39 );
40
41 $css_generator->add_class_styles(
42 '{{WRAPPER}} .ablocks-block--search-button > span',
43 $this->get_Button_css( $attributes ),
44 $this->get_Button_css( $attributes, 'Tablet' ),
45 $this->get_Button_css( $attributes, 'Mobile' )
46 );
47
48 $css_generator->add_class_styles(
49 '{{WRAPPER}} .ablocks-block--search-button > span > svg',
50 $this->get_Icon_css( $attributes ),
51 $this->get_Icon_css( $attributes, 'Tablet' ),
52 $this->get_Icon_css( $attributes, 'Mobile' )
53 );
54 $css_generator->add_class_styles(
55 '{{WRAPPER}} .ablocks-block--search-result',
56 $this->get_search_result_list( $attributes ),
57 $this->get_search_result_list( $attributes, 'Tablet' ),
58 $this->get_search_result_list( $attributes, 'Mobile' )
59 );
60 $css_generator->add_class_styles(
61 '{{WRAPPER}} .ablocks-block--search-result:hover',
62 $this->get_search_result_list_hover( $attributes ),
63 $this->get_search_result_list_hover( $attributes, 'Tablet' ),
64 $this->get_search_result_list_hover( $attributes, 'Mobile' )
65 );
66 $css_generator->add_class_styles(
67 '{{WRAPPER}} .ablocks-block--search-result__list',
68 $this->get_search_result_item( $attributes ),
69 $this->get_search_result_item( $attributes, 'Tablet' ),
70 $this->get_search_result_item( $attributes, 'Mobile' )
71 );
72 $css_generator->add_class_styles(
73 '{{WRAPPER}} .ablocks-block--search-result__list:hover',
74 $this->get_search_result_item_hover( $attributes ),
75 $this->get_search_result_item_hover( $attributes, 'Tablet' ),
76 $this->get_search_result_item_hover( $attributes, 'Mobile' )
77 );
78 $css_generator->add_class_styles(
79 '{{WRAPPER}} .ablocks-block--search-result__list-thumbnail',
80 $this->get_search_result_img_css( $attributes ),
81 $this->get_search_result_img_css( $attributes, 'Tablet' ),
82 $this->get_search_result_img_css( $attributes, 'Mobile' )
83 );
84 $css_generator->add_class_styles(
85 '{{WRAPPER}} a.ablocks-block--search-result__list-title',
86 $this->get_search_result_title_css( $attributes ),
87 $this->get_search_result_title_css( $attributes, 'Tablet' ),
88 $this->get_search_result_title_css( $attributes, 'Mobile' )
89 );
90 $css_generator->add_class_styles(
91 '{{WRAPPER}} .ablocks-search-block__spin',
92 $this->get_loading_spinner_css( $attributes ),
93 $this->get_loading_spinner_css( $attributes, 'Tablet' ),
94 $this->get_loading_spinner_css( $attributes, 'Mobile' )
95 );
96
97 return $css_generator->generate_css();
98 }
99 public function get_SearchBar_css( $attributes, $device = '' ) {
100
101 return array_merge(
102 Range::get_css([
103 'attributeValue' => $attributes['gap'],
104 'attribute_object_key' => 'value',
105 'isResponsive' => true,
106 'defaultValue' => 0,
107 'unitDefaultValue' => 'px',
108 'hasUnit' => true,
109 'property' => 'gap',
110 'device' => $device,
111 ]),
112 isset( $attributes['fullscreenButtonAlignment'] ) ? Alignment::get_css( $attributes['fullscreenButtonAlignment'], 'justify-content', $device ) : [],
113 );
114 }
115
116 public function get_Input_css( $attributes, $device = '' ) {
117 $css = [];
118 $input_color = isset( $attributes['inputTextColor'] ) ? $attributes['inputTextColor'] : '';
119
120 if ( isset( $attributes['inputTextColor'] ) && ! empty( $attributes['inputTextColor'] ) ) {
121 $css['color'] = $input_color;
122 }
123
124 return array_merge(
125 $css,
126 isset( $attributes['inputTypography'] ) ? Typography::get_css( $attributes['inputTypography'], '', $device ) : [],
127 isset( $attributes['inputTextStroke'] ) ? TextStroke::get_css( $attributes['inputTextStroke'], '', $device ) : [],
128 isset( $attributes['inputTextShadow'] ) ? TextShadow::get_css( $attributes['inputTextShadow'], '', $device ) : [],
129 );
130 }
131 public function get_loading_spinner_css( $attributes, $device = '' ) {
132 $css = [];
133 $loadingSpinnerColor = isset( $attributes['loadingSpinnerColor'] ) ? $attributes['loadingSpinnerColor'] : '';
134
135 if ( isset( $attributes['loadingSpinnerColor'] ) && ! empty( $attributes['loadingSpinnerColor'] ) ) {
136 $css['color'] = $loadingSpinnerColor;
137 }
138
139 return array_merge(
140 $css,
141 );
142 }
143 public function get_search_result_title_css( $attributes, $device = '' ) {
144 $css = [];
145 $searchResTColor = isset( $attributes['searchResTColor'] ) ? $attributes['searchResTColor'] : '';
146
147 if ( isset( $attributes['searchResTColor'] ) && ! empty( $attributes['searchResTColor'] ) ) {
148 $css['color'] = $searchResTColor;
149 }
150
151 return array_merge(
152 $css,
153 isset( $attributes['searchResTypography'] ) ? Typography::get_css( $attributes['searchResTypography'], '', $device ) : [],
154 );
155 }
156 public function get_search_result_list( $attributes, $device = '' ) {
157 $css = [];
158 $defaultUnit = 'px';
159 $offset_top = Range::get_css([
160 'attributeValue' => $attributes['verticalOffset'],
161 'attribute_object_key' => 'value',
162 'isResponsive' => true,
163 'hasUnit' => true,
164 'defaultValue' => 230,
165 'unitDefaultValue' => 'px',
166 'property' => 'top',
167 'device' => $device,
168 ]);
169 $offset_bottom = Range::get_css([
170 'attributeValue' => $attributes['verticalOffset'],
171 'attribute_object_key' => 'value',
172 'isResponsive' => true,
173 'hasUnit' => true,
174 'defaultValue' => 230,
175 'unitDefaultValue' => 'px',
176 'property' => 'bottom',
177 'device' => $device,
178 ]);
179 $offset_left = Range::get_css([
180 'attributeValue' => $attributes['horizontalOffset'],
181 'attribute_object_key' => 'value',
182 'isResponsive' => true,
183 'hasUnit' => true,
184 'defaultValue' => 230,
185 'unitDefaultValue' => 'px',
186 'property' => 'left',
187 'device' => $device,
188 ]);
189 $offset_right = Range::get_css([
190 'attributeValue' => $attributes['horizontalOffset'],
191 'attribute_object_key' => 'value',
192 'isResponsive' => true,
193 'hasUnit' => true,
194 'defaultValue' => 230,
195 'unitDefaultValue' => 'px',
196 'property' => 'right',
197 'device' => $device,
198 ]);
199 // Set the position property
200 if ( ! empty( $attributes['position'] ) ) {
201 $css['position'] = $attributes['position'];
202 }
203
204 // Ensure offsets only apply when the position is not "default"
205 if ( ! empty( $attributes['position'] ) && $attributes['position'] === 'default' ) {
206 unset( $offset_left['left'], $offset_right['right'], $offset_top['top'], $offset_bottom['bottom'] );
207 }
208 return array_merge(
209 $css,
210 Range::get_css([
211 'attributeValue' => $attributes['listWidth'],
212 'attribute_object_key' => 'value',
213 'isResponsive' => true,
214 'hasUnit' => true,
215 'defaultValue' => '',
216 'unitDefaultValue' => '%',
217 'property' => 'width',
218 'device' => $device,
219 ]),
220 Range::get_css([
221 'attributeValue' => $attributes['listGap'],
222 'attribute_object_key' => 'value',
223 'isResponsive' => true,
224 'hasUnit' => true,
225 'defaultValue' => 0,
226 'unitDefaultValue' => 'px',
227 'property' => 'gap',
228 'device' => $device,
229 ]),
230 $offset_top,
231 $offset_bottom,
232 $offset_left,
233 $offset_right,
234 isset( $attributes['listPadding'] ) ? Dimensions::get_css( $attributes['listPadding'], 'padding', $device ) : [],
235 isset( $attributes['listBorder'] ) ? Border::get_css( $attributes['listBorder'], '', $device ) : [],
236 );
237 }
238 public function get_search_result_list_hover( $attributes, $device = '' ) {
239 $css = [];
240
241 return array_merge(
242 $css,
243 isset( $attributes['listBorder'] ) ? Border::get_hover_css( $attributes['listBorder'], $device ) : [],
244 );
245 }
246
247 public function get_search_result_item( $attributes, $device = '' ) {
248 return array_merge(
249 Range::get_css([
250 'attributeValue' => $attributes['itemWidth'],
251 'attribute_object_key' => 'value',
252 'isResponsive' => true,
253 'hasUnit' => true,
254 'defaultValue' => '',
255 'unitDefaultValue' => 'px',
256 'property' => 'width',
257 'device' => $device,
258 ]),
259 Range::get_css([
260 'attributeValue' => $attributes['itemGap'],
261 'attribute_object_key' => 'value',
262 'isResponsive' => true,
263 'hasUnit' => true,
264 'defaultValue' => '',
265 'unitDefaultValue' => 'px',
266 'property' => 'gap',
267 'device' => $device,
268 ]),
269 isset( $attributes['itemPadding'] ) ? Dimensions::get_css( $attributes['itemPadding'], 'padding', $device ) : [],
270 isset( $attributes['itemBorder'] ) ? Border::get_css( $attributes['itemBorder'], '', $device ) : [],
271 );
272 }
273
274 public function get_search_result_item_hover( $attributes, $device = '' ) {
275 $css = [];
276
277 return array_merge(
278 $css,
279 isset( $attributes['itemBorder'] ) ? Border::get_hover_css( $attributes['itemBorder'], '', $device ) : [],
280 );
281 }
282
283 public function get_search_result_img_css( $attributes, $device = '' ) {
284 return array_merge(
285 Range::get_css([
286 'attributeValue' => $attributes['thumbnailWidth'],
287 'attribute_object_key' => 'value',
288 'isResponsive' => true,
289 'hasUnit' => true,
290 'defaultValue' => '',
291 'unitDefaultValue' => '%',
292 'property' => 'width',
293 'device' => $device,
294 ]),
295 Range::get_css([
296 'attributeValue' => $attributes['thumbnailHeight'],
297 'attribute_object_key' => 'value',
298 'isResponsive' => true,
299 'hasUnit' => true,
300 'defaultValue' => '',
301 'unitDefaultValue' => 'px',
302 'property' => 'height',
303 'device' => $device,
304 ]),
305 );
306 }
307
308 public function get_Button_css( $attributes, $device = '' ) {
309 $css = [];
310 $button_color = isset( $attributes['buttonTextColor'] ) ? $attributes['buttonTextColor'] : '';
311
312 if ( isset( $attributes['buttonTextColor'] ) && ! empty( $attributes['buttonTextColor'] ) ) {
313 $css['color'] = $button_color;
314 }
315
316 return array_merge(
317 $css,
318 isset( $attributes['buttonTypography'] ) ? Typography::get_css( $attributes['buttonTypography'], '', $device ) : [],
319 isset( $attributes['buttonTextStroke'] ) ? TextStroke::get_css( $attributes['buttonTextStroke'], '', $device ) : [],
320 isset( $attributes['buttonTextShadow'] ) ? TextStroke::get_css( $attributes['buttonTextShadow'], '', $device ) : [],
321 );
322 }
323
324
325 public function get_Icon_css( $attributes, $device = '' ) {
326
327 return array_merge(
328 Range::get_css([
329 'attributeValue' => $attributes['iconWidth'],
330 'attribute_object_key' => 'value',
331 'isResponsive' => true,
332 'defaultValue' => 18,
333 'unitDefaultValue' => 'px',
334 'hasUnit' => true,
335 'property' => 'width',
336 'device' => $device,
337 ]),
338 );
339 }
340
341
342
343 public function render_block_content( $attributes, $content, $block_instance ) {
344
345 // Sanitize and escape input attributes
346 $currentPostID = isset( $attributes['currentPostID'] ) ? (int) sanitize_text_field( $attributes['currentPostID'] ) : '';
347 $source = isset( $attributes['source'] ) ? sanitize_text_field( $attributes['source'] ) : '';
348 $placeholder = isset( $attributes['placeholder'] ) ? sanitize_text_field( $attributes['placeholder'] ) : esc_html__( 'Write anything...', 'ablocks' );
349 $variant = isset( $attributes['variant'] ) ? sanitize_key( $attributes['variant'] ) : 'classic';
350 $isIcon = isset( $attributes['isIcon'] ) ? sanitize_key( $attributes['isIcon'] ) : 'icon';
351 $buttonText = isset( $attributes['buttonText'] ) ? sanitize_text_field( $attributes['buttonText'] ) : esc_html__( 'Search', 'ablocks' );
352 $buttonAlignment = isset( $attributes['buttonAlignment']['value'] ) ? sanitize_key( $attributes['buttonAlignment']['value'] ) : 'left';
353
354 ob_start();
355 ?>
356 <div class="ablocks-block--search-bar <?php echo esc_attr( $variant ); ?>">
357 <form method="post" class="ablocks-block--search-form <?php echo esc_attr( ( $isIcon === 'both' || $isIcon === 'text' ) ? $isIcon : '' ); ?>">
358 <?php if ( 'left' === $buttonAlignment && 'classic' !== $variant ) : ?>
359 <button type="button" class="ablocks-block--search-button <?php echo esc_attr( ( $isIcon === 'both' || $isIcon === 'text' ) ? $isIcon : '' ); ?>">
360 <span class="button-content">
361 <?php
362 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
363 echo $this->render_button( $isIcon, $buttonText );
364 ?>
365 </span>
366 <span class="loading-spinner">
367 <?php
368 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
369 echo $this->loading_spinner();
370 ?>
371 </span>
372 </button>
373 <?php endif; ?>
374 <input class="ablocks-block--search-input <?php echo esc_attr( ( $isIcon === 'both' || $isIcon === 'text' ) ? $isIcon : '' ); ?>" type="text" placeholder="<?php echo esc_attr( $placeholder ); ?>" value=""/>
375 <?php if ( 'right' === $buttonAlignment || 'classic' === $variant ) : ?>
376 <button type="button" class="ablocks-block--search-button <?php echo esc_attr( ( $isIcon === 'both' || $isIcon === 'text' ) ? $isIcon : '' ); ?>">
377 <span class="button-content">
378 <?php
379 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
380 echo $this->render_button( $isIcon, $buttonText );
381 ?>
382 </span>
383 <span class="loading-spinner">
384 <?php
385 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
386 echo $this->loading_spinner();
387 ?>
388 </span>
389 </button>
390 <?php endif; ?>
391 <input type="hidden" class="ablocks-block--search-source" value="<?php echo esc_attr( $source ); ?>"/>
392 <input type="hidden" class="ablocks-block--search__current-post-id" value="<?php echo esc_attr( $currentPostID ); ?>"/>
393 </form>
394 <ul class="ablocks-block--search-result ablocks-block--search-empty-result">
395 <!-- Search results will go here -->
396 </ul>
397 </div>
398 <?php
399 return ob_get_clean();
400 }
401
402
403 private function render_button( $isIcon, $buttonText ) {
404 $search_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search">
405 <circle cx="11" cy="11" r="8"></circle>
406 <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
407 </svg>';
408
409 if ( 'icon' === $isIcon ) {
410 return $search_icon;
411 } elseif ( 'text' === $isIcon ) {
412 return '<span>' . esc_html( $buttonText ) . '</span>';
413 } else {
414 return $search_icon . '<span>' . esc_html( $buttonText ) . '</span>';
415 }
416 }
417
418 private function loading_spinner() {
419 $spinner = '<svg viewBox="0 0 800 800" xmlns="http://www.w3.org/2000/svg">
420 <circle class="ablocks-search-block__spin" cx="400" cy="400" fill="none"
421 r="200" stroke-width="50" stroke="currentColor"
422 stroke-dasharray="700 1400"
423 stroke-linecap="round" />
424 </svg>';
425 return $spinner;
426 }
427 }
428
429