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