Ticket #29848: 29848.3.patch
File 29848.3.patch, 9.0 KB (added by , 10 years ago) |
---|
-
src/wp-includes/taxonomy.php
diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php index 5846844..723547f 100644
function wp_insert_term( $term, $taxonomy, $args = array() ) { 2447 2447 2448 2448 $term_group = 0; 2449 2449 if ( $args['alias_of'] ) { 2450 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ));2451 if ( $alias->term_group) {2450 $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy ); 2451 if ( ! empty( $alias->term_group ) ) { 2452 2452 // The alias we want is already in a group, so let's use that one. 2453 2453 $term_group = $alias->term_group; 2454 } else { 2455 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. 2456 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; 2457 2458 /** 2459 * Fires immediately before the given terms are edited. 2460 * 2461 * @since 2.9.0 2462 * 2463 * @param int $term_id Term ID. 2464 * @param string $taxonomy Taxonomy slug. 2454 } else if ( ! empty( $alias->term_id ) ) { 2455 /* 2456 * The alias is not in a group, so we create a new one 2457 * and add the alias to it. 2465 2458 */ 2466 do_action( 'edit_terms', $alias->term_id, $taxonomy ); 2467 $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) ); 2459 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; 2468 2460 2469 /** 2470 * Fires immediately after the given terms are edited. 2471 * 2472 * @since 2.9.0 2473 * 2474 * @param int $term_id Term ID 2475 * @param string $taxonomy Taxonomy slug. 2476 */ 2477 do_action( 'edited_terms', $alias->term_id, $taxonomy ); 2461 wp_update_term( $alias->term_id, $taxonomy, array( 2462 'term_group' => $term_group, 2463 ) ); 2478 2464 } 2479 2465 } 2480 2466 … … function wp_update_term( $term_id, $taxonomy, $args = array() ) { 2959 2945 2960 2946 $term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0; 2961 2947 if ( $args['alias_of'] ) { 2962 $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ));2963 if ( $alias->term_group) {2948 $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy ); 2949 if ( ! empty( $alias->term_group ) ) { 2964 2950 // The alias we want is already in a group, so let's use that one. 2965 2951 $term_group = $alias->term_group; 2966 } else { 2967 // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. 2952 } else if ( ! empty( $alias->term_id ) ) { 2953 /* 2954 * The alias is not in a group, so we create a new one 2955 * and add the alias to it. 2956 */ 2968 2957 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; 2969 2958 2970 /** This action is documented in wp-includes/taxonomy.php */ 2971 do_action( 'edit_terms', $alias->term_id, $taxonomy ); 2972 $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) ); 2973 2974 /** This action is documented in wp-includes/taxonomy.php */ 2975 do_action( 'edited_terms', $alias->term_id, $taxonomy ); 2959 wp_update_term( $alias->term_id, $taxonomy, array( 2960 'term_group' => $term_group, 2961 ) ); 2976 2962 } 2977 2963 2978 2964 $parsed_args['term_group'] = $term_group; … … function wp_update_term( $term_id, $taxonomy, $args = array() ) { 3004 2990 return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); 3005 2991 } 3006 2992 3007 /** This action is documented in wp-includes/taxonomy.php */ 2993 /** 2994 * Fires immediately before the given terms are edited. 2995 * 2996 * @since 2.9.0 2997 * 2998 * @param int $term_id Term ID. 2999 * @param string $taxonomy Taxonomy slug. 3000 */ 3008 3001 do_action( 'edit_terms', $term_id, $taxonomy ); 3009 3002 $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); 3010 3003 if ( empty($slug) ) { … … function wp_update_term( $term_id, $taxonomy, $args = array() ) { 3012 3005 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); 3013 3006 } 3014 3007 3015 /** This action is documented in wp-includes/taxonomy.php */ 3008 /** 3009 * Fires immediately after the given terms are edited. 3010 * 3011 * @since 2.9.0 3012 * 3013 * @param int $term_id Term ID 3014 * @param string $taxonomy Taxonomy slug. 3015 */ 3016 3016 do_action( 'edited_terms', $term_id, $taxonomy ); 3017 3017 3018 3018 $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) ); -
tests/phpunit/tests/term.php
diff --git tests/phpunit/tests/term.php tests/phpunit/tests/term.php index 180e87d..f7d8615 100644
class Tests_Term extends WP_UnitTestCase { 417 417 $this->assertFalse( is_wp_error( $term20 ) ); 418 418 } 419 419 420 public function test_wp_insert_term_alias_of_no_term_group() { 421 register_taxonomy( 'wptests_tax', 'post' ); 422 $t1 = $this->factory->term->create( array( 423 'taxonomy' => 'wptests_tax', 424 ) ); 425 $term_1 = get_term( $t1, 'wptests_tax' ); 426 427 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array( 428 'alias_of' => $term_1->slug, 429 ) ); 430 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 431 432 $updated_term_1 = get_term( $term_1->term_id, 'wptests_tax' ); 433 434 $term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 435 _unregister_taxonomy( 'wptests_tax' ); 436 437 $this->assertSame( 0, $term_1->term_group ); 438 $this->assertNotEmpty( $created_term->term_group ); 439 $this->assertSame( $created_term->term_group, $updated_term_1->term_group ); 440 } 441 442 public function test_wp_insert_term_alias_of_existing_term_group() { 443 register_taxonomy( 'wptests_tax', 'post' ); 444 $t1 = $this->factory->term->create( array( 445 'taxonomy' => 'wptests_tax', 446 ) ); 447 $term_1 = get_term( $t1, 'wptests_tax' ); 448 449 $t2 = $this->factory->term->create( array( 450 'taxonomy' => 'wptests_tax', 451 'alias_of' => $term_1->slug, 452 ) ); 453 $term_2 = get_term( $t2, 'wptests_tax' ); 454 455 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array( 456 'alias_of' => $term_2->slug, 457 ) ); 458 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 459 _unregister_taxonomy( 'wptests_tax' ); 460 461 $this->assertNotEmpty( $created_term->term_group ); 462 $this->assertSame( $created_term->term_group, $term_2->term_group ); 463 } 464 465 public function test_wp_insert_term_alias_of_nonexistent_term() { 466 register_taxonomy( 'wptests_tax', 'post' ); 467 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array( 468 'alias_of' => 'foo', 469 ) ); 470 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 471 _unregister_taxonomy( 'wptests_tax' ); 472 473 $this->assertSame( 0, $created_term->term_group ); 474 } 475 420 476 public function test_wp_insert_term_duplicate_name_slug_non_hierarchical() { 421 477 register_taxonomy( 'foo', 'post', array() ); 422 478 … … class Tests_Term extends WP_UnitTestCase { 537 593 $this->assertTrue( in_array( $found['term_id'], $cached_children[ $t ] ) ); 538 594 } 539 595 596 public function test_wp_update_term_alias_of_no_term_group() { 597 register_taxonomy( 'wptests_tax', 'post' ); 598 $t1 = $this->factory->term->create( array( 599 'taxonomy' => 'wptests_tax', 600 ) ); 601 $term_1 = get_term( $t1, 'wptests_tax' ); 602 603 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' ); 604 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array( 605 'alias_of' => $term_1->slug, 606 ) ); 607 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 608 609 $updated_term_1 = get_term( $t1, 'wptests_tax' ); 610 _unregister_taxonomy( 'wptests_tax' ); 611 612 $this->assertSame( 0, $term_1->term_group ); 613 $this->assertNotEmpty( $created_term->term_group ); 614 $this->assertSame( $created_term->term_group, $updated_term_1->term_group ); 615 } 616 617 public function test_wp_update_term_alias_of_existing_term_group() { 618 register_taxonomy( 'wptests_tax', 'post' ); 619 $t1 = $this->factory->term->create( array( 620 'taxonomy' => 'wptests_tax', 621 ) ); 622 $term_1 = get_term( $t1, 'wptests_tax' ); 623 624 $t2 = $this->factory->term->create( array( 625 'taxonomy' => 'wptests_tax', 626 'alias_of' => $term_1->slug, 627 ) ); 628 $term_2 = get_term( $t2, 'wptests_tax' ); 629 630 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' ); 631 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array( 632 'alias_of' => $term_2->slug, 633 ) ); 634 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 635 _unregister_taxonomy( 'wptests_tax' ); 636 637 $this->assertNotEmpty( $created_term->term_group ); 638 $this->assertSame( $created_term->term_group, $term_2->term_group ); 639 } 640 641 public function test_wp_update_term_alias_of_nonexistent_term() { 642 register_taxonomy( 'wptests_tax', 'post' ); 643 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' ); 644 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array( 645 'alias_of' => 'bar', 646 ) ); 647 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 648 _unregister_taxonomy( 'wptests_tax' ); 649 650 $this->assertSame( 0, $created_term->term_group ); 651 } 540 652 /** 541 653 * @ticket 5381 542 654 */