PluginProbe
Elementor Website Builder – more than just a page builder / 3.23.0-dev2
Elementor Website Builder – more than just a page builder v3.23.0-dev2
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 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / nested-accordion / widgets / nested-accordion.php

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

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