| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Match the user agent |
| 5 |
*/ |
| 6 |
class Agent_Match extends Red_Match { |
| 7 |
use FromNotFrom_Match; |
| 8 |
|
| 9 |
/** |
| 10 |
* User agent. |
| 11 |
* |
| 12 |
* @var String |
| 13 |
*/ |
| 14 |
public $agent = ''; |
| 15 |
|
| 16 |
/** |
| 17 |
* Is this a regex match? |
| 18 |
* |
| 19 |
* @var boolean |
| 20 |
*/ |
| 21 |
public $regex = false; |
| 22 |
|
| 23 |
public function name() { |
| 24 |
return __( 'URL and user agent', 'redirection' ); |
| 25 |
} |
| 26 |
|
| 27 |
public function save( array $details, $no_target_url = false ) { |
| 28 |
$data = array( |
| 29 |
'regex' => isset( $details['regex'] ) && $details['regex'] ? true : false, |
| 30 |
'agent' => isset( $details['agent'] ) ? $this->sanitize_agent( $details['agent'] ) : '', |
| 31 |
); |
| 32 |
|
| 33 |
return $this->save_data( $details, $no_target_url, $data ); |
| 34 |
} |
| 35 |
|
| 36 |
private function sanitize_agent( $agent ) { |
| 37 |
return $this->sanitize_url( $agent ); |
| 38 |
} |
| 39 |
|
| 40 |
public function is_match( $url ) { |
| 41 |
if ( $this->regex ) { |
| 42 |
$regex = new Red_Regex( $this->agent, true ); |
| 43 |
return $regex->is_match( Redirection_Request::get_user_agent() ); |
| 44 |
} |
| 45 |
|
| 46 |
return $this->agent === Redirection_Request::get_user_agent(); |
| 47 |
} |
| 48 |
|
| 49 |
public function get_data() { |
| 50 |
return array_merge( array( |
| 51 |
'regex' => $this->regex, |
| 52 |
'agent' => $this->agent, |
| 53 |
), $this->get_from_data() ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Load the match data into this instance. |
| 58 |
* |
| 59 |
* @param String $values Match values, as read from the database (plain text or serialized PHP). |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function load( $values ) { |
| 63 |
$values = $this->load_data( $values ); |
| 64 |
$this->regex = isset( $values['regex'] ) ? $values['regex'] : false; |
| 65 |
$this->agent = isset( $values['agent'] ) ? $values['agent'] : ''; |
| 66 |
} |
| 67 |
} |
| 68 |
|