PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
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-classic-editor.php +118 -71 2.93.7.7 View file →
@@ -2,8 +2,10 @@
2 2 /**
3 3 * @package Polylang
4 4 */
5 5
6 +defined( 'ABSPATH' ) || exit;
7 +
6 8 /**
7 9 * Manages filters and actions related to the classic editor
8 10 *
9 11 * @since 2.4
@@ -8,16 +10,38 @@
8 10 *
9 11 * @since 2.4
10 12 */
11 13 class PLL_Admin_Classic_Editor {
12 - public $model, $links, $curlang, $pref_lang;
14 + /**
15 + * @var PLL_Model
16 + */
17 + public $model;
13 18
14 19 /**
15 - * Constructor: setups filters and actions
20 + * @var PLL_Admin_Links
21 + */
22 + public $links;
23 +
24 + /**
25 + * Current language (used to filter the content).
16 26 *
27 + * @var PLL_Language|null
28 + */
29 + public $curlang;
30 +
31 + /**
32 + * Preferred language to assign to new contents.
33 + *
34 + * @var PLL_Language|null
35 + */
36 + public $pref_lang;
37 +
38 + /**
39 + * Constructor: setups filters and actions.
40 + *
17 41 * @since 2.4
18 42 *
19 - * @param object $polylang
43 + * @param object $polylang The Polylang object.
20 44 */
21 45 public function __construct( &$polylang ) {
22 46 $this->model = &$polylang->model;
23 47 $this->links = &$polylang->links;
@@ -43,8 +67,9 @@
43 67 *
44 68 * @since 0.1
45 69 *
46 70 * @param string $post_type Current post type
71 + * @return void
47 72 */
48 73 public function add_meta_boxes( $post_type ) {
49 74 if ( $this->model->is_translated_post_type( $post_type ) ) {
50 75 add_meta_box(
@@ -64,8 +89,10 @@
64 89 /**
65 90 * Displays the Languages metabox in the 'Edit Post' and 'Edit Page' panels
66 91 *
67 92 * @since 0.1
93 + *
94 + * @return void
68 95 */
69 96 public function post_language() {
70 97 global $post_ID;
71 98 $post_type = get_post_type( $post_ID );
@@ -126,44 +153,45 @@
126 153 /**
127 154 * Ajax response for changing the language in the post metabox
128 155 *
129 156 * @since 0.2
157 + *
158 + * @return void
130 159 */
131 160 public function post_lang_choice() {
132 161 check_ajax_referer( 'pll_language', '_pll_nonce' );
133 162
134 163 if ( ! isset( $_POST['post_id'], $_POST['lang'], $_POST['post_type'] ) ) {
135 - wp_die( 0 );
164 + wp_die( 'The request is missing the parameter "post_type", "lang" and/or "post_id".' );
136 165 }
137 166
138 167 global $post_ID; // Obliged to use the global variable for wp_popular_terms_checklist
139 168 $post_ID = (int) $_POST['post_id'];
140 - $lang = $this->model->get_language( sanitize_key( $_POST['lang'] ) );
169 + $lang_slug = sanitize_key( $_POST['lang'] );
170 + $lang = $this->model->get_language( $lang_slug );
141 171 $post_type = sanitize_key( $_POST['post_type'] );
142 172
143 - if ( ! post_type_exists( $post_type ) ) {
144 - wp_die( 0 );
173 + if ( empty( $lang ) ) {
174 + wp_die( esc_html( "{$lang_slug} is not a valid language code." ) );
145 175 }
146 176
147 177 $post_type_object = get_post_type_object( $post_type );
178 +
179 + if ( empty( $post_type_object ) ) {
180 + wp_die( esc_html( "{$post_type} is not a valid post type." ) );
181 + }
182 +
148 183 if ( ! current_user_can( $post_type_object->cap->edit_post, $post_ID ) ) {
149 - wp_die( -1 );
184 + wp_die( 'You are not allowed to edit this post.' );
150 185 }
151 186
152 - $this->model->post->set_language( $post_ID, $lang ); // Save language, useful to set the language when uploading media from post
187 + $this->model->post->set_language( $post_ID, $lang );
153 188
154 - // We also need to save the translations to match the language change
155 - $translations = $this->model->post->get_translations( $post_ID );
156 - $translations = array_diff( $translations, array( $post_ID ) );
157 - $this->model->post->save_translations( $post_ID, $translations );
158 -
159 189 ob_start();
160 - if ( $lang ) {
161 - if ( 'attachment' === $post_type ) {
162 - include __DIR__ . '/view-translations-media.php';
163 - } else {
164 - include __DIR__ . '/view-translations-post.php';
165 - }
190 + if ( 'attachment' === $post_type ) {
191 + include __DIR__ . '/view-translations-media.php';
192 + } else {
193 + include __DIR__ . '/view-translations-post.php';
166 194 }
167 195 $x = new WP_Ajax_Response( array( 'what' => 'translations', 'data' => ob_get_contents() ) );
168 196 ob_end_clean();
169 197
@@ -173,32 +201,34 @@
173 201
174 202 foreach ( array_map( 'sanitize_key', $_POST['taxonomies'] ) as $taxname ) {
175 203 $taxonomy = get_taxonomy( $taxname );
176 204
177 - ob_start();
178 - $popular_ids = wp_popular_terms_checklist( $taxonomy->name );
179 - $supplemental['populars'] = ob_get_contents();
180 - ob_end_clean();
205 + if ( ! empty( $taxonomy ) ) {
206 + ob_start();
207 + $popular_ids = wp_popular_terms_checklist( $taxonomy->name );
208 + $supplemental['populars'] = ob_get_contents();
209 + ob_end_clean();
181 210
182 - ob_start();
183 - // Use $post_ID to remember checked terms in case we come back to the original language
184 - wp_terms_checklist( $post_ID, array( 'taxonomy' => $taxonomy->name, 'popular_cats' => $popular_ids ) );
185 - $supplemental['all'] = ob_get_contents();
186 - ob_end_clean();
211 + ob_start();
212 + // Use $post_ID to remember checked terms in case we come back to the original language
213 + wp_terms_checklist( $post_ID, array( 'taxonomy' => $taxonomy->name, 'popular_cats' => $popular_ids ) );
214 + $supplemental['all'] = ob_get_contents();
215 + ob_end_clean();
187 216
188 - $supplemental['dropdown'] = wp_dropdown_categories(
189 - array(
190 - 'taxonomy' => $taxonomy->name,
191 - 'hide_empty' => 0,
192 - 'name' => 'new' . $taxonomy->name . '_parent',
193 - 'orderby' => 'name',
194 - 'hierarchical' => 1,
195 - 'show_option_none' => '— ' . $taxonomy->labels->parent_item . ' —',
196 - 'echo' => 0,
197 - )
198 - );
217 + $supplemental['dropdown'] = wp_dropdown_categories(
218 + array(
219 + 'taxonomy' => $taxonomy->name,
220 + 'hide_empty' => 0,
221 + 'name' => 'new' . $taxonomy->name . '_parent',
222 + 'orderby' => 'name',
223 + 'hierarchical' => 1,
224 + 'show_option_none' => '— ' . $taxonomy->labels->parent_item . ' —',
225 + 'echo' => 0,
226 + )
227 + );
199 228
200 - $x->Add( array( 'what' => 'taxonomy', 'data' => $taxonomy->name, 'supplemental' => $supplemental ) );
229 + $x->Add( array( 'what' => 'taxonomy', 'data' => $taxonomy->name, 'supplemental' => $supplemental ) );
230 + }
201 231 }
202 232 }
203 233
204 234 // Parent dropdown list ( only for hierarchical post types )
@@ -204,23 +234,29 @@
204 234 // Parent dropdown list ( only for hierarchical post types )
205 235 if ( in_array( $post_type, get_post_types( array( 'hierarchical' => true ) ) ) ) {
206 236 $post = get_post( $post_ID );
207 237
208 - // Args and filter from 'page_attributes_meta_box' in wp-admin/includes/meta-boxes.php of WP 4.2.1
209 - $dropdown_args = array(
210 - 'post_type' => $post->post_type,
211 - 'exclude_tree' => $post->ID,
212 - 'selected' => $post->post_parent,
213 - 'name' => 'parent_id',
214 - 'show_option_none' => __( '(no parent)', 'polylang' ),
215 - 'sort_column' => 'menu_order, post_title',
216 - 'echo' => 0,
217 - );
238 + if ( ! empty( $post ) ) {
239 + // Args and filter from 'page_attributes_meta_box' in wp-admin/includes/meta-boxes.php of WP 4.2.1
240 + $dropdown_args = array(
241 + 'post_type' => $post->post_type,
242 + 'exclude_tree' => $post->ID,
243 + 'selected' => $post->post_parent,
244 + 'name' => 'parent_id',
245 + 'show_option_none' => __( '(no parent)', 'polylang' ),
246 + 'sort_column' => 'menu_order, post_title',
247 + 'echo' => 0,
248 + );
218 249
219 - /** This filter is documented in wp-admin/includes/meta-boxes.php */
220 - $dropdown_args = apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post ); // Since WP 3.3
250 + /** This filter is documented in wp-admin/includes/meta-boxes.php */
251 + $dropdown_args = (array) apply_filters( 'page_attributes_dropdown_pages_args', $dropdown_args, $post ); // Since WP 3.3.
252 + $dropdown_args['echo'] = 0; // Make sure to not print it.
221 253
222 - $x->Add( array( 'what' => 'pages', 'data' => wp_dropdown_pages( $dropdown_args ) ) ); // phpcs:ignore WordPress.Security.EscapeOutput
254 + /** @var string $data */
255 + $data = wp_dropdown_pages( $dropdown_args ); // phpcs:ignore WordPress.Security.EscapeOutput
256 +
257 + $x->Add( array( 'what' => 'pages', 'data' => $data ) );
258 + }
223 259 }
224 260
225 261 // Flag
226 262 $x->Add( array( 'what' => 'flag', 'data' => empty( $lang->flag ) ? esc_html( $lang->slug ) : $lang->flag ) );
@@ -234,8 +270,10 @@
234 270 /**
235 271 * Ajax response for input in translation autocomplete input box
236 272 *
237 273 * @since 1.5
274 + *
275 + * @return void
238 276 */
239 277 public function ajax_posts_not_translated() {
240 278 check_ajax_referer( 'pll_language', '_pll_nonce' );
241 279
@@ -269,16 +307,19 @@
269 307
270 308 // Add current translation in list
271 309 if ( $post_id = $this->model->post->get_translation( (int) $_GET['pll_post_id'], $translation_language ) ) {
272 310 $post = get_post( $post_id );
273 - array_unshift(
274 - $return,
275 - array(
276 - 'id' => $post_id,
277 - 'value' => $post->post_title,
278 - 'link' => $this->links->edit_post_translation_link( $post_id ),
279 - )
280 - );
311 +
312 + if ( ! empty( $post ) ) {
313 + array_unshift(
314 + $return,
315 + array(
316 + 'id' => $post_id,
317 + 'value' => $post->post_title,
318 + 'link' => $this->links->edit_post_translation_link( $post_id ),
319 + )
320 + );
321 + }
281 322 }
282 323
283 324 wp_die( wp_json_encode( $return ) );
284 325 }
@@ -283,31 +324,37 @@
283 324 wp_die( wp_json_encode( $return ) );
284 325 }
285 326
286 327 /**
287 - * Filters the pages by language in the parent dropdown list in the page attributes metabox
328 + * Filters the pages by language in the parent dropdown list in the page attributes metabox.
288 329 *
289 330 * @since 0.6
290 331 *
291 - * @param array $dropdown_args Arguments passed to wp_dropdown_pages
292 - * @param object $post
293 - * @return array Modified arguments
332 + * @param array $dropdown_args Arguments passed to wp_dropdown_pages().
333 + * @param WP_Post $post The page being edited.
334 + * @return array Modified arguments.
294 335 */
295 336 public function page_attributes_dropdown_pages_args( $dropdown_args, $post ) {
296 - $dropdown_args['lang'] = isset( $_POST['lang'] ) ? $this->model->get_language( sanitize_key( $_POST['lang'] ) ) : $this->model->post->get_language( $post->ID ); // phpcs:ignore WordPress.Security.NonceVerification
297 - if ( ! $dropdown_args['lang'] ) {
298 - $dropdown_args['lang'] = $this->pref_lang;
337 + $language = isset( $_POST['lang'] ) ? $this->model->get_language( sanitize_key( $_POST['lang'] ) ) : $this->model->post->get_language( $post->ID ); // phpcs:ignore WordPress.Security.NonceVerification
338 +
339 + if ( empty( $language ) ) {
340 + $language = $this->pref_lang;
299 341 }
300 342
343 + if ( ! empty( $language ) ) {
344 + $dropdown_args['lang'] = $language->slug;
345 + }
346 +
301 347 return $dropdown_args;
302 348 }
303 349
304 350 /**
305 - * Displays a notice if the user has not sufficient rights to overwrite synchronized taxonomies and metas
351 + * Displays a notice if the user has not sufficient rights to overwrite synchronized taxonomies and metas.
306 352 *
307 353 * @since 2.6
308 354 *
309 - * @param object $post Post currently being edited
355 + * @param WP_Post $post the post currently being edited.
356 + * @return void
310 357 */
311 358 public function edit_form_top( $post ) {
312 359 if ( ! $this->model->post->current_user_can_synchronize( $post->ID ) ) {
313 360 ?>