| 1 |
<?php |
| 2 |
|
| 3 |
class Role_Match extends Red_Match { |
| 4 |
public $role; |
| 5 |
public $url_from; |
| 6 |
public $url_notfrom; |
| 7 |
|
| 8 |
function name() { |
| 9 |
return __( 'URL and role/capability', 'redirection' ); |
| 10 |
} |
| 11 |
|
| 12 |
public function save( array $details, $no_target_url = false ) { |
| 13 |
$data = array( 'role' => isset( $details['role'] ) ? $details['role'] : '' ); |
| 14 |
|
| 15 |
if ( $no_target_url === false ) { |
| 16 |
$data['url_from'] = isset( $details['url_from'] ) ? $this->sanitize_url( $details['url_from'] ) : ''; |
| 17 |
$data['url_notfrom'] = isset( $details['url_notfrom'] ) ? $this->sanitize_url( $details['url_notfrom'] ) : ''; |
| 18 |
} |
| 19 |
|
| 20 |
return $data; |
| 21 |
} |
| 22 |
|
| 23 |
public function get_target( $requested_url, $source_url, Red_Source_Flags $flags ) { |
| 24 |
// Check if referrer matches |
| 25 |
$matched = current_user_can( $this->role ); |
| 26 |
|
| 27 |
$target = false; |
| 28 |
if ( $this->url_from !== '' && $matched ) { |
| 29 |
$target = $this->url_from; |
| 30 |
} elseif ( $this->url_notfrom !== '' && ! $matched ) { |
| 31 |
$target = $this->url_notfrom; |
| 32 |
} |
| 33 |
|
| 34 |
if ( $flags->is_regex() && $target ) { |
| 35 |
$target = $this->get_target_regex_url( $source_url, $target, $requested_url, $flags ); |
| 36 |
} |
| 37 |
|
| 38 |
return $target; |
| 39 |
} |
| 40 |
|
| 41 |
public function get_data() { |
| 42 |
return array( |
| 43 |
'url_from' => $this->url_from, |
| 44 |
'url_notfrom' => $this->url_notfrom, |
| 45 |
'role' => $this->role, |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
public function load( $values ) { |
| 50 |
$values = unserialize( $values ); |
| 51 |
|
| 52 |
if ( isset( $values['url_from'] ) ) { |
| 53 |
$this->url_from = $values['url_from']; |
| 54 |
$this->url_notfrom = $values['url_notfrom']; |
| 55 |
} |
| 56 |
|
| 57 |
$this->role = $values['role']; |
| 58 |
} |
| 59 |
} |
| 60 |
|