| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Choose the language when the language code is added to all urls |
| 5 |
* The language is set in plugins_loaded with priority 1 as done by WPML |
| 6 |
* Some actions have to be delayed to wait for $wp_rewrite availibility |
| 7 |
* |
| 8 |
* @since 1.2 |
| 9 |
*/ |
| 10 |
class PLL_Choose_Lang_Url extends PLL_Choose_lang { |
| 11 |
protected $index = 'index.php'; // need this before $wp_rewrite is created, also harcoded in wp-includes/rewrite.php |
| 12 |
|
| 13 |
/** |
| 14 |
* sets the language |
| 15 |
* |
| 16 |
* @since 1.8 |
| 17 |
*/ |
| 18 |
public function init() { |
| 19 |
parent::init(); |
| 20 |
|
| 21 |
if ( ! did_action( 'pll_language_defined' ) ) { |
| 22 |
$this->set_language_from_url(); |
| 23 |
} |
| 24 |
|
| 25 |
add_action( 'request', array( $this, 'request' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* finds the language according to information found in the url |
| 30 |
* |
| 31 |
* @since 1.2 |
| 32 |
*/ |
| 33 |
public function set_language_from_url() { |
| 34 |
$host = str_replace( 'www.', '', parse_url( $this->links_model->home, PHP_URL_HOST ) ); |
| 35 |
$home_path = parse_url( $this->links_model->home, PHP_URL_PATH ); |
| 36 |
|
| 37 |
$requested_host = str_replace( 'www.', '', $_SERVER['HTTP_HOST'] ); |
| 38 |
$requested_uri = rtrim( str_replace( $this->index, '', $_SERVER['REQUEST_URI'] ), '/' ); // some PHP setups turn requests for / into /index.php in REQUEST_URI |
| 39 |
|
| 40 |
// home is resquested |
| 41 |
if ( $requested_host == $host && $requested_uri == $home_path && empty( $_SERVER['QUERY_STRING'] ) ) { |
| 42 |
$this->home_language(); |
| 43 |
add_action( 'setup_theme', array( $this, 'home_requested' ) ); |
| 44 |
} |
| 45 |
|
| 46 |
// take care to post & page preview http://wordpress.org/support/topic/static-frontpage-url-parameter-url-language-information |
| 47 |
elseif ( isset( $_GET['preview'] ) && ( ( isset( $_GET['p'] ) && $id = (int) $_GET['p'] ) || ( isset( $_GET['page_id'] ) && $id = (int) $_GET['page_id'] ) ) ) { |
| 48 |
$curlang = ( $lg = $this->model->post->get_language( $id ) ) ? $lg : $this->model->get_language( $this->options['default_lang'] ); |
| 49 |
} |
| 50 |
|
| 51 |
// take care to ( unattached ) attachments |
| 52 |
elseif ( isset( $_GET['attachment_id'] ) && $id = (int) $_GET['attachment_id'] ) { |
| 53 |
$curlang = ( $lg = $this->model->post->get_language( $id ) ) ? $lg : $this->get_preferred_language(); |
| 54 |
} |
| 55 |
|
| 56 |
elseif ( $slug = $this->links_model->get_language_from_url() ) { |
| 57 |
$curlang = $this->model->get_language( $slug ); |
| 58 |
} |
| 59 |
|
| 60 |
elseif ( $this->options['hide_default'] ) { |
| 61 |
$curlang = $this->model->get_language( $this->options['default_lang'] ); |
| 62 |
} |
| 63 |
|
| 64 |
// if no language found, check_language_code_in_url will attempt to find one and redirect to the correct url |
| 65 |
// otherwise 404 will be fired in the preferred language |
| 66 |
$this->set_language( empty( $curlang ) ? $this->get_preferred_language() : $curlang ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* adds the current language in query vars |
| 72 |
* useful for subdomains and multiple domains |
| 73 |
* |
| 74 |
* @since 1.8 |
| 75 |
* |
| 76 |
* @param array $qv main request query vars |
| 77 |
* @return array modified query vars |
| 78 |
*/ |
| 79 |
public function request( $qv ) { |
| 80 |
// FIXME take care not to break untranslated content |
| 81 |
// FIXME media ? |
| 82 |
|
| 83 |
// untranslated post types |
| 84 |
if ( isset( $qv['post_type'] ) && ! $this->model->is_translated_post_type( $qv['post_type'] ) ) { |
| 85 |
return $qv; |
| 86 |
} |
| 87 |
|
| 88 |
// untranslated taxonomies |
| 89 |
$tax_qv = array_filter( wp_list_pluck( get_taxonomies( array(), 'objects' ), 'query_var' ) ); // get all taxonomies query vars |
| 90 |
$tax_qv = array_intersect( $tax_qv, array_keys( $qv ) ); // get all queried taxonomies query vars |
| 91 |
|
| 92 |
if ( ! $this->model->is_translated_taxonomy( array_keys( $tax_qv ) ) ) { |
| 93 |
return $qv; |
| 94 |
} |
| 95 |
|
| 96 |
if ( isset( $this->curlang ) && empty( $qv['lang'] ) ) { |
| 97 |
$qv['lang'] = $this->curlang->slug; |
| 98 |
} |
| 99 |
|
| 100 |
return $qv; |
| 101 |
} |
| 102 |
} |
| 103 |
|