PluginProbe
Elementor Website Builder – more than just a page builder / 3.35.0-dev1
Elementor Website Builder – more than just a page builder v3.35.0-dev1
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 / atomic-widgets / elements / atomic-tabs / atomic-tabs.php

atomic-tabs.php in Elementor Website Builder – more than just a page builder 3.35.0-dev1, at modules/atomic-widgets/elements/atomic-tabs/atomic-tabs.php

268 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Tabs;
3
4 use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Element_Base;
5 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
6 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
7 use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
8 use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
9 use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
10 use Elementor\Modules\AtomicWidgets\Controls\Section;
11 use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control;
12 use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
13 use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type;
14 use Elementor\Modules\AtomicWidgets\PropTypes\Dimensions_Prop_Type;
15 use Elementor\Modules\AtomicWidgets\Controls\Types\Elements\Tabs_Control;
16 use Elementor\Modules\AtomicWidgets\Elements\Loader\Frontend_Assets_Loader;
17 use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type;
18 use Elementor\Core\Utils\Collection;
19 use Elementor\Utils;
20 use Elementor\Plugin;
21
22 if ( ! defined( 'ABSPATH' ) ) {
23 exit; // Exit if accessed directly.
24 }
25
26 class Atomic_Tabs extends Atomic_Element_Base {
27 const BASE_STYLE_KEY = 'base';
28 const ELEMENT_TYPE_TABS_MENU = 'e-tabs-menu';
29 const ELEMENT_TYPE_TABS_CONTENT_AREA = 'e-tabs-content-area';
30 const ELEMENT_TYPE_TAB = 'e-tab';
31 const ELEMENT_TYPE_TAB_CONTENT = 'e-tab-content';
32
33 public static $widget_description = 'Create a tabbed interface with customizable tabs and content areas. LLM support: Each child element will be represented as a tab, the menu auto-generates based on the children';
34
35 public function __construct( $data = [], $args = null ) {
36 parent::__construct( $data, $args );
37 $this->meta( 'is_container', true );
38 }
39
40 public static function get_type() {
41 return 'e-tabs';
42 }
43
44 public static function get_element_type(): string {
45 return 'e-tabs';
46 }
47
48 public function get_title() {
49 return esc_html__( 'Tabs', 'elementor' );
50 }
51
52 public function get_keywords() {
53 return [ 'ato', 'atom', 'atoms', 'atomic' ];
54 }
55
56 public function get_icon() {
57 return 'eicon-tabs';
58 }
59
60 protected static function define_props_schema(): array {
61 return [
62 'classes' => Classes_Prop_Type::make()
63 ->default( [] ),
64 'default-active-tab' => Number_Prop_Type::make()
65 ->default( 0 ),
66 'attributes' => Attributes_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ),
67 ];
68 }
69
70 protected function define_atomic_controls(): array {
71 return [
72 Section::make()
73 ->set_label( __( 'Content', 'elementor' ) )
74 ->set_id( 'content' )
75 ->set_items( [
76 Tabs_Control::make()
77 ->set_label( __( 'Menu items', 'elementor' ) )
78 ->set_meta( [
79 'layout' => 'custom',
80 ] ),
81 ] ),
82 Section::make()
83 ->set_label( __( 'Settings', 'elementor' ) )
84 ->set_id( 'settings' )
85 ->set_items( [
86 Text_Control::bind_to( '_cssid' )
87 ->set_label( __( 'ID', 'elementor' ) )
88 ->set_meta( [
89 'layout' => 'two-columns',
90 ] ),
91 ] ),
92 ];
93 }
94
95 protected function define_base_styles(): array {
96 $styles = [
97 'display' => String_Prop_Type::generate( 'flex' ),
98 'flex-direction' => String_Prop_Type::generate( 'column' ),
99 'gap' => Size_Prop_Type::generate( [
100 'size' => 30,
101 'unit' => 'px',
102 ]),
103 'padding' => Dimensions_Prop_Type::generate( [
104 'block-start' => Size_Prop_Type::generate( [
105 'size' => 0,
106 'unit' => 'px',
107 ]),
108 ] ),
109 ];
110
111 return [
112 static::BASE_STYLE_KEY => Style_Definition::make()
113 ->add_variant(
114 Style_Variant::make()
115 ->add_props( $styles )
116 ),
117 ];
118 }
119
120 protected function define_default_children() {
121 $default_tab_count = 3;
122 $tab_elements = [];
123 $tab_content_elements = [];
124
125 foreach ( range( 1, $default_tab_count ) as $i ) {
126 $tab_elements[] = Atomic_Tab::generate()
127 ->editor_settings( [
128 'title' => "Tab {$i} trigger",
129 'initial_position' => $i,
130 ] )
131 ->is_locked( true )
132 ->build();
133
134 $tab_content_elements[] = Atomic_Tab_Content::generate()
135 ->is_locked( true )
136 ->editor_settings( [
137 'title' => "Tab {$i} content",
138 'initial_position' => $i,
139 ] )
140 ->build();
141 }
142
143 $tabs_menu = Atomic_Tabs_Menu::generate()
144 ->children( $tab_elements )
145 ->is_locked( true )
146 ->build();
147
148 $tabs_content_area = Atomic_Tabs_Content_Area::generate()
149 ->children( $tab_content_elements )
150 ->is_locked( true )
151 ->build();
152
153 return [
154 $tabs_menu,
155 $tabs_content_area,
156 ];
157 }
158
159 public function get_script_depends() {
160 $global_depends = parent::get_script_depends();
161
162 if ( Plugin::$instance->preview->is_preview_mode() ) {
163 return array_merge( $global_depends, [ 'elementor-tabs-handler', 'elementor-tabs-preview-handler' ] );
164 }
165
166 return array_merge( $global_depends, [ 'elementor-tabs-handler' ] );
167 }
168
169 public function register_frontend_handlers() {
170 $assets_url = ELEMENTOR_ASSETS_URL;
171 $min_suffix = ( Utils::is_script_debug() || Utils::is_elementor_tests() ) ? '' : '.min';
172
173 wp_register_script(
174 'elementor-tabs-handler',
175 "{$assets_url}js/tabs-handler{$min_suffix}.js",
176 [ Frontend_Assets_Loader::FRONTEND_HANDLERS_HANDLE, Frontend_Assets_Loader::ALPINEJS_HANDLE ],
177 ELEMENTOR_VERSION,
178 true
179 );
180
181 wp_register_script(
182 'elementor-tabs-preview-handler',
183 "{$assets_url}js/tabs-preview-handler{$min_suffix}.js",
184 [ Frontend_Assets_Loader::FRONTEND_HANDLERS_HANDLE, Frontend_Assets_Loader::ALPINEJS_HANDLE ],
185 ELEMENTOR_VERSION,
186 true
187 );
188 }
189
190 private function get_filtered_children_ids( $parent_element, $child_type ) {
191 if ( ! $parent_element ) {
192 return [];
193 }
194
195 return Collection::make( $parent_element->get_children() )
196 ->filter( fn( $element ) => $element->get_type() === $child_type )
197 ->map( fn( $element ) => $element->get_id() )
198 ->flip()
199 ->all();
200 }
201
202 private function get_tab_index( $tab_id ) {
203 $direct_children = Collection::make( $this->get_children() );
204 $tabs_menu = $direct_children->filter( fn( $child ) => $child->get_type() === self::ELEMENT_TYPE_TABS_MENU )->first();
205
206 $tab_ids = $this->get_filtered_children_ids( $tabs_menu, self::ELEMENT_TYPE_TAB );
207
208 return $tab_ids[ $tab_id ];
209 }
210
211 private function get_tab_content_index( $tab_content_id ) {
212 $direct_children = Collection::make( $this->get_children() );
213 $tabs_content_area = $direct_children->filter( fn( $child ) => $child->get_type() === self::ELEMENT_TYPE_TABS_CONTENT_AREA )->first();
214
215 $tab_content_ids = $this->get_filtered_children_ids( $tabs_content_area, self::ELEMENT_TYPE_TAB_CONTENT );
216
217 return $tab_content_ids[ $tab_content_id ];
218 }
219
220 protected function define_render_context(): array {
221 $default_active_tab = $this->get_atomic_setting( 'default-active-tab' );
222
223 return [
224 'context' => [
225 'default-active-tab' => $default_active_tab,
226 'get-tab-index' => fn( $tab_id ) => $this->get_tab_index( $tab_id ),
227 'get-tab-content-index' => fn( $tab_content_id ) => $this->get_tab_content_index( $tab_content_id ),
228 'tabs-id' => $this->get_id(),
229 ],
230 ];
231 }
232
233 protected function add_render_attributes() {
234 parent::add_render_attributes();
235 $settings = $this->get_atomic_settings();
236 $base_style_class = $this->get_base_styles_dictionary()[ static::BASE_STYLE_KEY ];
237 $initial_attributes = $this->define_initial_attributes();
238
239 $default_active_tab = $settings['default-active-tab'] ?? 0;
240 $default_active_tab_id = static::get_tab_id( $this->get_id(), $default_active_tab );
241
242 $attributes = [
243 'class' => [
244 'e-con',
245 'e-atomic-element',
246 $base_style_class,
247 ...( $settings['classes'] ?? [] ),
248 ],
249 'x-data' => 'eTabs' . $this->get_id(),
250 'data-e-settings' => json_encode( [ 'default-active-tab' => esc_js( $default_active_tab_id ) ] ),
251 ];
252
253 if ( ! empty( $settings['_cssid'] ) ) {
254 $attributes['id'] = esc_attr( $settings['_cssid'] );
255 }
256
257 $this->add_render_attribute( '_wrapper', array_merge( $initial_attributes, $attributes ) );
258 }
259
260 public static function get_tab_id( $tabs_id, $index ) {
261 return "{$tabs_id}-tab-{$index}";
262 }
263
264 public static function get_tab_content_id( $tabs_id, $index ) {
265 return "{$tabs_id}-tab-content-{$index}";
266 }
267 }
268