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