← All changes
|
modules/editor-one/classes/menu-data-provider.php
+58
-8
4.1.0-dev3
→
4.3.0-beta3
View file →
| @@ -4,9 +4,11 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Elementor\Core\Admin\EditorOneMenu\Interfaces\Menu_Item_Interface; |
| 6 | 6 | use Elementor\Core\Admin\EditorOneMenu\Interfaces\Menu_Item_Third_Level_Interface; |
| 7 | 7 | use Elementor\Core\Admin\EditorOneMenu\Interfaces\Menu_Item_With_Custom_Url_Interface; |
| 8 | +use Elementor\Core\Admin\EditorOneMenu\Interfaces\Menu_Item_With_Event_Id_Interface; | |
| 8 | 9 | use Elementor\Plugin; |
| 10 | +use Elementor\Utils; | |
| 9 | 11 | |
| 10 | 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | 13 | exit; |
| 12 | 14 | } |
| @@ -22,8 +24,9 @@ | ||
| 22 | 24 | private ?array $cached_level3_sidebar_data = null; |
| 23 | 25 | private ?array $cached_level4_sidebar_data = null; |
| 24 | 26 | private ?array $cached_flyout_menu_data = null; |
| 25 | 27 | private Slug_Normalizer $slug_normalizer; |
| 28 | + private array $registration_options = []; | |
| 26 | 29 | |
| 27 | 30 | public static function instance(): self { |
| 28 | 31 | if ( null === self::$instance ) { |
| 29 | 32 | self::$instance = new self(); |
| @@ -39,9 +42,13 @@ | ||
| 39 | 42 | public function get_slug_normalizer(): Slug_Normalizer { |
| 40 | 43 | return $this->slug_normalizer; |
| 41 | 44 | } |
| 42 | 45 | |
| 43 | - public function register_menu( Menu_Item_Interface $item ): void { | |
| 46 | + public function register_menu( Menu_Item_Interface $item, array $options = [] ): void { | |
| 47 | + if ( ! empty( $options['preserve_label_casing'] ) ) { | |
| 48 | + $this->registration_options[ $item->get_slug() ]['preserve_label_casing'] = true; | |
| 49 | + } | |
| 50 | + | |
| 44 | 51 | if ( ! ( $item instanceof Menu_Item_Third_Level_Interface ) ) { |
| 45 | 52 | $this->register_level4_item( $item ); |
| 46 | 53 | return; |
| 47 | 54 | } |
| @@ -287,19 +294,24 @@ | ||
| 287 | 294 | } ) ); |
| 288 | 295 | } |
| 289 | 296 | |
| 290 | 297 | private function build_level3_flyout_items(): array { |
| 291 | - return $this->build_flyout_items( false ); | |
| 298 | + return $this->build_flyout_items( false, Menu_Config::get_excluded_level3_slugs() ); | |
| 292 | 299 | } |
| 293 | 300 | |
| 294 | 301 | private function build_flyout_items_with_expanded_third_party(): array { |
| 295 | - return $this->build_flyout_items( true ); | |
| 302 | + return $this->build_flyout_items( | |
| 303 | + true, | |
| 304 | + array_merge( | |
| 305 | + Menu_Config::get_excluded_level3_slugs(), | |
| 306 | + Menu_Config::get_excluded_flyout_menu_level3_slugs() | |
| 307 | + ) | |
| 308 | + ); | |
| 296 | 309 | } |
| 297 | 310 | |
| 298 | - private function build_flyout_items( bool $expand_third_party ): array { | |
| 311 | + private function build_flyout_items( bool $expand_third_party, array $excluded_slugs ): array { | |
| 299 | 312 | $items = []; |
| 300 | 313 | $existing_slugs = []; |
| 301 | - $excluded_slugs = Menu_Config::get_excluded_level3_slugs(); | |
| 302 | 314 | $excluded_level4_slugs = $expand_third_party ? Menu_Config::get_excluded_level4_slugs() : []; |
| 303 | 315 | |
| 304 | 316 | foreach ( $this->level3_items as $group_items ) { |
| 305 | 317 | foreach ( $group_items as $item_slug => $item ) { |
| @@ -358,13 +370,14 @@ | ||
| 358 | 370 | $url = $this->resolve_item_url( $item, $item_slug ); |
| 359 | 371 | |
| 360 | 372 | return [ |
| 361 | 373 | 'slug' => $item_slug, |
| 362 | - 'label' => $this->title_case( $item->get_label() ), | |
| 374 | + 'label' => $this->format_flyout_label( $item, $item_slug ), | |
| 363 | 375 | 'url' => $url, |
| 364 | 376 | 'group_id' => '', |
| 365 | 377 | 'priority' => $this->get_item_priority( $item ), |
| 366 | 378 | 'has_divider_before' => $is_first, |
| 379 | + 'event_id' => $this->resolve_event_id( $item, $item_slug ), | |
| 367 | 380 | ]; |
| 368 | 381 | } |
| 369 | 382 | |
| 370 | 383 | private function should_include_flyout_item( Menu_Item_Interface $item, string $item_slug, array $existing_slugs, array $excluded_slugs ): bool { |
| @@ -393,14 +406,15 @@ | ||
| 393 | 406 | $is_third_party_parent = Menu_Config::THIRD_PARTY_GROUP_ID === $item->get_group_id(); |
| 394 | 407 | |
| 395 | 408 | return [ |
| 396 | 409 | 'slug' => $item_slug, |
| 397 | - 'label' => $this->title_case( $item->get_label() ), | |
| 410 | + 'label' => $this->format_flyout_label( $item, $item_slug ), | |
| 398 | 411 | 'url' => $this->resolve_flyout_item_url( $item, $item_slug ), |
| 399 | 412 | 'icon' => $item->get_icon(), |
| 400 | 413 | 'group_id' => $group_id, |
| 401 | 414 | 'priority' => $this->get_item_priority( $item ), |
| 402 | 415 | 'has_divider_before' => $is_third_party_parent, |
| 416 | + 'event_id' => $this->resolve_event_id( $item, $item_slug ), | |
| 403 | 417 | ]; |
| 404 | 418 | } |
| 405 | 419 | |
| 406 | 420 | private function resolve_flyout_item_url( Menu_Item_Interface $item, string $item_slug ): string { |
| @@ -471,11 +485,12 @@ | ||
| 471 | 485 | $url = $this->resolve_item_url( $item, $item_slug ); |
| 472 | 486 | |
| 473 | 487 | $groups[ $group_id ]['items'][] = [ |
| 474 | 488 | 'slug' => $item_slug, |
| 475 | - 'label' => $this->title_case( $item->get_label() ), | |
| 489 | + 'label' => $this->format_flyout_label( $item, $item_slug ), | |
| 476 | 490 | 'url' => $url, |
| 477 | 491 | 'priority' => $this->get_item_priority( $item ), |
| 492 | + 'event_id' => $this->resolve_event_id( $item, $item_slug ), | |
| 478 | 493 | ]; |
| 479 | 494 | |
| 480 | 495 | $existing_labels[] = $label_lower; |
| 481 | 496 | } |
| @@ -531,8 +546,43 @@ | ||
| 531 | 546 | private function sort_items_by_priority( array &$items ): void { |
| 532 | 547 | usort( $items, function ( array $a, array $b ): int { |
| 533 | 548 | return ( $a['priority'] ?? 100 ) <=> ( $b['priority'] ?? 100 ); |
| 534 | 549 | } ); |
| 550 | + } | |
| 551 | + | |
| 552 | + private function resolve_event_id( Menu_Item_Interface $item, string $item_slug ): string { | |
| 553 | + if ( $item instanceof Menu_Item_With_Event_Id_Interface ) { | |
| 554 | + return $item->get_event_id(); | |
| 555 | + } | |
| 556 | + | |
| 557 | + return $this->derive_event_id_from_slug( $item_slug ); | |
| 558 | + } | |
| 559 | + | |
| 560 | + private function derive_event_id_from_slug( string $slug ): string { | |
| 561 | + $slug_event_map = [ | |
| 562 | + 'edit.php?post_type=elementor_library' => 'saved_templates', | |
| 563 | + 'elementor-app' => 'theme_builder', | |
| 564 | + ]; | |
| 565 | + | |
| 566 | + if ( isset( $slug_event_map[ $slug ] ) ) { | |
| 567 | + return $slug_event_map[ $slug ]; | |
| 568 | + } | |
| 569 | + | |
| 570 | + $event_id = preg_replace( '/^elementor-/', '', $slug ); | |
| 571 | + $event_id = preg_replace( '/[?#].*$/', '', $event_id ); | |
| 572 | + $event_id = str_replace( [ '-', ' ' ], '_', $event_id ); | |
| 573 | + | |
| 574 | + return strtolower( $event_id ); | |
| 575 | + } | |
| 576 | + | |
| 577 | + private function format_flyout_label( Menu_Item_Interface $item, string $item_slug ): string { | |
| 578 | + $label = $item->get_label(); | |
| 579 | + | |
| 580 | + if ( ! empty( $this->registration_options[ $item_slug ]['preserve_label_casing'] ) ) { | |
| 581 | + return $label; | |
| 582 | + } | |
| 583 | + | |
| 584 | + return $this->title_case( $label ); | |
| 535 | 585 | } |
| 536 | 586 | |
| 537 | 587 | private function title_case( string $text ): string { |
| 538 | 588 | if ( function_exists( 'mb_convert_case' ) ) { |