PluginProbe
Polylang / 2.0.3
Polylang v2.0.3
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.0.3, at admin/admin-filters.php

252 lines 8.3 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 // ugrades 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' );
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
45 /**
46 * modifies the widgets forms to add our language dropdwown list
47 *
48 * @since 0.3
49 *
50 * @param object $widget
51 */
52 public function in_widget_form( $widget, $return, $instance ) {
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(
58 array_merge(
59 array( (object) array( 'slug' => 0, 'name' => __( 'All languages', 'polylang' ) ) ),
60 $this->model->get_languages_list()
61 ),
62 array(
63 'name' => $widget->id.'_lang_choice',
64 'class' => 'tags-input',
65 'selected' => empty( $instance['pll_lang'] ) ? '' : $instance['pll_lang'],
66 )
67 )
68 );
69 }
70
71 /**
72 * called when widget options are saved
73 * saves the language associated to the widget
74 *
75 * @since 0.3
76 *
77 * @param array $instance widget options
78 * @param array $new_instance not used
79 * @param array $old_instance not used
80 * @param object $widget WP_Widget object
81 * @return array widget options
82 */
83 public function widget_update_callback( $instance, $new_instance, $old_instance, $widget ) {
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 ];
86 }
87 else {
88 unset( $instance['pll_lang'] );
89 }
90
91 return $instance;
92 }
93
94 /**
95 * updates language user preference set in user profile
96 *
97 * @since 0.4
98 *
99 * @param int $user_id
100 */
101 public function personal_options_update( $user_id ) {
102 // admin language
103 $user_lang = in_array( $_POST['user_lang'], $this->model->get_languages_list( array( 'fields' => 'locale' ) ) ) ? $_POST['user_lang'] : 0;
104 update_user_meta( $user_id, 'user_lang', $user_lang );
105
106 // biography translations
107 foreach ( $this->model->get_languages_list() as $lang ) {
108 $meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug;
109 $description = empty( $_POST[ 'description_' . $lang->slug ] ) ? '' : trim( $_POST[ 'description_' . $lang->slug ] );
110
111 /** This filter is documented in wp-includes/user.php */
112 $description = apply_filters( 'pre_user_description', $description ); // applies WP default filter wp_filter_kses
113 update_user_meta( $user_id, $meta, $description );
114 }
115 }
116
117 /**
118 * form for language user preference in user profile
119 *
120 * @since 0.4
121 *
122 * @param object $profileuser
123 */
124 public function personal_options( $profileuser ) {
125 $dropdown = new PLL_Walker_Dropdown();
126 printf( '
127 <tr>
128 <th><label for="user_lang">%s</label></th>
129 <td>%s</td>
130 </tr>',
131 esc_html__( 'Admin language', 'polylang' ),
132 $dropdown->walk(
133 array_merge(
134 array( (object) array( 'locale' => 0, 'name' => __( 'WordPress default', 'polylang' ) ) ),
135 $this->model->get_languages_list()
136 ),
137 array(
138 'name' => 'user_lang',
139 'value' => 'locale',
140 'selected' => get_user_meta( $profileuser->ID, 'user_lang', true ),
141 )
142 )
143 );
144
145 // hidden informations to modify the biography form with js
146 foreach ( $this->model->get_languages_list() as $lang ) {
147 $meta = $lang->slug == $this->options['default_lang'] ? 'description' : 'description_' . $lang->slug;
148
149 /** This filter is documented in wp-includes/user.php */
150 $description = apply_filters( 'user_description', get_user_meta( $profileuser->ID, $meta, true ) ); // applies WP default filter wp_kses_data
151
152 printf( '<input type="hidden" class="biography" name="%s___%s" value="%s" />',
153 esc_attr( $lang->slug ),
154 esc_attr( $lang->name ),
155 esc_attr( $description )
156 );
157 }
158 }
159
160 /**
161 * ugprades languages files after a core upgrade
162 * only for backward compatibility WP < 4.0 *AND* Polylang < 1.6
163 *
164 * @since 0.6
165 *
166 * @param string $version new WP version
167 */
168 public function upgrade_languages( $version ) {
169 // $GLOBALS['wp_version'] is the old WP version
170 if ( version_compare( $version, '4.0', '>=' ) && version_compare( $GLOBALS['wp_version'], '4.0', '<' ) ) {
171
172 /** This filter is documented in wp-admin/includes/update-core.php */
173 apply_filters( 'update_feedback', __( 'Upgrading language files&#8230;', 'polylang' ) );
174 PLL_Upgrade::download_language_packs();
175 }
176 }
177
178 /**
179 * allows to update translations files for plugins and themes
180 *
181 * @since 1.6
182 *
183 * @param array $locale not used
184 * @return array list of locales to update
185 */
186 function update_check_locales( $locales ) {
187 return $this->model->get_languages_list( array( 'fields' => 'locale' ) );
188 }
189
190 /**
191 * Filters the locale according to the current language instead of the language
192 * of the admin interface
193 *
194 * @since 2.0
195 *
196 * @param string $locale
197 * @return string
198 */
199 public function get_locale( $locale ) {
200 return $this->curlang->locale;
201 }
202
203 /**
204 * Maybe fix the result of sanitize_title() in case the languages include German or Danish
205 * Without this filter, if language of the title being sanitized is different from the language
206 * used for the admin interface and if one this language is German or Danish, some specific
207 * characters such as ä, ö, ü, ß are incorrectly sanitized.
208 *
209 * @since 2.0
210 *
211 * @param string $title Sanitized title.
212 * @param string $raw_title The title prior to sanitization.
213 * @param string $context The context for which the title is being sanitized.
214 * @return string
215 */
216 public function sanitize_title( $title, $raw_title, $context ) {
217 static $once = false;
218
219 if ( ! $once && 'save' == $context && ! empty( $this->curlang ) && ! empty( $title ) ) {
220 $once = true;
221 add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface
222 $title = sanitize_title( $raw_title, '', $context );
223 remove_filter( 'locale', array( $this, 'get_locale' ), 20 );
224 $once = false;
225 }
226 return $title;
227 }
228
229 /**
230 * Maybe fix the result of sanitize_user() in case the languages include German or Danish
231 *
232 * @since 2.0
233 *
234 * @param string $username Sanitized username.
235 * @param string $raw_username The username prior to sanitization.
236 * @param bool $strict Whether to limit the sanitization to specific characters. Default false.
237 * @return string
238 */
239 public function sanitize_user( $username, $raw_username, $strict ) {
240 static $once = false;
241
242 if ( ! $once && ! empty( $this->curlang ) ) {
243 $once = true;
244 add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface
245 $title = sanitize_title( $raw_username, '', $strict );
246 remove_filter( 'locale', array( $this, 'get_locale' ), 20 );
247 $once = false;
248 }
249 return $username;
250 }
251 }
252