PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
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
polylang / admin / admin-filters.php

admin-filters.php in Polylang 2.3.6, at admin/admin-filters.php

282 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Setup miscellaneous admin filters as well as filters common to admin and frontend
5 *
6 * @since 1.2
7 */
8 class PLL_Admin_Filters extends PLL_Filters {
9
10 /**
11 * Constructor: setups filters and actions
12 *
13 * @since 1.2
14 *
15 * @param object $polylang
16 */
17 public function __construct( &$polylang ) {
18 parent::__construct( $polylang );
19
20 // Widgets languages filter
21 add_action( 'in_widget_form', array( $this, 'in_widget_form' ), 10, 3 );
22 add_filter( 'widget_update_callback', array( $this, 'widget_update_callback' ), 10, 4 );
23
24 // Language management for users
25 add_action( 'personal_options_update', array( $this, 'personal_options_update' ) );
26 add_action( 'edit_user_profile_update', array( $this, 'personal_options_update' ) );
27 add_action( 'personal_options', array( $this, 'personal_options' ) );
28
29 // Upgrades languages files after a core upgrade ( timing is important )
30 // Backward compatibility WP < 4.0 *AND* Polylang < 1.6
31 add_action( '_core_updated_successfully', array( $this, 'upgrade_languages' ), 1 ); // since WP 3.3
32
33 // Upgrades plugins and themes translations files
34 add_filter( 'themes_update_check_locales', array( $this, 'update_check_locales' ) );
35 add_filter( 'plugins_update_check_locales', array( $this, 'update_check_locales' ) );
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
44 add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
45 }
46
47 /**
48 * Modifies the widgets forms to add our language dropdown list
49 *
50 * @since 0.3
51 *
52 * @param object $widget Widget instance
53 * @param null $return Not used
54 * @param array $instance Widget settings
55 */
56 public function in_widget_form( $widget, $return, $instance ) {
57 $screen = get_current_screen();
58
59 // Test the Widgets screen and the Customizer to avoid displaying the option in page builders
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'] ) ) {
62 $dropdown = new PLL_Walker_Dropdown();
63 printf( '<p><label for="%1$s">%2$s %3$s</label></p>',
64 esc_attr( $widget->id . '_lang_choice' ),
65 esc_html__( 'The widget is displayed for:', 'polylang' ),
66 $dropdown->walk(
67 array_merge(
68 array( (object) array( 'slug' => 0, 'name' => __( 'All languages', 'polylang' ) ) ),
69 $this->model->get_languages_list()
70 ),
71 array(
72 'name' => $widget->id . '_lang_choice',
73 'class' => 'tags-input',
74 'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'],
75 )
76 )
77 );
78 }
79 }
80
81 /**
82 * Called when widget options are saved
83 * saves the language associated to the widget
84 *
85 * @since 0.3
86 *
87 * @param array $instance Widget options
88 * @param array $new_instance Not used
89 * @param array $old_instance Not used
90 * @param object $widget WP_Widget object
91 * @return array Widget options
92 */
93 public function widget_update_callback( $instance, $new_instance, $old_instance, $widget ) {
94 if ( ! empty( $_POST[ $key = $widget->id . '_lang_choice' ] ) && in_array( $_POST[ $key ], $this->model->get_languages_list( array( 'fields' => 'slug' ) ) ) ) {
95 $instance['pll_lang'] = $_POST[ $key ];
96 } else {
97 unset( $instance['pll_lang'] );
98 }
99
100 return $instance;
101 }
102
103 /**
104 * Updates language user preference set in user profile
105 *
106 * @since 0.4
107 *
108 * @param int $user_id
109 */
110 public function personal_options_update( $user_id ) {
111 // Admin language
112 // FIXME Backward compatibility with WP < 4.7
113 if ( version_compare( $GLOBALS['wp_version'], '4.7alpha', '<' ) ) {
114 $user_lang = in_array( $_POST['user_lang'], $this->model->get_languages_list( array( 'fields' => 'locale' ) ) ) ? $_POST['user_lang'] : 0;
115 update_user_meta( $user_id, 'locale', $user_lang );
116 }
117
118 // Biography translations
119 foreach ( $this->model->get_languages_list() as $lang ) {
120 $meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug;
121 $description = empty( $_POST[ 'description_' . $lang->slug ] ) ? '' : trim( $_POST[ 'description_' . $lang->slug ] );
122
123 /** This filter is documented in wp-includes/user.php */
124 $description = apply_filters( 'pre_user_description', $description ); // Applies WP default filter wp_filter_kses
125 update_user_meta( $user_id, $meta, $description );
126 }
127 }
128
129 /**
130 * Form for language user preference in user profile
131 *
132 * @since 0.4
133 *
134 * @param object $profileuser
135 */
136 public function personal_options( $profileuser ) {
137 // FIXME: Backward compatibility with WP < 4.7
138 if ( version_compare( $GLOBALS['wp_version'], '4.7alpha', '<' ) ) {
139 $dropdown = new PLL_Walker_Dropdown();
140 printf( '
141 <tr>
142 <th><label for="user_lang">%s</label></th>
143 <td>%s</td>
144 </tr>',
145 esc_html__( 'Admin language', 'polylang' ),
146 $dropdown->walk(
147 array_merge(
148 array( (object) array( 'locale' => 0, 'name' => __( 'WordPress default', 'polylang' ) ) ),
149 $this->model->get_languages_list()
150 ),
151 array(
152 'name' => 'user_lang',
153 'value' => 'locale',
154 'selected' => get_user_meta( $profileuser->ID, 'locale', true ),
155 )
156 )
157 );
158 }
159
160 // Hidden information to modify the biography form with js
161 foreach ( $this->model->get_languages_list() as $lang ) {
162 $meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug;
163
164 /** This filter is documented in wp-includes/user.php */
165 $description = apply_filters( 'user_description', get_user_meta( $profileuser->ID, $meta, true ) ); // Applies WP default filter wp_kses_data
166
167 printf( '<input type="hidden" class="biography" name="%s___%s" value="%s" />',
168 esc_attr( $lang->slug ),
169 esc_attr( $lang->name ),
170 esc_attr( $description )
171 );
172 }
173 }
174
175 /**
176 * Upgrades languages files after a core upgrade
177 * only for backward compatibility WP < 4.0 *AND* Polylang < 1.6
178 *
179 * @since 0.6
180 *
181 * @param string $version new WP version
182 */
183 public function upgrade_languages( $version ) {
184 // $GLOBALS['wp_version'] is the old WP version
185 if ( version_compare( $version, '4.0', '>=' ) && version_compare( $GLOBALS['wp_version'], '4.0', '<' ) ) {
186
187 /** This filter is documented in wp-admin/includes/update-core.php */
188 apply_filters( 'update_feedback', __( 'Upgrading language files&#8230;', 'polylang' ) );
189 PLL_Upgrade::download_language_packs();
190 }
191 }
192
193 /**
194 * Allows to update translations files for plugins and themes
195 *
196 * @since 1.6
197 *
198 * @param array $locales Not used
199 * @return array list of locales to update
200 */
201 function update_check_locales( $locales ) {
202 return $this->model->get_languages_list( array( 'fields' => 'locale' ) );
203 }
204
205 /**
206 * Filters the locale according to the current language instead of the language
207 * of the admin interface
208 *
209 * @since 2.0
210 *
211 * @param string $locale
212 * @return string
213 */
214 public function get_locale( $locale ) {
215 return $this->curlang->locale;
216 }
217
218 /**
219 * Maybe fix the result of sanitize_title() in case the languages include German or Danish
220 * Without this filter, if language of the title being sanitized is different from the language
221 * used for the admin interface and if one this language is German or Danish, some specific
222 * characters such as ä, ö, ü, ß are incorrectly sanitized.
223 *
224 * @since 2.0
225 *
226 * @param string $title Sanitized title.
227 * @param string $raw_title The title prior to sanitization.
228 * @param string $context The context for which the title is being sanitized.
229 * @return string
230 */
231 public function sanitize_title( $title, $raw_title, $context ) {
232 static $once = false;
233
234 if ( ! $once && 'save' == $context && ! empty( $this->curlang ) && ! empty( $title ) ) {
235 $once = true;
236 add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface
237 $title = sanitize_title( $raw_title, '', $context );
238 remove_filter( 'locale', array( $this, 'get_locale' ), 20 );
239 $once = false;
240 }
241 return $title;
242 }
243
244 /**
245 * Maybe fix the result of sanitize_user() in case the languages include German or Danish
246 *
247 * @since 2.0
248 *
249 * @param string $username Sanitized username.
250 * @param string $raw_username The username prior to sanitization.
251 * @param bool $strict Whether to limit the sanitization to specific characters. Default false.
252 * @return string
253 */
254 public function sanitize_user( $username, $raw_username, $strict ) {
255 static $once = false;
256
257 if ( ! $once && ! empty( $this->curlang ) ) {
258 $once = true;
259 add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface
260 $username = sanitize_user( $raw_username, '', $strict );
261 remove_filter( 'locale', array( $this, 'get_locale' ), 20 );
262 $once = false;
263 }
264 return $username;
265 }
266
267 /**
268 * Adds a text direction dependent class to the body
269 *
270 * @since 2.2
271 *
272 * @param string $classes Space-separated list of CSS classes.
273 * @return string
274 */
275 public function admin_body_class( $classes ) {
276 if ( ! empty( $this->curlang ) ) {
277 $classes .= ' pll-dir-' . ( $this->curlang->is_rtl ? 'rtl' : 'ltr' );
278 }
279 return $classes;
280 }
281 }
282