| @@ -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' ) ); |
| @@ -172,14 +179,14 @@ | ||
| 172 | 179 | $scripts['post'] = array( array( 'edit', 'upload' ), array( 'jquery', 'wp-ajax-response' ), 0, 1 ); |
| 173 | 180 | |
| 174 | 181 | // Classic editor. |
| 175 | 182 | 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' ), 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 ); | |
| 177 | 184 | } |
| 178 | 185 | |
| 179 | 186 | // Block editor with legacy metabox in WP 5.0+. |
| 180 | 187 | 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' ), 0, 1 ); | |
| 188 | + $scripts['block-editor'] = array( array( 'post' ), array( 'jquery', 'wp-ajax-response', 'wp-api-fetch', 'jquery-ui-dialog', 'wp-i18n' ), 0, 1 ); | |
| 182 | 189 | } |
| 183 | 190 | } |
| 184 | 191 | |
| 185 | 192 | if ( ! empty( $screen->taxonomy ) && $this->model->is_translated_taxonomy( $screen->taxonomy ) ) { |
| @@ -187,14 +194,17 @@ | ||
| 187 | 194 | } |
| 188 | 195 | |
| 189 | 196 | foreach ( $scripts as $script => $v ) { |
| 190 | 197 | 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_BASENAME ), $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 | + } | |
| 192 | 202 | } |
| 193 | 203 | } |
| 194 | 204 | |
| 195 | - wp_register_style( 'polylang_admin', plugins_url( '/css/build/admin' . $suffix . '.css', POLYLANG_BASENAME ), array( 'wp-jquery-ui-dialog' ), POLYLANG_VERSION ); | |
| 196 | - wp_enqueue_style( 'polylang_dialog', plugins_url( '/css/build/dialog' . $suffix . '.css', POLYLANG_BASENAME ), array( 'polylang_admin' ), 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 ); | |
| 197 | 207 | |
| 198 | 208 | $this->localize_scripts(); |
| 199 | 209 | } |
| 200 | 210 | |
| @@ -207,9 +217,9 @@ | ||
| 207 | 217 | */ |
| 208 | 218 | public function customize_controls_enqueue_scripts() { |
| 209 | 219 | if ( $this->model->get_languages_list() ) { |
| 210 | 220 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 211 | - wp_enqueue_script( 'pll_widgets', plugins_url( '/js/build/widgets' . $suffix . '.js', POLYLANG_BASENAME ), 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 ); | |
| 212 | 222 | $this->localize_scripts(); |
| 213 | 223 | } |
| 214 | 224 | } |
| 215 | 225 | |
| @@ -269,13 +279,23 @@ | ||
| 269 | 279 | function( $ ){ |
| 270 | 280 | $.ajaxPrefilter( function ( options, originalOptions, jqXHR ) { |
| 271 | 281 | if ( -1 != options.url.indexOf( ajaxurl ) || -1 != ajaxurl.indexOf( options.url ) ) { |
| 272 | 282 | |
| 273 | - function addStringParameters() { | |
| 274 | - 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. | |
| 275 | 286 | options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>'; |
| 276 | 287 | } else { |
| 277 | - 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 | + } | |
| 278 | 298 | } |
| 279 | 299 | } |
| 280 | 300 | |
| 281 | 301 | /* |
| @@ -281,11 +301,14 @@ | ||
| 281 | 301 | /* |
| 282 | 302 | * options.processData set to true is the default jQuery process where the data is converted in a query string by using jQuery.param(). |
| 283 | 303 | * This step is done before applying filters. Thus here the options.data is already a string in this case. |
| 284 | 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. | |
| 285 | 308 | */ |
| 286 | 309 | if ( options.processData ) { |
| 287 | - addStringParameters(); | |
| 310 | + addPolylangParametersAsString(); | |
| 288 | 311 | } else { |
| 289 | 312 | /* |
| 290 | 313 | * If options.processData is set to false data could be undefined or pass as a string. |
| 291 | 314 | * So data as to be processed as if options.processData is set to true. |
| @@ -290,12 +313,12 @@ | ||
| 290 | 313 | * If options.processData is set to false data could be undefined or pass as a string. |
| 291 | 314 | * So data as to be processed as if options.processData is set to true. |
| 292 | 315 | */ |
| 293 | 316 | if ( 'undefined' === typeof options.data || 'string' === typeof options.data ) { |
| 294 | - addStringParameters(); | |
| 317 | + addPolylangParametersAsString(); | |
| 295 | 318 | } else { |
| 296 | 319 | // Otherwise options.data is probably an object. |
| 297 | - 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 ?> ); | |
| 298 | 321 | } |
| 299 | 322 | } |
| 300 | 323 | } |
| 301 | 324 | }); |
| @@ -440,17 +463,23 @@ | ||
| 440 | 463 | * @param array $items The admin languages filter submenu items. |
| 441 | 464 | */ |
| 442 | 465 | $items = apply_filters( 'pll_admin_languages_filter', array_merge( array( $all_item ), $this->model->get_languages_list() ) ); |
| 443 | 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 | + | |
| 444 | 480 | if ( ! empty( $items ) ) { |
| 445 | - $wp_admin_bar->add_menu( | |
| 446 | - array( | |
| 447 | - 'id' => 'languages', | |
| 448 | - 'title' => $selected->flag . $title, | |
| 449 | - 'href' => esc_url( add_query_arg( 'lang', $selected->slug, remove_query_arg( 'paged' ) ) ), | |
| 450 | - 'meta' => array( 'title' => __( 'Filters content by language', 'polylang' ) ), | |
| 451 | - ) | |
| 452 | - ); | |
| 481 | + $wp_admin_bar->add_menu( $menu ); | |
| 453 | 482 | } |
| 454 | 483 | |
| 455 | 484 | foreach ( $items as $lang ) { |
| 456 | 485 | if ( $selected->slug === $lang->slug ) { |