| 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 |
->meta( Overridable_Prop_Type::ignore() ), |
| 67 |
'attributes' => Attributes_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ), |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
protected function define_atomic_controls(): array { |
| 72 |
return [ |
| 73 |
Section::make() |
| 74 |
->set_label( __( 'Content', 'elementor' ) ) |
| 75 |
->set_id( 'content' ) |
| 76 |
->set_items( [ |
| 77 |
Tabs_Control::make() |
| 78 |
->set_label( __( 'Menu items', 'elementor' ) ) |
| 79 |
->set_meta( [ |
| 80 |
'layout' => 'custom', |
| 81 |
] ), |
| 82 |
] ), |
| 83 |
Section::make() |
| 84 |
->set_label( __( 'Settings', 'elementor' ) ) |
| 85 |
->set_id( 'settings' ) |
| 86 |
->set_items( [ |
| 87 |
Text_Control::bind_to( '_cssid' ) |
| 88 |
->set_label( __( 'ID', 'elementor' ) ) |
| 89 |
->set_meta( [ |
| 90 |
'layout' => 'two-columns', |
| 91 |
] ), |
| 92 |
] ), |
| 93 |
]; |
| 94 |
} |
| 95 |
|
| 96 |
protected function define_base_styles(): array { |
| 97 |
$styles = [ |
| 98 |
'display' => String_Prop_Type::generate( 'flex' ), |
| 99 |
'flex-direction' => String_Prop_Type::generate( 'column' ), |
| 100 |
'gap' => Size_Prop_Type::generate( [ |
| 101 |
'size' => 30, |
| 102 |
'unit' => 'px', |
| 103 |
]), |
| 104 |
'padding' => Dimensions_Prop_Type::generate( [ |
| 105 |
'block-start' => Size_Prop_Type::generate( [ |
| 106 |
'size' => 0, |
| 107 |
'unit' => 'px', |
| 108 |
]), |
| 109 |
] ), |
| 110 |
]; |
| 111 |
|
| 112 |
return [ |
| 113 |
static::BASE_STYLE_KEY => Style_Definition::make() |
| 114 |
->add_variant( |
| 115 |
Style_Variant::make() |
| 116 |
->add_props( $styles ) |
| 117 |
), |
| 118 |
]; |
| 119 |
} |
| 120 |
|
| 121 |
protected function define_default_children() { |
| 122 |
$default_tab_count = 3; |
| 123 |
$tab_elements = []; |
| 124 |
$tab_content_elements = []; |
| 125 |
|
| 126 |
foreach ( range( 1, $default_tab_count ) as $i ) { |
| 127 |
$tab_elements[] = Atomic_Tab::generate() |
| 128 |
->editor_settings( [ |
| 129 |
'title' => "Tab {$i} trigger", |
| 130 |
'initial_position' => $i, |
| 131 |
] ) |
| 132 |
->is_locked( true ) |
| 133 |
->build(); |
| 134 |
|
| 135 |
$tab_content_elements[] = Atomic_Tab_Content::generate() |
| 136 |
->is_locked( true ) |
| 137 |
->editor_settings( [ |
| 138 |
'title' => "Tab {$i} content", |
| 139 |
'initial_position' => $i, |
| 140 |
] ) |
| 141 |
->build(); |
| 142 |
} |
| 143 |
|
| 144 |
$tabs_menu = Atomic_Tabs_Menu::generate() |
| 145 |
->children( $tab_elements ) |
| 146 |
->is_locked( true ) |
| 147 |
->build(); |
| 148 |
|
| 149 |
$tabs_content_area = Atomic_Tabs_Content_Area::generate() |
| 150 |
->children( $tab_content_elements ) |
| 151 |
->is_locked( true ) |
| 152 |
->build(); |
| 153 |
|
| 154 |
return [ |
| 155 |
$tabs_menu, |
| 156 |
$tabs_content_area, |
| 157 |
]; |
| 158 |
} |
| 159 |
|
| 160 |
public function get_script_depends() { |
| 161 |
$global_depends = parent::get_script_depends(); |
| 162 |
|
| 163 |
if ( Plugin::$instance->preview->is_preview_mode() ) { |
| 164 |
return array_merge( $global_depends, [ 'elementor-tabs-handler', 'elementor-tabs-preview-handler' ] ); |
| 165 |
} |
| 166 |
|
| 167 |
return array_merge( $global_depends, [ 'elementor-tabs-handler' ] ); |
| 168 |
} |
| 169 |
|
| 170 |
public function register_frontend_handlers() { |
| 171 |
$assets_url = ELEMENTOR_ASSETS_URL; |
| 172 |
$min_suffix = ( Utils::is_script_debug() || Utils::is_elementor_tests() ) ? '' : '.min'; |
| 173 |
|
| 174 |
wp_register_script( |
| 175 |
'elementor-tabs-handler', |
| 176 |
"{$assets_url}js/tabs-handler{$min_suffix}.js", |
| 177 |
[ Frontend_Assets_Loader::FRONTEND_HANDLERS_HANDLE, Frontend_Assets_Loader::ALPINEJS_HANDLE ], |
| 178 |
ELEMENTOR_VERSION, |
| 179 |
true |
| 180 |
); |
| 181 |
|
| 182 |
wp_register_script( |
| 183 |
'elementor-tabs-preview-handler', |
| 184 |
"{$assets_url}js/tabs-preview-handler{$min_suffix}.js", |
| 185 |
[ Frontend_Assets_Loader::FRONTEND_HANDLERS_HANDLE, Frontend_Assets_Loader::ALPINEJS_HANDLE ], |
| 186 |
ELEMENTOR_VERSION, |
| 187 |
true |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
private function get_filtered_children_ids( $parent_element, $child_type ) { |
| 192 |
if ( ! $parent_element ) { |
| 193 |
return []; |
| 194 |
} |
| 195 |
|
| 196 |
return Collection::make( $parent_element->get_children() ) |
| 197 |
->filter( fn( $element ) => $element->get_type() === $child_type ) |
| 198 |
->map( fn( $element ) => $element->get_id() ) |
| 199 |
->flip() |
| 200 |
->all(); |
| 201 |
} |
| 202 |
|
| 203 |
private function get_tab_index( $tab_id ) { |
| 204 |
$direct_children = Collection::make( $this->get_children() ); |
| 205 |
$tabs_menu = $direct_children->filter( fn( $child ) => $child->get_type() === self::ELEMENT_TYPE_TABS_MENU )->first(); |
| 206 |
|
| 207 |
$tab_ids = $this->get_filtered_children_ids( $tabs_menu, self::ELEMENT_TYPE_TAB ); |
| 208 |
|
| 209 |
return $tab_ids[ $tab_id ]; |
| 210 |
} |
| 211 |
|
| 212 |
private function get_tab_content_index( $tab_content_id ) { |
| 213 |
$direct_children = Collection::make( $this->get_children() ); |
| 214 |
$tabs_content_area = $direct_children->filter( fn( $child ) => $child->get_type() === self::ELEMENT_TYPE_TABS_CONTENT_AREA )->first(); |
| 215 |
|
| 216 |
$tab_content_ids = $this->get_filtered_children_ids( $tabs_content_area, self::ELEMENT_TYPE_TAB_CONTENT ); |
| 217 |
|
| 218 |
return $tab_content_ids[ $tab_content_id ]; |
| 219 |
} |
| 220 |
|
| 221 |
protected function define_render_context(): array { |
| 222 |
$default_active_tab = $this->get_atomic_setting( 'default-active-tab' ); |
| 223 |
|
| 224 |
return [ |
| 225 |
'context' => [ |
| 226 |
'default-active-tab' => $default_active_tab, |
| 227 |
'get-tab-index' => fn( $tab_id ) => $this->get_tab_index( $tab_id ), |
| 228 |
'get-tab-content-index' => fn( $tab_content_id ) => $this->get_tab_content_index( $tab_content_id ), |
| 229 |
'tabs-id' => $this->get_id(), |
| 230 |
], |
| 231 |
]; |
| 232 |
} |
| 233 |
|
| 234 |
protected function add_render_attributes() { |
| 235 |
parent::add_render_attributes(); |
| 236 |
$settings = $this->get_atomic_settings(); |
| 237 |
$base_style_class = $this->get_base_styles_dictionary()[ static::BASE_STYLE_KEY ]; |
| 238 |
$initial_attributes = $this->define_initial_attributes(); |
| 239 |
|
| 240 |
$default_active_tab = $settings['default-active-tab'] ?? 0; |
| 241 |
$default_active_tab_id = static::get_tab_id( $this->get_id(), $default_active_tab ); |
| 242 |
|
| 243 |
$attributes = [ |
| 244 |
'class' => [ |
| 245 |
'e-con', |
| 246 |
'e-atomic-element', |
| 247 |
$base_style_class, |
| 248 |
...( $settings['classes'] ?? [] ), |
| 249 |
], |
| 250 |
'x-data' => 'eTabs' . $this->get_id(), |
| 251 |
'data-e-settings' => json_encode( [ 'default-active-tab' => esc_js( $default_active_tab_id ) ] ), |
| 252 |
]; |
| 253 |
|
| 254 |
if ( ! empty( $settings['_cssid'] ) ) { |
| 255 |
$attributes['id'] = esc_attr( $settings['_cssid'] ); |
| 256 |
} |
| 257 |
|
| 258 |
$this->add_render_attribute( '_wrapper', array_merge( $initial_attributes, $attributes ) ); |
| 259 |
} |
| 260 |
|
| 261 |
public static function get_tab_id( $tabs_id, $index ) { |
| 262 |
return "{$tabs_id}-tab-{$index}"; |
| 263 |
} |
| 264 |
|
| 265 |
public static function get_tab_content_id( $tabs_id, $index ) { |
| 266 |
return "{$tabs_id}-tab-content-{$index}"; |
| 267 |
} |
| 268 |
} |
| 269 |
|