| @@ -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 |
| @@ -55,30 +56,26 @@ | ||
| 55 | 56 | public function in_widget_form( $widget, $return, $instance ) { |
| 56 | 57 | $screen = get_current_screen(); |
| 57 | 58 | |
| 58 | 59 | // Test the Widgets screen and the Customizer to avoid displaying the option in page builders |
| 59 | - // Saving the widget reloads the form. And curiously the action is in $_REQUEST but neither in $_POST, nor in $_GET. | |
| 60 | - if ( ( isset( $screen ) && 'widgets' === $screen->base ) || ( isset( $_REQUEST['action'] ) && 'save-widget' === $_REQUEST['action'] ) || isset( $GLOBALS['wp_customize'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 60 | + // Saving the widget reloads the form. And curiously the action is in $_REQUEST but neither in $_POST, not in $_GET. | |
| 61 | + if ( ( isset( $screen ) && 'widgets' === $screen->base ) || ( isset( $_REQUEST['action'] ) && 'save-widget' === $_REQUEST['action'] ) || isset( $GLOBALS['wp_customize'] ) ) { | |
| 61 | 62 | $dropdown = new PLL_Walker_Dropdown(); |
| 62 | - | |
| 63 | - $dropdown_html = $dropdown->walk( | |
| 64 | - array_merge( | |
| 65 | - array( (object) array( 'slug' => 0, 'name' => __( 'All languages', 'polylang' ) ) ), | |
| 66 | - $this->model->get_languages_list() | |
| 67 | - ), | |
| 68 | - -1, | |
| 69 | - array( | |
| 70 | - 'name' => $widget->id . '_lang_choice', | |
| 71 | - 'class' => 'tags-input pll-lang-choice', | |
| 72 | - 'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'], | |
| 73 | - ) | |
| 74 | - ); | |
| 75 | - | |
| 76 | 63 | printf( |
| 77 | 64 | '<p><label for="%1$s">%2$s %3$s</label></p>', |
| 78 | 65 | esc_attr( $widget->id . '_lang_choice' ), |
| 79 | 66 | esc_html__( 'The widget is displayed for:', 'polylang' ), |
| 80 | - $dropdown_html // phpcs:ignore WordPress.Security.EscapeOutput | |
| 67 | + $dropdown->walk( | |
| 68 | + array_merge( | |
| 69 | + array( (object) array( 'slug' => 0, 'name' => __( 'All languages', 'polylang' ) ) ), | |
| 70 | + $this->model->get_languages_list() | |
| 71 | + ), | |
| 72 | + array( | |
| 73 | + 'name' => $widget->id . '_lang_choice', | |
| 74 | + 'class' => 'tags-input pll-lang-choice', | |
| 75 | + 'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'], | |
| 76 | + ) | |
| 77 | + ) | |
| 81 | 78 | ); |
| 82 | 79 | } |
| 83 | 80 | } |
| 84 | 81 | |
| @@ -94,12 +91,10 @@ | ||
| 94 | 91 | * @param object $widget WP_Widget object |
| 95 | 92 | * @return array Widget options |
| 96 | 93 | */ |
| 97 | 94 | public function widget_update_callback( $instance, $new_instance, $old_instance, $widget ) { |
| 98 | - $key = $widget->id . '_lang_choice'; | |
| 99 | - | |
| 100 | - if ( ! empty( $_POST[ $key ] ) && $lang = $this->model->get_language( sanitize_key( $_POST[ $key ] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 101 | - $instance['pll_lang'] = $lang->slug; | |
| 95 | + if ( ! empty( $_POST[ $key = $widget->id . '_lang_choice' ] ) && in_array( $_POST[ $key ], $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) ) { | |
| 96 | + $instance['pll_lang'] = $_POST[ $key ]; | |
| 102 | 97 | } else { |
| 103 | 98 | unset( $instance['pll_lang'] ); |
| 104 | 99 | } |
| 105 | 100 | |
| @@ -116,9 +111,9 @@ | ||
| 116 | 111 | public function personal_options_update( $user_id ) { |
| 117 | 112 | // Biography translations |
| 118 | 113 | foreach ( $this->model->get_languages_list() as $lang ) { |
| 119 | 114 | $meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug; |
| 120 | - $description = empty( $_POST[ 'description_' . $lang->slug ] ) ? '' : trim( $_POST[ 'description_' . $lang->slug ] ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput | |
| 115 | + $description = empty( $_POST[ 'description_' . $lang->slug ] ) ? '' : trim( $_POST[ 'description_' . $lang->slug ] ); | |
| 121 | 116 | |
| 122 | 117 | /** This filter is documented in wp-includes/user.php */ |
| 123 | 118 | $description = apply_filters( 'pre_user_description', $description ); // Applies WP default filter wp_filter_kses |
| 124 | 119 | update_user_meta( $user_id, $meta, $description ); |
| @@ -166,49 +161,102 @@ | ||
| 166 | 161 | } |
| 167 | 162 | } |
| 168 | 163 | |
| 169 | 164 | /** |
| 170 | - * Allows to update translations files for plugins and themes. | |
| 165 | + * Allows to update translations files for plugins and themes | |
| 171 | 166 | * |
| 172 | 167 | * @since 1.6 |
| 173 | 168 | * |
| 174 | - * @param array $locales List of locales to update for plugins and themes. | |
| 175 | - * @return array | |
| 169 | + * @param array $locales Not used | |
| 170 | + * @return array list of locales to update | |
| 176 | 171 | */ |
| 177 | 172 | public function update_check_locales( $locales ) { |
| 178 | - return array_merge( $locales, $this->model->get_languages_list( array( 'fields' => 'locale' ) ) ); | |
| 173 | + return $this->model->get_languages_list( array( 'fields' => 'locale' ) ); | |
| 179 | 174 | } |
| 180 | 175 | |
| 181 | 176 | /** |
| 182 | - * Adds a text direction dependent class to the body | |
| 177 | + * Filters the locale according to the current language instead of the language | |
| 178 | + * of the admin interface | |
| 183 | 179 | * |
| 184 | - * @since 2.2 | |
| 180 | + * @since 2.0 | |
| 185 | 181 | * |
| 186 | - * @param string $classes Space-separated list of CSS classes. | |
| 182 | + * @param string $locale | |
| 187 | 183 | * @return string |
| 188 | 184 | */ |
| 189 | - public function admin_body_class( $classes ) { | |
| 190 | - if ( ! empty( $this->curlang ) ) { | |
| 191 | - $classes .= ' pll-dir-' . ( $this->curlang->is_rtl ? 'rtl' : 'ltr' ); | |
| 185 | + public function get_locale( $locale ) { | |
| 186 | + if ( isset( $_POST['post_lang_choice'] ) && $lang = $this->model->get_language( $_POST['post_lang_choice'] ) ) { | |
| 187 | + $locale = $lang->locale; | |
| 188 | + } elseif ( isset( $_POST['term_lang_choice'] ) && $lang = $this->model->get_language( $_POST['term_lang_choice'] ) ) { | |
| 189 | + $locale = $lang->locale; | |
| 190 | + } elseif ( isset( $_POST['inline_lang_choice'] ) && $lang = $this->model->get_language( $_POST['inline_lang_choice'] ) ) { | |
| 191 | + $locale = $lang->locale; | |
| 192 | + } elseif ( ! empty( $this->curlang ) ) { | |
| 193 | + $locale = $this->curlang->locale; | |
| 192 | 194 | } |
| 193 | - return $classes; | |
| 195 | + | |
| 196 | + return $locale; | |
| 194 | 197 | } |
| 195 | 198 | |
| 196 | 199 | /** |
| 197 | - * Add post state for translations of the privacy policy page | |
| 200 | + * Maybe fix the result of sanitize_title() in case the languages include German or Danish | |
| 201 | + * Without this filter, if language of the title being sanitized is different from the language | |
| 202 | + * used for the admin interface and if one this language is German or Danish, some specific | |
| 203 | + * characters such as ä, ö, ü, ß are incorrectly sanitized. | |
| 198 | 204 | * |
| 199 | - * @since 2.7 | |
| 205 | + * @since 2.0 | |
| 200 | 206 | * |
| 201 | - * @param array $post_states An array of post display states. | |
| 202 | - * @param object $post The current post object. | |
| 203 | - * @return array | |
| 207 | + * @param string $title Sanitized title. | |
| 208 | + * @param string $raw_title The title prior to sanitization. | |
| 209 | + * @param string $context The context for which the title is being sanitized. | |
| 210 | + * @return string | |
| 204 | 211 | */ |
| 205 | - public function display_post_states( $post_states, $post ) { | |
| 206 | - $page_for_privacy_policy = get_option( 'wp_page_for_privacy_policy' ); | |
| 212 | + public function sanitize_title( $title, $raw_title, $context ) { | |
| 213 | + static $once = false; | |
| 207 | 214 | |
| 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' ); | |
| 215 | + if ( ! $once && 'save' == $context && ! empty( $title ) ) { | |
| 216 | + $once = true; | |
| 217 | + add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface | |
| 218 | + $title = sanitize_title( $raw_title, '', $context ); | |
| 219 | + remove_filter( 'locale', array( $this, 'get_locale' ), 20 ); | |
| 220 | + $once = false; | |
| 210 | 221 | } |
| 222 | + return $title; | |
| 223 | + } | |
| 211 | 224 | |
| 212 | - return $post_states; | |
| 225 | + /** | |
| 226 | + * Maybe fix the result of sanitize_user() in case the languages include German or Danish | |
| 227 | + * | |
| 228 | + * @since 2.0 | |
| 229 | + * | |
| 230 | + * @param string $username Sanitized username. | |
| 231 | + * @param string $raw_username The username prior to sanitization. | |
| 232 | + * @param bool $strict Whether to limit the sanitization to specific characters. Default false. | |
| 233 | + * @return string | |
| 234 | + */ | |
| 235 | + public function sanitize_user( $username, $raw_username, $strict ) { | |
| 236 | + static $once = false; | |
| 237 | + | |
| 238 | + if ( ! $once ) { | |
| 239 | + $once = true; | |
| 240 | + add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface | |
| 241 | + $username = sanitize_user( $raw_username, '', $strict ); | |
| 242 | + remove_filter( 'locale', array( $this, 'get_locale' ), 20 ); | |
| 243 | + $once = false; | |
| 244 | + } | |
| 245 | + return $username; | |
| 246 | + } | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * Adds a text direction dependent class to the body | |
| 250 | + * | |
| 251 | + * @since 2.2 | |
| 252 | + * | |
| 253 | + * @param string $classes Space-separated list of CSS classes. | |
| 254 | + * @return string | |
| 255 | + */ | |
| 256 | + public function admin_body_class( $classes ) { | |
| 257 | + if ( ! empty( $this->curlang ) ) { | |
| 258 | + $classes .= ' pll-dir-' . ( $this->curlang->is_rtl ? 'rtl' : 'ltr' ); | |
| 259 | + } | |
| 260 | + return $classes; | |
| 213 | 261 | } |
| 214 | 262 | } |