PluginProbe
Elementor Website Builder – more than just a page builder / 3.24.7
Elementor Website Builder – more than just a page builder v3.24.7
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.24.7, at modules/nested-accordion/widgets/nested-accordion.php

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