| @@ -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 terms on admin side |
| 8 | 10 | * |
| 9 | 11 | * @since 1.2 |
| @@ -9,15 +11,8 @@ | ||
| 9 | 11 | * @since 1.2 |
| 10 | 12 | */ |
| 11 | 13 | class PLL_Admin_Filters_Term { |
| 12 | 14 | /** |
| 13 | - * Stores the plugin options. | |
| 14 | - * | |
| 15 | - * @var array | |
| 16 | - */ | |
| 17 | - public $options; | |
| 18 | - | |
| 19 | - /** | |
| 20 | 15 | * @var PLL_Model |
| 21 | 16 | */ |
| 22 | 17 | public $model; |
| 23 | 18 | |
| @@ -40,31 +35,35 @@ | ||
| 40 | 35 | */ |
| 41 | 36 | public $pref_lang; |
| 42 | 37 | |
| 43 | 38 | /** |
| 44 | - * Stores the term name before creating a slug if needed. | |
| 39 | + * Stores the current post_id when bulk editing posts. | |
| 45 | 40 | * |
| 46 | - * @var string | |
| 41 | + * @var int | |
| 47 | 42 | */ |
| 48 | - protected $pre_term_name; | |
| 43 | + protected $post_id = 0; | |
| 49 | 44 | |
| 50 | 45 | /** |
| 51 | - * Stores the current post_id when bulk editing posts. | |
| 46 | + * A reference to the PLL_Admin_Default_Term instance. | |
| 52 | 47 | * |
| 53 | - * @var int | |
| 48 | + * @since 2.8 | |
| 49 | + * | |
| 50 | + * @var PLL_Admin_Default_Term|null | |
| 54 | 51 | */ |
| 55 | - protected $post_id; | |
| 52 | + protected $default_term; | |
| 56 | 53 | |
| 57 | 54 | /** |
| 58 | - * Constructor: setups filters and actions | |
| 55 | + * Constructor: setups filters and actions. | |
| 59 | 56 | * |
| 60 | - * @param object $polylang | |
| 57 | + * @since 1.2 | |
| 58 | + * | |
| 59 | + * @param object $polylang The Polylang object. | |
| 61 | 60 | */ |
| 62 | 61 | public function __construct( &$polylang ) { |
| 63 | - $this->links = &$polylang->links; | |
| 64 | - $this->model = &$polylang->model; | |
| 65 | - $this->options = &$polylang->options; | |
| 66 | - $this->pref_lang = &$polylang->pref_lang; | |
| 62 | + $this->links = &$polylang->links; | |
| 63 | + $this->model = &$polylang->model; | |
| 64 | + $this->pref_lang = &$polylang->pref_lang; | |
| 65 | + $this->default_term = &$polylang->default_term; | |
| 67 | 66 | |
| 68 | 67 | foreach ( $this->model->get_translated_taxonomies() as $tax ) { |
| 69 | 68 | // Adds the language field in the 'Categories' and 'Post Tags' panels |
| 70 | 69 | add_action( $tax . '_add_form_fields', array( $this, 'add_term_form' ) ); |
| @@ -77,19 +76,15 @@ | ||
| 77 | 76 | add_filter( 'wp_dropdown_cats', array( $this, 'wp_dropdown_cats' ) ); |
| 78 | 77 | add_action( 'create_term', array( $this, 'save_term' ), 900, 3 ); |
| 79 | 78 | add_action( 'edit_term', array( $this, 'save_term' ), 900, 3 ); // Late as it may conflict with other plugins, see http://wordpress.org/support/topic/polylang-and-wordpress-seo-by-yoast |
| 80 | 79 | add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); |
| 81 | - add_filter( 'pre_term_name', array( $this, 'pre_term_name' ) ); | |
| 82 | - add_filter( 'pre_term_slug', array( $this, 'pre_term_slug' ), 10, 2 ); | |
| 80 | + add_filter( 'pll_inserted_term_language', array( $this, 'get_inserted_term_language' ) ); | |
| 81 | + add_filter( 'pll_inserted_term_parent', array( $this, 'get_inserted_term_parent' ), 10, 2 ); | |
| 83 | 82 | |
| 84 | 83 | // Ajax response for edit term form |
| 85 | 84 | add_action( 'wp_ajax_term_lang_choice', array( $this, 'term_lang_choice' ) ); |
| 86 | 85 | add_action( 'wp_ajax_pll_terms_not_translated', array( $this, 'ajax_terms_not_translated' ) ); |
| 87 | 86 | |
| 88 | - // Allows to get the default categories in all languages | |
| 89 | - add_filter( 'option_default_category', array( $this, 'option_default_category' ) ); | |
| 90 | - add_action( 'update_option_default_category', array( $this, 'update_option_default_category' ), 10, 2 ); | |
| 91 | - | |
| 92 | 87 | // Updates the translations term ids when splitting a shared term |
| 93 | 88 | add_action( 'split_shared_term', array( $this, 'split_shared_term' ), 10, 4 ); // WP 4.2 |
| 94 | 89 | } |
| 95 | 90 | |
| @@ -185,10 +180,10 @@ | ||
| 185 | 180 | |
| 186 | 181 | $lang = $this->model->term->get_language( $term_id ); |
| 187 | 182 | $lang = empty( $lang ) ? $this->pref_lang : $lang; |
| 188 | 183 | |
| 189 | - // Disable the language dropdown and the translations input fields for default categories to prevent removal | |
| 190 | - $disabled = in_array( get_option( 'default_category' ), $this->model->term->get_translations( $term_id ) ); | |
| 184 | + // Disable the language dropdown and the translations input fields for default terms to prevent removal | |
| 185 | + $disabled = $this->default_term->is_default_term( $term_id ); | |
| 191 | 186 | |
| 192 | 187 | $dropdown = new PLL_Walker_Dropdown(); |
| 193 | 188 | |
| 194 | 189 | $dropdown_html = $dropdown->walk( |
| @@ -251,13 +246,13 @@ | ||
| 251 | 246 | return $output; |
| 252 | 247 | } |
| 253 | 248 | |
| 254 | 249 | /** |
| 255 | - * Stores the current post_id when bulk editing posts for use in save_language and pre_term_slug | |
| 250 | + * Stores the current post_id when bulk editing posts for use in save_language and get_inserted_term_language. | |
| 256 | 251 | * |
| 257 | 252 | * @since 1.7 |
| 258 | 253 | * |
| 259 | - * @param int $post_id | |
| 254 | + * @param int $post_id Post ID. | |
| 260 | 255 | * @return void |
| 261 | 256 | */ |
| 262 | 257 | public function pre_post_update( $post_id ) { |
| 263 | 258 | if ( isset( $_GET['bulk_edit'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| @@ -265,14 +260,14 @@ | ||
| 265 | 260 | } |
| 266 | 261 | } |
| 267 | 262 | |
| 268 | 263 | /** |
| 269 | - * Saves language | |
| 264 | + * Saves the language of a term. | |
| 270 | 265 | * |
| 271 | 266 | * @since 1.5 |
| 272 | 267 | * |
| 273 | - * @param int $term_id | |
| 274 | - * @param string $taxonomy | |
| 268 | + * @param int $term_id Term ID. | |
| 269 | + * @param string $taxonomy Taxonomy name. | |
| 275 | 270 | * @return void |
| 276 | 271 | */ |
| 277 | 272 | protected function save_language( $term_id, $taxonomy ) { |
| 278 | 273 | global $wpdb; |
| @@ -286,9 +281,13 @@ | ||
| 286 | 281 | } else { |
| 287 | 282 | check_admin_referer( 'pll_language', '_pll_nonce' ); // Edit tags or tags metabox |
| 288 | 283 | } |
| 289 | 284 | |
| 290 | - $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ) ); | |
| 285 | + $language = $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ); | |
| 286 | + | |
| 287 | + if ( ! empty( $language ) ) { | |
| 288 | + $this->model->term->set_language( $term_id, $language ); | |
| 289 | + } | |
| 291 | 290 | } |
| 292 | 291 | |
| 293 | 292 | // *Post* bulk edit, in case a new term is created |
| 294 | 293 | elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) { |
| @@ -295,27 +294,36 @@ | ||
| 295 | 294 | check_admin_referer( 'bulk-posts' ); |
| 296 | 295 | |
| 297 | 296 | // Bulk edit does not modify the language |
| 298 | 297 | // So we possibly create a tag in several languages |
| 299 | - if ( -1 == $_GET['inline_lang_choice'] ) { | |
| 300 | - // The language of the current term is set a according to the language of the current post | |
| 301 | - $this->model->term->set_language( $term_id, $this->model->post->get_language( $this->post_id ) ); | |
| 302 | - $term = get_term( $term_id, $taxonomy ); | |
| 298 | + if ( -1 === (int) $_GET['inline_lang_choice'] ) { | |
| 299 | + // The language of the current term is set a according to the language of the current post. | |
| 300 | + $language = $this->model->post->get_language( $this->post_id ); | |
| 303 | 301 | |
| 302 | + if ( empty( $language ) ) { | |
| 303 | + return; | |
| 304 | + } | |
| 305 | + | |
| 306 | + $this->model->term->set_language( $term_id, $language ); | |
| 307 | + $term = get_term( $term_id, $taxonomy ); | |
| 308 | + $terms = array(); | |
| 309 | + | |
| 304 | 310 | // Get all terms with the same name |
| 305 | 311 | // FIXME backward compatibility WP < 4.2 |
| 306 | 312 | // No WP function to get all terms with the exact same name so let's use a custom query |
| 307 | 313 | // $terms = get_terms( $taxonomy, array( 'name' => $term->name, 'hide_empty' => false, 'fields' => 'ids' ) ); should be OK in 4.2 |
| 308 | 314 | // I may need to rework the loop below |
| 309 | - $terms = $wpdb->get_results( | |
| 310 | - $wpdb->prepare( | |
| 311 | - "SELECT t.term_id FROM $wpdb->terms AS t | |
| 312 | - INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id | |
| 313 | - WHERE tt.taxonomy = %s AND t.name = %s", | |
| 314 | - $taxonomy, | |
| 315 | - $term->name | |
| 316 | - ) | |
| 317 | - ); | |
| 315 | + if ( $term instanceof WP_Term ) { | |
| 316 | + $terms = $wpdb->get_results( | |
| 317 | + $wpdb->prepare( | |
| 318 | + "SELECT t.term_id FROM $wpdb->terms AS t | |
| 319 | + INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id | |
| 320 | + WHERE tt.taxonomy = %s AND t.name = %s", | |
| 321 | + $taxonomy, | |
| 322 | + $term->name | |
| 323 | + ) | |
| 324 | + ); | |
| 325 | + } | |
| 318 | 326 | |
| 319 | 327 | // If we have several terms with the same name, they are translations of each other |
| 320 | 328 | if ( count( $terms ) > 1 ) { |
| 321 | 329 | $translations = array(); |
| @@ -327,9 +335,9 @@ | ||
| 327 | 335 | $this->model->term->save_translations( $term_id, $translations ); |
| 328 | 336 | } |
| 329 | 337 | } |
| 330 | 338 | |
| 331 | - else { | |
| 339 | + elseif ( current_user_can( 'edit_term', $term_id ) ) { | |
| 332 | 340 | $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) ) ); |
| 333 | 341 | } |
| 334 | 342 | } |
| 335 | 343 | |
| @@ -339,31 +347,21 @@ | ||
| 339 | 347 | isset( $_POST['action'] ) && 'inline-save' == $_POST['action'] ? 'inlineeditnonce' : 'taxinlineeditnonce', // Post quick edit or tag quick edit ? |
| 340 | 348 | '_inline_edit' |
| 341 | 349 | ); |
| 342 | 350 | |
| 343 | - $old_lang = $this->model->term->get_language( $term_id ); // Stores the old language | |
| 344 | - $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ); // New language | |
| 345 | - $translations = $this->model->term->get_translations( $term_id ); | |
| 346 | - | |
| 347 | - // Checks if the new language already exists in the translation group | |
| 348 | - if ( $old_lang && $old_lang->slug != $lang->slug ) { | |
| 349 | - if ( array_key_exists( $lang->slug, $translations ) ) { | |
| 350 | - $this->model->term->delete_translation( $term_id ); | |
| 351 | - } | |
| 352 | - | |
| 353 | - elseif ( array_key_exists( $old_lang->slug, $translations ) ) { | |
| 354 | - unset( $translations[ $old_lang->slug ] ); | |
| 355 | - $this->model->term->save_translations( $term_id, $translations ); | |
| 356 | - } | |
| 357 | - } | |
| 358 | - | |
| 359 | - $this->model->term->set_language( $term_id, $lang ); // Set new language | |
| 351 | + $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ); | |
| 352 | + $this->model->term->set_language( $term_id, $lang ); | |
| 360 | 353 | } |
| 361 | 354 | |
| 362 | 355 | // Edit post |
| 363 | 356 | elseif ( isset( $_POST['post_lang_choice'] ) ) { // FIXME should be useless now |
| 364 | 357 | check_admin_referer( 'pll_language', '_pll_nonce' ); |
| 365 | - $this->model->term->set_language( $term_id, $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) ) ); | |
| 358 | + | |
| 359 | + $language = $this->model->get_language( sanitize_key( $_POST['post_lang_choice'] ) ); | |
| 360 | + | |
| 361 | + if ( ! empty( $language ) ) { | |
| 362 | + $this->model->term->set_language( $term_id, $language ); | |
| 363 | + } | |
| 366 | 364 | } |
| 367 | 365 | } |
| 368 | 366 | |
| 369 | 367 | /** |
| @@ -370,9 +368,9 @@ | ||
| 370 | 368 | * Save translations from our form. |
| 371 | 369 | * |
| 372 | 370 | * @since 1.5 |
| 373 | 371 | * |
| 374 | - * @param int $term_id The term id of teh term being saved. | |
| 372 | + * @param int $term_id The term id of the term being saved. | |
| 375 | 373 | * @return int[] The array of translated term ids. |
| 376 | 374 | */ |
| 377 | 375 | protected function save_translations( $term_id ) { |
| 378 | 376 | // Security check as 'wp_update_term' can be called from outside WP admin. |
| @@ -398,11 +396,11 @@ | ||
| 398 | 396 | * Saves language and translations |
| 399 | 397 | * |
| 400 | 398 | * @since 0.1 |
| 401 | 399 | * |
| 402 | - * @param int $term_id | |
| 403 | - * @param int $tt_id term taxonomy id | |
| 404 | - * @param string $taxonomy | |
| 400 | + * @param int $term_id Term ID. | |
| 401 | + * @param int $tt_id Term taxonomy ID. | |
| 402 | + * @param string $taxonomy Taxonomy name. | |
| 405 | 403 | * @return void |
| 406 | 404 | */ |
| 407 | 405 | public function save_term( $term_id, $tt_id, $taxonomy ) { |
| 408 | 406 | // Does nothing except on taxonomies which are filterable |
| @@ -428,60 +426,8 @@ | ||
| 428 | 426 | } |
| 429 | 427 | } |
| 430 | 428 | |
| 431 | 429 | /** |
| 432 | - * Stores the term name for use in pre_term_slug | |
| 433 | - * | |
| 434 | - * @since 0.9.5 | |
| 435 | - * | |
| 436 | - * @param string $name term name | |
| 437 | - * @return string unmodified term name | |
| 438 | - */ | |
| 439 | - public function pre_term_name( $name ) { | |
| 440 | - return $this->pre_term_name = $name; | |
| 441 | - } | |
| 442 | - | |
| 443 | - /** | |
| 444 | - * Creates the term slug in case the term already exists in another language | |
| 445 | - * | |
| 446 | - * @since 0.9.5 | |
| 447 | - * | |
| 448 | - * @param string $slug | |
| 449 | - * @param string $taxonomy | |
| 450 | - * @return string | |
| 451 | - */ | |
| 452 | - public function pre_term_slug( $slug, $taxonomy ) { | |
| 453 | - $name = sanitize_title( $this->pre_term_name ); | |
| 454 | - | |
| 455 | - // If the term already exists in another language | |
| 456 | - if ( ! $slug && $this->model->is_translated_taxonomy( $taxonomy ) && term_exists( $name, $taxonomy ) ) { | |
| 457 | - if ( isset( $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 458 | - $lang = $this->model->get_language( sanitize_key( $_POST['term_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 459 | - } | |
| 460 | - | |
| 461 | - elseif ( isset( $_POST['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 462 | - $lang = $this->model->get_language( sanitize_key( $_POST['inline_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 463 | - } | |
| 464 | - | |
| 465 | - // *Post* bulk edit, in case a new term is created | |
| 466 | - elseif ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 467 | - // Bulk edit does not modify the language | |
| 468 | - if ( -1 == $_GET['inline_lang_choice'] ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 469 | - $lang = $this->model->post->get_language( $this->post_id ); | |
| 470 | - } else { | |
| 471 | - $lang = $this->model->get_language( sanitize_key( $_GET['inline_lang_choice'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 472 | - } | |
| 473 | - } | |
| 474 | - | |
| 475 | - if ( ! empty( $lang ) && ! $this->model->term_exists_by_slug( $name, $lang, $taxonomy ) ) { | |
| 476 | - $slug = $name . '-' . $lang->slug; | |
| 477 | - } | |
| 478 | - } | |
| 479 | - | |
| 480 | - return $slug; | |
| 481 | - } | |
| 482 | - | |
| 483 | - /** | |
| 484 | 430 | * Ajax response for edit term form |
| 485 | 431 | * |
| 486 | 432 | * @since 0.2 |
| 487 | 433 | * |
| @@ -533,9 +479,12 @@ | ||
| 533 | 479 | if ( current_user_can( $tax->cap->edit_terms ) ) { |
| 534 | 480 | $args = array_merge( $args, array( 'link' => 'edit' ) ); |
| 535 | 481 | } |
| 536 | 482 | |
| 537 | - if ( $tag_cloud = wp_tag_cloud( $args ) ) { | |
| 483 | + $tag_cloud = wp_tag_cloud( $args ); | |
| 484 | + | |
| 485 | + if ( ! empty( $tag_cloud ) ) { | |
| 486 | + /** @phpstan-var non-falsy-string $tag_cloud */ | |
| 538 | 487 | $html = sprintf( '<div class="tagcloud"><h2>%1$s</h2>%2$s</div>', esc_html( $tax->labels->popular_items ), $tag_cloud ); |
| 539 | 488 | $x->Add( array( 'what' => 'tag_cloud', 'data' => $html ) ); |
| 540 | 489 | } |
| 541 | 490 | } |
| @@ -547,9 +496,9 @@ | ||
| 547 | 496 | $x->send(); |
| 548 | 497 | } |
| 549 | 498 | |
| 550 | 499 | /** |
| 551 | - * Ajax response for input in translation autocomplete input box | |
| 500 | + * Ajax response for input in translation autocomplete input box. | |
| 552 | 501 | * |
| 553 | 502 | * @since 1.5 |
| 554 | 503 | * |
| 555 | 504 | * @return void |
| @@ -582,9 +531,9 @@ | ||
| 582 | 531 | $terms = array( get_term( $term_id, $taxonomy ) ); |
| 583 | 532 | } |
| 584 | 533 | |
| 585 | 534 | // It is more efficient to use one common query for all languages as soon as there are more than 2. |
| 586 | - $all_terms = get_terms( $taxonomy, 'hide_empty=0&lang=0&name__like=' . $s ); | |
| 535 | + $all_terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false, 'lang' => '', 'name__like' => $s ) ); | |
| 587 | 536 | if ( is_array( $all_terms ) ) { |
| 588 | 537 | foreach ( $all_terms as $term ) { |
| 589 | 538 | $lang = $this->model->term->get_language( $term->term_id ); |
| 590 | 539 | |
| @@ -595,69 +544,33 @@ | ||
| 595 | 544 | } |
| 596 | 545 | |
| 597 | 546 | // Format the ajax response. |
| 598 | 547 | foreach ( $terms as $term ) { |
| 599 | - if ( $term instanceof WP_Term ) { | |
| 600 | - $return[] = array( | |
| 601 | - 'id' => $term->term_id, | |
| 602 | - 'value' => rtrim( // Trim the seperator added at the end by WP. | |
| 603 | - get_term_parents_list( | |
| 604 | - $term->term_id, | |
| 605 | - $term->taxonomy, | |
| 606 | - array( | |
| 607 | - 'separator' => ' > ', | |
| 608 | - 'link' => false, | |
| 609 | - ) | |
| 610 | - ), | |
| 611 | - ' >' | |
| 612 | - ), | |
| 613 | - 'link' => $this->links->edit_term_translation_link( $term->term_id, $term->taxonomy, $post_type ), | |
| 614 | - ); | |
| 548 | + if ( ! $term instanceof WP_Term ) { | |
| 549 | + continue; | |
| 615 | 550 | } |
| 616 | - } | |
| 617 | 551 | |
| 618 | - wp_die( wp_json_encode( $return ) ); | |
| 619 | - } | |
| 552 | + $parents_list = get_term_parents_list( | |
| 553 | + $term->term_id, | |
| 554 | + $term->taxonomy, | |
| 555 | + array( | |
| 556 | + 'separator' => ' > ', | |
| 557 | + 'link' => false, | |
| 558 | + ) | |
| 559 | + ); | |
| 620 | 560 | |
| 621 | - /** | |
| 622 | - * Filters the default category in note below the category list table and in settings->writing dropdown | |
| 623 | - * | |
| 624 | - * @since 1.2 | |
| 625 | - * | |
| 626 | - * @param int $value | |
| 627 | - * @return int | |
| 628 | - */ | |
| 629 | - public function option_default_category( $value ) { | |
| 630 | - if ( isset( $this->pref_lang ) && $tr = $this->model->term->get( $value, $this->pref_lang ) ) { | |
| 631 | - $value = $tr; | |
| 632 | - } | |
| 633 | - return $value; | |
| 634 | - } | |
| 561 | + if ( ! is_string( $parents_list ) ) { | |
| 562 | + continue; | |
| 563 | + } | |
| 635 | 564 | |
| 636 | - /** | |
| 637 | - * Checks if the new default category is translated in all languages | |
| 638 | - * If not, create the translations | |
| 639 | - * | |
| 640 | - * @since 1.7 | |
| 641 | - * | |
| 642 | - * @param int $old_value | |
| 643 | - * @param int $value | |
| 644 | - * @return void | |
| 645 | - */ | |
| 646 | - public function update_option_default_category( $old_value, $value ) { | |
| 647 | - $default_cat_lang = $this->model->term->get_language( $value ); | |
| 648 | - | |
| 649 | - // Assign a default language to default category | |
| 650 | - if ( ! $default_cat_lang ) { | |
| 651 | - $default_cat_lang = $this->model->get_language( $this->options['default_lang'] ); | |
| 652 | - $this->model->term->set_language( (int) $value, $default_cat_lang ); | |
| 565 | + $return[] = array( | |
| 566 | + 'id' => $term->term_id, | |
| 567 | + 'value' => rtrim( $parents_list, ' >' ), // Trim the separator added at the end by WP. | |
| 568 | + 'link' => $this->links->edit_term_translation_link( $term->term_id, $term->taxonomy, $post_type ), | |
| 569 | + ); | |
| 653 | 570 | } |
| 654 | 571 | |
| 655 | - foreach ( $this->model->get_languages_list() as $language ) { | |
| 656 | - if ( $language->slug != $default_cat_lang->slug && ! $this->model->term->get_translation( $value, $language ) ) { | |
| 657 | - $this->model->create_default_category( $language ); | |
| 658 | - } | |
| 659 | - } | |
| 572 | + wp_die( wp_json_encode( $return ) ); | |
| 660 | 573 | } |
| 661 | 574 | |
| 662 | 575 | /** |
| 663 | 576 | * Updates the translations term ids when splitting a shared term |
| @@ -664,12 +577,12 @@ | ||
| 664 | 577 | * Splits translations if these are shared terms too |
| 665 | 578 | * |
| 666 | 579 | * @since 1.7 |
| 667 | 580 | * |
| 668 | - * @param int $term_id Shared term_id | |
| 669 | - * @param int $new_term_id | |
| 670 | - * @param int $term_taxonomy_id | |
| 671 | - * @param string $taxonomy | |
| 581 | + * @param int $term_id ID of the formerly shared term. | |
| 582 | + * @param int $new_term_id ID of the new term created for the $term_taxonomy_id. | |
| 583 | + * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split. | |
| 584 | + * @param string $taxonomy Taxonomy name. | |
| 672 | 585 | * @return void |
| 673 | 586 | */ |
| 674 | 587 | public function split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) { |
| 675 | 588 | if ( ! $this->model->is_translated_taxonomy( $taxonomy ) ) { |
| @@ -695,18 +608,111 @@ | ||
| 695 | 608 | $translations[ $key ] = $new_term_id; |
| 696 | 609 | } |
| 697 | 610 | else { |
| 698 | 611 | $tr_term = get_term( $tr_id, $taxonomy ); |
| 699 | - $translations[ $key ] = _split_shared_term( $tr_id, $tr_term->term_taxonomy_id ); | |
| 700 | 612 | |
| 613 | + if ( ! $tr_term instanceof WP_Term ) { | |
| 614 | + continue; | |
| 615 | + } | |
| 616 | + | |
| 617 | + $split_term_id = _split_shared_term( $tr_id, $tr_term->term_taxonomy_id ); | |
| 618 | + | |
| 619 | + if ( is_int( $split_term_id ) ) { | |
| 620 | + $translations[ $key ] = $split_term_id; | |
| 621 | + } else { | |
| 622 | + $translations[ $key ] = $tr_id; | |
| 623 | + } | |
| 624 | + | |
| 701 | 625 | // Hack translation ids sent by the form to avoid overwrite in PLL_Admin_Filters_Term::save_translations |
| 702 | 626 | if ( isset( $_POST['term_tr_lang'][ $key ] ) && $_POST['term_tr_lang'][ $key ] == $tr_id ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 703 | 627 | $_POST['term_tr_lang'][ $key ] = $translations[ $key ]; |
| 704 | 628 | } |
| 705 | 629 | } |
| 630 | + | |
| 706 | 631 | $this->model->term->set_language( $translations[ $key ], $key ); |
| 707 | 632 | } |
| 708 | 633 | |
| 709 | 634 | $this->model->term->save_translations( $new_term_id, $translations ); |
| 710 | 635 | $avoid_recursion = false; |
| 636 | + } | |
| 637 | + | |
| 638 | + /** | |
| 639 | + * Returns the language for subsequently inserted term in admin. | |
| 640 | + * | |
| 641 | + * @since 3.3 | |
| 642 | + * | |
| 643 | + * @param PLL_Language|null $lang Term language object if found, null otherwise. | |
| 644 | + * @return PLL_Language|null Language object, null if none found. | |
| 645 | + */ | |
| 646 | + public function get_inserted_term_language( $lang ) { | |
| 647 | + if ( $lang instanceof PLL_Language ) { | |
| 648 | + return $lang; | |
| 649 | + } | |
| 650 | + | |
| 651 | + if ( ! empty( $_POST['term_lang_choice'] ) && is_string( $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 652 | + $lang_slug = sanitize_key( $_POST['term_lang_choice'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 653 | + $lang = $this->model->get_language( $lang_slug ); | |
| 654 | + return $lang instanceof PLL_Language ? $lang : null; | |
| 655 | + } | |
| 656 | + | |
| 657 | + if ( ! empty( $_POST['inline_lang_choice'] ) && is_string( $_POST['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 658 | + $lang_slug = sanitize_key( $_POST['inline_lang_choice'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 659 | + $lang = $this->model->get_language( $lang_slug ); | |
| 660 | + return $lang instanceof PLL_Language ? $lang : null; | |
| 661 | + } | |
| 662 | + | |
| 663 | + // *Post* bulk edit, in case a new term is created | |
| 664 | + if ( isset( $_GET['bulk_edit'], $_GET['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 665 | + // Bulk edit does not modify the language | |
| 666 | + if ( -1 === (int) $_GET['inline_lang_choice'] ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 667 | + $lang = $this->model->post->get_language( $this->post_id ); | |
| 668 | + return $lang instanceof PLL_Language ? $lang : null; | |
| 669 | + } elseif ( is_string( $_GET['inline_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 670 | + $lang_slug = sanitize_key( $_GET['inline_lang_choice'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 671 | + $lang = $this->model->get_language( $lang_slug ); | |
| 672 | + return $lang instanceof PLL_Language ? $lang : null; | |
| 673 | + } | |
| 674 | + } | |
| 675 | + | |
| 676 | + // Special cases for default categories as the select is disabled. | |
| 677 | + $default_term = get_option( 'default_category' ); | |
| 678 | + | |
| 679 | + if ( ! is_numeric( $default_term ) ) { | |
| 680 | + return null; | |
| 681 | + } | |
| 682 | + | |
| 683 | + if ( ! empty( $_POST['tag_ID'] ) && in_array( (int) $default_term, $this->model->term->get_translations( (int) $_POST['tag_ID'] ), true ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 684 | + $lang = $this->model->term->get_language( (int) $_POST['tag_ID'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 685 | + return $lang instanceof PLL_Language ? $lang : null; | |
| 686 | + } | |
| 687 | + | |
| 688 | + if ( ! empty( $_POST['tax_ID'] ) && in_array( (int) $default_term, $this->model->term->get_translations( (int) $_POST['tax_ID'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 689 | + $lang = $this->model->term->get_language( (int) $_POST['tax_ID'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 690 | + return $lang instanceof PLL_Language ? $lang : null; | |
| 691 | + } | |
| 692 | + | |
| 693 | + return null; | |
| 694 | + } | |
| 695 | + | |
| 696 | + /** | |
| 697 | + * Filters the subsequently inserted term parent in admin. | |
| 698 | + * | |
| 699 | + * @since 3.3 | |
| 700 | + * | |
| 701 | + * @param int $parent Parent term ID, 0 if none found. | |
| 702 | + * @param string $taxonomy Term taxonomy. | |
| 703 | + * @return int Parent term ID if found, 0 otherwise. | |
| 704 | + */ | |
| 705 | + public function get_inserted_term_parent( $parent, $taxonomy ) { | |
| 706 | + if ( $parent ) { | |
| 707 | + return $parent; | |
| 708 | + } | |
| 709 | + | |
| 710 | + if ( isset( $_POST['parent'], $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 711 | + $parent = intval( $_POST['parent'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 712 | + } elseif ( isset( $_POST[ "new{$taxonomy}_parent" ], $_POST['term_lang_choice'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 713 | + $parent = intval( $_POST[ "new{$taxonomy}_parent" ] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 714 | + } | |
| 715 | + | |
| 716 | + return $parent; | |
| 711 | 717 | } |
| 712 | 718 | } |