PluginProbe
Polylang / 2.8
Polylang v2.8
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-base.php +56 -134 3.0.32.8 View file →
@@ -3,64 +3,29 @@
3 3 * @package Polylang
4 4 */
5 5
6 6 /**
7 - * Setup features available on all admin pages.
7 + * Base class for both admin
8 8 *
9 9 * @since 1.8
10 10 */
11 -abstract class PLL_Admin_Base extends PLL_Base {
12 - /**
13 - * Current language (used to filter the content).
14 - *
15 - * @var PLL_Language
16 - */
17 - public $curlang;
11 +class PLL_Admin_Base extends PLL_Base {
12 + public $filter_lang, $curlang, $pref_lang;
18 13
19 14 /**
20 - * Language selected in the admin language filter.
15 + * Loads the polylang text domain
16 + * Setups actions needed on all admin pages
21 17 *
22 - * @var PLL_Language
23 - */
24 - public $filter_lang;
25 -
26 - /**
27 - * Preferred language to assign to new contents.
28 - *
29 - * @var PLL_Language
30 - */
31 - public $pref_lang;
32 -
33 - /**
34 - * @var PLL_Filters_Links
35 - */
36 - public $filters_links;
37 -
38 - /**
39 - * @var PLL_Admin_Links
40 - */
41 - public $links;
42 -
43 - /**
44 - * @var PLL_Admin_Notices
45 - */
46 - public $notices;
47 -
48 - /**
49 - * @var PLL_Admin_Static_Pages
50 - */
51 - public $static_pages;
52 -
53 - /**
54 - * Setups actions needed on all admin pages.
55 - *
56 18 * @since 1.8
57 19 *
58 - * @param PLL_Links_Model $links_model Reference to the links model.
20 + * @param object $links_model
59 21 */
60 22 public function __construct( &$links_model ) {
61 23 parent::__construct( $links_model );
62 24
25 + // Plugin i18n, only needed for backend
26 + load_plugin_textdomain( 'polylang' );
27 +
63 28 // Adds the link to the languages panel in the WordPress admin menu
64 29 add_action( 'admin_menu', array( $this, 'add_menus' ) );
65 30
66 31 // Setup js scripts and css styles
@@ -101,10 +66,8 @@
101 66 /**
102 67 * Adds the link to the languages panel in the WordPress admin menu
103 68 *
104 69 * @since 0.1
105 - *
106 - * @return void
107 70 */
108 71 public function add_menus() {
109 72 global $admin_page_hooks;
110 73
@@ -132,9 +95,9 @@
132 95 foreach ( $tabs as $tab => $title ) {
133 96 $page = 'lang' === $tab ? 'mlang' : "mlang_$tab";
134 97 if ( empty( $parent ) ) {
135 98 $parent = $page;
136 - add_menu_page( $title, __( 'Languages', 'polylang' ), 'manage_options', $page, '__return_null', 'dashicons-translation' );
99 + add_menu_page( $title, __( 'Languages', 'polylang' ), 'manage_options', $page, null, 'dashicons-translation' );
137 100 $admin_page_hooks[ $page ] = 'languages'; // Hack to avoid the localization of the hook name. See: https://core.trac.wordpress.org/ticket/18857
138 101 }
139 102
140 103 add_submenu_page( $parent, $title, $title, 'manage_options', $page, array( $this, 'languages_page' ) );
@@ -144,10 +107,8 @@
144 107 /**
145 108 * Setup js scripts & css styles ( only on the relevant pages )
146 109 *
147 110 * @since 0.6
148 - *
149 - * @return void
150 111 */
151 112 public function admin_enqueue_scripts() {
152 113 $screen = get_current_screen();
153 114 if ( empty( $screen ) ) {
@@ -172,14 +133,14 @@
172 133 $scripts['post'] = array( array( 'edit', 'upload' ), array( 'jquery', 'wp-ajax-response' ), 0, 1 );
173 134
174 135 // Classic editor.
175 136 if ( ! method_exists( $screen, 'is_block_editor' ) || ! $screen->is_block_editor() ) {
176 - $scripts['classic-editor'] = array( array( 'post', 'media', 'async-upload' ), array( 'jquery', 'wp-ajax-response', 'post', 'jquery-ui-dialog', 'wp-i18n' ), 0, 1 );
137 + $scripts['classic-editor'] = array( array( 'post', 'media', 'async-upload' ), array( 'jquery', 'wp-ajax-response', 'post' ), 0, 1 );
177 138 }
178 139
179 140 // Block editor with legacy metabox in WP 5.0+.
180 141 if ( method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() && ! pll_use_block_editor_plugin() ) {
181 - $scripts['block-editor'] = array( array( 'post' ), array( 'jquery', 'wp-ajax-response', 'wp-api-fetch', 'jquery-ui-dialog', 'wp-i18n' ), 0, 1 );
142 + $scripts['block-editor'] = array( array( 'post' ), array( 'jquery', 'wp-ajax-response', 'wp-api-fetch' ), 0, 1 );
182 143 }
183 144 }
184 145
185 146 if ( ! empty( $screen->taxonomy ) && $this->model->is_translated_taxonomy( $screen->taxonomy ) ) {
@@ -187,17 +148,13 @@
187 148 }
188 149
189 150 foreach ( $scripts as $script => $v ) {
190 151 if ( in_array( $screen->base, $v[0] ) && ( $v[2] || $this->model->get_languages_list() ) ) {
191 - wp_enqueue_script( 'pll_' . $script, plugins_url( '/js/build/' . $script . $suffix . '.js', POLYLANG_ROOT_FILE ), $v[1], POLYLANG_VERSION, $v[3] );
192 - if ( 'classic-editor' === $script || 'block-editor' === $script ) {
193 - wp_set_script_translations( 'pll_' . $script, 'polylang' );
194 - }
152 + wp_enqueue_script( 'pll_' . $script, plugins_url( '/js/' . $script . $suffix . '.js', POLYLANG_FILE ), $v[1], POLYLANG_VERSION, $v[3] );
195 153 }
196 154 }
197 155
198 - wp_register_style( 'polylang_admin', plugins_url( '/css/build/admin' . $suffix . '.css', POLYLANG_ROOT_FILE ), array( 'wp-jquery-ui-dialog' ), POLYLANG_VERSION );
199 - wp_enqueue_style( 'polylang_dialog', plugins_url( '/css/build/dialog' . $suffix . '.css', POLYLANG_ROOT_FILE ), array( 'polylang_admin' ), POLYLANG_VERSION );
156 + wp_enqueue_style( 'polylang_admin', plugins_url( '/css/admin' . $suffix . '.css', POLYLANG_FILE ), array(), POLYLANG_VERSION );
200 157
201 158 $this->localize_scripts();
202 159 }
203 160
@@ -204,15 +161,13 @@
204 161 /**
205 162 * Enqueue scripts to the WP Customizer.
206 163 *
207 164 * @since 2.4.0
208 - *
209 - * @return void
210 165 */
211 166 public function customize_controls_enqueue_scripts() {
212 167 if ( $this->model->get_languages_list() ) {
213 168 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
214 - wp_enqueue_script( 'pll_widgets', plugins_url( '/js/build/widgets' . $suffix . '.js', POLYLANG_ROOT_FILE ), array( 'jquery' ), POLYLANG_VERSION, true );
169 + wp_enqueue_script( 'pll_widgets', plugins_url( '/js/widgets' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery' ), POLYLANG_VERSION, true );
215 170 $this->localize_scripts();
216 171 }
217 172 }
218 173
@@ -219,10 +174,8 @@
219 174 /**
220 175 * Localize scripts.
221 176 *
222 177 * @since 2.4.0
223 - *
224 - * @return void
225 178 */
226 179 public function localize_scripts() {
227 180 if ( wp_script_is( 'pll_widgets', 'enqueued' ) ) {
228 181 wp_localize_script(
@@ -247,10 +200,8 @@
247 200 * when options.data is an empty string (GET request with the method 'load')
248 201 * see: https://wordpress.org/support/topic/invalid-url-during-wordpress-new-dashboard-widget-operation
249 202 *
250 203 * @since 1.4
251 - *
252 - * @return void
253 204 */
254 205 public function admin_print_footer_scripts() {
255 206 global $post_ID, $tag_ID;
256 207
@@ -267,57 +218,34 @@
267 218 $arr = wp_json_encode( $params );
268 219 ?>
269 220 <script type="text/javascript">
270 221 if (typeof jQuery != 'undefined') {
271 - jQuery(
272 - function( $ ){
273 - $.ajaxPrefilter( function ( options, originalOptions, jqXHR ) {
274 - if ( -1 != options.url.indexOf( ajaxurl ) || -1 != ajaxurl.indexOf( options.url ) ) {
275 -
276 - function addPolylangParametersAsString() {
277 - if ( 'undefined' === typeof options.data || '' === options.data.trim() ) {
278 - // Only Polylang data need to be send. So it could be as a simple query string.
279 - options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>';
222 + (function($){
223 + $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
224 + if ( -1 != options.url.indexOf( ajaxurl ) || -1 != ajaxurl.indexOf( options.url ) ) {
225 + if ( 'undefined' === typeof options.data ) {
226 + options.data = ( 'get' === options.type.toLowerCase() ) ? '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>' : <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>;
227 + } else {
228 + if ( 'string' === typeof options.data ) {
229 + if ( '' === options.data && 'get' === options.type.toLowerCase() ) {
230 + options.url = options.url+'&<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>';
280 231 } else {
281 - /*
282 - * In some cases data could be a JSON string like in third party plugins.
283 - * So we need not to break their process by adding polylang parameters as valid JSON datas.
284 - */
285 232 try {
286 - options.data = JSON.stringify( Object.assign( JSON.parse( options.data ), <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?> ) );
287 - } catch( exception ) {
288 - // Add Polylang data to the existing query string.
289 - options.data = options.data + '&<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>';
233 + var o = $.parseJSON(options.data);
234 + o = $.extend(o, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>);
235 + options.data = JSON.stringify(o);
290 236 }
237 + catch(e) {
238 + options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>&'+options.data;
239 + }
291 240 }
292 - }
293 -
294 - /*
295 - * options.processData set to true is the default jQuery process where the data is converted in a query string by using jQuery.param().
296 - * This step is done before applying filters. Thus here the options.data is already a string in this case.
297 - * @See https://github.com/jquery/jquery/blob/3.5.1/src/ajax.js#L563-L569 jQuery ajax function.
298 - * It is the most case WordPress send ajax request this way however third party plugins or themes could be send JSON string.
299 - * Use JSON format is recommended in jQuery.param() documentation to be able to send complex data structures.
300 - * @See https://api.jquery.com/jquery.param/ jQuery param function.
301 - */
302 - if ( options.processData ) {
303 - addPolylangParametersAsString();
304 241 } else {
305 - /*
306 - * If options.processData is set to false data could be undefined or pass as a string.
307 - * So data as to be processed as if options.processData is set to true.
308 - */
309 - if ( 'undefined' === typeof options.data || 'string' === typeof options.data ) {
310 - addPolylangParametersAsString();
311 - } else {
312 - // Otherwise options.data is probably an object.
313 - options.data = Object.assign( options.data, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?> );
314 - }
242 + options.data = $.extend(options.data, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>);
315 243 }
316 244 }
317 - });
318 - }
319 - );
245 + }
246 + });
247 + })(jQuery)
320 248 }
321 249 </script>
322 250 <?php
323 251 }
@@ -325,10 +253,8 @@
325 253 /**
326 254 * Sets the admin current language, used to filter the content
327 255 *
328 256 * @since 2.0
329 - *
330 - * @return void
331 257 */
332 258 public function set_current_language() {
333 259 $this->curlang = $this->filter_lang;
334 260
@@ -334,9 +260,9 @@
334 260
335 261 // Edit Post
336 262 if ( isset( $_REQUEST['pll_post_id'] ) && $lang = $this->model->post->get_language( (int) $_REQUEST['pll_post_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
337 263 $this->curlang = $lang;
338 - } elseif ( 'post.php' === $GLOBALS['pagenow'] && isset( $_GET['post'] ) && $this->model->is_translated_post_type( get_post_type( (int) $_GET['post'] ) ) && $lang = $this->model->post->get_language( (int) $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
264 + } elseif ( 'post.php' === $GLOBALS['pagenow'] && isset( $_GET['post'] ) && $lang = $this->model->post->get_language( (int) $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
339 265 $this->curlang = $lang;
340 266 } elseif ( 'post-new.php' === $GLOBALS['pagenow'] && ( empty( $_GET['post_type'] ) || $this->model->is_translated_post_type( sanitize_key( $_GET['post_type'] ) ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
341 267 $this->curlang = empty( $_GET['new_lang'] ) ? $this->pref_lang : $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
342 268 }
@@ -341,14 +267,15 @@
341 267 $this->curlang = empty( $_GET['new_lang'] ) ? $this->pref_lang : $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
342 268 }
343 269
344 270 // Edit Term
345 - elseif ( isset( $_REQUEST['pll_term_id'] ) && $lang = $this->model->term->get_language( (int) $_REQUEST['pll_term_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
271 + // FIXME 'edit-tags.php' for backward compatibility with WP < 4.5
272 + elseif ( in_array( $GLOBALS['pagenow'], array( 'edit-tags.php', 'term.php' ) ) && isset( $_GET['tag_ID'] ) && $lang = $this->model->term->get_language( (int) $_GET['tag_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
346 273 $this->curlang = $lang;
347 - } elseif ( in_array( $GLOBALS['pagenow'], array( 'edit-tags.php', 'term.php' ) ) && isset( $_GET['taxonomy'] ) && $this->model->is_translated_taxonomy( sanitize_key( $_GET['taxonomy'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
348 - if ( isset( $_GET['tag_ID'] ) && $lang = $this->model->term->get_language( (int) $_GET['tag_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
349 - $this->curlang = $lang;
350 - } elseif ( ! empty( $_GET['new_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
274 + } elseif ( isset( $_REQUEST['pll_term_id'] ) && $lang = $this->model->term->get_language( (int) $_REQUEST['pll_term_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
275 + $this->curlang = $lang;
276 + } elseif ( 'edit-tags.php' === $GLOBALS['pagenow'] && isset( $_GET['taxonomy'] ) && $this->model->is_translated_taxonomy( sanitize_key( $_GET['taxonomy'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
277 + if ( ! empty( $_GET['new_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
351 278 $this->curlang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
352 279 } elseif ( empty( $this->curlang ) ) {
353 280 $this->curlang = $this->pref_lang;
354 281 }
@@ -357,17 +284,8 @@
357 284 // Ajax
358 285 if ( wp_doing_ajax() && ! empty( $_REQUEST['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
359 286 $this->curlang = $this->model->get_language( sanitize_key( $_REQUEST['lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
360 287 }
361 -
362 - // Inform that the admin language has been set.
363 - if ( $this->curlang ) {
364 - /** This action is documented in frontend/choose-lang.php */
365 - do_action( 'pll_language_defined', $this->curlang->slug, $this->curlang );
366 - } else {
367 - /** This action is documented in include/class-polylang.php */
368 - do_action( 'pll_no_language_defined' ); // To load overridden textdomains.
369 - }
370 288 }
371 289
372 290 /**
373 291 * Defines the backend language and the admin language filter based on user preferences
@@ -372,10 +290,8 @@
372 290 /**
373 291 * Defines the backend language and the admin language filter based on user preferences
374 292 *
375 293 * @since 1.2.3
376 - *
377 - * @return void
378 294 */
379 295 public function init_user() {
380 296 // Language for admin language filter: may be empty
381 297 // $_GET['lang'] is numeric when editing a language, not when selecting a new language in the filter
@@ -389,21 +305,28 @@
389 305 // Set preferred language for use when saving posts and terms: must not be empty
390 306 $this->pref_lang = empty( $this->filter_lang ) ? $this->model->get_language( $this->options['default_lang'] ) : $this->filter_lang;
391 307
392 308 /**
393 - * Filters the preferred language on admin side.
394 - * The preferred language is used for example to determine the language of a new post.
309 + * Filter the preferred language on admin side
310 + * The preferred language is used for example to determine the language of a new post
395 311 *
396 312 * @since 1.2.3
397 313 *
398 - * @param PLL_Language $pref_lang Preferred language.
314 + * @param object $pref_lang preferred language
399 315 */
400 316 $this->pref_lang = apply_filters( 'pll_admin_preferred_language', $this->pref_lang );
401 317
402 318 $this->set_current_language();
403 319
404 - // Plugin i18n, only needed for backend.
405 - load_plugin_textdomain( 'polylang' );
320 + // Inform that the admin language has been set
321 + // Only if the admin language is one of the Polylang defined language
322 + if ( $curlang = $this->model->get_language( get_user_locale() ) ) {
323 + /** This action is documented in frontend/choose-lang.php */
324 + do_action( 'pll_language_defined', $curlang->slug, $curlang );
325 + } else {
326 + /** This action is documented in include/class-polylang.php */
327 + do_action( 'pll_no_language_defined' ); // to load overridden textdomains
328 + }
406 329 }
407 330
408 331 /**
409 332 * Avoids parsing a tax query when all languages are requested
@@ -408,9 +331,9 @@
408 331 /**
409 332 * Avoids parsing a tax query when all languages are requested
410 333 * Fixes https://wordpress.org/support/topic/notice-undefined-offset-0-in-wp-includesqueryphp-on-line-3877 introduced in WP 4.1
411 334 *
412 - * @see https://core.trac.wordpress.org/ticket/31246 the suggestion of @boonebgorges.
335 + * @see the suggestion of @boonebgorges, https://core.trac.wordpress.org/ticket/31246
413 336 *
414 337 * @since 1.6.5
415 338 *
416 339 * @param array $qvars
@@ -424,14 +347,13 @@
424 347 return $qvars;
425 348 }
426 349
427 350 /**
428 - * Adds the languages list in admin bar for the admin languages filter.
351 + * Adds the languages list in admin bar for the admin languages filter
429 352 *
430 353 * @since 0.9
431 354 *
432 - * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar global object.
433 - * @return void
355 + * @param object $wp_admin_bar
434 356 */
435 357 public function admin_bar_menu( $wp_admin_bar ) {
436 358 $all_item = (object) array(
437 359 'slug' => 'all',