PluginProbe
Polylang / 2.0.12
Polylang v2.0.12
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | admin/admin-filters.php +59 -96 2.82.0.12 View file →
@@ -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 *
@@ -28,9 +25,9 @@
28 25 add_action( 'personal_options_update', array( $this, 'personal_options_update' ) );
29 26 add_action( 'edit_user_profile_update', array( $this, 'personal_options_update' ) );
30 27 add_action( 'personal_options', array( $this, 'personal_options' ) );
31 28
32 - // Upgrades languages files after a core upgrade ( timing is important )
29 + // Ugrades languages files after a core upgrade ( timing is important )
33 30 // Backward compatibility WP < 4.0 *AND* Polylang < 1.6
34 31 add_action( '_core_updated_successfully', array( $this, 'upgrade_languages' ), 1 ); // since WP 3.3
35 32
36 33 // Upgrades plugins and themes translations files
@@ -37,57 +34,39 @@
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
40 37 // 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' );
38 + $specific_locales = array( 'da_DK', 'de_DE', 'de_DE_formal', 'de_CH', 'de_CH_informal' );
42 39 if ( array_intersect( $this->model->get_languages_list( array( 'fields' => 'locale' ) ), $specific_locales ) ) {
43 40 add_filter( 'sanitize_title', array( $this, 'sanitize_title' ), 10, 3 );
44 41 add_filter( 'sanitize_user', array( $this, 'sanitize_user' ), 10, 3 );
45 42 }
46 -
47 - add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
48 -
49 - // Add post state for translations of the privacy policy page
50 - add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
51 43 }
52 44
53 45 /**
54 - * Modifies the widgets forms to add our language dropdown list
46 + * Modifies the widgets forms to add our language dropdwown list
55 47 *
56 48 * @since 0.3
57 49 *
58 - * @param object $widget Widget instance
59 - * @param null $return Not used
60 - * @param array $instance Widget settings
50 + * @param object $widget
61 51 */
62 52 public function in_widget_form( $widget, $return, $instance ) {
63 - $screen = get_current_screen();
64 -
65 - // Test the Widgets screen and the Customizer to avoid displaying the option in page builders
66 - // Saving the widget reloads the form. And curiously the action is in $_REQUEST but neither in $_POST, nor in $_GET.
67 - if ( ( isset( $screen ) && 'widgets' === $screen->base ) || ( isset( $_REQUEST['action'] ) && 'save-widget' === $_REQUEST['action'] ) || isset( $GLOBALS['wp_customize'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
68 - $dropdown = new PLL_Walker_Dropdown();
69 -
70 - $dropdown_html = $dropdown->walk(
53 + $dropdown = new PLL_Walker_Dropdown();
54 + printf( '<p><label for="%1$s">%2$s %3$s</label></p>',
55 + esc_attr( $widget->id.'_lang_choice' ),
56 + esc_html__( 'The widget is displayed for:', 'polylang' ),
57 + $dropdown->walk(
71 58 array_merge(
72 59 array( (object) array( 'slug' => 0, 'name' => __( 'All languages', 'polylang' ) ) ),
73 60 $this->model->get_languages_list()
74 61 ),
75 - -1,
76 62 array(
77 - 'name' => $widget->id . '_lang_choice',
78 - 'class' => 'tags-input pll-lang-choice',
79 - 'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'],
63 + 'name' => $widget->id.'_lang_choice',
64 + 'class' => 'tags-input',
65 + 'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'],
80 66 )
81 - );
82 -
83 - printf(
84 - '<p><label for="%1$s">%2$s %3$s</label></p>',
85 - esc_attr( $widget->id . '_lang_choice' ),
86 - esc_html__( 'The widget is displayed for:', 'polylang' ),
87 - $dropdown_html // phpcs:ignore WordPress.Security.EscapeOutput
88 - );
89 - }
67 + )
68 + );
90 69 }
91 70
92 71 /**
93 72 * Called when widget options are saved
@@ -101,12 +80,10 @@
101 80 * @param object $widget WP_Widget object
102 81 * @return array Widget options
103 82 */
104 83 public function widget_update_callback( $instance, $new_instance, $old_instance, $widget ) {
105 - $key = $widget->id . '_lang_choice';
106 -
107 - if ( ! empty( $_POST[ $key ] ) && $lang = $this->model->get_language( sanitize_key( $_POST[ $key ] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
108 - $instance['pll_lang'] = $lang->slug;
84 + if ( ! empty( $_POST[ $key = $widget->id.'_lang_choice' ] ) && in_array( $_POST[ $key ], $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) ) {
85 + $instance['pll_lang'] = $_POST[ $key ];
109 86 } else {
110 87 unset( $instance['pll_lang'] );
111 88 }
112 89
@@ -120,12 +97,19 @@
120 97 *
121 98 * @param int $user_id
122 99 */
123 100 public function personal_options_update( $user_id ) {
101 + // Admin language
102 + // FIXME Backward compatibility with WP < 4.7
103 + if ( version_compare( $GLOBALS['wp_version'], '4.7alpha', '<' ) ) {
104 + $user_lang = in_array( $_POST['user_lang'], $this->model->get_languages_list( array( 'fields' => 'locale' ) ) ) ? $_POST['user_lang'] : 0;
105 + update_user_meta( $user_id, 'locale', $user_lang );
106 + }
107 +
124 108 // Biography translations
125 109 foreach ( $this->model->get_languages_list() as $lang ) {
126 110 $meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug;
127 - $description = empty( $_POST[ 'description_' . $lang->slug ] ) ? '' : trim( $_POST[ 'description_' . $lang->slug ] ); // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput
111 + $description = empty( $_POST[ 'description_' . $lang->slug ] ) ? '' : trim( $_POST[ 'description_' . $lang->slug ] );
128 112
129 113 /** This filter is documented in wp-includes/user.php */
130 114 $description = apply_filters( 'pre_user_description', $description ); // Applies WP default filter wp_filter_kses
131 115 update_user_meta( $user_id, $meta, $description );
@@ -132,9 +116,9 @@
132 116 }
133 117 }
134 118
135 119 /**
136 - * Outputs hidden information to modify the biography form with js
120 + * Form for language user preference in user profile
137 121 *
138 122 * @since 0.4
139 123 *
140 124 * @param object $profileuser
@@ -139,8 +123,32 @@
139 123 *
140 124 * @param object $profileuser
141 125 */
142 126 public function personal_options( $profileuser ) {
127 + // FIXME: Backward compatibility with WP < 4.7
128 + if ( version_compare( $GLOBALS['wp_version'], '4.7alpha', '<' ) ) {
129 + $dropdown = new PLL_Walker_Dropdown();
130 + printf( '
131 + <tr>
132 + <th><label for="user_lang">%s</label></th>
133 + <td>%s</td>
134 + </tr>',
135 + esc_html__( 'Admin language', 'polylang' ),
136 + $dropdown->walk(
137 + array_merge(
138 + array( (object) array( 'locale' => 0, 'name' => __( 'WordPress default', 'polylang' ) ) ),
139 + $this->model->get_languages_list()
140 + ),
141 + array(
142 + 'name' => 'user_lang',
143 + 'value' => 'locale',
144 + 'selected' => get_user_meta( $profileuser->ID, 'locale', true ),
145 + )
146 + )
147 + );
148 + }
149 +
150 + // Hidden informations to modify the biography form with js
143 151 foreach ( $this->model->get_languages_list() as $lang ) {
144 152 $meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug;
145 153
146 154 /** This filter is documented in wp-includes/user.php */
@@ -145,10 +153,9 @@
145 153
146 154 /** This filter is documented in wp-includes/user.php */
147 155 $description = apply_filters( 'user_description', get_user_meta( $profileuser->ID, $meta, true ) ); // Applies WP default filter wp_kses_data
148 156
149 - printf(
150 - '<input type="hidden" class="biography" name="%s___%s" value="%s" />',
157 + printf( '<input type="hidden" class="biography" name="%s___%s" value="%s" />',
151 158 esc_attr( $lang->slug ),
152 159 esc_attr( $lang->name ),
153 160 esc_attr( $description )
154 161 );
@@ -155,9 +162,9 @@
155 162 }
156 163 }
157 164
158 165 /**
159 - * Upgrades languages files after a core upgrade
166 + * Ugprades languages files after a core upgrade
160 167 * only for backward compatibility WP < 4.0 *AND* Polylang < 1.6
161 168 *
162 169 * @since 0.6
163 170 *
@@ -177,12 +184,12 @@
177 184 * Allows to update translations files for plugins and themes
178 185 *
179 186 * @since 1.6
180 187 *
181 - * @param array $locales Not used
188 + * @param array $locale not used
182 189 * @return array list of locales to update
183 190 */
184 - public function update_check_locales( $locales ) {
191 + function update_check_locales( $locales ) {
185 192 return $this->model->get_languages_list( array( 'fields' => 'locale' ) );
186 193 }
187 194
188 195 /**
@@ -194,19 +201,9 @@
194 201 * @param string $locale
195 202 * @return string
196 203 */
197 204 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;
205 + return $this->curlang->locale;
209 206 }
210 207
211 208 /**
212 209 * Maybe fix the result of sanitize_title() in case the languages include German or Danish
@@ -223,9 +220,9 @@
223 220 */
224 221 public function sanitize_title( $title, $raw_title, $context ) {
225 222 static $once = false;
226 223
227 - if ( ! $once && 'save' == $context && ! empty( $title ) ) {
224 + if ( ! $once && 'save' == $context && ! empty( $this->curlang ) && ! empty( $title ) ) {
228 225 $once = true;
229 226 add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface
230 227 $title = sanitize_title( $raw_title, '', $context );
231 228 remove_filter( 'locale', array( $this, 'get_locale' ), 20 );
@@ -238,56 +235,22 @@
238 235 * Maybe fix the result of sanitize_user() in case the languages include German or Danish
239 236 *
240 237 * @since 2.0
241 238 *
242 - * @param string $username Sanitized username.
239 + * @param string $username Sanitized username.
243 240 * @param string $raw_username The username prior to sanitization.
244 - * @param bool $strict Whether to limit the sanitization to specific characters. Default false.
241 + * @param bool $strict Whether to limit the sanitization to specific characters. Default false.
245 242 * @return string
246 243 */
247 244 public function sanitize_user( $username, $raw_username, $strict ) {
248 245 static $once = false;
249 246
250 - if ( ! $once ) {
247 + if ( ! $once && ! empty( $this->curlang ) ) {
251 248 $once = true;
252 249 add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface
253 - $username = sanitize_user( $raw_username, '', $strict );
250 + $title = sanitize_title( $raw_username, '', $strict );
254 251 remove_filter( 'locale', array( $this, 'get_locale' ), 20 );
255 252 $once = false;
256 253 }
257 254 return $username;
258 - }
259 -
260 - /**
261 - * Adds a text direction dependent class to the body
262 - *
263 - * @since 2.2
264 - *
265 - * @param string $classes Space-separated list of CSS classes.
266 - * @return string
267 - */
268 - public function admin_body_class( $classes ) {
269 - if ( ! empty( $this->curlang ) ) {
270 - $classes .= ' pll-dir-' . ( $this->curlang->is_rtl ? 'rtl' : 'ltr' );
271 - }
272 - return $classes;
273 - }
274 -
275 - /**
276 - * Add post state for translations of the privacy policy page
277 - *
278 - * @since 2.7
279 - *
280 - * @param array $post_states An array of post display states.
281 - * @param object $post The current post object.
282 - * @return array
283 - */
284 - public function display_post_states( $post_states, $post ) {
285 - $page_for_privacy_policy = get_option( 'wp_page_for_privacy_policy' );
286 -
287 - if ( $page_for_privacy_policy && in_array( $post->ID, $this->model->post->get_translations( $page_for_privacy_policy ) ) ) {
288 - $post_states['page_for_privacy_policy'] = __( 'Privacy Policy Page', 'polylang' );
289 - }
290 -
291 - return $post_states;
292 255 }
293 256 }