← All changes
|
modules/atomic-widgets/elements/base/has-atomic-base.php
+152
-105
4.1.3
→
3.35.0-dev4
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; |
| @@ -17,9 +15,8 @@ | ||
| 17 | 15 | use Elementor\Modules\AtomicWidgets\PropTypes\Link_Prop_Type; |
| 18 | 16 | use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type; |
| 19 | 17 | use Elementor\Utils; |
| 20 | 18 | use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type; |
| 21 | -use Elementor\Modules\AtomicWidgets\Styles\Atomic_Widget_Styles; | |
| 22 | 19 | |
| 23 | 20 | if ( ! defined( 'ABSPATH' ) ) { |
| 24 | 21 | exit; // Exit if accessed directly. |
| 25 | 22 | } |
| @@ -135,8 +132,67 @@ | ||
| 135 | 132 | |
| 136 | 133 | return []; |
| 137 | 134 | } |
| 138 | 135 | |
| 136 | + private function convert_prop_type_interactions_to_legacy( $interactions ) { | |
| 137 | + $legacy_items = []; | |
| 138 | + | |
| 139 | + foreach ( $interactions['items'] as $item ) { | |
| 140 | + if ( isset( $item['$$type'] ) && 'interaction-item' === $item['$$type'] ) { | |
| 141 | + $legacy_item = $this->extract_legacy_interaction_from_prop_type( $item ); | |
| 142 | + if ( $legacy_item ) { | |
| 143 | + $legacy_items[] = $legacy_item; | |
| 144 | + } | |
| 145 | + } else { | |
| 146 | + $legacy_items[] = $item; | |
| 147 | + } | |
| 148 | + } | |
| 149 | + | |
| 150 | + return [ | |
| 151 | + 'version' => $interactions['version'] ?? 1, | |
| 152 | + 'items' => $legacy_items, | |
| 153 | + ]; | |
| 154 | + } | |
| 155 | + | |
| 156 | + private function extract_legacy_interaction_from_prop_type( $item ) { | |
| 157 | + if ( ! isset( $item['value'] ) || ! is_array( $item['value'] ) ) { | |
| 158 | + return null; | |
| 159 | + } | |
| 160 | + | |
| 161 | + $item_value = $item['value']; | |
| 162 | + | |
| 163 | + $interaction_id = $this->extract_prop_value( $item_value, 'interaction_id' ); | |
| 164 | + $trigger = $this->extract_prop_value( $item_value, 'trigger' ); | |
| 165 | + $animation = $this->extract_prop_value( $item_value, 'animation' ); | |
| 166 | + | |
| 167 | + if ( ! is_array( $animation ) ) { | |
| 168 | + return null; | |
| 169 | + } | |
| 170 | + | |
| 171 | + $effect = $this->extract_prop_value( $animation, 'effect' ); | |
| 172 | + $type = $this->extract_prop_value( $animation, 'type' ); | |
| 173 | + $direction = $this->extract_prop_value( $animation, 'direction' ); | |
| 174 | + $timing_config = $this->extract_prop_value( $animation, 'timing_config' ); | |
| 175 | + | |
| 176 | + $duration = 300; | |
| 177 | + $delay = 0; | |
| 178 | + | |
| 179 | + if ( is_array( $timing_config ) ) { | |
| 180 | + $duration = $this->extract_prop_value( $timing_config, 'duration', 300 ); | |
| 181 | + $delay = $this->extract_prop_value( $timing_config, 'delay', 0 ); | |
| 182 | + } | |
| 183 | + | |
| 184 | + $animation_id = implode( '-', [ $trigger, $effect, $type, $direction, $duration, $delay ] ); | |
| 185 | + | |
| 186 | + return [ | |
| 187 | + 'interaction_id' => $interaction_id, | |
| 188 | + 'animation' => [ | |
| 189 | + 'animation_id' => $animation_id, | |
| 190 | + 'animation_type' => 'full-preset', | |
| 191 | + ], | |
| 192 | + ]; | |
| 193 | + } | |
| 194 | + | |
| 139 | 195 | private function extract_prop_value( $data, $key, $default = '' ) { |
| 140 | 196 | if ( ! is_array( $data ) || ! isset( $data[ $key ] ) ) { |
| 141 | 197 | return $default; |
| 142 | 198 | } |
| @@ -225,9 +281,9 @@ | ||
| 225 | 281 | |
| 226 | 282 | final public function get_raw_data( $with_html_content = false ) { |
| 227 | 283 | $raw_data = parent::get_raw_data( $with_html_content ); |
| 228 | 284 | |
| 229 | - $raw_data['styles'] = Atomic_Widget_Styles::get_license_based_filtered_styles( $this->styles ?? [] ); | |
| 285 | + $raw_data['styles'] = $this->styles; | |
| 230 | 286 | $raw_data['interactions'] = $this->interactions ?? []; |
| 231 | 287 | $raw_data['editor_settings'] = $this->editor_settings; |
| 232 | 288 | |
| 233 | 289 | return $raw_data; |
| @@ -242,31 +298,18 @@ | ||
| 242 | 298 | |
| 243 | 299 | public function get_atomic_settings(): array { |
| 244 | 300 | $schema = static::get_props_schema(); |
| 245 | 301 | $props = $this->get_settings(); |
| 302 | + $initial_attributes = $this->get_initial_attributes(); | |
| 246 | 303 | |
| 247 | - $merged_attribute_values = array_merge( | |
| 248 | - $this->get_initial_attributes()['value'] ?? [], | |
| 304 | + $props['attributes'] = Attributes_Prop_Type::generate( array_merge( | |
| 305 | + $initial_attributes['value'] ?? [], | |
| 249 | 306 | $props['attributes']['value'] ?? [] |
| 250 | - ); | |
| 251 | - $props['attributes'] = Attributes_Prop_Type::generate( $merged_attribute_values ); | |
| 307 | + ) ); | |
| 252 | 308 | |
| 253 | - $parsed = Render_Props_Resolver::for_settings()->resolve( $schema, $props ); | |
| 254 | - | |
| 255 | - return $this->transform_link_for_render( $parsed ); | |
| 309 | + return Render_Props_Resolver::for_settings()->resolve( $schema, $props ); | |
| 256 | 310 | } |
| 257 | 311 | |
| 258 | - protected function transform_link_for_render( array $parsed ): array { | |
| 259 | - $link_attributes = isset( $parsed['link'] ) ? $this->get_link_attributes_string( $parsed['link'] ) : ''; | |
| 260 | - | |
| 261 | - $parsed['link'] = ! empty( $link_attributes ) ? [ | |
| 262 | - 'tag' => $parsed['link']['tag'], | |
| 263 | - 'attributes' => $link_attributes, | |
| 264 | - ] : null; | |
| 265 | - | |
| 266 | - return $parsed; | |
| 267 | - } | |
| 268 | - | |
| 269 | 312 | protected function get_initial_attributes() { |
| 270 | 313 | return Attributes_Prop_Type::generate( [ |
| 271 | 314 | Key_Value_Prop_Type::generate( [ |
| 272 | 315 | 'key' => String_Prop_Type::generate( 'data-e-type' ), |
| @@ -308,13 +351,10 @@ | ||
| 308 | 351 | } |
| 309 | 352 | |
| 310 | 353 | public static function get_props_schema(): array { |
| 311 | 354 | $schema = static::define_props_schema(); |
| 355 | + $schema['_cssid'] = String_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ); | |
| 312 | 356 | |
| 313 | - if ( ! isset( $schema['_cssid'] ) ) { | |
| 314 | - $schema['_cssid'] = String_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ); | |
| 315 | - } | |
| 316 | - | |
| 317 | 357 | return apply_filters( |
| 318 | 358 | 'elementor/atomic-widgets/props-schema', |
| 319 | 359 | $schema |
| 320 | 360 | ); |
| @@ -319,117 +359,124 @@ | ||
| 319 | 359 | $schema |
| 320 | 360 | ); |
| 321 | 361 | } |
| 322 | 362 | |
| 323 | - protected function set_render_context( array $context_pairs ): void { | |
| 324 | - foreach ( $context_pairs as $context_pair ) { | |
| 325 | - $context_key = $context_pair['context_key'] ?? static::class; | |
| 326 | - $context = $context_pair['context']; | |
| 327 | - Render_Context::push( $context_key, $context ); | |
| 363 | + public function get_interactions_ids() { | |
| 364 | + $animation_ids = []; | |
| 365 | + | |
| 366 | + $list_of_interactions = ( is_array( $this->interactions ) && isset( $this->interactions['items'] ) ) | |
| 367 | + ? $this->interactions['items'] | |
| 368 | + : []; | |
| 369 | + | |
| 370 | + foreach ( $list_of_interactions as $interaction ) { | |
| 371 | + if ( isset( $interaction['$$type'] ) && 'interaction-item' === $interaction['$$type'] ) { | |
| 372 | + $animation_id = $this->extract_animation_id_from_prop_type( $interaction ); | |
| 373 | + if ( $animation_id ) { | |
| 374 | + $animation_ids[] = $animation_id; | |
| 375 | + } | |
| 376 | + } elseif ( isset( $interaction['animation']['animation_id'] ) ) { | |
| 377 | + $animation_ids[] = $interaction['animation']['animation_id']; | |
| 378 | + } | |
| 328 | 379 | } |
| 380 | + | |
| 381 | + return $animation_ids; | |
| 329 | 382 | } |
| 330 | 383 | |
| 331 | - protected function clear_render_context( array $context_pairs ): void { | |
| 332 | - foreach ( $context_pairs as $context_pair ) { | |
| 333 | - $context_key = $context_pair['context_key'] ?? static::class; | |
| 334 | - Render_Context::pop( $context_key ); | |
| 384 | + private function extract_animation_id_from_prop_type( $item ) { | |
| 385 | + if ( ! isset( $item['value'] ) || ! is_array( $item['value'] ) ) { | |
| 386 | + return null; | |
| 335 | 387 | } |
| 388 | + | |
| 389 | + $item_value = $item['value']; | |
| 390 | + | |
| 391 | + $trigger = $this->extract_prop_value( $item_value, 'trigger' ); | |
| 392 | + $animation = $this->extract_prop_value( $item_value, 'animation' ); | |
| 393 | + | |
| 394 | + if ( ! is_array( $animation ) ) { | |
| 395 | + return null; | |
| 396 | + } | |
| 397 | + | |
| 398 | + $effect = $this->extract_prop_value( $animation, 'effect' ); | |
| 399 | + $type = $this->extract_prop_value( $animation, 'type' ); | |
| 400 | + $direction = $this->extract_prop_value( $animation, 'direction' ); | |
| 401 | + $timing_config = $this->extract_prop_value( $animation, 'timing_config' ); | |
| 402 | + $config = $this->extract_prop_value( $animation, 'config' ); | |
| 403 | + | |
| 404 | + $duration = 300; | |
| 405 | + $delay = 0; | |
| 406 | + $replay = 0; | |
| 407 | + $relative_to = 'viewport'; | |
| 408 | + $offset_top = 15; | |
| 409 | + $offset_bottom = 85; | |
| 410 | + | |
| 411 | + if ( is_array( $timing_config ) ) { | |
| 412 | + $duration = $this->extract_prop_value( $timing_config, 'duration', 300 ); | |
| 413 | + $delay = $this->extract_prop_value( $timing_config, 'delay', 0 ); | |
| 414 | + } | |
| 415 | + | |
| 416 | + if ( is_array( $config ) ) { | |
| 417 | + $relative_to = $this->extract_prop_value( $config, 'relative_to', 'viewport' ); | |
| 418 | + $offset_top = $this->extract_prop_value( $config, 'offset_top', 15 ); | |
| 419 | + $offset_bottom = $this->extract_prop_value( $config, 'offset_bottom', 85 ); | |
| 420 | + | |
| 421 | + $replay = $this->extract_prop_value( $config, 'replay', 0 ); | |
| 422 | + if ( empty( $replay ) && 0 !== $replay && '0' !== $replay ) { | |
| 423 | + $replay = 0; | |
| 424 | + } | |
| 425 | + } else { | |
| 426 | + $replay = 0; | |
| 427 | + } | |
| 428 | + | |
| 429 | + return implode( '-', [ $trigger, $effect, $type, $direction, $duration, $delay, $replay, $relative_to, $offset_top, $offset_bottom ] ); | |
| 336 | 430 | } |
| 337 | 431 | |
| 338 | 432 | public function print_content() { |
| 339 | 433 | $defined_context = $this->define_render_context(); |
| 340 | 434 | |
| 341 | - if ( empty( $defined_context ) ) { | |
| 435 | + $context_key = $defined_context['context_key'] ?? static::class; | |
| 436 | + $element_context = $defined_context['context'] ?? []; | |
| 437 | + | |
| 438 | + $has_context = ! empty( $element_context ); | |
| 439 | + | |
| 440 | + if ( ! $has_context ) { | |
| 342 | 441 | return parent::print_content(); |
| 343 | 442 | } |
| 344 | 443 | |
| 345 | - $this->set_render_context( $defined_context ); | |
| 444 | + Render_Context::push( $context_key, $element_context ); | |
| 346 | 445 | |
| 347 | 446 | parent::print_content(); |
| 348 | 447 | |
| 349 | - $this->clear_render_context( $defined_context ); | |
| 448 | + Render_Context::pop( $context_key ); | |
| 350 | 449 | } |
| 351 | 450 | |
| 352 | 451 | /** |
| 353 | 452 | * Define the context for element's Render_Context. |
| 354 | 453 | * |
| 355 | - * @return array Array of context pairs. Each pair is an associative array with: | |
| 356 | - * - 'context_key' (optional): The context key. Defaults to static::class if not provided. | |
| 357 | - * - 'context' (required): The context value (can be any type). | |
| 358 | - * | |
| 359 | - * @example | |
| 360 | - * [ | |
| 361 | - * [ | |
| 362 | - * 'context_key' => 'custom-key', | |
| 363 | - * 'context' => ['some' => 'data'], | |
| 364 | - * ], | |
| 365 | - * [ | |
| 366 | - * 'context' => ['instance_id' => $this->get_id()], | |
| 367 | - * ], | |
| 368 | - * ] | |
| 454 | + * @return array{context_key: ?string, context: array} | |
| 369 | 455 | */ |
| 370 | 456 | protected function define_render_context(): array { |
| 371 | - return []; | |
| 457 | + return [ | |
| 458 | + 'context_key' => null, | |
| 459 | + 'context' => [], | |
| 460 | + ]; | |
| 372 | 461 | } |
| 373 | 462 | |
| 374 | - protected function get_link_attributes( $link_settings ) { | |
| 375 | - if ( empty( $link_settings['href'] ) ) { | |
| 376 | - return []; | |
| 377 | - } | |
| 378 | - | |
| 463 | + protected function get_link_attributes( $link_settings, $add_key_to_result = false ) { | |
| 379 | 464 | $tag = $link_settings['tag'] ?? Link_Prop_Type::DEFAULT_TAG; |
| 380 | - $url = $link_settings['href']; | |
| 465 | + $href = $link_settings['href']; | |
| 381 | 466 | $target = $link_settings['target'] ?? '_self'; |
| 382 | - $is_action_link = 'button' === $tag; | |
| 383 | - $url_attr_key = $is_action_link ? 'data-action-link' : 'href'; | |
| 384 | 467 | |
| 385 | - return [ | |
| 386 | - $url_attr_key => $url, | |
| 468 | + $is_button = 'button' === $tag; | |
| 469 | + $href_attribute_key = $is_button ? 'data-action-link' : 'href'; | |
| 470 | + | |
| 471 | + $result = [ | |
| 472 | + $href_attribute_key => $href, | |
| 387 | 473 | 'target' => $target, |
| 388 | 474 | ]; |
| 389 | - } | |
| 390 | 475 | |
| 391 | - private function get_link_attributes_string( $link_settings ) { | |
| 392 | - $link_attributes = $this->get_link_attributes( $link_settings ); | |
| 393 | - | |
| 394 | - if ( empty( $link_attributes ) ) { | |
| 395 | - return ''; | |
| 476 | + if ( $add_key_to_result ) { | |
| 477 | + $result['key'] = $href_attribute_key; | |
| 396 | 478 | } |
| 397 | 479 | |
| 398 | - $parts = []; | |
| 399 | - | |
| 400 | - foreach ( $link_attributes as $key => $value ) { | |
| 401 | - if ( 'tag' === $key ) { | |
| 402 | - continue; | |
| 403 | - } | |
| 404 | - | |
| 405 | - $parts[] = sprintf( '%s="%s"', $key, esc_attr( $value ) ); | |
| 406 | - } | |
| 407 | - | |
| 408 | - return implode( ' ', $parts ); | |
| 409 | - } | |
| 410 | - | |
| 411 | - public function has_action_link() { | |
| 412 | - if ( ! $this->get_id() ) { | |
| 413 | - return true; | |
| 414 | - } | |
| 415 | - | |
| 416 | - $link_settings = $this->get_atomic_setting( 'link' ) ?? null; | |
| 417 | - $attributes = $this->get_link_attributes( $link_settings ); | |
| 418 | - | |
| 419 | - return isset( $attributes['data-action-link'] ); | |
| 420 | - } | |
| 421 | - | |
| 422 | - public function get_script_depends() { | |
| 423 | - $depends = parent::get_script_depends(); | |
| 424 | - | |
| 425 | - if ( $this->has_action_link() ) { | |
| 426 | - $depends[] = Frontend_Assets_Loader::ACTION_LINK_HANDLERS_HANDLE; | |
| 427 | - } | |
| 428 | - | |
| 429 | - if ( Atomic_Form::is_instance_form( $this ) ) { | |
| 430 | - $depends[] = Frontend_Assets_Loader::FORM_HANDLERS_HANDLE; | |
| 431 | - } | |
| 432 | - | |
| 433 | - return $depends; | |
| 480 | + return $result; | |
| 434 | 481 | } |
| 435 | 482 | } |