| @@ -2,8 +2,10 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * @package Polylang |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | +use WP_Syntex\Polylang\Model\Languages; | |
| 7 | + | |
| 6 | 8 | /** |
| 7 | 9 | * Base class to choose the language |
| 8 | 10 | * |
| 9 | 11 | * @since 1.2 |
| @@ -59,15 +61,22 @@ | ||
| 59 | 61 | * |
| 60 | 62 | * @return void |
| 61 | 63 | */ |
| 62 | 64 | public function init() { |
| 63 | - if ( Polylang::is_ajax_on_front() || ! wp_using_themes() ) { | |
| 64 | - $this->set_language( empty( $_REQUEST['lang'] ) ? $this->get_preferred_language() : $this->model->get_language( sanitize_key( $_REQUEST['lang'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 65 | + add_action( 'pre_comment_on_post', array( $this, 'pre_comment_on_post' ) ); // sets the language of comment. | |
| 66 | + add_action( 'parse_query', array( $this, 'parse_main_query' ), 2 ); // sets the language in special cases. | |
| 67 | + add_action( 'wp', array( $this, 'maybe_setcookie' ), 7 ); | |
| 68 | + | |
| 69 | + if ( ! Polylang::is_ajax_on_front() && wp_using_themes() ) { | |
| 70 | + return; | |
| 65 | 71 | } |
| 66 | 72 | |
| 67 | - add_action( 'pre_comment_on_post', array( $this, 'pre_comment_on_post' ) ); // sets the language of comment | |
| 68 | - add_action( 'parse_query', array( $this, 'parse_main_query' ), 2 ); // sets the language in special cases | |
| 69 | - add_action( 'wp', array( $this, 'maybe_setcookie' ), 7 ); | |
| 73 | + if ( isset( $_REQUEST['lang'] ) && is_string( $_REQUEST['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 74 | + // Let's accept either WordPress locales or language codes. | |
| 75 | + $lang = $this->model->languages->get( Languages::is_locale( $_REQUEST['lang'] ) ? $_REQUEST['lang'] : sanitize_key( $_REQUEST['lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 76 | + } | |
| 77 | + | |
| 78 | + $this->set_language( empty( $lang ) ? $this->get_preferred_language() : $lang ); | |
| 70 | 79 | } |
| 71 | 80 | |
| 72 | 81 | /** |
| 73 | 82 | * Sets the current language. |
| @@ -334,9 +343,9 @@ | ||
| 334 | 343 | */ |
| 335 | 344 | if ( $lang = apply_filters( 'pll_set_language_from_query', false, $query ) ) { |
| 336 | 345 | $this->set_language( $lang ); |
| 337 | 346 | $this->set_curlang_in_query( $query ); |
| 338 | - } elseif ( ( count( $query->query ) == 1 || ( is_paged() && count( $query->query ) == 2 ) ) && $lang = get_query_var( 'lang' ) ) { | |
| 347 | + } elseif ( ( count( (array) $query->query ) == 1 || ( is_paged() && count( (array) $query->query ) == 2 ) ) && $lang = get_query_var( 'lang' ) ) { | |
| 339 | 348 | $lang = $this->model->get_language( $lang ); |
| 340 | 349 | $this->set_language( $lang ); // Set the language now otherwise it will be too late to filter sticky posts! |
| 341 | 350 | |
| 342 | 351 | // Set is_home on translated home page when it displays posts. It must be true on page 2, 3... too. |