| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\EditorOne\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Active_Menu_Resolver { |
| 10 |
|
| 11 |
private const HOME_SLUG = 'elementor'; |
| 12 |
|
| 13 |
private Url_Matcher $url_matcher; |
| 14 |
|
| 15 |
public function __construct( Url_Matcher $url_matcher ) { |
| 16 |
$this->url_matcher = $url_matcher; |
| 17 |
} |
| 18 |
|
| 19 |
public function resolve( array $menu_items, array $level4_groups, string $current_page, string $current_uri ): array { |
| 20 |
if ( 'elementor-editor' === $current_page || Menu_Config::EDITOR_MENU_SLUG === $current_page || Menu_Config::ELEMENTOR_MENU_SLUG === $current_page ) { |
| 21 |
return $this->create_active_state( self::HOME_SLUG ); |
| 22 |
} |
| 23 |
|
| 24 |
$pro_post_type_match = $this->get_pro_post_type_active_state(); |
| 25 |
|
| 26 |
if ( $pro_post_type_match ) { |
| 27 |
return $pro_post_type_match; |
| 28 |
} |
| 29 |
|
| 30 |
return $this->find_best_matching_menu_item( $menu_items, $level4_groups, $current_uri ); |
| 31 |
} |
| 32 |
|
| 33 |
public function create_active_state( string $menu_slug, string $child_slug = '', int $score = 0 ): array { |
| 34 |
return [ |
| 35 |
'menu_slug' => $menu_slug, |
| 36 |
'child_slug' => $child_slug, |
| 37 |
'score' => $score, |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
private function find_best_matching_menu_item( array $menu_items, array $level4_groups, string $current_uri ): array { |
| 42 |
$best_match = $this->create_active_state( '', '', -1 ); |
| 43 |
|
| 44 |
foreach ( $menu_items as $item ) { |
| 45 |
$best_match = $this->update_best_match_from_level4( |
| 46 |
$item, |
| 47 |
$level4_groups, |
| 48 |
$current_uri, |
| 49 |
$best_match |
| 50 |
); |
| 51 |
|
| 52 |
$score = $this->url_matcher->get_match_score( $item['url'], $current_uri ); |
| 53 |
|
| 54 |
if ( $score > $best_match['score'] ) { |
| 55 |
$best_match = $this->create_active_state( $item['slug'], '', $score ); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return $this->create_active_state( $best_match['menu_slug'], $best_match['child_slug'] ); |
| 60 |
} |
| 61 |
|
| 62 |
private function update_best_match_from_level4( |
| 63 |
array $item, |
| 64 |
array $level4_groups, |
| 65 |
string $current_uri, |
| 66 |
array $best_match |
| 67 |
): array { |
| 68 |
if ( empty( $item['group_id'] ) || ! isset( $level4_groups[ $item['group_id'] ] ) ) { |
| 69 |
return $best_match; |
| 70 |
} |
| 71 |
|
| 72 |
$group = $level4_groups[ $item['group_id'] ]; |
| 73 |
|
| 74 |
if ( empty( $group['items'] ) ) { |
| 75 |
return $best_match; |
| 76 |
} |
| 77 |
|
| 78 |
foreach ( $group['items'] as $child_item ) { |
| 79 |
$score = $this->url_matcher->get_match_score( $child_item['url'], $current_uri ); |
| 80 |
|
| 81 |
if ( $score > $best_match['score'] ) { |
| 82 |
$best_match = $this->create_active_state( $item['slug'], $child_item['slug'], $score ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $best_match; |
| 87 |
} |
| 88 |
|
| 89 |
private function get_pro_post_type_active_state(): ?array { |
| 90 |
$current_post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? ''; |
| 91 |
|
| 92 |
if ( empty( $current_post_type ) ) { |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
return Menu_Data_Provider::get_elementor_post_types()[ $current_post_type ] ?? null; |
| 97 |
} |
| 98 |
} |
| 99 |
|