| @@ -36,8 +36,15 @@ | ||
| 36 | 36 | // Upgrades plugins and themes translations files |
| 37 | 37 | add_filter( 'themes_update_check_locales', array( $this, 'update_check_locales' ) ); |
| 38 | 38 | add_filter( 'plugins_update_check_locales', array( $this, 'update_check_locales' ) ); |
| 39 | 39 | |
| 40 | + // We need specific filters for German and Danish | |
| 41 | + $specific_locales = array( 'da_DK', 'de_DE', 'de_DE_formal', 'de_CH', 'de_CH_informal', 'ca', 'sr_RS', 'bs_BA' ); | |
| 42 | + if ( array_intersect( $this->model->get_languages_list( array( 'fields' => 'locale' ) ), $specific_locales ) ) { | |
| 43 | + add_filter( 'sanitize_title', array( $this, 'sanitize_title' ), 10, 3 ); | |
| 44 | + add_filter( 'sanitize_user', array( $this, 'sanitize_user' ), 10, 3 ); | |
| 45 | + } | |
| 46 | + | |
| 40 | 47 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
| 41 | 48 | |
| 42 | 49 | // Add post state for translations of the privacy policy page |
| 43 | 50 | add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 ); |
| @@ -166,17 +173,89 @@ | ||
| 166 | 173 | } |
| 167 | 174 | } |
| 168 | 175 | |
| 169 | 176 | /** |
| 170 | - * Allows to update translations files for plugins and themes. | |
| 177 | + * Allows to update translations files for plugins and themes | |
| 171 | 178 | * |
| 172 | 179 | * @since 1.6 |
| 173 | 180 | * |
| 174 | - * @param array $locales List of locales to update for plugins and themes. | |
| 175 | - * @return array | |
| 181 | + * @param array $locales Not used | |
| 182 | + * @return array list of locales to update | |
| 176 | 183 | */ |
| 177 | 184 | public function update_check_locales( $locales ) { |
| 178 | - return array_merge( $locales, $this->model->get_languages_list( array( 'fields' => 'locale' ) ) ); | |
| 185 | + return $this->model->get_languages_list( array( 'fields' => 'locale' ) ); | |
| 186 | + } | |
| 187 | + | |
| 188 | + /** | |
| 189 | + * Filters the locale according to the current language instead of the language | |
| 190 | + * of the admin interface | |
| 191 | + * | |
| 192 | + * @since 2.0 | |
| 193 | + * | |
| 194 | + * @param string $locale | |
| 195 | + * @return string | |
| 196 | + */ | |
| 197 | + public function get_locale( $locale ) { | |
| 198 | + if ( isset( $_POST['post_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 199 | + $locale = $lang->locale; | |
| 200 | + } elseif ( isset( $_POST['term_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 201 | + $locale = $lang->locale; | |
| 202 | + } elseif ( isset( $_POST['inline_lang_choice'] ) && $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 203 | + $locale = $lang->locale; | |
| 204 | + } elseif ( ! empty( $this->curlang ) ) { | |
| 205 | + $locale = $this->curlang->locale; | |
| 206 | + } | |
| 207 | + | |
| 208 | + return $locale; | |
| 209 | + } | |
| 210 | + | |
| 211 | + /** | |
| 212 | + * Maybe fix the result of sanitize_title() in case the languages include German or Danish | |
| 213 | + * Without this filter, if language of the title being sanitized is different from the language | |
| 214 | + * used for the admin interface and if one this language is German or Danish, some specific | |
| 215 | + * characters such as ä, ö, ü, ß are incorrectly sanitized. | |
| 216 | + * | |
| 217 | + * @since 2.0 | |
| 218 | + * | |
| 219 | + * @param string $title Sanitized title. | |
| 220 | + * @param string $raw_title The title prior to sanitization. | |
| 221 | + * @param string $context The context for which the title is being sanitized. | |
| 222 | + * @return string | |
| 223 | + */ | |
| 224 | + public function sanitize_title( $title, $raw_title, $context ) { | |
| 225 | + static $once = false; | |
| 226 | + | |
| 227 | + if ( ! $once && 'save' == $context && ! empty( $title ) ) { | |
| 228 | + $once = true; | |
| 229 | + add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface | |
| 230 | + $title = sanitize_title( $raw_title, '', $context ); | |
| 231 | + remove_filter( 'locale', array( $this, 'get_locale' ), 20 ); | |
| 232 | + $once = false; | |
| 233 | + } | |
| 234 | + return $title; | |
| 235 | + } | |
| 236 | + | |
| 237 | + /** | |
| 238 | + * Maybe fix the result of sanitize_user() in case the languages include German or Danish | |
| 239 | + * | |
| 240 | + * @since 2.0 | |
| 241 | + * | |
| 242 | + * @param string $username Sanitized username. | |
| 243 | + * @param string $raw_username The username prior to sanitization. | |
| 244 | + * @param bool $strict Whether to limit the sanitization to specific characters. Default false. | |
| 245 | + * @return string | |
| 246 | + */ | |
| 247 | + public function sanitize_user( $username, $raw_username, $strict ) { | |
| 248 | + static $once = false; | |
| 249 | + | |
| 250 | + if ( ! $once ) { | |
| 251 | + $once = true; | |
| 252 | + add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface | |
| 253 | + $username = sanitize_user( $raw_username, '', $strict ); | |
| 254 | + remove_filter( 'locale', array( $this, 'get_locale' ), 20 ); | |
| 255 | + $once = false; | |
| 256 | + } | |
| 257 | + return $username; | |
| 179 | 258 | } |
| 180 | 259 | |
| 181 | 260 | /** |
| 182 | 261 | * Adds a text direction dependent class to the body |