PluginProbe
Elementor Website Builder – more than just a page builder / 3.20.1
Elementor Website Builder – more than just a page builder v3.20.1
4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 All 457 releases
elementor / modules / nested-accordion / widgets / nested-accordion.php

nested-accordion.php in Elementor Website Builder – more than just a page builder 3.20.1, at modules/nested-accordion/widgets/nested-accordion.php

967 lines 27.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\NestedAccordion\Widgets;
3
4 use Elementor\Controls_Manager;
5 use Elementor\Group_Control_Background;
6 use Elementor\Group_Control_Border;
7 use Elementor\Group_Control_Text_Shadow;
8 use Elementor\Group_Control_Typography;
9 use Elementor\Icons_Manager;
10 use Elementor\Modules\NestedElements\Base\Widget_Nested_Base;
11 use Elementor\Modules\NestedElements\Controls\Control_Nested_Repeater;
12 use Elementor\Plugin;
13 use Elementor\Repeater;
14 use Elementor\Utils;
15 use Elementor\Group_Control_Text_Stroke;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly
19 }
20
21 /**
22 * Elementor Nested Accordion widget.
23 *
24 * Elementor widget that displays a collapsible display of content in an
25 * accordion style.
26 *
27 * @since 3.15.0
28 */
29 class Nested_Accordion extends Widget_Nested_Base {
30
31 public function get_name() {
32 return 'nested-accordion';
33 }
34
35 public function get_title() {
36 return esc_html__( 'Accordion', 'elementor' );
37 }
38
39 public function get_icon() {
40 return 'eicon-accordion';
41 }
42
43 public function get_keywords() {
44 return [ 'nested', 'tabs', 'accordion', 'toggle' ];
45 }
46
47 public function show_in_panel(): bool {
48 return Plugin::$instance->experiments->is_feature_active( 'nested-elements' );
49 }
50
51 protected function item_content_container( int $index ) {
52 return [
53 'elType' => 'container',
54 'settings' => [
55 '_title' => sprintf( __( 'item #%s', 'elementor' ), $index ),
56 'content_width' => 'full',
57 ],
58 ];
59 }
60
61 protected function get_default_children_elements() {
62 return [
63 $this->item_content_container( 1 ),
64 $this->item_content_container( 2 ),
65 $this->item_content_container( 3 ),
66 ];
67 }
68
69 protected function get_default_repeater_title_setting_key() {
70 return 'item_title';
71 }
72
73 protected function get_default_children_title() {
74 return esc_html__( 'Item #%d', 'elementor' );
75 }
76
77 protected function get_default_children_placeholder_selector() {
78 return '.e-n-accordion';
79 }
80
81 protected function get_html_wrapper_class() {
82 return 'elementor-widget-n-accordion';
83 }
84
85 protected function register_controls() {
86 $this->start_controls_section( 'section_items', [
87 'label' => esc_html__( 'Layout', 'elementor' ),
88 ] );
89
90 $repeater = new Repeater();
91
92 $repeater->add_control(
93 'item_title',
94 [
95 'label' => esc_html__( 'Title', 'elementor' ),
96 'type' => Controls_Manager::TEXT,
97 'default' => esc_html__( 'Item Title', 'elementor' ),
98 'placeholder' => esc_html__( 'Item Title', 'elementor' ),
99 'label_block' => true,
100 'dynamic' => [
101 'active' => true,
102 ],
103 ]
104 );
105
106 $repeater->add_control(
107 'element_css_id',
108 [
109 'label' => esc_html__( 'CSS ID', 'elementor' ),
110 'type' => Controls_Manager::TEXT,
111 'default' => '',
112 'dynamic' => [
113 'active' => true,
114 ],
115 'ai' => [
116 'active' => false,
117 ],
118 'title' => esc_html__( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'elementor' ),
119 'style_transfer' => false,
120 ]
121 );
122
123 $this->add_control(
124 'items',
125 [
126 'label' => esc_html__( 'Items', 'elementor' ),
127 'type' => Control_Nested_Repeater::CONTROL_TYPE,
128 'fields' => $repeater->get_controls(),
129 'default' => [
130 [
131 'item_title' => esc_html__( 'Item #1', 'elementor' ),
132 ],
133 [
134 'item_title' => esc_html__( 'Item #2', 'elementor' ),
135 ],
136 [
137 'item_title' => esc_html__( 'Item #3', 'elementor' ),
138 ],
139 ],
140 'title_field' => '{{{ item_title }}}',
141 'button_text' => 'Add Item',
142 ]
143 );
144
145 $this->add_responsive_control(
146 'accordion_item_title_position_horizontal',
147 [
148 'label' => esc_html__( 'Item Position', 'elementor' ),
149 'type' => Controls_Manager::CHOOSE,
150 'separator' => 'before',
151 'options' => [
152 'start' => [
153 'title' => esc_html__( 'Start', 'elementor' ),
154 'icon' => 'eicon-flex eicon-align-start-h',
155 ],
156 'center' => [
157 'title' => esc_html__( 'Center', 'elementor' ),
158 'icon' => 'eicon-h-align-center',
159 ],
160 'end' => [
161 'title' => esc_html__( 'End', 'elementor' ),
162 'icon' => 'eicon-flex eicon-align-end-h',
163 ],
164 'stretch' => [
165 'title' => esc_html__( 'Stretch', 'elementor' ),
166 'icon' => 'eicon-h-align-stretch',
167 ],
168 ],
169 'selectors_dictionary' => [
170 'start' => '--n-accordion-title-justify-content: initial; --n-accordion-title-flex-grow: initial;',
171 'center' => '--n-accordion-title-justify-content: center; --n-accordion-title-flex-grow: initial;',
172 'end' => '--n-accordion-title-justify-content: flex-end; --n-accordion-title-flex-grow: initial;',
173 'stretch' => '--n-accordion-title-justify-content: space-between; --n-accordion-title-flex-grow: 1;',
174 ],
175 'selectors' => [
176 '{{WRAPPER}}' => '{{VALUE}}',
177 ],
178 ]
179 );
180
181 $this->add_control(
182 'heading_accordion_item_title_icon',
183 [
184 'type' => Controls_Manager::HEADING,
185 'label' => esc_html__( 'Icon', 'elementor' ),
186 'separator' => 'before',
187 ]
188 );
189
190 $this->add_responsive_control(
191 'accordion_item_title_icon_position',
192 [
193 'label' => esc_html__( 'Position', 'elementor' ),
194 'type' => Controls_Manager::CHOOSE,
195 'options' => [
196 'start' => [
197 'title' => esc_html__( 'Start', 'elementor' ),
198 'icon' => 'eicon-h-align-left',
199 ],
200 'end' => [
201 'title' => esc_html__( 'End', 'elementor' ),
202 'icon' => 'eicon-h-align-right',
203 ],
204 ],
205 'selectors_dictionary' => [
206 'start' => '--n-accordion-title-icon-order: -1;',
207 'end' => '--n-accordion-title-icon-order: initial;',
208 ],
209 'selectors' => [
210 '{{WRAPPER}}' => '{{VALUE}}',
211 ],
212 ]
213 );
214
215 $this->add_control(
216 'accordion_item_title_icon',
217 [
218 'label' => esc_html__( 'Expand', 'elementor' ),
219 'type' => Controls_Manager::ICONS,
220 'default' => [
221 'value' => 'fas fa-plus',
222 'library' => 'fa-solid',
223 ],
224 'skin' => 'inline',
225 'label_block' => false,
226 ]
227 );
228
229 $this->add_control(
230 'accordion_item_title_icon_active',
231 [
232 'label' => esc_html__( 'Collapse', 'elementor' ),
233 'type' => Controls_Manager::ICONS,
234 'fa4compatibility' => 'icon_active',
235 'default' => [
236 'value' => 'fas fa-minus',
237 'library' => 'fa-solid',
238 ],
239 'condition' => [
240 'accordion_item_title_icon[value]!' => '',
241 ],
242 'skin' => 'inline',
243 'label_block' => false,
244 ]
245 );
246
247 $this->add_control(
248 'title_tag',
249 [
250 'label' => esc_html__( 'Title HTML Tag', 'elementor' ),
251 'type' => Controls_Manager::SELECT,
252 'options' => [
253 'h1' => 'H1',
254 'h2' => 'H2',
255 'h3' => 'H3',
256 'h4' => 'H4',
257 'h5' => 'H5',
258 'h6' => 'H6',
259 'div' => 'div',
260 'span' => 'span',
261 'p' => 'p',
262 ],
263 'selectors_dictionary' => [
264 'h1' => '--n-accordion-title-font-size: 2.5rem;',
265 'h2' => '--n-accordion-title-font-size: 2rem;',
266 'h3' => '--n-accordion-title-font-size: 1,75rem;',
267 'h4' => '--n-accordion-title-font-size: 1.5rem;',
268 'h5' => '--n-accordion-title-font-size: 1rem;',
269 'h6' => '--n-accordion-title-font-size: 1rem; ',
270 'div' => '--n-accordion-title-font-size: 1rem;',
271 'span' => '--n-accordion-title-font-size: 1rem; ',
272 'p' => '--n-accordion-title-font-size: 1rem;',
273 ],
274 'selectors' => [
275 '{{WRAPPER}}' => '{{VALUE}}',
276 ],
277 'default' => 'div',
278 'separator' => 'before',
279 'render_type' => 'template',
280
281 ]
282 );
283
284 $this->add_control(
285 'faq_schema',
286 [
287 'label' => esc_html__( 'FAQ Schema', 'elementor' ),
288 'type' => Controls_Manager::SWITCHER,
289 'label_on' => esc_html__( 'Yes', 'elementor' ),
290 'label_off' => esc_html__( 'No', 'elementor' ),
291 'default' => 'no',
292 ]
293 );
294
295 $this->add_control(
296 'faq_schema_message',
297 [
298 'type' => Controls_Manager::ALERT,
299 'alert_type' => 'info',
300 'content' => esc_html__( 'Let Google know that this section contains an FAQ. Make sure to only use it only once per page', 'elementor' ),
301 'condition' => [
302 'faq_schema[value]' => 'yes',
303 ],
304 ]
305 );
306
307 $this->end_controls_section();
308
309 $this->start_controls_section(
310 'section_interactions',
311 [
312 'label' => esc_html__( 'Interactions', 'elementor' ),
313 ]
314 );
315
316 $this->add_control(
317 'default_state',
318 [
319 'label' => esc_html__( 'Default State', 'elementor' ),
320 'type' => Controls_Manager::SELECT,
321 'options' => [
322 'expanded' => esc_html__( 'First expanded', 'elementor' ),
323 'all_collapsed' => esc_html__( 'All collapsed', 'elementor' ),
324 ],
325 'default' => 'expanded',
326 'frontend_available' => true,
327 ]
328 );
329
330 $this->add_control(
331 'max_items_expended',
332 [
333 'label' => esc_html__( 'Max Items Expanded', 'elementor' ),
334 'type' => Controls_Manager::SELECT,
335 'options' => [
336 'one' => esc_html__( 'One', 'elementor' ),
337 'multiple' => esc_html__( 'Multiple', 'elementor' ),
338 ],
339 'default' => 'one',
340 'frontend_available' => true,
341 ]
342 );
343
344 $this->add_control(
345 'n_accordion_animation_duration',
346 [
347 'label' => esc_html__( 'Animation Duration', 'elementor' ),
348 'type' => Controls_Manager::SLIDER,
349 'size_units' => [ 's', 'ms' ],
350 'default' => [
351 'unit' => 'ms',
352 'size' => 400,
353 ],
354 'frontend_available' => true,
355 ]
356 );
357
358 $this->end_controls_section();
359
360 $this->add_style_tab();
361 }
362
363 private function add_style_tab() {
364 $this->add_accordion_style_section();
365 $this->add_header_style_section();
366 $this->add_content_style_section();
367 }
368
369 private function add_accordion_style_section() {
370 $this->start_controls_section(
371 'section_accordion_style',
372 [
373 'label' => esc_html__( 'Accordion', 'elementor' ),
374 'tab' => Controls_Manager::TAB_STYLE,
375 ]
376 );
377
378 $this->add_responsive_control(
379 'accordion_item_title_space_between',
380 [
381 'label' => esc_html__( 'Space between Items', 'elementor' ),
382 'type' => Controls_Manager::SLIDER,
383 'size_units' => [ 'px', 'em', 'rem', 'custom' ],
384 'range' => [
385 'px' => [
386 'max' => 200,
387 ],
388 'em' => [
389 'max' => 20,
390 ],
391 'rem' => [
392 'max' => 20,
393 ],
394 ],
395 'default' => [
396 'size' => 0,
397 ],
398 'selectors' => [
399 '{{WRAPPER}}' => '--n-accordion-item-title-space-between: {{SIZE}}{{UNIT}}',
400 ],
401 ]
402 );
403
404 $this->add_responsive_control(
405 'accordion_item_title_distance_from_content',
406 [
407 'label' => esc_html__( 'Distance from content', 'elementor' ),
408 'type' => Controls_Manager::SLIDER,
409 'size_units' => [ 'px', 'em', 'rem', 'custom' ],
410 'range' => [
411 'px' => [
412 'max' => 200,
413 ],
414 'em' => [
415 'max' => 20,
416 ],
417 'rem' => [
418 'max' => 20,
419 ],
420 ],
421 'default' => [
422 'size' => 0,
423 ],
424 'selectors' => [
425 '{{WRAPPER}}' => '--n-accordion-item-title-distance-from-content: {{SIZE}}{{UNIT}}',
426 ],
427 ]
428 );
429
430 $this->start_controls_tabs( 'accordion_border_and_background' );
431
432 foreach ( [ 'normal', 'hover', 'active' ] as $state ) {
433 $this->add_border_and_radius_style( $state );
434 }
435
436 $this->end_controls_tabs();
437
438 $this->add_responsive_control(
439 'accordion_border_radius',
440 [
441 'label' => esc_html__( 'Border Radius', 'elementor' ),
442 'type' => Controls_Manager::DIMENSIONS,
443 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
444 'selectors' => [
445 '{{WRAPPER}}' => '--n-accordion-border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
446 ],
447 'separator' => 'before',
448 ]
449 );
450
451 $this->add_responsive_control(
452 'accordion_padding',
453 [
454 'label' => esc_html__( 'Padding', 'elementor' ),
455 'type' => Controls_Manager::DIMENSIONS,
456 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
457 'selectors' => [
458 '{{WRAPPER}} ' => '--n-accordion-padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
459 ],
460 ]
461 );
462
463 $this->end_controls_section();
464 }
465
466 private function add_content_style_section() {
467
468 $low_specificity_accordion_item_selector = ':where( {{WRAPPER}} > .elementor-widget-container > .e-n-accordion > .e-n-accordion-item ) > .e-con';
469
470 $this->start_controls_section(
471 'section_content_style',
472 [
473 'label' => esc_html__( 'Content', 'elementor' ),
474 'tab' => Controls_Manager::TAB_STYLE,
475 ]
476 );
477
478 $this->add_group_control(
479 Group_Control_Background::get_type(),
480 [
481 'name' => 'content_background',
482 'types' => [ 'classic', 'gradient' ],
483 'exclude' => [ 'image' ],
484 'selector' => $low_specificity_accordion_item_selector,
485 ]
486 );
487
488 $this->add_group_control(
489 Group_Control_Border::get_type(),
490 [
491 'name' => 'content_border',
492 'selector' => $low_specificity_accordion_item_selector,
493 'fields_options' => [
494 'color' => [
495 'label' => esc_html__( 'Border Color', 'elementor' ),
496 ],
497 'width' => [
498 'label' => esc_html__( 'Border Width', 'elementor' ),
499 ],
500 ],
501 ]
502 );
503
504 $this->add_responsive_control(
505 'content_border_radius',
506 [
507 'label' => esc_html__( 'Border Radius', 'elementor' ),
508 'type' => Controls_Manager::DIMENSIONS,
509 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
510 'selectors' => [
511 $low_specificity_accordion_item_selector => '--border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
512 ],
513 ]
514 );
515
516 $logical_dimensions_inline_start = is_rtl() ? '{{RIGHT}}{{UNIT}}' : '{{LEFT}}{{UNIT}}';
517 $logical_dimensions_inline_end = is_rtl() ? '{{LEFT}}{{UNIT}}' : '{{RIGHT}}{{UNIT}}';
518
519 $this->add_responsive_control(
520 'content_padding',
521 [
522 'label' => esc_html__( 'Padding', 'elementor' ),
523 'type' => Controls_Manager::DIMENSIONS,
524 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
525 'selectors' => [
526 $low_specificity_accordion_item_selector => "--padding-block-start: {{TOP}}{{UNIT}}; --padding-inline-end: $logical_dimensions_inline_end; --padding-block-end: {{BOTTOM}}{{UNIT}}; --padding-inline-start: $logical_dimensions_inline_start;",
527 ],
528 ]
529 );
530
531 $this->end_controls_section();
532 }
533
534 private function add_header_style_section() {
535 $this->start_controls_section(
536 'section_header_style',
537 [
538 'label' => esc_html__( 'Header', 'elementor' ),
539 'tab' => Controls_Manager::TAB_STYLE,
540 ]
541 );
542
543 $this->add_control(
544 'heading_header_style_title',
545 [
546 'type' => Controls_Manager::HEADING,
547 'label' => esc_html__( 'Title', 'elementor' ),
548
549 ]
550 );
551
552 $this->add_group_control(
553 Group_Control_Typography::get_type(),
554 [
555 'name' => 'title_typography',
556 'selector' => ':where( {{WRAPPER}} > .elementor-widget-container > .e-n-accordion > .e-n-accordion-item > .e-n-accordion-item-title > .e-n-accordion-item-title-header ) > .e-n-accordion-item-title-text',
557 'fields_options' => [
558 'font_size' => [
559 'selectors' => [
560 '{{WRAPPER}}' => '--n-accordion-title-font-size: {{SIZE}}{{UNIT}}',
561 ],
562 ],
563 ],
564 ]
565 );
566
567 $this->start_controls_tabs( 'header_title_color_style' );
568
569 foreach ( [ 'normal', 'hover', 'active' ] as $state ) {
570 $this->add_header_style( $state, 'title' );
571 }
572
573 $this->end_controls_tabs();
574
575 $this->add_control(
576 'heading_icon_style_title',
577 [
578 'type' => Controls_Manager::HEADING,
579 'label' => esc_html__( 'Icon', 'elementor' ),
580 'separator' => 'before',
581 ]
582 );
583
584 $this->add_responsive_control(
585 'icon_size',
586 [
587 'label' => esc_html__( 'Size', 'elementor' ),
588 'type' => Controls_Manager::SLIDER,
589 'range' => [
590 'em' => [
591 'max' => 10,
592 ],
593 'rem' => [
594 'max' => 10,
595 ],
596 ],
597 'default' => [
598 'unit' => 'px',
599 'size' => 15,
600 ],
601 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
602 'selectors' => [
603 '{{WRAPPER}}' => '--n-accordion-icon-size: {{SIZE}}{{UNIT}}',
604 ],
605 ]
606 );
607
608 $this->add_responsive_control(
609 'icon_spacing',
610 [
611 'label' => esc_html__( 'Spacing', 'elementor' ),
612 'type' => Controls_Manager::SLIDER,
613 'range' => [
614 'px' => [
615 'max' => 400,
616 ],
617 'vw' => [
618 'max' => 50,
619 'step' => 0.1,
620 ],
621 ],
622 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
623 'selectors' => [
624 '{{WRAPPER}}' => '--n-accordion-icon-gap: {{SIZE}}{{UNIT}}',
625 ],
626 'condition' => [
627 'accordion_item_title_position_horizontal!' => 'stretch',
628 ],
629 ]
630 );
631
632 $this->start_controls_tabs( 'header_icon_color_style' );
633
634 foreach ( [ 'normal', 'hover', 'active' ] as $state ) {
635 $this->add_header_style( $state, 'icon' );
636 }
637
638 $this->end_controls_tabs();
639 $this->end_controls_section();
640 }
641
642 private function add_header_style( $state, $context ) {
643 $variable = '--n-accordion-' . $context . '-' . $state . '-color';
644
645 switch ( $state ) {
646 case 'hover':
647 $translated_tab_text = esc_html__( 'Hover', 'elementor' );
648 $translated_tab_css_selector = ':where( {{WRAPPER}} > .elementor-widget-container > .e-n-accordion > .e-n-accordion-item:not([open]) > .e-n-accordion-item-title:hover > .e-n-accordion-item-title-header ) > .e-n-accordion-item-title-text';
649 break;
650 case 'active':
651 $translated_tab_text = esc_html__( 'Active', 'elementor' );
652 $translated_tab_css_selector = ':where( {{WRAPPER}} > .elementor-widget-container > .e-n-accordion > .e-n-accordion-item[open] > .e-n-accordion-item-title > .e-n-accordion-item-title-header ) > .e-n-accordion-item-title-text';
653 break;
654 default:
655 $translated_tab_text = esc_html__( 'Normal', 'elementor' );
656 $translated_tab_css_selector = ':where( {{WRAPPER}} > .elementor-widget-container > .e-n-accordion > .e-n-accordion-item:not([open]) > .e-n-accordion-item-title:not(hover) > .e-n-accordion-item-title-header ) > .e-n-accordion-item-title-text';
657 break;
658 }
659
660 $this->start_controls_tab(
661 'header_' . $state . '_' . $context,
662 [
663 'label' => $translated_tab_text,
664 ]
665 );
666
667 $this->add_control(
668 $state . '_' . $context . '_color',
669 [
670 'label' => esc_html__( 'Color', 'elementor' ),
671 'type' => Controls_Manager::COLOR,
672 'selectors' => [
673 '{{WRAPPER}}' => $variable . ': {{VALUE}};',
674 ],
675 ]
676 );
677
678 if ( 'title' === $context ) {
679 $this->add_group_control(
680 Group_Control_Text_Shadow::get_type(),
681 [
682 'name' => $context . '_' . $state . '_text_shadow',
683 'selector' => '{{WRAPPER}} ' . $translated_tab_css_selector,
684 'fields_options' => [
685 'text_shadow_type' => [
686 'label' => esc_html__( 'Shadow', 'elementor' ),
687 ],
688 ],
689 ]
690 );
691
692 $this->add_group_control(
693 Group_Control_Text_Stroke::get_type(),
694 [
695 'name' => $context . '_' . $state . '_stroke',
696 'selector' => '{{WRAPPER}} ' . $translated_tab_css_selector,
697 ]
698 );
699 }
700
701 $this->end_controls_tab();
702 }
703
704 /**
705 * @string $state
706 */
707 private function add_border_and_radius_style( $state ) {
708 $selector = '{{WRAPPER}} > .elementor-widget-container > .e-n-accordion > .e-n-accordion-item > .e-n-accordion-item-title';
709 $translated_tab_text = esc_html__( 'Normal', 'elementor' );
710
711 switch ( $state ) {
712 case 'hover':
713 $selector .= ':hover';
714 $translated_tab_text = esc_html__( 'Hover', 'elementor' );
715 break;
716 case 'active':
717 $selector = '{{WRAPPER}} > .elementor-widget-container > .e-n-accordion > .e-n-accordion-item[open] > .e-n-accordion-item-title';
718 $translated_tab_text = esc_html__( 'Active', 'elementor' );
719 break;
720 }
721
722 $this->start_controls_tab(
723 'accordion_' . $state . '_border_and_background',
724 [
725 'label' => $translated_tab_text,
726 ]
727 );
728
729 $this->add_group_control(
730 Group_Control_Background::get_type(),
731 [
732 'name' => 'accordion_background_' . $state,
733 'types' => [ 'classic', 'gradient' ],
734 'exclude' => [ 'image' ],
735 'fields_options' => [
736 'color' => [
737 'label' => esc_html__( 'Color', 'elementor' ),
738 ],
739 ],
740 'selector' => $selector,
741 ]
742 );
743
744 $this->add_group_control(
745 Group_Control_Border::get_type(),
746 [
747 'name' => 'accordion_border_' . $state,
748 'selector' => $selector,
749 ]
750 );
751
752 $this->end_controls_tab();
753 }
754
755 private function is_active_icon_exist( $settings ):bool {
756 return array_key_exists( 'accordion_item_title_icon_active', $settings ) && ! empty( $settings['accordion_item_title_icon_active'] ) && ! empty( $settings['accordion_item_title_icon_active']['value'] );
757 }
758
759 private function render_accordion_icons( $settings ) {
760 $icon_html = Icons_Manager::try_get_icon_html( $settings['accordion_item_title_icon'], [ 'aria-hidden' => 'true' ] );
761 $icon_active_html = $this->is_active_icon_exist( $settings )
762 ? Icons_Manager::try_get_icon_html( $settings['accordion_item_title_icon_active'], [ 'aria-hidden' => 'true' ] )
763 : $icon_html;
764
765 ob_start();
766 ?>
767 <span class='e-n-accordion-item-title-icon'>
768 <span class='e-opened' ><?php echo $icon_active_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
769 <span class='e-closed'><?php echo $icon_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
770 </span>
771
772 <?php
773 return ob_get_clean();
774 }
775
776 protected function render() {
777 $settings = $this->get_settings_for_display();
778 $items = $settings['items'];
779 $id_int = substr( $this->get_id_int(), 0, 3 );
780 $items_title_html = '';
781 $icons_content = $this->render_accordion_icons( $settings );
782 $this->add_render_attribute( 'elementor-accordion', 'class', 'e-n-accordion' );
783 $this->add_render_attribute( 'elementor-accordion', 'aria-label', 'Accordion. Open links with Enter or Space, close with Escape, and navigate with Arrow Keys' );
784 $default_state = $settings['default_state'];
785 $title_html_tag = Utils::validate_html_tag( $settings['title_tag'] );
786
787 $faq_schema = [];
788
789 foreach ( $items as $index => $item ) {
790 $accordion_count = $index + 1;
791 $item_setting_key = $this->get_repeater_setting_key( 'item_title', 'items', $index );
792 $item_summary_key = $this->get_repeater_setting_key( 'item_summary', 'items', $index );
793 $item_classes = [ 'e-n-accordion-item' ];
794 $item_id = empty( $item['element_css_id'] ) ? 'e-n-accordion-item-' . $id_int . $index : $item['element_css_id'];
795 $item_title = $item['item_title'];
796 $is_open = 'expanded' === $default_state && 0 === $index ? 'open' : '';
797 $aria_expanded = 'expanded' === $default_state && 0 === $index;
798
799 $this->add_render_attribute( $item_setting_key, [
800 'id' => $item_id,
801 'class' => $item_classes,
802 ] );
803
804 $this->add_render_attribute( $item_summary_key, [
805 'class' => [ 'e-n-accordion-item-title' ],
806 'data-accordion-index' => $accordion_count,
807 'tabindex' => 0 === $index ? 0 : -1,
808 'aria-expanded' => $aria_expanded ? 'true' : 'false',
809 'aria-controls' => $item_id,
810 ] );
811
812 $title_render_attributes = $this->get_render_attribute_string( $item_setting_key );
813 $title_render_attributes = $title_render_attributes . ' ' . $is_open;
814
815 $summary_render_attributes = $this->get_render_attribute_string( $item_summary_key );
816
817 // items content.
818 ob_start();
819 $this->print_child( $index, $item_id );
820 $item_content = ob_get_clean();
821
822 $faq_schema[ $item_title ] = $item_content;
823
824 ob_start();
825 ?>
826 <details <?php echo wp_kses_post( $title_render_attributes ); ?>>
827 <summary <?php echo wp_kses_post( $summary_render_attributes ); ?> >
828 <span class='e-n-accordion-item-title-header'><?php echo wp_kses_post( "<$title_html_tag class=\"e-n-accordion-item-title-text\"> $item_title </$title_html_tag>" ); ?></span>
829 <?php if ( ! empty( $settings['accordion_item_title_icon']['value'] ) ) {
830 echo $icons_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
831 } ?>
832 </summary>
833 <?php echo $item_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
834 </details>
835 <?php
836 $items_title_html .= ob_get_clean();
837 }
838
839 ?>
840 <div <?php $this->print_render_attribute_string( 'elementor-accordion' ); ?>>
841 <?php echo $items_title_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
842 </div>
843 <?php
844 if ( isset( $settings['faq_schema'] ) && 'yes' === $settings['faq_schema'] ) {
845 $json = [
846 '@context' => 'https://schema.org',
847 '@type' => 'FAQPage',
848 'mainEntity' => [],
849 ];
850
851 foreach ( $faq_schema as $name => $text ) {
852 $json['mainEntity'][] = [
853 '@type' => 'Question',
854 'name' => wp_strip_all_tags( $name ),
855 'acceptedAnswer' => [
856 '@type' => 'Answer',
857 'text' => wp_strip_all_tags( $text ),
858 ],
859 ];
860 }
861 ?>
862 <script type="application/ld+json"><?php echo wp_json_encode( $json ); ?></script>
863 <?php
864 }
865 }
866
867 public function print_child( $index, $item_id = null ) {
868 $children = $this->get_children();
869
870 if ( ! empty( $children[ $index ] ) ) {
871 // Add data-tab-index attribute to the content area.
872 $add_attribute_to_container = function ( $should_render, $container ) use ( $item_id ) {
873 $this->add_attributes_to_container( $container, $item_id );
874
875 return $should_render;
876 };
877
878 add_filter( 'elementor/frontend/container/should_render', $add_attribute_to_container, 10, 3 );
879 $children[ $index ]->print_element();
880 remove_filter( 'elementor/frontend/container/should_render', $add_attribute_to_container );
881 }
882 }
883
884 protected function add_attributes_to_container( $container, $item_id ) {
885 $container->add_render_attribute( '_wrapper', [
886 'role' => 'region',
887 'aria-labelledby' => $item_id,
888 ] );
889 }
890
891 protected function content_template() {
892 ?>
893 <div class="e-n-accordion" aria-label="Accordion. Open links with Enter or Space, close with Escape, and navigate with Arrow Keys">
894 <# if ( settings['items'] ) {
895 const elementUid = view.getIDInt().toString().substring( 0, 3 ),
896 titleHTMLTag = elementor.helpers.validateHTMLTag( settings.title_tag ),
897 defaultState = settings.default_state,
898 itemTitleIcon = elementor.helpers.renderIcon( view, settings['accordion_item_title_icon'], { 'aria-hidden': true }, 'i', 'object' ) ?? '',
899 itemTitleIconActive = '' === settings.accordion_item_title_icon_active.value
900 ? itemTitleIcon
901 : elementor.helpers.renderIcon( view, settings['accordion_item_title_icon_active'], { 'aria-hidden': true }, 'i', 'object' );
902 #>
903
904 <# _.each( settings['items'], function( item, index ) {
905 const itemCount = index + 1,
906 itemUid = elementUid + index,
907 itemTitleTextKey = 'item-title-text-' + itemUid,
908 itemWrapperKey = itemUid,
909 itemTitleKey = 'item-' + itemUid,
910 ariaExpanded = 'expanded' === defaultState && 0 === index ? 'true' : 'false';
911
912 if ( '' !== item.element_css_id ) {
913 itemId = item.element_css_id;
914 } else {
915 itemId = 'e-n-accordion-item-' + itemUid;
916 }
917
918 const itemWrapperAttributes = {
919 'id': itemId,
920 'class': [ 'e-n-accordion-item', 'e-normal' ],
921 };
922
923 if ( defaultState === 'expanded' && index === 0) {
924 itemWrapperAttributes['open'] = true;
925 }
926
927 view.addRenderAttribute( itemWrapperKey, itemWrapperAttributes );
928
929 view.addRenderAttribute( itemTitleKey, {
930 'class': ['e-n-accordion-item-title'],
931 'data-accordion-index': itemCount,
932 'tabindex': 0 === index ? 0 : -1,
933 'aria-expanded': ariaExpanded,
934 'aria-controls': itemId,
935 });
936
937 view.addRenderAttribute( itemTitleTextKey, {
938 'class': ['e-n-accordion-item-title-text'],
939 'data-binding-type': 'repeater-item',
940 'data-binding-repeater-name': 'items',
941 'data-binding-setting': ['item_title'],
942 'data-binding-index': itemCount,
943 });
944 #>
945
946 <details {{{ view.getRenderAttributeString( itemWrapperKey ) }}}>
947 <summary {{{ view.getRenderAttributeString( itemTitleKey ) }}}>
948 <span class="e-n-accordion-item-title-header">
949 <{{{ titleHTMLTag }}} {{{ view.getRenderAttributeString( itemTitleTextKey ) }}}>
950 {{{ item.item_title }}}
951 </{{{ titleHTMLTag }}}>
952 </span>
953 <# if (settings.accordion_item_title_icon.value) { #>
954 <span class="e-n-accordion-item-title-icon">
955 <span class="e-opened">{{{ itemTitleIconActive.value }}}</span>
956 <span class="e-closed">{{{ itemTitleIcon.value }}}</span>
957 </span>
958 <# } #>
959 </summary>
960 </details>
961 <# } ); #>
962 <# } #>
963 </div>
964 <?php
965 }
966 }
967