| @@ -3,22 +3,65 @@ | ||
| 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 | |
| @@ -49,8 +92,10 @@ | ||
| 49 | 92 | |
| 50 | 93 | $this->links = new PLL_Admin_Links( $this ); // FIXME needed here ? |
| 51 | 94 | $this->static_pages = new PLL_Admin_Static_Pages( $this ); // FIXME needed here ? |
| 52 | 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(); | |
| 53 | 98 | |
| 54 | 99 | // Filter admin language for users |
| 55 | 100 | // We must not call user info before WordPress defines user roles in wp-settings.php |
| 56 | 101 | add_action( 'setup_theme', array( $this, 'init_user' ) ); |
| @@ -63,8 +108,10 @@ | ||
| 63 | 108 | /** |
| 64 | 109 | * Adds the link to the languages panel in the WordPress admin menu |
| 65 | 110 | * |
| 66 | 111 | * @since 0.1 |
| 112 | + * | |
| 113 | + * @return void | |
| 67 | 114 | */ |
| 68 | 115 | public function add_menus() { |
| 69 | 116 | global $admin_page_hooks; |
| 70 | 117 | |
| @@ -92,9 +139,9 @@ | ||
| 92 | 139 | foreach ( $tabs as $tab => $title ) { |
| 93 | 140 | $page = 'lang' === $tab ? 'mlang' : "mlang_$tab"; |
| 94 | 141 | if ( empty( $parent ) ) { |
| 95 | 142 | $parent = $page; |
| 96 | - 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' ); | |
| 97 | 144 | $admin_page_hooks[ $page ] = 'languages'; // Hack to avoid the localization of the hook name. See: https://core.trac.wordpress.org/ticket/18857 |
| 98 | 145 | } |
| 99 | 146 | |
| 100 | 147 | add_submenu_page( $parent, $title, $title, 'manage_options', $page, array( $this, 'languages_page' ) ); |
| @@ -104,8 +151,10 @@ | ||
| 104 | 151 | /** |
| 105 | 152 | * Setup js scripts & css styles ( only on the relevant pages ) |
| 106 | 153 | * |
| 107 | 154 | * @since 0.6 |
| 155 | + * | |
| 156 | + * @return void | |
| 108 | 157 | */ |
| 109 | 158 | public function admin_enqueue_scripts() { |
| 110 | 159 | $screen = get_current_screen(); |
| 111 | 160 | if ( empty( $screen ) ) { |
| @@ -130,14 +179,14 @@ | ||
| 130 | 179 | $scripts['post'] = array( array( 'edit', 'upload' ), array( 'jquery', 'wp-ajax-response' ), 0, 1 ); |
| 131 | 180 | |
| 132 | 181 | // Classic editor. |
| 133 | 182 | if ( ! method_exists( $screen, 'is_block_editor' ) || ! $screen->is_block_editor() ) { |
| 134 | - $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 ); | |
| 135 | 184 | } |
| 136 | 185 | |
| 137 | 186 | // Block editor with legacy metabox in WP 5.0+. |
| 138 | 187 | if ( method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() && ! pll_use_block_editor_plugin() ) { |
| 139 | - $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 ); | |
| 140 | 189 | } |
| 141 | 190 | } |
| 142 | 191 | |
| 143 | 192 | if ( ! empty( $screen->taxonomy ) && $this->model->is_translated_taxonomy( $screen->taxonomy ) ) { |
| @@ -145,13 +194,17 @@ | ||
| 145 | 194 | } |
| 146 | 195 | |
| 147 | 196 | foreach ( $scripts as $script => $v ) { |
| 148 | 197 | if ( in_array( $screen->base, $v[0] ) && ( $v[2] || $this->model->get_languages_list() ) ) { |
| 149 | - 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 | + } | |
| 150 | 202 | } |
| 151 | 203 | } |
| 152 | 204 | |
| 153 | - 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 ); | |
| 154 | 207 | |
| 155 | 208 | $this->localize_scripts(); |
| 156 | 209 | } |
| 157 | 210 | |
| @@ -158,13 +211,15 @@ | ||
| 158 | 211 | /** |
| 159 | 212 | * Enqueue scripts to the WP Customizer. |
| 160 | 213 | * |
| 161 | 214 | * @since 2.4.0 |
| 215 | + * | |
| 216 | + * @return void | |
| 162 | 217 | */ |
| 163 | 218 | public function customize_controls_enqueue_scripts() { |
| 164 | 219 | if ( $this->model->get_languages_list() ) { |
| 165 | 220 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 166 | - 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 ); | |
| 167 | 222 | $this->localize_scripts(); |
| 168 | 223 | } |
| 169 | 224 | } |
| 170 | 225 | |
| @@ -171,8 +226,10 @@ | ||
| 171 | 226 | /** |
| 172 | 227 | * Localize scripts. |
| 173 | 228 | * |
| 174 | 229 | * @since 2.4.0 |
| 230 | + * | |
| 231 | + * @return void | |
| 175 | 232 | */ |
| 176 | 233 | public function localize_scripts() { |
| 177 | 234 | if ( wp_script_is( 'pll_widgets', 'enqueued' ) ) { |
| 178 | 235 | wp_localize_script( |
| @@ -197,8 +254,10 @@ | ||
| 197 | 254 | * when options.data is an empty string (GET request with the method 'load') |
| 198 | 255 | * see: https://wordpress.org/support/topic/invalid-url-during-wordpress-new-dashboard-widget-operation |
| 199 | 256 | * |
| 200 | 257 | * @since 1.4 |
| 258 | + * | |
| 259 | + * @return void | |
| 201 | 260 | */ |
| 202 | 261 | public function admin_print_footer_scripts() { |
| 203 | 262 | global $post_ID, $tag_ID; |
| 204 | 263 | |
| @@ -216,29 +275,50 @@ | ||
| 216 | 275 | ?> |
| 217 | 276 | <script type="text/javascript"> |
| 218 | 277 | if (typeof jQuery != 'undefined') { |
| 219 | 278 | jQuery( |
| 220 | - function($){ | |
| 221 | - $.ajaxPrefilter(function (options, originalOptions, jqXHR) { | |
| 279 | + function( $ ){ | |
| 280 | + $.ajaxPrefilter( function ( options, originalOptions, jqXHR ) { | |
| 222 | 281 | if ( -1 != options.url.indexOf( ajaxurl ) || -1 != ajaxurl.indexOf( options.url ) ) { |
| 223 | - if ( 'undefined' === typeof options.data ) { | |
| 224 | - options.data = ( 'get' === options.type.toLowerCase() ) ? '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>' : <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>; | |
| 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 ?>'; | |
| 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 | + */ | |
| 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 | + } | |
| 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(); | |
| 225 | 311 | } else { |
| 226 | - if ( 'string' === typeof options.data ) { | |
| 227 | - if ( '' === options.data && 'get' === options.type.toLowerCase() ) { | |
| 228 | - options.url = options.url+'&<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>'; | |
| 229 | - } else { | |
| 230 | - try { | |
| 231 | - var o = JSON.parse(options.data); | |
| 232 | - o = $.extend(o, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>); | |
| 233 | - options.data = JSON.stringify(o); | |
| 234 | - } | |
| 235 | - catch(e) { | |
| 236 | - options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>&'+options.data; | |
| 237 | - } | |
| 238 | - } | |
| 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(); | |
| 239 | 318 | } else { |
| 240 | - options.data = $.extend(options.data, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>); | |
| 319 | + // Otherwise options.data is probably an object. | |
| 320 | + options.data = Object.assign( options.data || {} , <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?> ); | |
| 241 | 321 | } |
| 242 | 322 | } |
| 243 | 323 | } |
| 244 | 324 | }); |
| @@ -252,8 +332,10 @@ | ||
| 252 | 332 | /** |
| 253 | 333 | * Sets the admin current language, used to filter the content |
| 254 | 334 | * |
| 255 | 335 | * @since 2.0 |
| 336 | + * | |
| 337 | + * @return void | |
| 256 | 338 | */ |
| 257 | 339 | public function set_current_language() { |
| 258 | 340 | $this->curlang = $this->filter_lang; |
| 259 | 341 | |
| @@ -297,8 +379,10 @@ | ||
| 297 | 379 | /** |
| 298 | 380 | * Defines the backend language and the admin language filter based on user preferences |
| 299 | 381 | * |
| 300 | 382 | * @since 1.2.3 |
| 383 | + * | |
| 384 | + * @return void | |
| 301 | 385 | */ |
| 302 | 386 | public function init_user() { |
| 303 | 387 | // Language for admin language filter: may be empty |
| 304 | 388 | // $_GET['lang'] is numeric when editing a language, not when selecting a new language in the filter |
| @@ -312,14 +396,14 @@ | ||
| 312 | 396 | // Set preferred language for use when saving posts and terms: must not be empty |
| 313 | 397 | $this->pref_lang = empty( $this->filter_lang ) ? $this->model->get_language( $this->options['default_lang'] ) : $this->filter_lang; |
| 314 | 398 | |
| 315 | 399 | /** |
| 316 | - * Filter the preferred language on admin side | |
| 317 | - * 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. | |
| 318 | 402 | * |
| 319 | 403 | * @since 1.2.3 |
| 320 | 404 | * |
| 321 | - * @param object $pref_lang preferred language | |
| 405 | + * @param PLL_Language $pref_lang Preferred language. | |
| 322 | 406 | */ |
| 323 | 407 | $this->pref_lang = apply_filters( 'pll_admin_preferred_language', $this->pref_lang ); |
| 324 | 408 | |
| 325 | 409 | $this->set_current_language(); |
| @@ -347,13 +431,14 @@ | ||
| 347 | 431 | return $qvars; |
| 348 | 432 | } |
| 349 | 433 | |
| 350 | 434 | /** |
| 351 | - * 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. | |
| 352 | 436 | * |
| 353 | 437 | * @since 0.9 |
| 354 | 438 | * |
| 355 | - * @param object $wp_admin_bar | |
| 439 | + * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar global object. | |
| 440 | + * @return void | |
| 356 | 441 | */ |
| 357 | 442 | public function admin_bar_menu( $wp_admin_bar ) { |
| 358 | 443 | $all_item = (object) array( |
| 359 | 444 | 'slug' => 'all', |
| @@ -378,17 +463,23 @@ | ||
| 378 | 463 | * @param array $items The admin languages filter submenu items. |
| 379 | 464 | */ |
| 380 | 465 | $items = apply_filters( 'pll_admin_languages_filter', array_merge( array( $all_item ), $this->model->get_languages_list() ) ); |
| 381 | 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 | + | |
| 382 | 480 | if ( ! empty( $items ) ) { |
| 383 | - $wp_admin_bar->add_menu( | |
| 384 | - array( | |
| 385 | - 'id' => 'languages', | |
| 386 | - 'title' => $selected->flag . $title, | |
| 387 | - 'href' => esc_url( add_query_arg( 'lang', $selected->slug, remove_query_arg( 'paged' ) ) ), | |
| 388 | - 'meta' => array( 'title' => __( 'Filters content by language', 'polylang' ) ), | |
| 389 | - ) | |
| 390 | - ); | |
| 481 | + $wp_admin_bar->add_menu( $menu ); | |
| 391 | 482 | } |
| 392 | 483 | |
| 393 | 484 | foreach ( $items as $lang ) { |
| 394 | 485 | if ( $selected->slug === $lang->slug ) { |