| @@ -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. |