| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\EditorOne\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Url_Matcher { |
| 10 |
|
| 11 |
public function get_match_score( string $menu_url, string $current_uri ): int { |
| 12 |
$menu_parsed = wp_parse_url( $menu_url ); |
| 13 |
|
| 14 |
if ( empty( $menu_parsed['path'] ) ) { |
| 15 |
return -1; |
| 16 |
} |
| 17 |
|
| 18 |
$current_parsed = wp_parse_url( $current_uri ); |
| 19 |
|
| 20 |
if ( empty( $current_parsed['path'] ) ) { |
| 21 |
return -1; |
| 22 |
} |
| 23 |
|
| 24 |
if ( basename( $menu_parsed['path'] ) !== basename( $current_parsed['path'] ) ) { |
| 25 |
return -1; |
| 26 |
} |
| 27 |
|
| 28 |
$menu_query = $this->parse_query_string( $menu_parsed['query'] ?? '' ); |
| 29 |
$current_query = $this->parse_query_string( $current_parsed['query'] ?? '' ); |
| 30 |
|
| 31 |
if ( ! $this->query_params_match( $menu_query, $current_query ) ) { |
| 32 |
return -1; |
| 33 |
} |
| 34 |
|
| 35 |
return count( $menu_query ); |
| 36 |
} |
| 37 |
|
| 38 |
public function parse_query_string( string $query ): array { |
| 39 |
$params = []; |
| 40 |
|
| 41 |
if ( '' !== $query ) { |
| 42 |
parse_str( $query, $params ); |
| 43 |
} |
| 44 |
|
| 45 |
return $params; |
| 46 |
} |
| 47 |
|
| 48 |
public function query_params_match( array $required, array $actual ): bool { |
| 49 |
foreach ( $required as $key => $value ) { |
| 50 |
if ( ! isset( $actual[ $key ] ) || $actual[ $key ] !== $value ) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return true; |
| 56 |
} |
| 57 |
} |
| 58 |
|