| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Check the client language |
| 5 |
*/ |
| 6 |
class Language_Match extends Red_Match { |
| 7 |
use FromNotFrom_Match; |
| 8 |
|
| 9 |
/** |
| 10 |
* Language to check. |
| 11 |
* |
| 12 |
* @var String |
| 13 |
*/ |
| 14 |
public $language = ''; |
| 15 |
|
| 16 |
public function name() { |
| 17 |
return __( 'URL and language', 'redirection' ); |
| 18 |
} |
| 19 |
|
| 20 |
public function save( array $details, $no_target_url = false ) { |
| 21 |
$data = array( 'language' => isset( $details['language'] ) ? $this->sanitize_language( $details['language'] ) : '' ); |
| 22 |
|
| 23 |
return $this->save_data( $details, $no_target_url, $data ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Sanitize the language value to a CSV string |
| 28 |
* |
| 29 |
* @param String $language User supplied language strings. |
| 30 |
* @return String |
| 31 |
*/ |
| 32 |
private function sanitize_language( $language ) { |
| 33 |
$parts = explode( ',', str_replace( ' ', '', $language ) ); |
| 34 |
return implode( ',', $parts ); |
| 35 |
} |
| 36 |
|
| 37 |
public function is_match( $url ) { |
| 38 |
$matches = explode( ',', $this->language ); |
| 39 |
$requested = Redirection_Request::get_accept_language(); |
| 40 |
|
| 41 |
foreach ( $matches as $match ) { |
| 42 |
if ( in_array( $match, $requested, true ) ) { |
| 43 |
return true; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
public function get_data() { |
| 51 |
return array_merge( array( |
| 52 |
'language' => $this->language, |
| 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->language = isset( $values['language'] ) ? $values['language'] : ''; |
| 65 |
} |
| 66 |
} |
| 67 |
|