| @@ -3,32 +3,74 @@ | ||
| 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|null | |
| 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|null | |
| 23 | + */ | |
| 24 | + public $filter_lang; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Preferred language to assign to new contents. | |
| 28 | + * | |
| 29 | + * @var PLL_Language|null | |
| 30 | + */ | |
| 31 | + public $pref_lang; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * @var PLL_Filters_Links|null | |
| 35 | + */ | |
| 36 | + public $filters_links; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * @var PLL_Admin_Links|null | |
| 40 | + */ | |
| 41 | + public $links; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * @var PLL_Admin_Notices|null | |
| 45 | + */ | |
| 46 | + public $notices; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * @var PLL_Admin_Static_Pages|null | |
| 50 | + */ | |
| 51 | + public $static_pages; | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * @var PLL_Admin_Default_Term|null | |
| 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 | |
| 71 | + add_action( 'admin_menu', array( $this, 'remove_customize_submenu' ) ); | |
| 72 | + | |
| 31 | 73 | // Setup js scripts and css styles |
| 32 | 74 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 33 | 75 | add_action( 'admin_print_footer_scripts', array( $this, 'admin_print_footer_scripts' ), 0 ); // High priority in case an ajax request is sent by an immediately invoked function |
| 34 | 76 | |
| @@ -45,8 +87,11 @@ | ||
| 45 | 87 | parent::init(); |
| 46 | 88 | |
| 47 | 89 | $this->notices = new PLL_Admin_Notices( $this ); |
| 48 | 90 | |
| 91 | + $this->default_term = new PLL_Admin_Default_Term( $this ); | |
| 92 | + $this->default_term->add_hooks(); | |
| 93 | + | |
| 49 | 94 | if ( ! $this->model->get_languages_list() ) { |
| 50 | 95 | return; |
| 51 | 96 | } |
| 52 | 97 | |
| @@ -66,8 +111,10 @@ | ||
| 66 | 111 | /** |
| 67 | 112 | * Adds the link to the languages panel in the WordPress admin menu |
| 68 | 113 | * |
| 69 | 114 | * @since 0.1 |
| 115 | + * | |
| 116 | + * @return void | |
| 70 | 117 | */ |
| 71 | 118 | public function add_menus() { |
| 72 | 119 | global $admin_page_hooks; |
| 73 | 120 | |
| @@ -75,9 +122,9 @@ | ||
| 75 | 122 | $tabs = array( 'lang' => __( 'Languages', 'polylang' ) ); |
| 76 | 123 | |
| 77 | 124 | // Only if at least one language has been created |
| 78 | 125 | if ( $this->model->get_languages_list() ) { |
| 79 | - $tabs['strings'] = __( 'Strings translations', 'polylang' ); | |
| 126 | + $tabs['strings'] = __( 'Translations', 'polylang' ); | |
| 80 | 127 | } |
| 81 | 128 | |
| 82 | 129 | $tabs['settings'] = __( 'Settings', 'polylang' ); |
| 83 | 130 | |
| @@ -95,9 +142,9 @@ | ||
| 95 | 142 | foreach ( $tabs as $tab => $title ) { |
| 96 | 143 | $page = 'lang' === $tab ? 'mlang' : "mlang_$tab"; |
| 97 | 144 | if ( empty( $parent ) ) { |
| 98 | 145 | $parent = $page; |
| 99 | - add_menu_page( $title, __( 'Languages', 'polylang' ), 'manage_options', $page, null, 'dashicons-translation' ); | |
| 146 | + add_menu_page( $title, __( 'Languages', 'polylang' ), 'manage_options', $page, '__return_null', 'dashicons-translation' ); | |
| 100 | 147 | $admin_page_hooks[ $page ] = 'languages'; // Hack to avoid the localization of the hook name. See: https://core.trac.wordpress.org/ticket/18857 |
| 101 | 148 | } |
| 102 | 149 | |
| 103 | 150 | add_submenu_page( $parent, $title, $title, 'manage_options', $page, array( $this, 'languages_page' ) ); |
| @@ -107,8 +154,10 @@ | ||
| 107 | 154 | /** |
| 108 | 155 | * Setup js scripts & css styles ( only on the relevant pages ) |
| 109 | 156 | * |
| 110 | 157 | * @since 0.6 |
| 158 | + * | |
| 159 | + * @return void | |
| 111 | 160 | */ |
| 112 | 161 | public function admin_enqueue_scripts() { |
| 113 | 162 | $screen = get_current_screen(); |
| 114 | 163 | if ( empty( $screen ) ) { |
| @@ -128,22 +177,26 @@ | ||
| 128 | 177 | 'user' => array( array( 'profile', 'user-edit' ), array( 'jquery' ), 0, 0 ), |
| 129 | 178 | 'widgets' => array( array( 'widgets' ), array( 'jquery' ), 0, 0 ), |
| 130 | 179 | ); |
| 131 | 180 | |
| 181 | + $block_screens = array( 'widgets', 'site-editor' ); | |
| 182 | + | |
| 132 | 183 | if ( ! empty( $screen->post_type ) && $this->model->is_translated_post_type( $screen->post_type ) ) { |
| 133 | 184 | $scripts['post'] = array( array( 'edit', 'upload' ), array( 'jquery', 'wp-ajax-response' ), 0, 1 ); |
| 134 | 185 | |
| 135 | 186 | // Classic editor. |
| 136 | 187 | 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 ); | |
| 188 | + $scripts['classic-editor'] = array( array( 'post', 'media', 'async-upload' ), array( 'jquery', 'wp-ajax-response', 'post', 'jquery-ui-dialog', 'wp-i18n' ), 0, 1 ); | |
| 138 | 189 | } |
| 139 | 190 | |
| 140 | 191 | // Block editor with legacy metabox in WP 5.0+. |
| 141 | - 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 ); | |
| 143 | - } | |
| 192 | + $block_screens[] = 'post'; | |
| 144 | 193 | } |
| 145 | 194 | |
| 195 | + if ( $this->is_block_editor( $screen ) ) { | |
| 196 | + $scripts['block-editor'] = array( $block_screens, array( 'jquery', 'wp-ajax-response', 'wp-api-fetch', 'jquery-ui-dialog', 'wp-i18n' ), 0, 1 ); | |
| 197 | + } | |
| 198 | + | |
| 146 | 199 | if ( ! empty( $screen->taxonomy ) && $this->model->is_translated_taxonomy( $screen->taxonomy ) ) { |
| 147 | 200 | $scripts['term'] = array( array( 'edit-tags', 'term' ), array( 'jquery', 'wp-ajax-response', 'jquery-ui-autocomplete' ), 0, 1 ); |
| 148 | 201 | } |
| 149 | 202 | |
| @@ -148,36 +201,66 @@ | ||
| 148 | 201 | } |
| 149 | 202 | |
| 150 | 203 | foreach ( $scripts as $script => $v ) { |
| 151 | 204 | 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] ); | |
| 205 | + wp_enqueue_script( 'pll_' . $script, plugins_url( '/js/build/' . $script . $suffix . '.js', POLYLANG_ROOT_FILE ), $v[1], POLYLANG_VERSION, $v[3] ); | |
| 206 | + if ( 'classic-editor' === $script || 'block-editor' === $script ) { | |
| 207 | + wp_set_script_translations( 'pll_' . $script, 'polylang' ); | |
| 208 | + } | |
| 153 | 209 | } |
| 154 | 210 | } |
| 155 | 211 | |
| 156 | - wp_enqueue_style( 'polylang_admin', plugins_url( '/css/admin' . $suffix . '.css', POLYLANG_FILE ), array(), POLYLANG_VERSION ); | |
| 212 | + wp_register_style( 'polylang_admin', plugins_url( '/css/build/admin' . $suffix . '.css', POLYLANG_ROOT_FILE ), array( 'wp-jquery-ui-dialog' ), POLYLANG_VERSION ); | |
| 213 | + wp_enqueue_style( 'polylang_dialog', plugins_url( '/css/build/dialog' . $suffix . '.css', POLYLANG_ROOT_FILE ), array( 'polylang_admin' ), POLYLANG_VERSION ); | |
| 157 | 214 | |
| 158 | - $this->localize_scripts(); | |
| 215 | + $this->add_inline_scripts(); | |
| 159 | 216 | } |
| 160 | 217 | |
| 161 | 218 | /** |
| 219 | + * Tells whether or not the given screen is block editor kind. | |
| 220 | + * e.g. widget, site or post editor. | |
| 221 | + * | |
| 222 | + * @since 3.3 | |
| 223 | + * | |
| 224 | + * @param WP_Screen $screen Screen object. | |
| 225 | + * @return bool True if the screen is a block editor, false otherwise. | |
| 226 | + */ | |
| 227 | + protected function is_block_editor( $screen ) { | |
| 228 | + return method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() && ! pll_use_block_editor_plugin(); | |
| 229 | + } | |
| 230 | + | |
| 231 | + /** | |
| 162 | 232 | * Enqueue scripts to the WP Customizer. |
| 163 | 233 | * |
| 164 | 234 | * @since 2.4.0 |
| 235 | + * | |
| 236 | + * @return void | |
| 165 | 237 | */ |
| 166 | 238 | public function customize_controls_enqueue_scripts() { |
| 167 | 239 | if ( $this->model->get_languages_list() ) { |
| 168 | 240 | $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 ); | |
| 170 | - $this->localize_scripts(); | |
| 241 | + wp_enqueue_script( 'pll_widgets', plugins_url( '/js/build/widgets' . $suffix . '.js', POLYLANG_ROOT_FILE ), array( 'jquery' ), POLYLANG_VERSION, true ); | |
| 242 | + $this->add_inline_scripts(); | |
| 171 | 243 | } |
| 172 | 244 | } |
| 173 | 245 | |
| 174 | 246 | /** |
| 175 | - * Localize scripts. | |
| 247 | + * Adds inline scripts to set the default language in JS | |
| 248 | + * and localizes scripts. | |
| 176 | 249 | * |
| 177 | - * @since 2.4.0 | |
| 250 | + * @since 3.3 | |
| 251 | + * | |
| 252 | + * @return void | |
| 178 | 253 | */ |
| 179 | - public function localize_scripts() { | |
| 254 | + private function add_inline_scripts() { | |
| 255 | + if ( wp_script_is( 'pll_block-editor', 'enqueued' ) ) { | |
| 256 | + $default_lang_script = 'const pllDefaultLanguage = "' . $this->options['default_lang'] . '";'; | |
| 257 | + wp_add_inline_script( | |
| 258 | + 'pll_block-editor', | |
| 259 | + $default_lang_script, | |
| 260 | + 'before' | |
| 261 | + ); | |
| 262 | + } | |
| 180 | 263 | if ( wp_script_is( 'pll_widgets', 'enqueued' ) ) { |
| 181 | 264 | wp_localize_script( |
| 182 | 265 | 'pll_widgets', |
| 183 | 266 | 'pll_widgets', |
| @@ -200,8 +283,10 @@ | ||
| 200 | 283 | * when options.data is an empty string (GET request with the method 'load') |
| 201 | 284 | * see: https://wordpress.org/support/topic/invalid-url-during-wordpress-new-dashboard-widget-operation |
| 202 | 285 | * |
| 203 | 286 | * @since 1.4 |
| 287 | + * | |
| 288 | + * @return void | |
| 204 | 289 | */ |
| 205 | 290 | public function admin_print_footer_scripts() { |
| 206 | 291 | global $post_ID, $tag_ID; |
| 207 | 292 | |
| @@ -218,34 +303,57 @@ | ||
| 218 | 303 | $arr = wp_json_encode( $params ); |
| 219 | 304 | ?> |
| 220 | 305 | <script type="text/javascript"> |
| 221 | 306 | 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 ?>'; | |
| 307 | + jQuery( | |
| 308 | + function( $ ){ | |
| 309 | + $.ajaxPrefilter( function ( options, originalOptions, jqXHR ) { | |
| 310 | + if ( -1 != options.url.indexOf( ajaxurl ) || -1 != ajaxurl.indexOf( options.url ) ) { | |
| 311 | + | |
| 312 | + function addPolylangParametersAsString() { | |
| 313 | + if ( 'undefined' === typeof options.data || '' === options.data.trim() ) { | |
| 314 | + // Only Polylang data need to be send. So it could be as a simple query string. | |
| 315 | + options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>'; | |
| 231 | 316 | } else { |
| 317 | + /* | |
| 318 | + * In some cases data could be a JSON string like in third party plugins. | |
| 319 | + * So we need not to break their process by adding polylang parameters as valid JSON datas. | |
| 320 | + */ | |
| 232 | 321 | 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); | |
| 322 | + options.data = JSON.stringify( Object.assign( JSON.parse( options.data ), <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?> ) ); | |
| 323 | + } catch( exception ) { | |
| 324 | + // Add Polylang data to the existing query string. | |
| 325 | + options.data = options.data + '&<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>'; | |
| 236 | 326 | } |
| 237 | - catch(e) { | |
| 238 | - options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>&'+options.data; | |
| 239 | - } | |
| 240 | 327 | } |
| 328 | + } | |
| 329 | + | |
| 330 | + /* | |
| 331 | + * options.processData set to true is the default jQuery process where the data is converted in a query string by using jQuery.param(). | |
| 332 | + * This step is done before applying filters. Thus here the options.data is already a string in this case. | |
| 333 | + * @See https://github.com/jquery/jquery/blob/3.5.1/src/ajax.js#L563-L569 jQuery ajax function. | |
| 334 | + * It is the most case WordPress send ajax request this way however third party plugins or themes could be send JSON string. | |
| 335 | + * Use JSON format is recommended in jQuery.param() documentation to be able to send complex data structures. | |
| 336 | + * @See https://api.jquery.com/jquery.param/ jQuery param function. | |
| 337 | + */ | |
| 338 | + if ( options.processData ) { | |
| 339 | + addPolylangParametersAsString(); | |
| 241 | 340 | } else { |
| 242 | - options.data = $.extend(options.data, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>); | |
| 341 | + /* | |
| 342 | + * If options.processData is set to false data could be undefined or pass as a string. | |
| 343 | + * So data as to be processed as if options.processData is set to true. | |
| 344 | + */ | |
| 345 | + if ( 'undefined' === typeof options.data || 'string' === typeof options.data ) { | |
| 346 | + addPolylangParametersAsString(); | |
| 347 | + } else { | |
| 348 | + // Otherwise options.data is probably an object. | |
| 349 | + options.data = Object.assign( options.data || {} , <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?> ); | |
| 350 | + } | |
| 243 | 351 | } |
| 244 | 352 | } |
| 245 | - } | |
| 246 | - }); | |
| 247 | - })(jQuery) | |
| 353 | + }); | |
| 354 | + } | |
| 355 | + ); | |
| 248 | 356 | } |
| 249 | 357 | </script> |
| 250 | 358 | <?php |
| 251 | 359 | } |
| @@ -253,8 +361,10 @@ | ||
| 253 | 361 | /** |
| 254 | 362 | * Sets the admin current language, used to filter the content |
| 255 | 363 | * |
| 256 | 364 | * @since 2.0 |
| 365 | + * | |
| 366 | + * @return void | |
| 257 | 367 | */ |
| 258 | 368 | public function set_current_language() { |
| 259 | 369 | $this->curlang = $this->filter_lang; |
| 260 | 370 | |
| @@ -260,22 +370,23 @@ | ||
| 260 | 370 | |
| 261 | 371 | // Edit Post |
| 262 | 372 | if ( isset( $_REQUEST['pll_post_id'] ) && $lang = $this->model->post->get_language( (int) $_REQUEST['pll_post_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 263 | 373 | $this->curlang = $lang; |
| 264 | - } elseif ( 'post.php' === $GLOBALS['pagenow'] && isset( $_GET['post'] ) && $lang = $this->model->post->get_language( (int) $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 374 | + } elseif ( 'post.php' === $GLOBALS['pagenow'] && isset( $_GET['post'] ) && false !== get_post_type( (int) $_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 | |
| 265 | 375 | $this->curlang = $lang; |
| 376 | + } elseif ( 'post.php' === $GLOBALS['pagenow'] && isset( $_POST['post_ID'] ) && false !== get_post_type( (int) $_POST['post_ID'] ) && $this->model->is_translated_post_type( get_post_type( (int) $_POST['post_ID'] ) ) && $lang = $this->model->post->get_language( (int) $_POST['post_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 377 | + $this->curlang = $lang; | |
| 266 | 378 | } 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 |
| 267 | 379 | $this->curlang = empty( $_GET['new_lang'] ) ? $this->pref_lang : $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 268 | 380 | } |
| 269 | 381 | |
| 270 | 382 | // Edit Term |
| 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 | |
| 383 | + elseif ( isset( $_REQUEST['pll_term_id'] ) && $lang = $this->model->term->get_language( (int) $_REQUEST['pll_term_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 273 | 384 | $this->curlang = $lang; |
| 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 | |
| 385 | + } elseif ( in_array( $GLOBALS['pagenow'], array( 'edit-tags.php', 'term.php' ) ) && isset( $_REQUEST['taxonomy'] ) && $this->model->is_translated_taxonomy( sanitize_key( $_REQUEST['taxonomy'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 386 | + if ( isset( $_REQUEST['tag_ID'] ) && $lang = $this->model->term->get_language( (int) $_REQUEST['tag_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 387 | + $this->curlang = $lang; | |
| 388 | + } elseif ( ! empty( $_GET['new_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 278 | 389 | $this->curlang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 279 | 390 | } elseif ( empty( $this->curlang ) ) { |
| 280 | 391 | $this->curlang = $this->pref_lang; |
| 281 | 392 | } |
| @@ -284,8 +395,27 @@ | ||
| 284 | 395 | // Ajax |
| 285 | 396 | if ( wp_doing_ajax() && ! empty( $_REQUEST['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 286 | 397 | $this->curlang = $this->model->get_language( sanitize_key( $_REQUEST['lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 287 | 398 | } |
| 399 | + | |
| 400 | + /** | |
| 401 | + * Filters the current language used by Polylang in the admin context. | |
| 402 | + * | |
| 403 | + * @since 3.2 | |
| 404 | + * | |
| 405 | + * @param PLL_Language|false|null $curlang Instance of the current language. | |
| 406 | + * @param PLL_Admin_Base $polylang Instance of the main Polylang's object. | |
| 407 | + */ | |
| 408 | + $this->curlang = apply_filters( 'pll_admin_current_language', $this->curlang, $this ); | |
| 409 | + | |
| 410 | + // Inform that the admin language has been set. | |
| 411 | + if ( $this->curlang instanceof PLL_Language ) { | |
| 412 | + /** This action is documented in frontend/choose-lang.php */ | |
| 413 | + do_action( 'pll_language_defined', $this->curlang->slug, $this->curlang ); | |
| 414 | + } else { | |
| 415 | + /** This action is documented in include/class-polylang.php */ | |
| 416 | + do_action( 'pll_no_language_defined' ); // To load overridden textdomains. | |
| 417 | + } | |
| 288 | 418 | } |
| 289 | 419 | |
| 290 | 420 | /** |
| 291 | 421 | * Defines the backend language and the admin language filter based on user preferences |
| @@ -290,8 +420,10 @@ | ||
| 290 | 420 | /** |
| 291 | 421 | * Defines the backend language and the admin language filter based on user preferences |
| 292 | 422 | * |
| 293 | 423 | * @since 1.2.3 |
| 424 | + * | |
| 425 | + * @return void | |
| 294 | 426 | */ |
| 295 | 427 | public function init_user() { |
| 296 | 428 | // Language for admin language filter: may be empty |
| 297 | 429 | // $_GET['lang'] is numeric when editing a language, not when selecting a new language in the filter |
| @@ -305,28 +437,21 @@ | ||
| 305 | 437 | // Set preferred language for use when saving posts and terms: must not be empty |
| 306 | 438 | $this->pref_lang = empty( $this->filter_lang ) ? $this->model->get_language( $this->options['default_lang'] ) : $this->filter_lang; |
| 307 | 439 | |
| 308 | 440 | /** |
| 309 | - * Filter the preferred language on admin side | |
| 310 | - * The preferred language is used for example to determine the language of a new post | |
| 441 | + * Filters the preferred language on admin side. | |
| 442 | + * The preferred language is used for example to determine the language of a new post. | |
| 311 | 443 | * |
| 312 | 444 | * @since 1.2.3 |
| 313 | 445 | * |
| 314 | - * @param object $pref_lang preferred language | |
| 446 | + * @param PLL_Language $pref_lang Preferred language. | |
| 315 | 447 | */ |
| 316 | 448 | $this->pref_lang = apply_filters( 'pll_admin_preferred_language', $this->pref_lang ); |
| 317 | 449 | |
| 318 | 450 | $this->set_current_language(); |
| 319 | 451 | |
| 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 | - } | |
| 452 | + // Plugin i18n, only needed for backend. | |
| 453 | + load_plugin_textdomain( 'polylang' ); | |
| 329 | 454 | } |
| 330 | 455 | |
| 331 | 456 | /** |
| 332 | 457 | * Avoids parsing a tax query when all languages are requested |
| @@ -331,9 +456,9 @@ | ||
| 331 | 456 | /** |
| 332 | 457 | * Avoids parsing a tax query when all languages are requested |
| 333 | 458 | * Fixes https://wordpress.org/support/topic/notice-undefined-offset-0-in-wp-includesqueryphp-on-line-3877 introduced in WP 4.1 |
| 334 | 459 | * |
| 335 | - * @see the suggestion of @boonebgorges, https://core.trac.wordpress.org/ticket/31246 | |
| 460 | + * @see https://core.trac.wordpress.org/ticket/31246 the suggestion of @boonebgorges. | |
| 336 | 461 | * |
| 337 | 462 | * @since 1.6.5 |
| 338 | 463 | * |
| 339 | 464 | * @param array $qvars |
| @@ -347,13 +472,14 @@ | ||
| 347 | 472 | return $qvars; |
| 348 | 473 | } |
| 349 | 474 | |
| 350 | 475 | /** |
| 351 | - * Adds the languages list in admin bar for the admin languages filter | |
| 476 | + * Adds the languages list in admin bar for the admin languages filter. | |
| 352 | 477 | * |
| 353 | 478 | * @since 0.9 |
| 354 | 479 | * |
| 355 | - * @param object $wp_admin_bar | |
| 480 | + * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar global object. | |
| 481 | + * @return void | |
| 356 | 482 | */ |
| 357 | 483 | public function admin_bar_menu( $wp_admin_bar ) { |
| 358 | 484 | $all_item = (object) array( |
| 359 | 485 | 'slug' => 'all', |
| @@ -378,17 +504,23 @@ | ||
| 378 | 504 | * @param array $items The admin languages filter submenu items. |
| 379 | 505 | */ |
| 380 | 506 | $items = apply_filters( 'pll_admin_languages_filter', array_merge( array( $all_item ), $this->model->get_languages_list() ) ); |
| 381 | 507 | |
| 508 | + $menu = array( | |
| 509 | + 'id' => 'languages', | |
| 510 | + 'title' => $selected->flag . $title, | |
| 511 | + 'href' => esc_url( add_query_arg( 'lang', $selected->slug, remove_query_arg( 'paged' ) ) ), | |
| 512 | + 'meta' => array( | |
| 513 | + 'title' => __( 'Filters content by language', 'polylang' ), | |
| 514 | + ), | |
| 515 | + ); | |
| 516 | + | |
| 517 | + if ( 'all' !== $selected->slug ) { | |
| 518 | + $menu['meta']['class'] = 'pll-filtered-languages'; | |
| 519 | + } | |
| 520 | + | |
| 382 | 521 | 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 | - ); | |
| 522 | + $wp_admin_bar->add_menu( $menu ); | |
| 391 | 523 | } |
| 392 | 524 | |
| 393 | 525 | foreach ( $items as $lang ) { |
| 394 | 526 | if ( $selected->slug === $lang->slug ) { |
| @@ -403,7 +535,34 @@ | ||
| 403 | 535 | 'href' => esc_url( add_query_arg( 'lang', $lang->slug, remove_query_arg( 'paged' ) ) ), |
| 404 | 536 | 'meta' => 'all' === $lang->slug ? array() : array( 'lang' => esc_attr( $lang->get_locale( 'display' ) ) ), |
| 405 | 537 | ) |
| 406 | 538 | ); |
| 539 | + } | |
| 540 | + } | |
| 541 | + | |
| 542 | + /** | |
| 543 | + * Remove the customize submenu when using a block theme. | |
| 544 | + * | |
| 545 | + * WordPress removes the Customizer menu if a block theme is activated and no other plugins interact with it. | |
| 546 | + * As Polylang interacts with the Customizer, we have to delete this menu ourselves in the case of a block theme, | |
| 547 | + * unless another plugin than Polylang interacts with the Customizer. | |
| 548 | + * | |
| 549 | + * @since 3.2 | |
| 550 | + * | |
| 551 | + * @return void | |
| 552 | + */ | |
| 553 | + public function remove_customize_submenu() { | |
| 554 | + if ( ! $this->should_customize_menu_be_removed() ) { | |
| 555 | + return; | |
| 556 | + } | |
| 557 | + | |
| 558 | + global $submenu; | |
| 559 | + | |
| 560 | + if ( ! empty( $submenu['themes.php'] ) ) { | |
| 561 | + foreach ( $submenu['themes.php'] as $submenu_item ) { | |
| 562 | + if ( 'customize' === $submenu_item[1] ) { | |
| 563 | + remove_submenu_page( 'themes.php', $submenu_item[2] ); | |
| 564 | + } | |
| 565 | + } | |
| 407 | 566 | } |
| 408 | 567 | } |
| 409 | 568 | } |