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