Ticket #29848: 29848.2.patch
File 29848.2.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..4b1f0a9 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 2454 } else if ( ! empty( $alias->term_id ) ) { 2458 2455 /** 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. 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 2576157..ed3d4ca 100644
class Tests_Term extends WP_UnitTestCase { 244 244 $this->assertEquals( 0, term_exists(NULL) ); 245 245 } 246 246 247 public function test_wp_insert_term_alias_of_no_term_group() { 248 register_taxonomy( 'wptests_tax', 'post' ); 249 $t1 = $this->factory->term->create( array( 250 'taxonomy' => 'wptests_tax', 251 ) ); 252 $term_1 = get_term( $t1, 'wptests_tax' ); 253 254 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array( 255 'alias_of' => $term_1->slug, 256 ) ); 257 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 258 259 $updated_term_1 = get_term( $term_1->term_id, 'wptests_tax' ); 260 261 $term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 262 _unregister_taxonomy( 'wptests_tax' ); 263 264 $this->assertSame( 0, $term_1->term_group ); 265 $this->assertNotEmpty( $created_term->term_group ); 266 $this->assertSame( $created_term->term_group, $updated_term_1->term_group ); 267 } 268 269 public function test_wp_insert_term_alias_of_existing_term_group() { 270 register_taxonomy( 'wptests_tax', 'post' ); 271 $t1 = $this->factory->term->create( array( 272 'taxonomy' => 'wptests_tax', 273 ) ); 274 $term_1 = get_term( $t1, 'wptests_tax' ); 275 276 $t2 = $this->factory->term->create( array( 277 'taxonomy' => 'wptests_tax', 278 'alias_of' => $term_1->slug, 279 ) ); 280 $term_2 = get_term( $t2, 'wptests_tax' ); 281 282 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array( 283 'alias_of' => $term_2->slug, 284 ) ); 285 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 286 _unregister_taxonomy( 'wptests_tax' ); 287 288 $this->assertNotEmpty( $created_term->term_group ); 289 $this->assertSame( $created_term->term_group, $term_2->term_group ); 290 } 291 292 public function test_wp_insert_term_alias_of_nonexistent_term() { 293 register_taxonomy( 'wptests_tax', 'post' ); 294 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array( 295 'alias_of' => 'foo', 296 ) ); 297 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 298 _unregister_taxonomy( 'wptests_tax' ); 299 300 $this->assertSame( 0, $created_term->term_group ); 301 } 302 247 303 public function test_wp_insert_term_duplicate_name_slug_non_hierarchical() { 248 304 register_taxonomy( 'foo', 'post', array() ); 249 305 … … class Tests_Term extends WP_UnitTestCase { 315 371 $this->assertEquals( $existing_term, $found->get_error_data() ); 316 372 } 317 373 374 public function test_wp_update_term_alias_of_no_term_group() { 375 register_taxonomy( 'wptests_tax', 'post' ); 376 $t1 = $this->factory->term->create( array( 377 'taxonomy' => 'wptests_tax', 378 ) ); 379 $term_1 = get_term( $t1, 'wptests_tax' ); 380 381 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' ); 382 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array( 383 'alias_of' => $term_1->slug, 384 ) ); 385 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 386 387 $updated_term_1 = get_term( $t1, 'wptests_tax' ); 388 _unregister_taxonomy( 'wptests_tax' ); 389 390 $this->assertSame( 0, $term_1->term_group ); 391 $this->assertNotEmpty( $created_term->term_group ); 392 $this->assertSame( $created_term->term_group, $updated_term_1->term_group ); 393 } 394 395 public function test_wp_update_term_alias_of_existing_term_group() { 396 register_taxonomy( 'wptests_tax', 'post' ); 397 $t1 = $this->factory->term->create( array( 398 'taxonomy' => 'wptests_tax', 399 ) ); 400 $term_1 = get_term( $t1, 'wptests_tax' ); 401 402 $t2 = $this->factory->term->create( array( 403 'taxonomy' => 'wptests_tax', 404 'alias_of' => $term_1->slug, 405 ) ); 406 $term_2 = get_term( $t2, 'wptests_tax' ); 407 408 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' ); 409 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array( 410 'alias_of' => $term_2->slug, 411 ) ); 412 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 413 _unregister_taxonomy( 'wptests_tax' ); 414 415 $this->assertNotEmpty( $created_term->term_group ); 416 $this->assertSame( $created_term->term_group, $term_2->term_group ); 417 } 418 419 public function test_wp_update_term_alias_of_nonexistent_term() { 420 register_taxonomy( 'wptests_tax', 'post' ); 421 $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' ); 422 wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array( 423 'alias_of' => 'bar', 424 ) ); 425 $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' ); 426 _unregister_taxonomy( 'wptests_tax' ); 427 428 $this->assertSame( 0, $created_term->term_group ); 429 } 318 430 /** 319 431 * @ticket 5381 320 432 */