| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Setup miscellaneous admin filters as well as filters common to admin and frontend |
| 8 | 5 | * |
| @@ -36,12 +33,16 @@ | ||
| 36 | 33 | // Upgrades plugins and themes translations files |
| 37 | 34 | add_filter( 'themes_update_check_locales', array( $this, 'update_check_locales' ) ); |
| 38 | 35 | add_filter( 'plugins_update_check_locales', array( $this, 'update_check_locales' ) ); |
| 39 | 36 | |
| 37 | + // We need specific filters for German and Danish | |
| 38 | + $specific_locales = array( 'da_DK', 'de_DE', 'de_DE_formal', 'de_CH', 'de_CH_informal', 'ca', 'sr_RS', 'bs_BA' ); | |
| 39 | + if ( array_intersect( $this->model->get_languages_list( array( 'fields' => 'locale' ) ), $specific_locales ) ) { | |
| 40 | + add_filter( 'sanitize_title', array( $this, 'sanitize_title' ), 10, 3 ); | |
| 41 | + add_filter( 'sanitize_user', array( $this, 'sanitize_user' ), 10, 3 ); | |
| 42 | + } | |
| 43 | + | |
| 40 | 44 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
| 41 | - | |
| 42 | - // Add post state for translations of the privacy policy page | |
| 43 | - add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 ); | |
| 44 | 45 | } |
| 45 | 46 | |
| 46 | 47 | /** |
| 47 | 48 | * Modifies the widgets forms to add our language dropdown list |
| @@ -64,9 +65,8 @@ | ||
| 64 | 65 | array_merge( |
| 65 | 66 | array( (object) array( 'slug' => 0, 'name' => __( 'All languages', 'polylang' ) ) ), |
| 66 | 67 | $this->model->get_languages_list() |
| 67 | 68 | ), |
| 68 | - -1, | |
| 69 | 69 | array( |
| 70 | 70 | 'name' => $widget->id . '_lang_choice', |
| 71 | 71 | 'class' => 'tags-input pll-lang-choice', |
| 72 | 72 | 'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'], |
| @@ -166,49 +166,102 @@ | ||
| 166 | 166 | } |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | /** |
| 170 | - * Allows to update translations files for plugins and themes. | |
| 170 | + * Allows to update translations files for plugins and themes | |
| 171 | 171 | * |
| 172 | 172 | * @since 1.6 |
| 173 | 173 | * |
| 174 | - * @param array $locales List of locales to update for plugins and themes. | |
| 175 | - * @return array | |
| 174 | + * @param array $locales Not used | |
| 175 | + * @return array list of locales to update | |
| 176 | 176 | */ |
| 177 | 177 | public function update_check_locales( $locales ) { |
| 178 | - return array_merge( $locales, $this->model->get_languages_list( array( 'fields' => 'locale' ) ) ); | |
| 178 | + return $this->model->get_languages_list( array( 'fields' => 'locale' ) ); | |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | /** |
| 182 | - * Adds a text direction dependent class to the body | |
| 182 | + * Filters the locale according to the current language instead of the language | |
| 183 | + * of the admin interface | |
| 183 | 184 | * |
| 184 | - * @since 2.2 | |
| 185 | + * @since 2.0 | |
| 185 | 186 | * |
| 186 | - * @param string $classes Space-separated list of CSS classes. | |
| 187 | + * @param string $locale | |
| 187 | 188 | * @return string |
| 188 | 189 | */ |
| 189 | - public function admin_body_class( $classes ) { | |
| 190 | - if ( ! empty( $this->curlang ) ) { | |
| 191 | - $classes .= ' pll-dir-' . ( $this->curlang->is_rtl ? 'rtl' : 'ltr' ); | |
| 190 | + public function get_locale( $locale ) { | |
| 191 | + if ( isset( $_POST['post_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 192 | + $locale = $lang->locale; | |
| 193 | + } elseif ( isset( $_POST['term_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 194 | + $locale = $lang->locale; | |
| 195 | + } elseif ( isset( $_POST['inline_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 196 | + $locale = $lang->locale; | |
| 197 | + } elseif ( ! empty( $this->curlang ) ) { | |
| 198 | + $locale = $this->curlang->locale; | |
| 192 | 199 | } |
| 193 | - return $classes; | |
| 200 | + | |
| 201 | + return $locale; | |
| 194 | 202 | } |
| 195 | 203 | |
| 196 | 204 | /** |
| 197 | - * Add post state for translations of the privacy policy page | |
| 205 | + * Maybe fix the result of sanitize_title() in case the languages include German or Danish | |
| 206 | + * Without this filter, if language of the title being sanitized is different from the language | |
| 207 | + * used for the admin interface and if one this language is German or Danish, some specific | |
| 208 | + * characters such as ä, ö, ü, ß are incorrectly sanitized. | |
| 198 | 209 | * |
| 199 | - * @since 2.7 | |
| 210 | + * @since 2.0 | |
| 200 | 211 | * |
| 201 | - * @param array $post_states An array of post display states. | |
| 202 | - * @param object $post The current post object. | |
| 203 | - * @return array | |
| 212 | + * @param string $title Sanitized title. | |
| 213 | + * @param string $raw_title The title prior to sanitization. | |
| 214 | + * @param string $context The context for which the title is being sanitized. | |
| 215 | + * @return string | |
| 204 | 216 | */ |
| 205 | - public function display_post_states( $post_states, $post ) { | |
| 206 | - $page_for_privacy_policy = get_option( 'wp_page_for_privacy_policy' ); | |
| 217 | + public function sanitize_title( $title, $raw_title, $context ) { | |
| 218 | + static $once = false; | |
| 207 | 219 | |
| 208 | - if ( $page_for_privacy_policy && in_array( $post->ID, $this->model->post->get_translations( $page_for_privacy_policy ) ) ) { | |
| 209 | - $post_states['page_for_privacy_policy'] = __( 'Privacy Policy Page', 'polylang' ); | |
| 220 | + if ( ! $once && 'save' == $context && ! empty( $title ) ) { | |
| 221 | + $once = true; | |
| 222 | + add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface | |
| 223 | + $title = sanitize_title( $raw_title, '', $context ); | |
| 224 | + remove_filter( 'locale', array( $this, 'get_locale' ), 20 ); | |
| 225 | + $once = false; | |
| 210 | 226 | } |
| 227 | + return $title; | |
| 228 | + } | |
| 211 | 229 | |
| 212 | - return $post_states; | |
| 230 | + /** | |
| 231 | + * Maybe fix the result of sanitize_user() in case the languages include German or Danish | |
| 232 | + * | |
| 233 | + * @since 2.0 | |
| 234 | + * | |
| 235 | + * @param string $username Sanitized username. | |
| 236 | + * @param string $raw_username The username prior to sanitization. | |
| 237 | + * @param bool $strict Whether to limit the sanitization to specific characters. Default false. | |
| 238 | + * @return string | |
| 239 | + */ | |
| 240 | + public function sanitize_user( $username, $raw_username, $strict ) { | |
| 241 | + static $once = false; | |
| 242 | + | |
| 243 | + if ( ! $once ) { | |
| 244 | + $once = true; | |
| 245 | + add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface | |
| 246 | + $username = sanitize_user( $raw_username, '', $strict ); | |
| 247 | + remove_filter( 'locale', array( $this, 'get_locale' ), 20 ); | |
| 248 | + $once = false; | |
| 249 | + } | |
| 250 | + return $username; | |
| 251 | + } | |
| 252 | + | |
| 253 | + /** | |
| 254 | + * Adds a text direction dependent class to the body | |
| 255 | + * | |
| 256 | + * @since 2.2 | |
| 257 | + * | |
| 258 | + * @param string $classes Space-separated list of CSS classes. | |
| 259 | + * @return string | |
| 260 | + */ | |
| 261 | + public function admin_body_class( $classes ) { | |
| 262 | + if ( ! empty( $this->curlang ) ) { | |
| 263 | + $classes .= ' pll-dir-' . ( $this->curlang->is_rtl ? 'rtl' : 'ltr' ); | |
| 264 | + } | |
| 265 | + return $classes; | |
| 213 | 266 | } |
| 214 | 267 | } |