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

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