| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Choose the language when the language code is added to all urls |
| 8 |
* The language is set in plugins_loaded with priority 1 as done by WPML |
| 9 |
* Some actions have to be delayed to wait for $wp_rewrite availability |
| 10 |
* |
| 11 |
* @since 1.2 |
| 12 |
*/ |
| 13 |
class PLL_Choose_Lang_Url extends PLL_Choose_Lang { |
| 14 |
/** |
| 15 |
* The name of the index file which is the entry point to all requests. |
| 16 |
* We need this before the global $wp_rewrite is created. |
| 17 |
* Also hardcoded in WP_Rewrite. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $index = 'index.php'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Sets the language |
| 25 |
* |
| 26 |
* @since 1.8 |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function init() { |
| 31 |
parent::init(); |
| 32 |
|
| 33 |
if ( ! did_action( 'pll_language_defined' ) ) { |
| 34 |
$this->set_language(); |
| 35 |
} |
| 36 |
|
| 37 |
add_filter( 'request', array( $this, 'request' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns the language according to information found in the url. |
| 42 |
* |
| 43 |
* @since 1.2 |
| 44 |
* @since 3.8 Renamed from `get_language_from_url()`. |
| 45 |
* |
| 46 |
* @return PLL_Language|false |
| 47 |
*/ |
| 48 |
protected function get_current_language() { |
| 49 |
$host = str_replace( 'www.', '', (string) wp_parse_url( $this->links_model->home, PHP_URL_HOST ) ); // Remove www. for the comparison. |
| 50 |
$home_path = (string) wp_parse_url( $this->links_model->home, PHP_URL_PATH ); |
| 51 |
|
| 52 |
$requested_url = pll_get_requested_url(); |
| 53 |
$requested_host = str_replace( 'www.', '', (string) wp_parse_url( $requested_url, PHP_URL_HOST ) ); // Remove www. for the comparison. |
| 54 |
$requested_path = rtrim( str_replace( $this->index, '', (string) wp_parse_url( $requested_url, PHP_URL_PATH ) ), '/' ); // Some PHP setups turn requests for / into /index.php in REQUEST_URI. |
| 55 |
$requested_query = wp_parse_url( $requested_url, PHP_URL_QUERY ); |
| 56 |
|
| 57 |
// Home is requested. |
| 58 |
if ( $requested_host === $host && $requested_path === $home_path && empty( $requested_query ) ) { |
| 59 |
add_action( 'setup_theme', array( $this, 'home_requested' ) ); |
| 60 |
return $this->get_home_language(); |
| 61 |
} |
| 62 |
|
| 63 |
// Take care to post & page preview http://wordpress.org/support/topic/static-frontpage-url-parameter-url-language-information. |
| 64 |
if ( isset( $_GET['preview'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 65 |
foreach ( array( 'p', 'page_id' ) as $var ) { |
| 66 |
if ( empty( $_GET[ $var ] ) || ! is_numeric( $_GET[ $var ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 67 |
continue; |
| 68 |
} |
| 69 |
$lang = $this->model->post->get_language( (int) $_GET[ $var ] ); // phpcs:ignore WordPress.Security.NonceVerification |
| 70 |
return $lang ?: $this->model->get_default_language(); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Take care to (unattached) attachments. |
| 75 |
if ( ! empty( $_GET['attachment_id'] ) && is_numeric( $_GET['attachment_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 76 |
return $this->model->post->get_language( (int) $_GET['attachment_id'] ); // phpcs:ignore WordPress.Security.NonceVerification |
| 77 |
} |
| 78 |
|
| 79 |
$slug = $this->links_model->get_language_from_url(); |
| 80 |
|
| 81 |
if ( ! empty( $slug ) ) { |
| 82 |
return $this->model->get_language( $slug ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( $this->options['hide_default'] ) { |
| 86 |
return $this->model->get_default_language(); |
| 87 |
} |
| 88 |
|
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* Adds the current language in query vars |
| 95 |
* useful for subdomains and multiple domains |
| 96 |
* |
| 97 |
* @since 1.8 |
| 98 |
* |
| 99 |
* @param array $qv main request query vars |
| 100 |
* @return array modified query vars |
| 101 |
*/ |
| 102 |
public function request( $qv ) { |
| 103 |
// FIXME take care not to break untranslated content |
| 104 |
// FIXME media ? |
| 105 |
|
| 106 |
// Untranslated post types |
| 107 |
if ( isset( $qv['post_type'] ) && ! $this->model->is_translated_post_type( $qv['post_type'] ) ) { |
| 108 |
return $qv; |
| 109 |
} |
| 110 |
|
| 111 |
// Untranslated taxonomies |
| 112 |
$tax_qv = array_filter( wp_list_pluck( get_taxonomies( array(), 'objects' ), 'query_var' ) ); // Get all taxonomies query vars |
| 113 |
$tax_qv = array_intersect( $tax_qv, array_keys( $qv ) ); // Get all queried taxonomies query vars |
| 114 |
|
| 115 |
if ( ! $this->model->is_translated_taxonomy( array_keys( $tax_qv ) ) ) { |
| 116 |
return $qv; |
| 117 |
} |
| 118 |
|
| 119 |
if ( isset( $this->curlang ) && empty( $qv['lang'] ) ) { |
| 120 |
$qv['lang'] = $this->curlang->slug; |
| 121 |
} |
| 122 |
|
| 123 |
return $qv; |
| 124 |
} |
| 125 |
} |
| 126 |
|