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