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 +129 -72 2.7.23.7.7 View file →
@@ -1,6 +1,11 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
6 +defined( 'ABSPATH' ) || exit;
7 +
3 8 /**
4 9 * Manages filters and actions related to the classic editor
5 10 *
6 11 * @since 2.4
@@ -5,16 +10,38 @@
5 10 *
6 11 * @since 2.4
7 12 */
8 13 class PLL_Admin_Classic_Editor {
9 - public $model, $links, $curlang, $pref_lang;
14 + /**
15 + * @var PLL_Model
16 + */
17 + public $model;
10 18
11 19 /**
12 - * Constructor: setups filters and actions
20 + * @var PLL_Admin_Links
21 + */
22 + public $links;
23 +
24 + /**
25 + * Current language (used to filter the content).
13 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 + *
14 41 * @since 2.4
15 42 *
16 - * @param object $polylang
43 + * @param object $polylang The Polylang object.
17 44 */
18 45 public function __construct( &$polylang ) {
19 46 $this->model = &$polylang->model;
20 47 $this->links = &$polylang->links;
@@ -21,9 +48,9 @@
21 48 $this->curlang = &$polylang->curlang;
22 49 $this->pref_lang = &$polylang->pref_lang;
23 50
24 51 // Adds the Languages box in the 'Edit Post' and 'Edit Page' panels
25 - add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 );
52 + add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
26 53
27 54 // Ajax response for changing the language in the post metabox
28 55 add_action( 'wp_ajax_post_lang_choice', array( $this, 'post_lang_choice' ) );
29 56 add_action( 'wp_ajax_pll_posts_not_translated', array( $this, 'ajax_posts_not_translated' ) );
@@ -40,11 +67,11 @@
40 67 *
41 68 * @since 0.1
42 69 *
43 70 * @param string $post_type Current post type
44 - * @param object $post Current post
71 + * @return void
45 72 */
46 - public function add_meta_boxes( $post_type, $post ) {
73 + public function add_meta_boxes( $post_type ) {
47 74 if ( $this->model->is_translated_post_type( $post_type ) ) {
48 75 add_meta_box(
49 76 'ml_box',
50 77 __( 'Languages', 'polylang' ),
@@ -62,14 +89,16 @@
62 89 /**
63 90 * Displays the Languages metabox in the 'Edit Post' and 'Edit Page' panels
64 91 *
65 92 * @since 0.1
93 + *
94 + * @return void
66 95 */
67 96 public function post_language() {
68 97 global $post_ID;
69 98 $post_type = get_post_type( $post_ID );
70 99
71 - // phpcs:ignore WordPress.Security.NonceVerification, WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable
100 + // phpcs:ignore WordPress.Security.NonceVerification, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
72 101 $from_post_id = isset( $_GET['from_post'] ) ? (int) $_GET['from_post'] : 0;
73 102
74 103 $lang = ( $lg = $this->model->post->get_language( $post_ID ) ) ? $lg :
75 104 ( isset( $_GET['new_lang'] ) ? $this->model->get_language( sanitize_key( $_GET['new_lang'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification
@@ -111,9 +140,13 @@
111 140 do_action( 'pll_before_post_translations', $post_type );
112 141
113 142 echo '<div id="post-translations" class="translations">';
114 143 if ( $lang ) {
115 - include PLL_ADMIN_INC . '/view-translations-' . ( 'attachment' == $post_type ? 'media' : 'post' ) . '.php';
144 + if ( 'attachment' === $post_type ) {
145 + include __DIR__ . '/view-translations-media.php';
146 + } else {
147 + include __DIR__ . '/view-translations-post.php';
148 + }
116 149 }
117 150 echo '</div>' . "\n";
118 151 }
119 152
@@ -120,40 +153,45 @@
120 153 /**
121 154 * Ajax response for changing the language in the post metabox
122 155 *
123 156 * @since 0.2
157 + *
158 + * @return void
124 159 */
125 160 public function post_lang_choice() {
126 161 check_ajax_referer( 'pll_language', '_pll_nonce' );
127 162
128 163 if ( ! isset( $_POST['post_id'], $_POST['lang'], $_POST['post_type'] ) ) {
129 - wp_die( 0 );
164 + wp_die( 'The request is missing the parameter "post_type", "lang" and/or "post_id".' );
130 165 }
131 166
132 167 global $post_ID; // Obliged to use the global variable for wp_popular_terms_checklist
133 168 $post_ID = (int) $_POST['post_id'];
134 - $lang = $this->model->get_language( sanitize_key( $_POST['lang'] ) );
169 + $lang_slug = sanitize_key( $_POST['lang'] );
170 + $lang = $this->model->get_language( $lang_slug );
135 171 $post_type = sanitize_key( $_POST['post_type'] );
136 172
137 - if ( ! post_type_exists( $post_type ) ) {
138 - wp_die( 0 );
173 + if ( empty( $lang ) ) {
174 + wp_die( esc_html( "{$lang_slug} is not a valid language code." ) );
139 175 }
140 176
141 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 +
142 183 if ( ! current_user_can( $post_type_object->cap->edit_post, $post_ID ) ) {
143 - wp_die( -1 );
184 + wp_die( 'You are not allowed to edit this post.' );
144 185 }
145 186
146 - $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 );
147 188
148 - // We also need to save the translations to match the language change
149 - $translations = $this->model->post->get_translations( $post_ID );
150 - $translations = array_diff( $translations, array( $post_ID ) );
151 - $this->model->post->save_translations( $post_ID, $translations );
152 -
153 189 ob_start();
154 - if ( $lang ) {
155 - include PLL_ADMIN_INC . '/view-translations-' . ( 'attachment' == $post_type ? 'media' : 'post' ) . '.php';
190 + if ( 'attachment' === $post_type ) {
191 + include __DIR__ . '/view-translations-media.php';
192 + } else {
193 + include __DIR__ . '/view-translations-post.php';
156 194 }
157 195 $x = new WP_Ajax_Response( array( 'what' => 'translations', 'data' => ob_get_contents() ) );
158 196 ob_end_clean();
159 197
@@ -163,32 +201,34 @@
163 201
164 202 foreach ( array_map( 'sanitize_key', $_POST['taxonomies'] ) as $taxname ) {
165 203 $taxonomy = get_taxonomy( $taxname );
166 204
167 - ob_start();
168 - $popular_ids = wp_popular_terms_checklist( $taxonomy->name );
169 - $supplemental['populars'] = ob_get_contents();
170 - 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();
171 210
172 - ob_start();
173 - // Use $post_ID to remember checked terms in case we come back to the original language
174 - wp_terms_checklist( $post_ID, array( 'taxonomy' => $taxonomy->name, 'popular_cats' => $popular_ids ) );
175 - $supplemental['all'] = ob_get_contents();
176 - 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();
177 216
178 - $supplemental['dropdown'] = wp_dropdown_categories(
179 - array(
180 - 'taxonomy' => $taxonomy->name,
181 - 'hide_empty' => 0,
182 - 'name' => 'new' . $taxonomy->name . '_parent',
183 - 'orderby' => 'name',
184 - 'hierarchical' => 1,
185 - 'show_option_none' => '&mdash; ' . $taxonomy->labels->parent_item . ' &mdash;',
186 - 'echo' => 0,
187 - )
188 - );
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' => '&mdash; ' . $taxonomy->labels->parent_item . ' &mdash;',
225 + 'echo' => 0,
226 + )
227 + );
189 228
190 - $x->Add( array( 'what' => 'taxonomy', 'data' => $taxonomy->name, 'supplemental' => $supplemental ) );
229 + $x->Add( array( 'what' => 'taxonomy', 'data' => $taxonomy->name, 'supplemental' => $supplemental ) );
230 + }
191 231 }
192 232 }
193 233
194 234 // Parent dropdown list ( only for hierarchical post types )
@@ -194,23 +234,29 @@
194 234 // Parent dropdown list ( only for hierarchical post types )
195 235 if ( in_array( $post_type, get_post_types( array( 'hierarchical' => true ) ) ) ) {
196 236 $post = get_post( $post_ID );
197 237
198 - // Args and filter from 'page_attributes_meta_box' in wp-admin/includes/meta-boxes.php of WP 4.2.1
199 - $dropdown_args = array(
200 - 'post_type' => $post->post_type,
201 - 'exclude_tree' => $post->ID,
202 - 'selected' => $post->post_parent,
203 - 'name' => 'parent_id',
204 - 'show_option_none' => __( '(no parent)', 'polylang' ),
205 - 'sort_column' => 'menu_order, post_title',
206 - 'echo' => 0,
207 - );
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 + );
208 249
209 - /** This filter is documented in wp-admin/includes/meta-boxes.php */
210 - $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.
211 253
212 - $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 + }
213 259 }
214 260
215 261 // Flag
216 262 $x->Add( array( 'what' => 'flag', 'data' => empty( $lang->flag ) ? esc_html( $lang->slug ) : $lang->flag ) );
@@ -224,8 +270,10 @@
224 270 /**
225 271 * Ajax response for input in translation autocomplete input box
226 272 *
227 273 * @since 1.5
274 + *
275 + * @return void
228 276 */
229 277 public function ajax_posts_not_translated() {
230 278 check_ajax_referer( 'pll_language', '_pll_nonce' );
231 279
@@ -259,16 +307,19 @@
259 307
260 308 // Add current translation in list
261 309 if ( $post_id = $this->model->post->get_translation( (int) $_GET['pll_post_id'], $translation_language ) ) {
262 310 $post = get_post( $post_id );
263 - array_unshift(
264 - $return,
265 - array(
266 - 'id' => $post_id,
267 - 'value' => $post->post_title,
268 - 'link' => $this->links->edit_post_translation_link( $post_id ),
269 - )
270 - );
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 + }
271 322 }
272 323
273 324 wp_die( wp_json_encode( $return ) );
274 325 }
@@ -273,31 +324,37 @@
273 324 wp_die( wp_json_encode( $return ) );
274 325 }
275 326
276 327 /**
277 - * 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.
278 329 *
279 330 * @since 0.6
280 331 *
281 - * @param array $dropdown_args Arguments passed to wp_dropdown_pages
282 - * @param object $post
283 - * @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.
284 335 */
285 336 public function page_attributes_dropdown_pages_args( $dropdown_args, $post ) {
286 - $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
287 - if ( ! $dropdown_args['lang'] ) {
288 - $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;
289 341 }
290 342
343 + if ( ! empty( $language ) ) {
344 + $dropdown_args['lang'] = $language->slug;
345 + }
346 +
291 347 return $dropdown_args;
292 348 }
293 349
294 350 /**
295 - * 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.
296 352 *
297 353 * @since 2.6
298 354 *
299 - * @param object $post Post currently being edited
355 + * @param WP_Post $post the post currently being edited.
356 + * @return void
300 357 */
301 358 public function edit_form_top( $post ) {
302 359 if ( ! $this->model->post->current_user_can_synchronize( $post->ID ) ) {
303 360 ?>