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