PluginProbe
Polylang / 3.7.1
Polylang v3.7.1
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 +230 -151 2.7.43.7.1 View file →
@@ -1,43 +1,84 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 - * Base class for both admin
7 + * Setup features available on all admin pages.
5 8 *
6 9 * @since 1.8
7 10 */
8 -class PLL_Admin_Base extends PLL_Base {
9 - 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;
10 18
11 19 /**
12 - * Loads the polylang text domain
13 - * Setups actions needed on all admin pages
20 + * Language selected in the admin language filter.
14 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 + *
15 61 * @since 1.8
16 62 *
17 - * @param object $links_model
63 + * @param PLL_Links_Model $links_model Reference to the links model.
18 64 */
19 65 public function __construct( &$links_model ) {
20 66 parent::__construct( $links_model );
21 67
22 - // Plugin i18n, only needed for backend
23 - load_plugin_textdomain( 'polylang', false, basename( POLYLANG_DIR ) . '/languages' );
24 -
25 68 // Adds the link to the languages panel in the WordPress admin menu
26 69 add_action( 'admin_menu', array( $this, 'add_menus' ) );
27 70
71 + add_action( 'admin_menu', array( $this, 'remove_customize_submenu' ) );
72 +
28 73 // Setup js scripts and css styles
29 - add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
30 - 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
74 + add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 0 ); // High priority in case an ajax request is sent by an immediately invoked function
31 75
32 76 add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ) );
33 77
34 - if ( defined( 'POLYLANG_PRO' ) ) {
35 - new PLL_Pro();
36 - } elseif ( ! defined( 'PLL_LINGOTEK_AD' ) || PLL_LINGOTEK_AD ) {
37 - require_once POLYLANG_DIR . '/lingotek/lingotek.php'; // Lingotek
38 - add_action( 'wp_loaded', array( new PLL_Lingotek(), 'init' ) );
39 - }
78 + // Early instantiated to be able to correctly initialize language properties.
79 + $this->static_pages = new PLL_Admin_Static_Pages( $this );
80 + $this->model->set_languages_ready();
40 81 }
41 82
42 83 /**
43 84 * Setups filters and action needed on all admin pages and on plugins page
@@ -49,22 +90,16 @@
49 90 parent::init();
50 91
51 92 $this->notices = new PLL_Admin_Notices( $this );
52 93
53 - if ( Polylang::is_wizard() && class_exists( 'PLL_Wizard_Pro' ) ) {
54 - // Instantiate PLL_Wizard_Pro class after all PLL_Admin object is all initialized.
55 - // After PLL_Admin::maybe_load_sync_post which is hooked on admin_init with priority 20.
56 - add_action( 'admin_init', array( $this, 'instantiate_wizard_pro' ), 30 );
57 - }
94 + $this->default_term = new PLL_Admin_Default_Term( $this );
95 + $this->default_term->add_hooks();
58 96
59 - $this->wizard = new PLL_Wizard( $this );
60 -
61 - if ( ! $this->model->get_languages_list() ) {
97 + if ( ! $this->model->has_languages() ) {
62 98 return;
63 99 }
64 100
65 101 $this->links = new PLL_Admin_Links( $this ); // FIXME needed here ?
66 - $this->static_pages = new PLL_Admin_Static_Pages( $this ); // FIXME needed here ?
67 102 $this->filters_links = new PLL_Filters_Links( $this ); // FIXME needed here ?
68 103
69 104 // Filter admin language for users
70 105 // We must not call user info before WordPress defines user roles in wp-settings.php
@@ -72,14 +107,8 @@
72 107 add_filter( 'request', array( $this, 'request' ) );
73 108
74 109 // Adds the languages in admin bar
75 110 add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 100 ); // 100 determines the position
76 -
77 - // Translate slugs, only for pretty permalinks
78 - if ( get_option( 'permalink_structure' ) && class_exists( 'PLL_Translate_Slugs' ) ) {
79 - $slugs_model = new PLL_Translate_Slugs_Model( $this );
80 - $this->translate_slugs = new PLL_Translate_Slugs( $slugs_model, $this->curlang );
81 - }
82 111 }
83 112
84 113 /**
85 114 * Adds the link to the languages panel in the WordPress admin menu
@@ -84,8 +113,10 @@
84 113 /**
85 114 * Adds the link to the languages panel in the WordPress admin menu
86 115 *
87 116 * @since 0.1
117 + *
118 + * @return void
88 119 */
89 120 public function add_menus() {
90 121 global $admin_page_hooks;
91 122
@@ -92,10 +123,10 @@
92 123 // Prepare the list of tabs
93 124 $tabs = array( 'lang' => __( 'Languages', 'polylang' ) );
94 125
95 126 // Only if at least one language has been created
96 - if ( $this->model->get_languages_list() ) {
97 - $tabs['strings'] = __( 'Strings translations', 'polylang' );
127 + if ( $this->model->has_languages() ) {
128 + $tabs['strings'] = __( 'Translations', 'polylang' );
98 129 }
99 130
100 131 $tabs['settings'] = __( 'Settings', 'polylang' );
101 132
@@ -113,9 +144,9 @@
113 144 foreach ( $tabs as $tab => $title ) {
114 145 $page = 'lang' === $tab ? 'mlang' : "mlang_$tab";
115 146 if ( empty( $parent ) ) {
116 147 $parent = $page;
117 - add_menu_page( $title, __( 'Languages', 'polylang' ), 'manage_options', $page, null, 'dashicons-translation' );
148 + add_menu_page( $title, __( 'Languages', 'polylang' ), 'manage_options', $page, '__return_null', 'dashicons-translation' );
118 149 $admin_page_hooks[ $page ] = 'languages'; // Hack to avoid the localization of the hook name. See: https://core.trac.wordpress.org/ticket/18857
119 150 }
120 151
121 152 add_submenu_page( $parent, $title, $title, 'manage_options', $page, array( $this, 'languages_page' ) );
@@ -122,96 +153,130 @@
122 153 }
123 154 }
124 155
125 156 /**
157 + * Dummy method to display the 3 tabs pages: languages, strings translations, settings.
158 + * Overwritten in `PLL_Settings`.
159 + *
160 + * @since 3.7
161 + *
162 + * @return void
163 + */
164 + public function languages_page() {}
165 +
166 + /**
126 167 * Setup js scripts & css styles ( only on the relevant pages )
127 168 *
128 169 * @since 0.6
170 + *
171 + * @return void
129 172 */
130 173 public function admin_enqueue_scripts() {
174 + $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
175 +
176 + wp_enqueue_script( 'pll_admin', plugins_url( "/js/build/admin{$suffix}.js", POLYLANG_ROOT_FILE ), array( 'jquery' ), POLYLANG_VERSION, true );
177 + $inline_script = sprintf( 'let pll_admin = %s;', wp_json_encode( array( 'ajax_filter' => $this->get_ajax_filter_data() ) ) );
178 + wp_add_inline_script( 'pll_admin', $inline_script, 'before' );
179 +
131 180 $screen = get_current_screen();
132 181 if ( empty( $screen ) ) {
133 182 return;
134 183 }
135 184
136 - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
137 -
138 185 /*
139 186 * For each script:
140 187 * 0 => the pages on which to load the script
141 188 * 1 => the scripts it needs to work
142 - * 2 => 1 if loaded even if languages have not been defined yet, 0 otherwise
143 - * 3 => 1 if loaded in footer
189 + * 2 => true if loaded even if languages have not been defined yet, false otherwise
190 + * 3 => true if loaded in footer
144 191 */
145 192 $scripts = array(
146 - 'user' => array( array( 'profile', 'user-edit' ), array( 'jquery' ), 0, 0 ),
147 - 'widgets' => array( array( 'widgets' ), array( 'jquery' ), 0, 0 ),
193 + 'user' => array( array( 'profile', 'user-edit' ), array( 'jquery' ), false, false ),
194 + 'widgets' => array( array( 'widgets' ), array( 'jquery' ), false, false ),
148 195 );
149 196
197 + $block_screens = array( 'widgets', 'site-editor' );
198 +
150 199 if ( ! empty( $screen->post_type ) && $this->model->is_translated_post_type( $screen->post_type ) ) {
151 - $scripts['post'] = array( array( 'edit', 'upload' ), array( 'jquery', 'wp-ajax-response' ), 0, 1 );
200 + $scripts['post'] = array( array( 'edit', 'upload' ), array( 'jquery', 'wp-ajax-response' ), false, true );
152 201
153 202 // Classic editor.
154 203 if ( ! method_exists( $screen, 'is_block_editor' ) || ! $screen->is_block_editor() ) {
155 - $scripts['classic-editor'] = array( array( 'post', 'media', 'async-upload' ), array( 'jquery', 'wp-ajax-response', 'post' ), 0, 1 );
204 + $scripts['classic-editor'] = array( array( 'post', 'media', 'async-upload' ), array( 'jquery', 'wp-ajax-response', 'post', 'jquery-ui-dialog', 'wp-i18n' ), false, true );
156 205 }
157 206
158 207 // Block editor with legacy metabox in WP 5.0+.
159 - if ( method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() && ! pll_use_block_editor_plugin() ) {
160 - $scripts['block-editor'] = array( array( 'post' ), array( 'jquery', 'wp-ajax-response', 'wp-api-fetch' ), 0, 1 );
161 - }
208 + $block_screens[] = 'post';
162 209 }
163 210
211 + if ( $this->is_block_editor( $screen ) ) {
212 + $scripts['block-editor'] = array( $block_screens, array( 'jquery', 'wp-ajax-response', 'wp-api-fetch', 'jquery-ui-dialog', 'wp-i18n' ), false, true );
213 + }
214 +
164 215 if ( ! empty( $screen->taxonomy ) && $this->model->is_translated_taxonomy( $screen->taxonomy ) ) {
165 - $scripts['term'] = array( array( 'edit-tags', 'term' ), array( 'jquery', 'wp-ajax-response', 'jquery-ui-autocomplete' ), 0, 1 );
216 + $scripts['term'] = array( array( 'edit-tags', 'term' ), array( 'jquery', 'wp-ajax-response', 'jquery-ui-autocomplete' ), false, true );
166 217 }
167 218
168 219 foreach ( $scripts as $script => $v ) {
169 - if ( in_array( $screen->base, $v[0] ) && ( $v[2] || $this->model->get_languages_list() ) ) {
170 - wp_enqueue_script( 'pll_' . $script, plugins_url( '/js/' . $script . $suffix . '.js', POLYLANG_FILE ), $v[1], POLYLANG_VERSION, $v[3] );
220 + if ( in_array( $screen->base, $v[0] ) && ( $v[2] || $this->model->has_languages() ) ) {
221 + wp_enqueue_script( "pll_{$script}", plugins_url( "/js/build/{$script}{$suffix}.js", POLYLANG_ROOT_FILE ), $v[1], POLYLANG_VERSION, $v[3] );
222 + if ( 'classic-editor' === $script || 'block-editor' === $script ) {
223 + wp_set_script_translations( "pll_{$script}", 'polylang' );
224 + }
171 225 }
172 226 }
173 227
174 - wp_enqueue_style( 'polylang_admin', plugins_url( '/css/admin' . $suffix . '.css', POLYLANG_FILE ), array(), POLYLANG_VERSION );
228 + wp_register_style( 'polylang_admin', plugins_url( "/css/build/admin{$suffix}.css", POLYLANG_ROOT_FILE ), array( 'wp-jquery-ui-dialog' ), POLYLANG_VERSION );
229 + wp_enqueue_style( 'polylang_dialog', plugins_url( "/css/build/dialog{$suffix}.css", POLYLANG_ROOT_FILE ), array( 'polylang_admin' ), POLYLANG_VERSION );
175 230
176 - $this->localize_scripts();
231 + $this->add_inline_scripts();
177 232 }
178 233
179 234 /**
235 + * Tells whether or not the given screen is block editor kind.
236 + * e.g. widget, site or post editor.
237 + *
238 + * @since 3.3
239 + *
240 + * @param WP_Screen $screen Screen object.
241 + * @return bool True if the screen is a block editor, false otherwise.
242 + */
243 + protected function is_block_editor( $screen ) {
244 + return method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() && ! pll_use_block_editor_plugin();
245 + }
246 +
247 + /**
180 248 * Enqueue scripts to the WP Customizer.
181 249 *
182 250 * @since 2.4.0
251 + *
252 + * @return void
183 253 */
184 254 public function customize_controls_enqueue_scripts() {
185 - if ( $this->model->get_languages_list() ) {
255 + if ( $this->model->has_languages() ) {
186 256 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
187 - wp_enqueue_script( 'pll_widgets', plugins_url( '/js/widgets' . $suffix . '.js', POLYLANG_FILE ), array( 'jquery' ), POLYLANG_VERSION, true );
188 - $this->localize_scripts();
257 + wp_enqueue_script( 'pll_widgets', plugins_url( '/js/build/widgets' . $suffix . '.js', POLYLANG_ROOT_FILE ), array( 'jquery' ), POLYLANG_VERSION, true );
258 + $this->add_inline_scripts();
189 259 }
190 260 }
191 261
192 262 /**
193 - * Localize scripts.
263 + * Adds inline scripts to set the default language in JS
264 + * and localizes scripts.
194 265 *
195 - * @since 2.4.0
266 + * @since 3.3
267 + *
268 + * @return void
196 269 */
197 - public function localize_scripts() {
198 - if ( wp_script_is( 'pll_classic-editor', 'enqueued' ) ) {
199 - wp_localize_script(
200 - 'pll_classic-editor',
201 - 'confirm_text',
202 - __( 'You are about to overwrite an existing translation. Are you sure you want to proceed?', 'polylang' )
203 - );
204 - }
205 -
270 + private function add_inline_scripts() {
206 271 if ( wp_script_is( 'pll_block-editor', 'enqueued' ) ) {
207 - wp_localize_script(
272 + $default_lang_script = 'const pllDefaultLanguage = "' . $this->options['default_lang'] . '";';
273 + wp_add_inline_script(
208 274 'pll_block-editor',
209 - 'confirm_text',
210 - __( 'You are about to overwrite an existing translation. Are you sure you want to proceed?', 'polylang' )
275 + $default_lang_script,
276 + 'before'
211 277 );
212 278 }
213 -
214 279 if ( wp_script_is( 'pll_widgets', 'enqueued' ) ) {
215 280 wp_localize_script(
216 281 'pll_widgets',
217 282 'pll_widgets',
@@ -222,22 +287,24 @@
222 287 }
223 288 }
224 289
225 290 /**
226 - * Sets pll_ajax_backend on all backend ajax request
227 - * The final goal is to detect if an ajax request is made on admin or frontend
291 + * Returns the data to use with the AJAX filter.
292 + * The final goal is to detect if an ajax request is made on admin or frontend.
228 293 *
229 294 * Takes care to various situations:
230 - * when the ajax request has no options.data thanks to ScreenfeedFr
231 - * see: https://wordpress.org/support/topic/ajaxprefilter-may-not-work-as-expected
232 - * when options.data is a json string
233 - * see: https://wordpress.org/support/topic/polylang-breaking-third-party-ajax-requests-on-admin-panels
234 - * when options.data is an empty string (GET request with the method 'load')
235 - * see: https://wordpress.org/support/topic/invalid-url-during-wordpress-new-dashboard-widget-operation
295 + * - When the AJAX request has no `options.data` thanks to ScreenfeedFr.
296 + * See: https://wordpress.org/support/topic/ajaxprefilter-may-not-work-as-expected.
297 + * - When `options.data` is a JSON string.
298 + * See: https://wordpress.org/support/topic/polylang-breaking-third-party-ajax-requests-on-admin-panels.
299 + * - When `options.data` is an empty string (GET request with the method 'load').
300 + * See: https://wordpress.org/support/topic/invalid-url-during-wordpress-new-dashboard-widget-operation.
236 301 *
237 - * @since 1.4
302 + * @since 3.7
303 + *
304 + * @return array
238 305 */
239 - public function admin_print_footer_scripts() {
306 + public function get_ajax_filter_data(): array {
240 307 global $post_ID, $tag_ID;
241 308
242 309 $params = array( 'pll_ajax_backend' => 1 );
243 310 if ( ! empty( $post_ID ) ) {
@@ -247,42 +314,16 @@
247 314 if ( ! empty( $tag_ID ) ) {
248 315 $params = array_merge( $params, array( 'pll_term_id' => (int) $tag_ID ) );
249 316 }
250 317
251 - $str = http_build_query( $params );
252 - $arr = wp_json_encode( $params );
253 - ?>
254 - <script type="text/javascript">
255 - if (typeof jQuery != 'undefined') {
256 - (function($){
257 - $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
258 - if ( -1 != options.url.indexOf( ajaxurl ) || -1 != ajaxurl.indexOf( options.url ) ) {
259 - if ( 'undefined' === typeof options.data ) {
260 - options.data = ( 'get' === options.type.toLowerCase() ) ? '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>' : <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>;
261 - } else {
262 - if ( 'string' === typeof options.data ) {
263 - if ( '' === options.data && 'get' === options.type.toLowerCase() ) {
264 - options.url = options.url+'&<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>';
265 - } else {
266 - try {
267 - var o = $.parseJSON(options.data);
268 - o = $.extend(o, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>);
269 - options.data = JSON.stringify(o);
270 - }
271 - catch(e) {
272 - options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>&'+options.data;
273 - }
274 - }
275 - } else {
276 - options.data = $.extend(options.data, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>);
277 - }
278 - }
279 - }
280 - });
281 - })(jQuery)
282 - }
283 - </script>
284 - <?php
318 + /**
319 + * Filters the list of parameters to add to the admin ajax request.
320 + *
321 + * @since 3.4.5
322 + *
323 + * @param array $params List of parameters to add to the admin ajax request.
324 + */
325 + return (array) apply_filters( 'pll_admin_ajax_params', $params );
285 326 }
286 327
287 328 /**
288 329 * Sets the admin current language, used to filter the content
@@ -287,8 +328,10 @@
287 328 /**
288 329 * Sets the admin current language, used to filter the content
289 330 *
290 331 * @since 2.0
332 + *
333 + * @return void
291 334 */
292 335 public function set_current_language() {
293 336 $this->curlang = $this->filter_lang;
294 337
@@ -294,9 +337,9 @@
294 337
295 338 // Edit Post
296 339 if ( isset( $_REQUEST['pll_post_id'] ) && $lang = $this->model->post->get_language( (int) $_REQUEST['pll_post_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
297 340 $this->curlang = $lang;
298 - } elseif ( 'post.php' === $GLOBALS['pagenow'] && isset( $_GET['post'] ) && $lang = $this->model->post->get_language( (int) $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
341 + } elseif ( 'post.php' === $GLOBALS['pagenow'] && isset( $_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
299 342 $this->curlang = $lang;
300 343 } 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
301 344 $this->curlang = empty( $_GET['new_lang'] ) ? $this->pref_lang : $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
302 345 }
@@ -301,15 +344,14 @@
301 344 $this->curlang = empty( $_GET['new_lang'] ) ? $this->pref_lang : $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
302 345 }
303 346
304 347 // Edit Term
305 - // FIXME 'edit-tags.php' for backward compatibility with WP < 4.5
306 - 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
348 + elseif ( isset( $_REQUEST['pll_term_id'] ) && $lang = $this->model->term->get_language( (int) $_REQUEST['pll_term_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
307 349 $this->curlang = $lang;
308 - } elseif ( isset( $_REQUEST['pll_term_id'] ) && $lang = $this->model->term->get_language( (int) $_REQUEST['pll_term_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
309 - $this->curlang = $lang;
310 - } elseif ( 'edit-tags.php' === $GLOBALS['pagenow'] && isset( $_GET['taxonomy'] ) && $this->model->is_translated_taxonomy( sanitize_key( $_GET['taxonomy'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
311 - if ( ! empty( $_GET['new_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
350 + } elseif ( in_array( $GLOBALS['pagenow'], array( 'edit-tags.php', 'term.php' ) ) && isset( $_GET['taxonomy'] ) && $this->model->is_translated_taxonomy( sanitize_key( $_GET['taxonomy'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification
351 + if ( isset( $_GET['tag_ID'] ) && $lang = $this->model->term->get_language( (int) $_GET['tag_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
352 + $this->curlang = $lang;
353 + } elseif ( ! empty( $_GET['new_lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
312 354 $this->curlang = $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
313 355 } elseif ( empty( $this->curlang ) ) {
314 356 $this->curlang = $this->pref_lang;
315 357 }
@@ -318,8 +360,27 @@
318 360 // Ajax
319 361 if ( wp_doing_ajax() && ! empty( $_REQUEST['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
320 362 $this->curlang = $this->model->get_language( sanitize_key( $_REQUEST['lang'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
321 363 }
364 +
365 + /**
366 + * Filters the current language used by Polylang in the admin context.
367 + *
368 + * @since 3.2
369 + *
370 + * @param PLL_Language|false|null $curlang Instance of the current language.
371 + * @param PLL_Admin_Base $polylang Instance of the main Polylang's object.
372 + */
373 + $this->curlang = apply_filters( 'pll_admin_current_language', $this->curlang, $this );
374 +
375 + // Inform that the admin language has been set.
376 + if ( $this->curlang instanceof PLL_Language ) {
377 + /** This action is documented in frontend/choose-lang.php */
378 + do_action( 'pll_language_defined', $this->curlang->slug, $this->curlang );
379 + } else {
380 + /** This action is documented in include/class-polylang.php */
381 + do_action( 'pll_no_language_defined' ); // To load overridden textdomains.
382 + }
322 383 }
323 384
324 385 /**
325 386 * Defines the backend language and the admin language filter based on user preferences
@@ -324,8 +385,10 @@
324 385 /**
325 386 * Defines the backend language and the admin language filter based on user preferences
326 387 *
327 388 * @since 1.2.3
389 + *
390 + * @return void
328 391 */
329 392 public function init_user() {
330 393 // Language for admin language filter: may be empty
331 394 // $_GET['lang'] is numeric when editing a language, not when selecting a new language in the filter
@@ -336,31 +399,21 @@
336 399
337 400 $this->filter_lang = $this->model->get_language( get_user_meta( get_current_user_id(), 'pll_filter_content', true ) );
338 401
339 402 // Set preferred language for use when saving posts and terms: must not be empty
340 - $this->pref_lang = empty( $this->filter_lang ) ? $this->model->get_language( $this->options['default_lang'] ) : $this->filter_lang;
403 + $this->pref_lang = empty( $this->filter_lang ) ? $this->model->get_default_language() : $this->filter_lang;
341 404
342 405 /**
343 - * Filter the preferred language on admin side
344 - * The preferred language is used for example to determine the language of a new post
406 + * Filters the preferred language on admin side.
407 + * The preferred language is used for example to determine the language of a new post.
345 408 *
346 409 * @since 1.2.3
347 410 *
348 - * @param object $pref_lang preferred language
411 + * @param PLL_Language $pref_lang Preferred language.
349 412 */
350 413 $this->pref_lang = apply_filters( 'pll_admin_preferred_language', $this->pref_lang );
351 414
352 415 $this->set_current_language();
353 -
354 - // Inform that the admin language has been set
355 - // Only if the admin language is one of the Polylang defined language
356 - if ( $curlang = $this->model->get_language( get_user_locale() ) ) {
357 - /** This action is documented in frontend/choose-lang.php */
358 - do_action( 'pll_language_defined', $curlang->slug, $curlang );
359 - } else {
360 - /** This action is documented in include/class-polylang.php */
361 - do_action( 'pll_no_language_defined' ); // to load overridden textdomains
362 - }
363 416 }
364 417
365 418 /**
366 419 * Avoids parsing a tax query when all languages are requested
@@ -365,13 +418,13 @@
365 418 /**
366 419 * Avoids parsing a tax query when all languages are requested
367 420 * Fixes https://wordpress.org/support/topic/notice-undefined-offset-0-in-wp-includesqueryphp-on-line-3877 introduced in WP 4.1
368 421 *
369 - * @see the suggestion of @boonebgorges, https://core.trac.wordpress.org/ticket/31246
422 + * @see https://core.trac.wordpress.org/ticket/31246 the suggestion of @boonebgorges.
370 423 *
371 424 * @since 1.6.5
372 425 *
373 - * @param array $qvars
426 + * @param array $qvars The array of requested query variables.
374 427 * @return array
375 428 */
376 429 public function request( $qvars ) {
377 430 if ( isset( $qvars['lang'] ) && 'all' === $qvars['lang'] ) {
@@ -381,13 +434,14 @@
381 434 return $qvars;
382 435 }
383 436
384 437 /**
385 - * Adds the languages list in admin bar for the admin languages filter
438 + * Adds the languages list in admin bar for the admin languages filter.
386 439 *
387 440 * @since 0.9
388 441 *
389 - * @param object $wp_admin_bar
442 + * @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar global object.
443 + * @return void
390 444 */
391 445 public function admin_bar_menu( $wp_admin_bar ) {
392 446 $all_item = (object) array(
393 447 'slug' => 'all',
@@ -398,9 +452,9 @@
398 452 $selected = empty( $this->filter_lang ) ? $all_item : $this->filter_lang;
399 453
400 454 $title = sprintf(
401 455 '<span class="ab-label"%1$s><span class="screen-reader-text">%2$s</span>%3$s</span>',
402 - 'all' === $selected->slug ? '' : sprintf( ' lang="%s"', esc_attr( $selected->get_locale( 'display' ) ) ),
456 + $selected instanceof PLL_Language ? sprintf( ' lang="%s"', esc_attr( $selected->get_locale( 'display' ) ) ) : '',
403 457 __( 'Filters content by language', 'polylang' ),
404 458 esc_html( $selected->name )
405 459 );
406 460
@@ -412,17 +466,23 @@
412 466 * @param array $items The admin languages filter submenu items.
413 467 */
414 468 $items = apply_filters( 'pll_admin_languages_filter', array_merge( array( $all_item ), $this->model->get_languages_list() ) );
415 469
470 + $menu = array(
471 + 'id' => 'languages',
472 + 'title' => $selected->flag . $title,
473 + 'href' => esc_url( add_query_arg( 'lang', $selected->slug, remove_query_arg( 'paged' ) ) ),
474 + 'meta' => array(
475 + 'title' => __( 'Filters content by language', 'polylang' ),
476 + ),
477 + );
478 +
479 + if ( 'all' !== $selected->slug ) {
480 + $menu['meta']['class'] = 'pll-filtered-languages';
481 + }
482 +
416 483 if ( ! empty( $items ) ) {
417 - $wp_admin_bar->add_menu(
418 - array(
419 - 'id' => 'languages',
420 - 'title' => $selected->flag . $title,
421 - 'href' => esc_url( add_query_arg( 'lang', $selected->slug, remove_query_arg( 'paged' ) ) ),
422 - 'meta' => array( 'title' => __( 'Filters content by language', 'polylang' ) ),
423 - )
424 - );
484 + $wp_admin_bar->add_menu( $menu );
425 485 }
426 486
427 487 foreach ( $items as $lang ) {
428 488 if ( $selected->slug === $lang->slug ) {
@@ -439,13 +499,32 @@
439 499 )
440 500 );
441 501 }
442 502 }
503 +
443 504 /**
444 - * Instantiate PLL_Wizard_Pro class.
505 + * Remove the customize submenu when using a block theme.
445 506 *
446 - * @since 2.7
507 + * WordPress removes the Customizer menu if a block theme is activated and no other plugins interact with it.
508 + * As Polylang interacts with the Customizer, we have to delete this menu ourselves in the case of a block theme,
509 + * unless another plugin than Polylang interacts with the Customizer.
510 + *
511 + * @since 3.2
512 + *
513 + * @return void
447 514 */
448 - public function instantiate_wizard_pro() {
449 - $this->wizard_pro = new PLL_Wizard_Pro( $this );
515 + public function remove_customize_submenu() {
516 + if ( ! $this->should_customize_menu_be_removed() ) {
517 + return;
518 + }
519 +
520 + global $submenu;
521 +
522 + if ( ! empty( $submenu['themes.php'] ) ) {
523 + foreach ( $submenu['themes.php'] as $submenu_item ) {
524 + if ( 'customize' === $submenu_item[1] ) {
525 + remove_submenu_page( 'themes.php', $submenu_item[2] );
526 + }
527 + }
528 + }
450 529 }
451 530 }