← All changes
|
modules/atomic-widgets/elements/base/has-atomic-base.php
+153
-154
4.2.2
→
3.35.7
View file →
| @@ -4,10 +4,8 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Elementor\Element_Base; |
| 6 | 6 | use Elementor\Modules\AtomicWidgets\Controls\Base\Atomic_Control_Base; |
| 7 | 7 | use Elementor\Modules\AtomicWidgets\Controls\Section; |
| 8 | -use Elementor\Modules\AtomicWidgets\Elements\Atomic_Form\Atomic_Form; | |
| 9 | -use Elementor\Modules\AtomicWidgets\Elements\Loader\Frontend_Assets_Loader; | |
| 10 | 8 | use Elementor\Modules\AtomicWidgets\PropsResolver\Render_Props_Resolver; |
| 11 | 9 | use Elementor\Modules\AtomicWidgets\PropTypes\Contracts\Prop_Type; |
| 12 | 10 | use Elementor\Modules\AtomicWidgets\Styles\Style_Schema; |
| 13 | 11 | use Elementor\Modules\AtomicWidgets\Parsers\Props_Parser; |
| @@ -28,9 +26,8 @@ | ||
| 28 | 26 | * @mixin Element_Base |
| 29 | 27 | */ |
| 30 | 28 | trait Has_Atomic_Base { |
| 31 | 29 | use Has_Base_Styles; |
| 32 | - use Has_Base_Settings; | |
| 33 | 30 | |
| 34 | 31 | public function has_widget_inner_wrapper(): bool { |
| 35 | 32 | return false; |
| 36 | 33 | } |
| @@ -93,11 +90,10 @@ | ||
| 93 | 90 | foreach ( $styles as $style_id => $style ) { |
| 94 | 91 | $result = $style_parser->parse( $style ); |
| 95 | 92 | |
| 96 | 93 | if ( ! $result->is_valid() ) { |
| 97 | - throw new \Exception( | |
| 98 | - esc_html( $this->format_styles_validation_error_message( $style_id, $data, $style, $result->errors()->to_string() ) ) | |
| 99 | - ); | |
| 94 | + $widget_id = $data['id'] ?? 'unknown'; | |
| 95 | + throw new \Exception( esc_html( "Styles validation failed for style `$style_id`. Widget ID: `$widget_id`. " . $result->errors()->to_string() ) ); | |
| 100 | 96 | } |
| 101 | 97 | |
| 102 | 98 | $styles[ $style_id ] = $result->unwrap(); |
| 103 | 99 | } |
| @@ -104,51 +100,8 @@ | ||
| 104 | 100 | |
| 105 | 101 | return $styles; |
| 106 | 102 | } |
| 107 | 103 | |
| 108 | - private function format_styles_validation_error_message( | |
| 109 | - string $style_id, | |
| 110 | - array $data, | |
| 111 | - array $style, | |
| 112 | - string $validation_errors | |
| 113 | - ): string { | |
| 114 | - $widget_id = $data['id'] ?? 'unknown'; | |
| 115 | - $structure_label = $this->get_editor_structure_label( $data ); | |
| 116 | - $style_label = isset( $style['label'] ) && is_string( $style['label'] ) ? $style['label'] : null; | |
| 117 | - | |
| 118 | - $message_parts = [ | |
| 119 | - "Styles validation failed for style `$style_id` (widget `$widget_id`)", | |
| 120 | - ]; | |
| 121 | - | |
| 122 | - if ( $structure_label ) { | |
| 123 | - $message_parts[] = "Structure label: `$structure_label`"; | |
| 124 | - } else { | |
| 125 | - $element_name = $this->get_title(); | |
| 126 | - | |
| 127 | - if ( '' === $element_name ) { | |
| 128 | - $element_name = $this->get_name(); | |
| 129 | - } | |
| 130 | - | |
| 131 | - $message_parts[] = "Element: `$element_name`"; | |
| 132 | - } | |
| 133 | - | |
| 134 | - if ( $style_label ) { | |
| 135 | - $message_parts[] = "Style label: `$style_label`"; | |
| 136 | - } | |
| 137 | - | |
| 138 | - return implode( '. ', $message_parts ) . '. ' . $validation_errors; | |
| 139 | - } | |
| 140 | - | |
| 141 | - private function get_editor_structure_label( array $data ): ?string { | |
| 142 | - $title = $data['editor_settings']['title'] ?? $this->editor_settings['title'] ?? null; | |
| 143 | - | |
| 144 | - if ( ! is_string( $title ) || '' === $title ) { | |
| 145 | - return null; | |
| 146 | - } | |
| 147 | - | |
| 148 | - return $title; | |
| 149 | - } | |
| 150 | - | |
| 151 | 104 | private function parse_atomic_settings( array $settings ): array { |
| 152 | 105 | $schema = static::get_props_schema(); |
| 153 | 106 | $props_parser = Props_Parser::make( $schema ); |
| 154 | 107 | |
| @@ -180,8 +133,67 @@ | ||
| 180 | 133 | |
| 181 | 134 | return []; |
| 182 | 135 | } |
| 183 | 136 | |
| 137 | + private function convert_prop_type_interactions_to_legacy( $interactions ) { | |
| 138 | + $legacy_items = []; | |
| 139 | + | |
| 140 | + foreach ( $interactions['items'] as $item ) { | |
| 141 | + if ( isset( $item['$$type'] ) && 'interaction-item' === $item['$$type'] ) { | |
| 142 | + $legacy_item = $this->extract_legacy_interaction_from_prop_type( $item ); | |
| 143 | + if ( $legacy_item ) { | |
| 144 | + $legacy_items[] = $legacy_item; | |
| 145 | + } | |
| 146 | + } else { | |
| 147 | + $legacy_items[] = $item; | |
| 148 | + } | |
| 149 | + } | |
| 150 | + | |
| 151 | + return [ | |
| 152 | + 'version' => $interactions['version'] ?? 1, | |
| 153 | + 'items' => $legacy_items, | |
| 154 | + ]; | |
| 155 | + } | |
| 156 | + | |
| 157 | + private function extract_legacy_interaction_from_prop_type( $item ) { | |
| 158 | + if ( ! isset( $item['value'] ) || ! is_array( $item['value'] ) ) { | |
| 159 | + return null; | |
| 160 | + } | |
| 161 | + | |
| 162 | + $item_value = $item['value']; | |
| 163 | + | |
| 164 | + $interaction_id = $this->extract_prop_value( $item_value, 'interaction_id' ); | |
| 165 | + $trigger = $this->extract_prop_value( $item_value, 'trigger' ); | |
| 166 | + $animation = $this->extract_prop_value( $item_value, 'animation' ); | |
| 167 | + | |
| 168 | + if ( ! is_array( $animation ) ) { | |
| 169 | + return null; | |
| 170 | + } | |
| 171 | + | |
| 172 | + $effect = $this->extract_prop_value( $animation, 'effect' ); | |
| 173 | + $type = $this->extract_prop_value( $animation, 'type' ); | |
| 174 | + $direction = $this->extract_prop_value( $animation, 'direction' ); | |
| 175 | + $timing_config = $this->extract_prop_value( $animation, 'timing_config' ); | |
| 176 | + | |
| 177 | + $duration = 300; | |
| 178 | + $delay = 0; | |
| 179 | + | |
| 180 | + if ( is_array( $timing_config ) ) { | |
| 181 | + $duration = $this->extract_prop_value( $timing_config, 'duration', 300 ); | |
| 182 | + $delay = $this->extract_prop_value( $timing_config, 'delay', 0 ); | |
| 183 | + } | |
| 184 | + | |
| 185 | + $animation_id = implode( '-', [ $trigger, $effect, $type, $direction, $duration, $delay ] ); | |
| 186 | + | |
| 187 | + return [ | |
| 188 | + 'interaction_id' => $interaction_id, | |
| 189 | + 'animation' => [ | |
| 190 | + 'animation_id' => $animation_id, | |
| 191 | + 'animation_type' => 'full-preset', | |
| 192 | + ], | |
| 193 | + ]; | |
| 194 | + } | |
| 195 | + | |
| 184 | 196 | private function extract_prop_value( $data, $key, $default = '' ) { |
| 185 | 197 | if ( ! is_array( $data ) || ! isset( $data[ $key ] ) ) { |
| 186 | 198 | return $default; |
| 187 | 199 | } |
| @@ -287,31 +299,18 @@ | ||
| 287 | 299 | |
| 288 | 300 | public function get_atomic_settings(): array { |
| 289 | 301 | $schema = static::get_props_schema(); |
| 290 | 302 | $props = $this->get_settings(); |
| 303 | + $initial_attributes = $this->get_initial_attributes(); | |
| 291 | 304 | |
| 292 | - $merged_attribute_values = array_merge( | |
| 293 | - $this->get_initial_attributes()['value'] ?? [], | |
| 305 | + $props['attributes'] = Attributes_Prop_Type::generate( array_merge( | |
| 306 | + $initial_attributes['value'] ?? [], | |
| 294 | 307 | $props['attributes']['value'] ?? [] |
| 295 | - ); | |
| 296 | - $props['attributes'] = Attributes_Prop_Type::generate( $merged_attribute_values ); | |
| 308 | + ) ); | |
| 297 | 309 | |
| 298 | - $parsed = Render_Props_Resolver::for_settings()->resolve( $schema, $props ); | |
| 299 | - | |
| 300 | - return $this->transform_link_for_render( $parsed ); | |
| 310 | + return Render_Props_Resolver::for_settings()->resolve( $schema, $props ); | |
| 301 | 311 | } |
| 302 | 312 | |
| 303 | - protected function transform_link_for_render( array $parsed ): array { | |
| 304 | - $link_attributes = isset( $parsed['link'] ) ? $this->get_link_attributes_string( $parsed['link'] ) : ''; | |
| 305 | - | |
| 306 | - $parsed['link'] = ! empty( $link_attributes ) ? [ | |
| 307 | - 'tag' => $parsed['link']['tag'], | |
| 308 | - 'attributes' => $link_attributes, | |
| 309 | - ] : null; | |
| 310 | - | |
| 311 | - return $parsed; | |
| 312 | - } | |
| 313 | - | |
| 314 | 313 | protected function get_initial_attributes() { |
| 315 | 314 | return Attributes_Prop_Type::generate( [ |
| 316 | 315 | Key_Value_Prop_Type::generate( [ |
| 317 | 316 | 'key' => String_Prop_Type::generate( 'data-e-type' ), |
| @@ -348,22 +347,15 @@ | ||
| 348 | 347 | if ( isset( $data['title'] ) && is_string( $data['title'] ) ) { |
| 349 | 348 | $editor_data['title'] = sanitize_text_field( $data['title'] ); |
| 350 | 349 | } |
| 351 | 350 | |
| 352 | - if ( isset( $data['grid_outline'] ) && is_bool( $data['grid_outline'] ) ) { | |
| 353 | - $editor_data['grid_outline'] = $data['grid_outline']; | |
| 354 | - } | |
| 355 | - | |
| 356 | 351 | return $editor_data; |
| 357 | 352 | } |
| 358 | 353 | |
| 359 | 354 | public static function get_props_schema(): array { |
| 360 | 355 | $schema = static::define_props_schema(); |
| 356 | + $schema['_cssid'] = String_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ); | |
| 361 | 357 | |
| 362 | - if ( ! isset( $schema['_cssid'] ) ) { | |
| 363 | - $schema['_cssid'] = String_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ); | |
| 364 | - } | |
| 365 | - | |
| 366 | 358 | return apply_filters( |
| 367 | 359 | 'elementor/atomic-widgets/props-schema', |
| 368 | 360 | $schema |
| 369 | 361 | ); |
| @@ -368,117 +360,124 @@ | ||
| 368 | 360 | $schema |
| 369 | 361 | ); |
| 370 | 362 | } |
| 371 | 363 | |
| 372 | - protected function set_render_context( array $context_pairs ): void { | |
| 373 | - foreach ( $context_pairs as $context_pair ) { | |
| 374 | - $context_key = $context_pair['context_key'] ?? static::class; | |
| 375 | - $context = $context_pair['context']; | |
| 376 | - Render_Context::push( $context_key, $context ); | |
| 364 | + public function get_interactions_ids() { | |
| 365 | + $animation_ids = []; | |
| 366 | + | |
| 367 | + $list_of_interactions = ( is_array( $this->interactions ) && isset( $this->interactions['items'] ) ) | |
| 368 | + ? $this->interactions['items'] | |
| 369 | + : []; | |
| 370 | + | |
| 371 | + foreach ( $list_of_interactions as $interaction ) { | |
| 372 | + if ( isset( $interaction['$$type'] ) && 'interaction-item' === $interaction['$$type'] ) { | |
| 373 | + $animation_id = $this->extract_animation_id_from_prop_type( $interaction ); | |
| 374 | + if ( $animation_id ) { | |
| 375 | + $animation_ids[] = $animation_id; | |
| 376 | + } | |
| 377 | + } elseif ( isset( $interaction['animation']['animation_id'] ) ) { | |
| 378 | + $animation_ids[] = $interaction['animation']['animation_id']; | |
| 379 | + } | |
| 377 | 380 | } |
| 381 | + | |
| 382 | + return $animation_ids; | |
| 378 | 383 | } |
| 379 | 384 | |
| 380 | - protected function clear_render_context( array $context_pairs ): void { | |
| 381 | - foreach ( $context_pairs as $context_pair ) { | |
| 382 | - $context_key = $context_pair['context_key'] ?? static::class; | |
| 383 | - Render_Context::pop( $context_key ); | |
| 385 | + private function extract_animation_id_from_prop_type( $item ) { | |
| 386 | + if ( ! isset( $item['value'] ) || ! is_array( $item['value'] ) ) { | |
| 387 | + return null; | |
| 384 | 388 | } |
| 389 | + | |
| 390 | + $item_value = $item['value']; | |
| 391 | + | |
| 392 | + $trigger = $this->extract_prop_value( $item_value, 'trigger' ); | |
| 393 | + $animation = $this->extract_prop_value( $item_value, 'animation' ); | |
| 394 | + | |
| 395 | + if ( ! is_array( $animation ) ) { | |
| 396 | + return null; | |
| 397 | + } | |
| 398 | + | |
| 399 | + $effect = $this->extract_prop_value( $animation, 'effect' ); | |
| 400 | + $type = $this->extract_prop_value( $animation, 'type' ); | |
| 401 | + $direction = $this->extract_prop_value( $animation, 'direction' ); | |
| 402 | + $timing_config = $this->extract_prop_value( $animation, 'timing_config' ); | |
| 403 | + $config = $this->extract_prop_value( $animation, 'config' ); | |
| 404 | + | |
| 405 | + $duration = 300; | |
| 406 | + $delay = 0; | |
| 407 | + $replay = 0; | |
| 408 | + $relative_to = 'viewport'; | |
| 409 | + $offset_top = 15; | |
| 410 | + $offset_bottom = 85; | |
| 411 | + | |
| 412 | + if ( is_array( $timing_config ) ) { | |
| 413 | + $duration = $this->extract_prop_value( $timing_config, 'duration', 300 ); | |
| 414 | + $delay = $this->extract_prop_value( $timing_config, 'delay', 0 ); | |
| 415 | + } | |
| 416 | + | |
| 417 | + if ( is_array( $config ) ) { | |
| 418 | + $relative_to = $this->extract_prop_value( $config, 'relative_to', 'viewport' ); | |
| 419 | + $offset_top = $this->extract_prop_value( $config, 'offset_top', 15 ); | |
| 420 | + $offset_bottom = $this->extract_prop_value( $config, 'offset_bottom', 85 ); | |
| 421 | + | |
| 422 | + $replay = $this->extract_prop_value( $config, 'replay', 0 ); | |
| 423 | + if ( empty( $replay ) && 0 !== $replay && '0' !== $replay ) { | |
| 424 | + $replay = 0; | |
| 425 | + } | |
| 426 | + } else { | |
| 427 | + $replay = 0; | |
| 428 | + } | |
| 429 | + | |
| 430 | + return implode( '-', [ $trigger, $effect, $type, $direction, $duration, $delay, $replay, $relative_to, $offset_top, $offset_bottom ] ); | |
| 385 | 431 | } |
| 386 | 432 | |
| 387 | 433 | public function print_content() { |
| 388 | 434 | $defined_context = $this->define_render_context(); |
| 389 | 435 | |
| 390 | - if ( empty( $defined_context ) ) { | |
| 436 | + $context_key = $defined_context['context_key'] ?? static::class; | |
| 437 | + $element_context = $defined_context['context'] ?? []; | |
| 438 | + | |
| 439 | + $has_context = ! empty( $element_context ); | |
| 440 | + | |
| 441 | + if ( ! $has_context ) { | |
| 391 | 442 | return parent::print_content(); |
| 392 | 443 | } |
| 393 | 444 | |
| 394 | - $this->set_render_context( $defined_context ); | |
| 445 | + Render_Context::push( $context_key, $element_context ); | |
| 395 | 446 | |
| 396 | 447 | parent::print_content(); |
| 397 | 448 | |
| 398 | - $this->clear_render_context( $defined_context ); | |
| 449 | + Render_Context::pop( $context_key ); | |
| 399 | 450 | } |
| 400 | 451 | |
| 401 | 452 | /** |
| 402 | 453 | * Define the context for element's Render_Context. |
| 403 | 454 | * |
| 404 | - * @return array Array of context pairs. Each pair is an associative array with: | |
| 405 | - * - 'context_key' (optional): The context key. Defaults to static::class if not provided. | |
| 406 | - * - 'context' (required): The context value (can be any type). | |
| 407 | - * | |
| 408 | - * @example | |
| 409 | - * [ | |
| 410 | - * [ | |
| 411 | - * 'context_key' => 'custom-key', | |
| 412 | - * 'context' => ['some' => 'data'], | |
| 413 | - * ], | |
| 414 | - * [ | |
| 415 | - * 'context' => ['instance_id' => $this->get_id()], | |
| 416 | - * ], | |
| 417 | - * ] | |
| 455 | + * @return array{context_key: ?string, context: array} | |
| 418 | 456 | */ |
| 419 | 457 | protected function define_render_context(): array { |
| 420 | - return []; | |
| 458 | + return [ | |
| 459 | + 'context_key' => null, | |
| 460 | + 'context' => [], | |
| 461 | + ]; | |
| 421 | 462 | } |
| 422 | 463 | |
| 423 | - protected function get_link_attributes( $link_settings ) { | |
| 424 | - if ( empty( $link_settings['href'] ) ) { | |
| 425 | - return []; | |
| 426 | - } | |
| 427 | - | |
| 464 | + protected function get_link_attributes( $link_settings, $add_key_to_result = false ) { | |
| 428 | 465 | $tag = $link_settings['tag'] ?? Link_Prop_Type::DEFAULT_TAG; |
| 429 | - $url = $link_settings['href']; | |
| 466 | + $href = $link_settings['href']; | |
| 430 | 467 | $target = $link_settings['target'] ?? '_self'; |
| 431 | - $is_action_link = 'button' === $tag; | |
| 432 | - $url_attr_key = $is_action_link ? 'data-action-link' : 'href'; | |
| 433 | 468 | |
| 434 | - return [ | |
| 435 | - $url_attr_key => $url, | |
| 469 | + $is_button = 'button' === $tag; | |
| 470 | + $href_attribute_key = $is_button ? 'data-action-link' : 'href'; | |
| 471 | + | |
| 472 | + $result = [ | |
| 473 | + $href_attribute_key => $href, | |
| 436 | 474 | 'target' => $target, |
| 437 | 475 | ]; |
| 438 | - } | |
| 439 | 476 | |
| 440 | - private function get_link_attributes_string( $link_settings ) { | |
| 441 | - $link_attributes = $this->get_link_attributes( $link_settings ); | |
| 442 | - | |
| 443 | - if ( empty( $link_attributes ) ) { | |
| 444 | - return ''; | |
| 477 | + if ( $add_key_to_result ) { | |
| 478 | + $result['key'] = $href_attribute_key; | |
| 445 | 479 | } |
| 446 | 480 | |
| 447 | - $parts = []; | |
| 448 | - | |
| 449 | - foreach ( $link_attributes as $key => $value ) { | |
| 450 | - if ( 'tag' === $key ) { | |
| 451 | - continue; | |
| 452 | - } | |
| 453 | - | |
| 454 | - $parts[] = sprintf( '%s="%s"', $key, esc_attr( $value ) ); | |
| 455 | - } | |
| 456 | - | |
| 457 | - return implode( ' ', $parts ); | |
| 458 | - } | |
| 459 | - | |
| 460 | - public function has_action_link() { | |
| 461 | - if ( ! $this->get_id() ) { | |
| 462 | - return true; | |
| 463 | - } | |
| 464 | - | |
| 465 | - $link_settings = $this->get_atomic_setting( 'link' ) ?? null; | |
| 466 | - $attributes = $this->get_link_attributes( $link_settings ); | |
| 467 | - | |
| 468 | - return isset( $attributes['data-action-link'] ); | |
| 469 | - } | |
| 470 | - | |
| 471 | - public function get_script_depends() { | |
| 472 | - $depends = parent::get_script_depends(); | |
| 473 | - | |
| 474 | - if ( $this->has_action_link() ) { | |
| 475 | - $depends[] = Frontend_Assets_Loader::ACTION_LINK_HANDLERS_HANDLE; | |
| 476 | - } | |
| 477 | - | |
| 478 | - if ( Atomic_Form::is_instance_form( $this ) ) { | |
| 479 | - $depends[] = Frontend_Assets_Loader::FORM_HANDLERS_HANDLE; | |
| 480 | - } | |
| 481 | - | |
| 482 | - return $depends; | |
| 481 | + return $result; | |
| 483 | 482 | } |
| 484 | 483 | } |