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