| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Trait to add redirect matching that adds a matched target |
| 5 |
*/ |
| 6 |
trait FromUrl_Match { |
| 7 |
/** |
| 8 |
* URL to match against |
| 9 |
* |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
public $url = ''; |
| 13 |
|
| 14 |
/** |
| 15 |
* Save data to an array, ready for serializing. |
| 16 |
* |
| 17 |
* @param array $details New match data. |
| 18 |
* @param boolean $no_target_url Does the action have a target URL. |
| 19 |
* @param array $data Existing match data. |
| 20 |
* @return array |
| 21 |
*/ |
| 22 |
private function save_data( array $details, $no_target_url, array $data ) { |
| 23 |
if ( $no_target_url === false ) { |
| 24 |
return array_merge( [ |
| 25 |
'url' => isset( $details['url'] ) ? $this->sanitize_url( $details['url'] ) : '', |
| 26 |
], $data ); |
| 27 |
} |
| 28 |
|
| 29 |
return $data; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get target URL for this match, depending on whether we match or not |
| 34 |
* |
| 35 |
* @param string $requested_url Request URL. |
| 36 |
* @param string $source_url Redirect source URL. |
| 37 |
* @param Red_Source_Flags $flags Redirect flags. |
| 38 |
* @param boolean $matched Is the URL matched. |
| 39 |
* @return string|false |
| 40 |
*/ |
| 41 |
public function get_target_url( $requested_url, $source_url, Red_Source_Flags $flags, $matched ) { |
| 42 |
$target = $this->get_matched_target( $matched ); |
| 43 |
|
| 44 |
if ( $flags->is_regex() && $target ) { |
| 45 |
return $this->get_target_regex_url( $source_url, $target, $requested_url, $flags ); |
| 46 |
} |
| 47 |
|
| 48 |
return $target; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Return the matched target, if one exists. |
| 53 |
* |
| 54 |
* @param boolean $matched Is it matched. |
| 55 |
* @return false|string |
| 56 |
*/ |
| 57 |
private function get_matched_target( $matched ) { |
| 58 |
if ( $matched ) { |
| 59 |
return $this->url; |
| 60 |
} |
| 61 |
|
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Load the data into the instance. |
| 67 |
* |
| 68 |
* @param String $values Serialized PHP data. |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
private function load_data( $values ) { |
| 72 |
$values = unserialize( $values ); |
| 73 |
|
| 74 |
if ( isset( $values['url'] ) ) { |
| 75 |
$this->url = $values['url']; |
| 76 |
} |
| 77 |
|
| 78 |
return $values; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get the loaded data as an array. |
| 83 |
* |
| 84 |
* @return array<url: string> |
| 85 |
*/ |
| 86 |
private function get_from_data() { |
| 87 |
return [ |
| 88 |
'url' => $this->url, |
| 89 |
]; |
| 90 |
} |
| 91 |
} |
| 92 |
|