| 1 |
<?php |
| 2 |
|
| 3 |
class Referrer_Match extends Red_Match { |
| 4 |
public $referrer; |
| 5 |
public $regex; |
| 6 |
|
| 7 |
function name() { |
| 8 |
return __( 'URL and referrer', 'redirection' ); |
| 9 |
} |
| 10 |
|
| 11 |
public function save( array $details, $no_target_url = false ) { |
| 12 |
$data = array( |
| 13 |
'regex' => isset( $details['action_data_regex'] ) && $details['action_data_regex'] === 'true' ? true : false, |
| 14 |
'referrer' => isset( $details['action_data_referrer'] ) ? $this->sanitize_referrer( $details['action_data_referrer'] ) : '', |
| 15 |
); |
| 16 |
|
| 17 |
if ( $no_target_url === false ) { |
| 18 |
$data['url_from'] = isset( $details['action_data_url_from'] ) ? $this->sanitize_url( $details['action_data_url_from'] ) : ''; |
| 19 |
$data['url_notfrom'] = isset( $details['action_data_url_notfrom'] ) ? $this->sanitize_url( $details['action_data_url_notfrom'] ) : ''; |
| 20 |
} |
| 21 |
|
| 22 |
return $data; |
| 23 |
} |
| 24 |
|
| 25 |
public function sanitize_referrer( $agent ) { |
| 26 |
return $this->sanitize_url( $agent ); |
| 27 |
} |
| 28 |
|
| 29 |
function initialize( $url ) { |
| 30 |
$this->url = array( $url, '' ); |
| 31 |
} |
| 32 |
|
| 33 |
function get_target( $url, $matched_url, $regex ) { |
| 34 |
$target = false; |
| 35 |
|
| 36 |
// Check if referrer matches |
| 37 |
if ( ( $this->regex === false && Redirection_Request::get_referrer() === $this->referrer ) || ( $this->regex === true && preg_match( '@'.str_replace( '@', '\\@', $this->referrer ).'@', Redirection_Request::get_referrer(), $matches ) ) ) { |
| 38 |
$target = $this->url_from; |
| 39 |
|
| 40 |
if ( $regex ) { |
| 41 |
$target = preg_replace( '@'.str_replace( '@', '\\@', $matched_url ).'@', $target, $url ); |
| 42 |
} |
| 43 |
} elseif ( $this->url_notfrom !== '' ) { |
| 44 |
$target = $this->url_notfrom; |
| 45 |
} |
| 46 |
return $target; |
| 47 |
} |
| 48 |
} |
| 49 |
|