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

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