PluginProbe
Elementor Website Builder – more than just a page builder / 3.17.2
Elementor Website Builder – more than just a page builder v3.17.2
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 4.1.0-beta2 4.1.0-dev2 All 455 releases
elementor / modules / nested-accordion / widgets / nested-accordion.php

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

970 lines 27.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 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 'title' => esc_html__( 'Add your custom id WITHOUT the Pound key. e.g: my-id', 'elementor' ),
116 'style_transfer' => false,
117 ]
118 );
119
120 $this->add_control(
121 'items',
122 [
123 'label' => esc_html__( 'Items', 'elementor' ),
124 'type' => Control_Nested_Repeater::CONTROL_TYPE,
125 'fields' => $repeater->get_controls(),
126 'default' => [
127 [
128 'item_title' => esc_html__( 'Item #1', 'elementor' ),
129 ],
130 [
131 'item_title' => esc_html__( 'Item #2', 'elementor' ),
132 ],
133 [
134 'item_title' => esc_html__( 'Item #3', 'elementor' ),
135 ],
136 ],
137 'title_field' => '{{{ item_title }}}',
138 'button_text' => 'Add Item',
139 ]
140 );
141
142 $this->add_responsive_control(
143 'accordion_item_title_position_horizontal',
144 [
145 'label' => esc_html__( 'Item Position', 'elementor' ),
146 'type' => Controls_Manager::CHOOSE,
147 'separator' => 'before',
148 'options' => [
149 'start' => [
150 'title' => esc_html__( 'Start', 'elementor' ),
151 'icon' => 'eicon-flex eicon-align-start-h',
152 ],
153 'center' => [
154 'title' => esc_html__( 'Center', 'elementor' ),
155 'icon' => 'eicon-h-align-center',
156 ],
157 'end' => [
158 'title' => esc_html__( 'End', 'elementor' ),
159 'icon' => 'eicon-flex eicon-align-end-h',
160 ],
161 'stretch' => [
162 'title' => esc_html__( 'Stretch', 'elementor' ),
163 'icon' => 'eicon-h-align-stretch',
164 ],
165 ],
166 'selectors_dictionary' => [
167 'start' => '--n-accordion-title-justify-content: initial; --n-accordion-title-flex-grow: initial;',
168 'center' => '--n-accordion-title-justify-content: center; --n-accordion-title-flex-grow: initial;',
169 'end' => '--n-accordion-title-justify-content: flex-end; --n-accordion-title-flex-grow: initial;',
170 'stretch' => '--n-accordion-title-justify-content: space-between; --n-accordion-title-flex-grow: 1;',
171 ],
172 'selectors' => [
173 '{{WRAPPER}}' => '{{VALUE}}',
174 ],
175 ]
176 );
177
178 $this->add_control(
179 'heading_accordion_item_title_icon',
180 [
181 'type' => Controls_Manager::HEADING,
182 'label' => esc_html__( 'Icon', 'elementor' ),
183 'separator' => 'before',
184 ]
185 );
186
187 $this->add_responsive_control(
188 'accordion_item_title_icon_position',
189 [
190 'label' => esc_html__( 'Position', 'elementor' ),
191 'type' => Controls_Manager::CHOOSE,
192 'options' => [
193 'start' => [
194 'title' => esc_html__( 'Start', 'elementor' ),
195 'icon' => 'eicon-h-align-left',
196 ],
197 'end' => [
198 'title' => esc_html__( 'End', 'elementor' ),
199 'icon' => 'eicon-h-align-right',
200 ],
201 ],
202 'selectors_dictionary' => [
203 'start' => '--n-accordion-title-icon-order: -1;',
204 'end' => '--n-accordion-title-icon-order: initial;',
205 ],
206 'selectors' => [
207 '{{WRAPPER}}' => '{{VALUE}}',
208 ],
209 ]
210 );
211
212 $this->add_control(
213 'accordion_item_title_icon',
214 [
215 'label' => esc_html__( 'Expand', 'elementor' ),
216 'type' => Controls_Manager::ICONS,
217 'default' => [
218 'value' => 'fas fa-plus',
219 'library' => 'fa-solid',
220 ],
221 'skin' => 'inline',
222 'label_block' => false,
223 ]
224 );
225
226 $this->add_control(
227 'accordion_item_title_icon_active',
228 [
229 'label' => esc_html__( 'Collapse', 'elementor' ),
230 'type' => Controls_Manager::ICONS,
231 'fa4compatibility' => 'icon_active',
232 'default' => [
233 'value' => 'fas fa-minus',
234 'library' => 'fa-solid',
235 ],
236 'condition' => [
237 'accordion_item_title_icon[value]!' => '',
238 ],
239 'skin' => 'inline',
240 'label_block' => false,
241 ]
242 );
243
244 $this->add_control(
245 'title_tag',
246 [
247 'label' => esc_html__( 'Title HTML Tag', 'elementor' ),
248 'type' => Controls_Manager::SELECT,
249 'options' => [
250 'h1' => 'H1',
251 'h2' => 'H2',
252 'h3' => 'H3',
253 'h4' => 'H4',
254 'h5' => 'H5',
255 'h6' => 'H6',
256 'div' => 'div',
257 'span' => 'span',
258 'p' => 'p',
259 ],
260 'selectors_dictionary' => [
261 'h1' => '--n-accordion-title-font-size: 2.5rem;',
262 'h2' => '--n-accordion-title-font-size: 2rem;',
263 'h3' => '--n-accordion-title-font-size: 1,75rem;',
264 'h4' => '--n-accordion-title-font-size: 1.5rem;',
265 'h5' => '--n-accordion-title-font-size: 1rem;',
266 'h6' => '--n-accordion-title-font-size: 1rem; ',
267 'div' => '--n-accordion-title-font-size: 1rem;',
268 'span' => '--n-accordion-title-font-size: 1rem; ',
269 'p' => '--n-accordion-title-font-size: 1rem;',
270 ],
271 'selectors' => [
272 '{{WRAPPER}}' => '{{VALUE}}',
273 ],
274 'default' => 'div',
275 'separator' => 'before',
276 'render_type' => 'template',
277
278 ]
279 );
280
281 $this->add_control(
282 'faq_schema',
283 [
284 'label' => esc_html__( 'FAQ Schema', 'elementor' ),
285 'type' => Controls_Manager::SWITCHER,
286 'label_on' => esc_html__( 'Yes', 'elementor' ),
287 'label_off' => esc_html__( 'No', 'elementor' ),
288 'default' => 'no',
289 ]
290 );
291
292 $this->add_control(
293 'faq_schema_message',
294 [
295 'type' => Controls_Manager::RAW_HTML,
296 'raw' => esc_html__( 'Let Google know that this section contains an FAQ. Make sure to only use it only once per page', 'elementor' ),
297 'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
298 'condition' => [
299 'faq_schema[value]' => 'yes',
300 ],
301 'separator' => 'none',
302 ]
303 );
304
305 $this->end_controls_section();
306
307 $this->start_controls_section(
308 'section_interactions',
309 [
310 'label' => esc_html__( 'Interactions', 'elementor' ),
311 ]
312 );
313
314 $this->add_control(
315 'default_state',
316 [
317 'label' => esc_html__( 'Default State', 'elementor' ),
318 'type' => Controls_Manager::SELECT,
319 'options' => [
320 'expanded' => esc_html__( 'First expanded', 'elementor' ),
321 'all_collapsed' => esc_html__( 'All collapsed', 'elementor' ),
322 ],
323 'default' => 'expanded',
324 'frontend_available' => true,
325 ]
326 );
327
328 $this->add_control(
329 'max_items_expended',
330 [
331 'label' => esc_html__( 'Max Items Expanded', 'elementor' ),
332 'type' => Controls_Manager::SELECT,
333 'options' => [
334 'one' => esc_html__( 'One', 'elementor' ),
335 'multiple' => esc_html__( 'Multiple', 'elementor' ),
336 ],
337 'default' => 'one',
338 'frontend_available' => true,
339 ]
340 );
341
342 $this->add_control(
343 'n_accordion_animation_duration',
344 [
345 'label' => esc_html__( 'Animation Duration', 'elementor' ),
346 'type' => Controls_Manager::SLIDER,
347 'size_units' => [ 's', 'ms' ],
348 'default' => [
349 'unit' => 'ms',
350 'size' => 400,
351 ],
352 'frontend_available' => true,
353 ]
354 );
355
356 $this->end_controls_section();
357
358 $this->add_style_tab();
359 }
360
361 private function add_style_tab() {
362 $this->add_accordion_style_section();
363 $this->add_header_style_section();
364 $this->add_content_style_section();
365 }
366
367 private function add_accordion_style_section() {
368 $this->start_controls_section(
369 'section_accordion_style',
370 [
371 'label' => esc_html__( 'Accordion', 'elementor' ),
372 'tab' => Controls_Manager::TAB_STYLE,
373 ]
374 );
375
376 $this->add_responsive_control(
377 'accordion_item_title_space_between',
378 [
379 'label' => esc_html__( 'Space between Items', 'elementor' ),
380 'type' => Controls_Manager::SLIDER,
381 'size_units' => [ 'px' ],
382 'range' => [
383 'px' => [
384 'min' => 0,
385 'max' => 200,
386 ],
387 ],
388 'default' => [
389 'size' => 0,
390 ],
391 'selectors' => [
392 '{{WRAPPER}}' => '--n-accordion-item-title-space-between: {{SIZE}}{{UNIT}}',
393 ],
394 ]
395 );
396
397 $this->add_responsive_control(
398 'accordion_item_title_distance_from_content',
399 [
400 'label' => esc_html__( 'Distance from content', 'elementor' ),
401 'type' => Controls_Manager::SLIDER,
402 'size_units' => [ 'px' ],
403 'range' => [
404 'px' => [
405 'min' => 0,
406 'max' => 200,
407 ],
408 ],
409 'default' => [
410 'size' => 0,
411 ],
412 'selectors' => [
413 '{{WRAPPER}}' => '--n-accordion-item-title-distance-from-content: {{SIZE}}{{UNIT}}',
414 ],
415 ]
416 );
417
418 $this->start_controls_tabs( 'accordion_border_and_background' );
419
420 foreach ( [ 'normal', 'hover', 'active' ] as $state ) {
421 $this->add_border_and_radius_style( $state );
422 }
423
424 $this->end_controls_tabs();
425
426 $this->add_responsive_control(
427 'accordion_border_radius',
428 [
429 'label' => esc_html__( 'Border Radius', 'elementor' ),
430 'type' => Controls_Manager::DIMENSIONS,
431 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
432 'selectors' => [
433 '{{WRAPPER}}' => '--n-accordion-border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
434 ],
435 'separator' => 'before',
436 ]
437 );
438
439 $this->add_responsive_control(
440 'accordion_padding',
441 [
442 'label' => esc_html__( 'Padding', 'elementor' ),
443 'type' => Controls_Manager::DIMENSIONS,
444 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
445 'selectors' => [
446 '{{WRAPPER}} ' => '--n-accordion-padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
447 ],
448 ]
449 );
450
451 $this->end_controls_section();
452 }
453
454 private function add_content_style_section() {
455
456 $low_specificity_accordion_item_selector = ':where( {{WRAPPER}} > .elementor-widget-container > .e-n-accordion > .e-n-accordion-item ) > .e-con';
457
458 $this->start_controls_section(
459 'section_content_style',
460 [
461 'label' => esc_html__( 'Content', 'elementor' ),
462 'tab' => Controls_Manager::TAB_STYLE,
463 ]
464 );
465
466 $this->add_group_control(
467 Group_Control_Background::get_type(),
468 [
469 'name' => 'content_background',
470 'types' => [ 'classic', 'gradient' ],
471 'exclude' => [ 'image' ],
472 'selector' => $low_specificity_accordion_item_selector,
473 ]
474 );
475
476 $this->add_group_control(
477 Group_Control_Border::get_type(),
478 [
479 'name' => 'content_border',
480 'selector' => $low_specificity_accordion_item_selector,
481 'fields_options' => [
482 'color' => [
483 'label' => esc_html__( 'Border Color', 'elementor' ),
484 ],
485 'width' => [
486 'label' => esc_html__( 'Border Width', 'elementor' ),
487 ],
488 ],
489 ]
490 );
491
492 $this->add_responsive_control(
493 'content_border_radius',
494 [
495 'label' => esc_html__( 'Border Radius', 'elementor' ),
496 'type' => Controls_Manager::DIMENSIONS,
497 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
498 'selectors' => [
499 $low_specificity_accordion_item_selector => '--border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
500 ],
501 ]
502 );
503
504 $logical_dimensions_inline_start = is_rtl() ? '{{RIGHT}}{{UNIT}}' : '{{LEFT}}{{UNIT}}';
505 $logical_dimensions_inline_end = is_rtl() ? '{{LEFT}}{{UNIT}}' : '{{RIGHT}}{{UNIT}}';
506
507 $this->add_responsive_control(
508 'content_padding',
509 [
510 'label' => esc_html__( 'Padding', 'elementor' ),
511 'type' => Controls_Manager::DIMENSIONS,
512 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
513 'selectors' => [
514 $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;",
515 ],
516 ]
517 );
518
519 $this->end_controls_section();
520 }
521
522 private function add_header_style_section() {
523 $this->start_controls_section(
524 'section_header_style',
525 [
526 'label' => esc_html__( 'Header', 'elementor' ),
527 'tab' => Controls_Manager::TAB_STYLE,
528 ]
529 );
530
531 $this->add_control(
532 'heading_header_style_title',
533 [
534 'type' => Controls_Manager::HEADING,
535 'label' => esc_html__( 'Title', 'elementor' ),
536
537 ]
538 );
539
540 $this->add_group_control(
541 Group_Control_Typography::get_type(),
542 [
543 'name' => 'title_typography',
544 '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',
545 'fields_options' => [
546 'font_size' => [
547 'selectors' => [
548 '{{WRAPPER}}' => '--n-accordion-title-font-size: {{SIZE}}{{UNIT}}',
549 ],
550 ],
551 ],
552 ]
553 );
554
555 $this->start_controls_tabs( 'header_title_color_style' );
556
557 foreach ( [ 'normal', 'hover', 'active' ] as $state ) {
558 $this->add_header_style( $state, 'title' );
559 }
560
561 $this->end_controls_tabs();
562
563 $this->add_control(
564 'header_section_divider',
565 [
566 'type' => Controls_Manager::DIVIDER,
567 ]
568 );
569
570 $this->add_control(
571 'heading_icon_style_title',
572 [
573 'type' => Controls_Manager::HEADING,
574 'label' => esc_html__( 'Icon', 'elementor' ),
575
576 ]
577 );
578
579 $this->add_responsive_control(
580 'icon_size',
581 [
582 'label' => esc_html__( 'Size', 'elementor' ),
583 'type' => Controls_Manager::SLIDER,
584 'range' => [
585 'em' => [
586 'min' => 0,
587 'max' => 10,
588 'step' => 0.1,
589 ],
590 'rem' => [
591 'min' => 0,
592 'max' => 10,
593 'step' => 0.1,
594 ],
595 ],
596 'default' => [
597 'unit' => 'px',
598 'size' => 15,
599 ],
600 'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
601 'selectors' => [
602 '{{WRAPPER}}' => '--n-accordion-icon-size: {{SIZE}}{{UNIT}}',
603 ],
604 ]
605 );
606
607 $this->add_responsive_control(
608 'icon_spacing',
609 [
610 'label' => esc_html__( 'Spacing', 'elementor' ),
611 'type' => Controls_Manager::SLIDER,
612 'range' => [
613 'px' => [
614 'min' => 0,
615 'max' => 400,
616 ],
617 'vw' => [
618 'min' => 0,
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 'role' => 'button',
808 'data-accordion-index' => $accordion_count,
809 'tabindex' => 0 === $index ? 0 : -1,
810 'aria-expanded' => $aria_expanded ? 'true' : 'false',
811 'aria-controls' => $item_id,
812 ] );
813
814 $title_render_attributes = $this->get_render_attribute_string( $item_setting_key );
815 $title_render_attributes = $title_render_attributes . ' ' . $is_open;
816
817 $summary_render_attributes = $this->get_render_attribute_string( $item_summary_key );
818
819 // items content.
820 ob_start();
821 $this->print_child( $index, $item_id );
822 $item_content = ob_get_clean();
823
824 $faq_schema[ $item_title ] = $item_content;
825
826 ob_start();
827 ?>
828 <details <?php echo wp_kses_post( $title_render_attributes ); ?>>
829 <summary <?php echo wp_kses_post( $summary_render_attributes ); ?> >
830 <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>
831 <?php if ( ! empty( $settings['accordion_item_title_icon']['value'] ) ) {
832 echo $icons_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
833 } ?>
834 </summary>
835 <?php echo $item_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
836 </details>
837 <?php
838 $items_title_html .= ob_get_clean();
839 }
840
841 ?>
842 <div <?php $this->print_render_attribute_string( 'elementor-accordion' ); ?>>
843 <?php echo $items_title_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
844 </div>
845 <?php
846 if ( isset( $settings['faq_schema'] ) && 'yes' === $settings['faq_schema'] ) {
847 $json = [
848 '@context' => 'https://schema.org',
849 '@type' => 'FAQPage',
850 'mainEntity' => [],
851 ];
852
853 foreach ( $faq_schema as $name => $text ) {
854 $json['mainEntity'][] = [
855 '@type' => 'Question',
856 'name' => wp_strip_all_tags( $name ),
857 'acceptedAnswer' => [
858 '@type' => 'Answer',
859 'text' => wp_strip_all_tags( $text ),
860 ],
861 ];
862 }
863 ?>
864 <script type="application/ld+json"><?php echo wp_json_encode( $json ); ?></script>
865 <?php
866 }
867 }
868
869 public function print_child( $index, $item_id = null ) {
870 $children = $this->get_children();
871
872 if ( ! empty( $children[ $index ] ) ) {
873 // Add data-tab-index attribute to the content area.
874 $add_attribute_to_container = function ( $should_render, $container ) use ( $item_id ) {
875 $this->add_attributes_to_container( $container, $item_id );
876
877 return $should_render;
878 };
879
880 add_filter( 'elementor/frontend/container/should_render', $add_attribute_to_container, 10, 3 );
881 $children[ $index ]->print_element();
882 remove_filter( 'elementor/frontend/container/should_render', $add_attribute_to_container );
883 }
884 }
885
886 protected function add_attributes_to_container( $container, $item_id ) {
887 $container->add_render_attribute( '_wrapper', [
888 'role' => 'region',
889 'aria-labelledby' => $item_id,
890 ] );
891 }
892
893 protected function content_template() {
894 ?>
895 <div class="e-n-accordion" aria-label="Accordion. Open links with Enter or Space, close with Escape, and navigate with Arrow Keys">
896 <# if ( settings['items'] ) {
897 const elementUid = view.getIDInt().toString().substring( 0, 3 ),
898 titleHTMLTag = elementor.helpers.validateHTMLTag( settings.title_tag ),
899 itemTitleText = 'item-title-text-' + elementUid,
900 defaultState = settings.default_state,
901 itemTitleIcon = elementor.helpers.renderIcon( view, settings['accordion_item_title_icon'], { 'aria-hidden': true }, 'i', 'object' ) ?? '',
902 itemTitleIconActive = '' === settings.accordion_item_title_icon_active.value
903 ? itemTitleIcon
904 : elementor.helpers.renderIcon( view, settings['accordion_item_title_icon_active'], { 'aria-hidden': true }, 'i', 'object' );
905 #>
906
907 <# _.each( settings['items'], function( item, index ) {
908 const itemCount = index + 1,
909 itemUid = elementUid + index,
910 itemWrapperKey = itemUid,
911 itemTitleKey = 'item-' + itemUid,
912 ariaExpanded = 'expanded' === defaultState && 0 === index ? 'true' : 'false';
913
914 if ( '' !== item.element_css_id ) {
915 itemId = item.element_css_id;
916 } else {
917 itemId = 'e-n-accordion-item-' + itemUid;
918 }
919
920 const itemWrapperAttributes = {
921 'id': itemId,
922 'class': [ 'e-n-accordion-item', 'e-normal' ],
923 };
924
925 if ( defaultState === 'expanded' && index === 0) {
926 itemWrapperAttributes['open'] = true;
927 }
928
929 view.addRenderAttribute( itemWrapperKey, itemWrapperAttributes );
930
931 view.addRenderAttribute( itemTitleKey, {
932 'class': ['e-n-accordion-item-title'],
933 'data-accordion-index': itemCount,
934 'tabindex': 0 === index ? 0 : -1,
935 'aria-expanded': ariaExpanded,
936 'aria-controls': itemId,
937 'role': 'button',
938 });
939
940 view.addRenderAttribute( itemTitleText, {
941 'class': ['e-n-accordion-item-title-text'],
942 'data-binding-type': 'repeater-item',
943 'data-binding-repeater-name': 'items',
944 'data-binding-setting': ['item_title'],
945 'data-binding-index': itemCount,
946 });
947 #>
948
949 <details {{{ view.getRenderAttributeString( itemWrapperKey ) }}}>
950 <summary {{{ view.getRenderAttributeString( itemTitleKey ) }}}>
951 <span class="e-n-accordion-item-title-header">
952 <{{{ titleHTMLTag }}} {{{ view.getRenderAttributeString( itemTitleText ) }}}>
953 {{{ item.item_title }}}
954 </{{{ titleHTMLTag }}}>
955 </span>
956 <# if (settings.accordion_item_title_icon.value) { #>
957 <span class="e-n-accordion-item-title-icon">
958 <span class="e-opened">{{{ itemTitleIconActive.value }}}</span>
959 <span class="e-closed">{{{ itemTitleIcon.value }}}</span>
960 </span>
961 <# } #>
962 </summary>
963 </details>
964 <# } ); #>
965 <# } #>
966 </div>
967 <?php
968 }
969 }
970