| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Url_Query { |
| 4 |
private $query = []; |
| 5 |
|
| 6 |
public function __construct( $url ) { |
| 7 |
$this->query = $this->get_url_query( $url ); |
| 8 |
} |
| 9 |
|
| 10 |
public function is_match( $url, Red_Source_Flags $flags ) { |
| 11 |
$target = $this->get_url_query( $url ); |
| 12 |
|
| 13 |
// All params in the source have to exist in the request, but in any order |
| 14 |
$matched = $this->get_query_same( $this->query, $target ); |
| 15 |
|
| 16 |
if ( count( $matched ) !== count( $this->query ) ) { |
| 17 |
// Source params arent matched exactly |
| 18 |
return false; |
| 19 |
}; |
| 20 |
|
| 21 |
// Get list of whatever is left over |
| 22 |
$query_diff = $this->get_query_diff( $this->query, $target ); |
| 23 |
|
| 24 |
if ( $flags->is_query_ignore() || $flags->is_query_pass() ) { |
| 25 |
return true; // This ignores all other query params |
| 26 |
} |
| 27 |
|
| 28 |
// In an exact match there shouldn't be any more params |
| 29 |
return count( $query_diff ) === 0; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Pass query params from one URL to another URL, ignoring any params that already exist on the target |
| 34 |
* |
| 35 |
* @param string $target_url The target URL to add params to |
| 36 |
* @param string $requested_url The source URL to pass params from |
| 37 |
* @param Red_Source_Flags $flags Any URL flags |
| 38 |
* @return string URL, modified or not |
| 39 |
*/ |
| 40 |
public static function add_to_target( $target_url, $requested_url, Red_Source_Flags $flags ) { |
| 41 |
if ( $flags->is_query_pass() && $target_url ) { |
| 42 |
$source_query = new Red_Url_Query( $target_url ); |
| 43 |
$request_query = new Red_Url_Query( $requested_url ); |
| 44 |
|
| 45 |
// Now add any remaining params |
| 46 |
$query_diff = $source_query->get_query_diff( $source_query->query, $request_query->query ); |
| 47 |
|
| 48 |
// Remove any params from $source that are present in $request - we dont allow |
| 49 |
// predefined params to be overridden |
| 50 |
foreach ( $query_diff as $key => $value ) { |
| 51 |
$query_diff[ $key ] = urlencode( $value ); |
| 52 |
|
| 53 |
if ( isset( $source_query->query[ $key ] ) ) { |
| 54 |
unset( $query_diff[ $key ] ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return add_query_arg( $query_diff, $target_url ); |
| 59 |
} |
| 60 |
|
| 61 |
return $target_url; |
| 62 |
} |
| 63 |
|
| 64 |
private function get_url_query( $url ) { |
| 65 |
$params = []; |
| 66 |
|
| 67 |
$query = wp_parse_url( $url, PHP_URL_QUERY ); |
| 68 |
wp_parse_str( $query ? $query : '', $params ); |
| 69 |
|
| 70 |
return $params; |
| 71 |
} |
| 72 |
|
| 73 |
private function get_query_same( array $source_query, array $target_query ) { |
| 74 |
return array_intersect_assoc( $source_query, $target_query ); |
| 75 |
} |
| 76 |
|
| 77 |
private function get_query_diff( array $source_query, array $target_query ) { |
| 78 |
return array_diff_assoc( $target_query, $source_query ); |
| 79 |
} |
| 80 |
} |
| 81 |
|