| @@ -50,8 +50,13 @@ | ||
| 50 | 50 | */ |
| 51 | 51 | public $static_pages; |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | + * @var PLL_Admin_Default_Term | |
| 55 | + */ | |
| 56 | + public $default_term; | |
| 57 | + | |
| 58 | + /** | |
| 54 | 59 | * Setups actions needed on all admin pages. |
| 55 | 60 | * |
| 56 | 61 | * @since 1.8 |
| 57 | 62 | * |
| @@ -87,8 +92,10 @@ | ||
| 87 | 92 | |
| 88 | 93 | $this->links = new PLL_Admin_Links( $this ); // FIXME needed here ? |
| 89 | 94 | $this->static_pages = new PLL_Admin_Static_Pages( $this ); // FIXME needed here ? |
| 90 | 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(); | |
| 91 | 98 | |
| 92 | 99 | // Filter admin language for users |
| 93 | 100 | // We must not call user info before WordPress defines user roles in wp-settings.php |
| 94 | 101 | add_action( 'setup_theme', array( $this, 'init_user' ) ); |
| @@ -272,13 +279,23 @@ | ||
| 272 | 279 | function( $ ){ |
| 273 | 280 | $.ajaxPrefilter( function ( options, originalOptions, jqXHR ) { |
| 274 | 281 | if ( -1 != options.url.indexOf( ajaxurl ) || -1 != ajaxurl.indexOf( options.url ) ) { |
| 275 | 282 | |
| 276 | - function addStringParameters() { | |
| 277 | - if ( 'undefined' === typeof options.data || '' === options.data ) { | |
| 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. | |
| 278 | 286 | options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>'; |
| 279 | 287 | } else { |
| 280 | - options.data = options.data + '&<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>'; | |
| 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 | + */ | |
| 292 | + try { | |
| 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 ?>'; | |
| 297 | + } | |
| 281 | 298 | } |
| 282 | 299 | } |
| 283 | 300 | |
| 284 | 301 | /* |
| @@ -284,11 +301,14 @@ | ||
| 284 | 301 | /* |
| 285 | 302 | * options.processData set to true is the default jQuery process where the data is converted in a query string by using jQuery.param(). |
| 286 | 303 | * This step is done before applying filters. Thus here the options.data is already a string in this case. |
| 287 | 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. | |
| 288 | 308 | */ |
| 289 | 309 | if ( options.processData ) { |
| 290 | - addStringParameters(); | |
| 310 | + addPolylangParametersAsString(); | |
| 291 | 311 | } else { |
| 292 | 312 | /* |
| 293 | 313 | * If options.processData is set to false data could be undefined or pass as a string. |
| 294 | 314 | * So data as to be processed as if options.processData is set to true. |
| @@ -293,12 +313,12 @@ | ||
| 293 | 313 | * If options.processData is set to false data could be undefined or pass as a string. |
| 294 | 314 | * So data as to be processed as if options.processData is set to true. |
| 295 | 315 | */ |
| 296 | 316 | if ( 'undefined' === typeof options.data || 'string' === typeof options.data ) { |
| 297 | - addStringParameters(); | |
| 317 | + addPolylangParametersAsString(); | |
| 298 | 318 | } else { |
| 299 | 319 | // Otherwise options.data is probably an object. |
| 300 | - options.data = Object.assign( options.data, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?> ); | |
| 320 | + options.data = Object.assign( options.data || {} , <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?> ); | |
| 301 | 321 | } |
| 302 | 322 | } |
| 303 | 323 | } |
| 304 | 324 | }); |
| @@ -443,17 +463,23 @@ | ||
| 443 | 463 | * @param array $items The admin languages filter submenu items. |
| 444 | 464 | */ |
| 445 | 465 | $items = apply_filters( 'pll_admin_languages_filter', array_merge( array( $all_item ), $this->model->get_languages_list() ) ); |
| 446 | 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 | + | |
| 447 | 480 | if ( ! empty( $items ) ) { |
| 448 | - $wp_admin_bar->add_menu( | |
| 449 | - array( | |
| 450 | - 'id' => 'languages', | |
| 451 | - 'title' => $selected->flag . $title, | |
| 452 | - 'href' => esc_url( add_query_arg( 'lang', $selected->slug, remove_query_arg( 'paged' ) ) ), | |
| 453 | - 'meta' => array( 'title' => __( 'Filters content by language', 'polylang' ) ), | |
| 454 | - ) | |
| 455 | - ); | |
| 481 | + $wp_admin_bar->add_menu( $menu ); | |
| 456 | 482 | } |
| 457 | 483 | |
| 458 | 484 | foreach ( $items as $lang ) { |
| 459 | 485 | if ( $selected->slug === $lang->slug ) { |