← All changes
|
modules/atomic-widgets/elements/base/has-atomic-base.php
+18
-127
4.2.0-beta2
→
4.0.0-dev3
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 | |
| @@ -287,31 +240,18 @@ | ||
| 287 | 240 | |
| 288 | 241 | public function get_atomic_settings(): array { |
| 289 | 242 | $schema = static::get_props_schema(); |
| 290 | 243 | $props = $this->get_settings(); |
| 244 | + $initial_attributes = $this->get_initial_attributes(); | |
| 291 | 245 | |
| 292 | - $merged_attribute_values = array_merge( | |
| 293 | - $this->get_initial_attributes()['value'] ?? [], | |
| 246 | + $props['attributes'] = Attributes_Prop_Type::generate( array_merge( | |
| 247 | + $initial_attributes['value'] ?? [], | |
| 294 | 248 | $props['attributes']['value'] ?? [] |
| 295 | - ); | |
| 296 | - $props['attributes'] = Attributes_Prop_Type::generate( $merged_attribute_values ); | |
| 249 | + ) ); | |
| 297 | 250 | |
| 298 | - $parsed = Render_Props_Resolver::for_settings()->resolve( $schema, $props ); | |
| 299 | - | |
| 300 | - return $this->transform_link_for_render( $parsed ); | |
| 251 | + return Render_Props_Resolver::for_settings()->resolve( $schema, $props ); | |
| 301 | 252 | } |
| 302 | 253 | |
| 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 | 254 | protected function get_initial_attributes() { |
| 315 | 255 | return Attributes_Prop_Type::generate( [ |
| 316 | 256 | Key_Value_Prop_Type::generate( [ |
| 317 | 257 | 'key' => String_Prop_Type::generate( 'data-e-type' ), |
| @@ -348,22 +288,15 @@ | ||
| 348 | 288 | if ( isset( $data['title'] ) && is_string( $data['title'] ) ) { |
| 349 | 289 | $editor_data['title'] = sanitize_text_field( $data['title'] ); |
| 350 | 290 | } |
| 351 | 291 | |
| 352 | - if ( isset( $data['grid_outline'] ) && is_bool( $data['grid_outline'] ) ) { | |
| 353 | - $editor_data['grid_outline'] = $data['grid_outline']; | |
| 354 | - } | |
| 355 | - | |
| 356 | 292 | return $editor_data; |
| 357 | 293 | } |
| 358 | 294 | |
| 359 | 295 | public static function get_props_schema(): array { |
| 360 | 296 | $schema = static::define_props_schema(); |
| 297 | + $schema['_cssid'] = String_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ); | |
| 361 | 298 | |
| 362 | - if ( ! isset( $schema['_cssid'] ) ) { | |
| 363 | - $schema['_cssid'] = String_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ); | |
| 364 | - } | |
| 365 | - | |
| 366 | 299 | return apply_filters( |
| 367 | 300 | 'elementor/atomic-widgets/props-schema', |
| 368 | 301 | $schema |
| 369 | 302 | ); |
| @@ -419,66 +352,24 @@ | ||
| 419 | 352 | protected function define_render_context(): array { |
| 420 | 353 | return []; |
| 421 | 354 | } |
| 422 | 355 | |
| 423 | - protected function get_link_attributes( $link_settings ) { | |
| 424 | - if ( empty( $link_settings['href'] ) ) { | |
| 425 | - return []; | |
| 426 | - } | |
| 427 | - | |
| 356 | + protected function get_link_attributes( $link_settings, $add_key_to_result = false ) { | |
| 428 | 357 | $tag = $link_settings['tag'] ?? Link_Prop_Type::DEFAULT_TAG; |
| 429 | - $url = $link_settings['href']; | |
| 358 | + $href = $link_settings['href']; | |
| 430 | 359 | $target = $link_settings['target'] ?? '_self'; |
| 431 | - $is_action_link = 'button' === $tag; | |
| 432 | - $url_attr_key = $is_action_link ? 'data-action-link' : 'href'; | |
| 433 | 360 | |
| 434 | - return [ | |
| 435 | - $url_attr_key => $url, | |
| 361 | + $is_button = 'button' === $tag; | |
| 362 | + $href_attribute_key = $is_button ? 'data-action-link' : 'href'; | |
| 363 | + | |
| 364 | + $result = [ | |
| 365 | + $href_attribute_key => $href, | |
| 436 | 366 | 'target' => $target, |
| 437 | 367 | ]; |
| 438 | - } | |
| 439 | 368 | |
| 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 ''; | |
| 369 | + if ( $add_key_to_result ) { | |
| 370 | + $result['key'] = $href_attribute_key; | |
| 445 | 371 | } |
| 446 | 372 | |
| 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; | |
| 373 | + return $result; | |
| 483 | 374 | } |
| 484 | 375 | } |