Reinstate term splitting on term update.

Originally introduced in [30241] and reverted in [30336], term splitting is
back and better than ever. Now with *more unit tests* and *improved treatment
of child terms*!

See #30335.

git-svn-id: https://develop.svn.wordpress.org/trunk@30344 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-11-14 21:26:48 +00:00
parent 1b1f18baf4
commit ab96cf6614
3 changed files with 259 additions and 0 deletions

View File

@ -3386,6 +3386,12 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) {
$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) );
// Check whether this is a shared term that needs splitting.
$_term_id = _split_shared_term( $term_id, $tt_id );
if ( ! is_wp_error( $_term_id ) ) {
$term_id = $_term_id;
}
/**
* Fires immediately before the given terms are edited.
*
@ -4039,6 +4045,85 @@ function _update_generic_term_count( $terms, $taxonomy ) {
}
}
/**
* Create a new term for a term_taxonomy item that currently shares its term.
*
* @since 4.1.0
* @access private
*
* @param int $term_id ID of the shared term.
* @param int $term_taxonomy_id ID of the term taxonomy item to receive a new term.
* @param array $shared_tts Sibling term taxonomies, used for busting caches.
* @return int Term ID.
*/
function _split_shared_term( $term_id, $term_taxonomy_id ) {
global $wpdb;
// Don't try to split terms if database schema does not support shared slugs.
$current_db_version = get_option( 'db_version' );
if ( $current_db_version < 30133 ) {
return $term_id;
}
// If there are no shared term_taxonomy rows, there's nothing to do here.
$shared_tt_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy tt WHERE tt.term_id = %d AND tt.term_taxonomy_id != %d", $term_id, $term_taxonomy_id ) );
if ( ! $shared_tt_count ) {
return $term_id;
}
// Pull up data about the currently shared slug, which we'll use to populate the new one.
$shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );
$new_term_data = array(
'name' => $shared_term->name,
'slug' => $shared_term->slug,
'term_group' => $shared_term->term_group,
);
if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) {
return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error );
}
$new_term_id = (int) $wpdb->insert_id;
// Update the existing term_taxonomy to point to the newly created term.
$wpdb->update( $wpdb->term_taxonomy,
array( 'term_id' => $new_term_id ),
array( 'term_taxonomy_id' => $term_taxonomy_id )
);
// Reassign child terms to the new parent.
$term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
$children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND parent = %d", $term_taxonomy->taxonomy, $term_id ) );
foreach ( $children_tt_ids as $child_tt_id ) {
$wpdb->update( $wpdb->term_taxonomy,
array( 'parent' => $new_term_id ),
array( 'term_taxonomy_id' => $child_tt_id )
);
clean_term_cache( $term_id, $term_taxonomy->taxonomy );
}
// Clean the cache for term taxonomies formerly shared with the current term.
$shared_term_taxonomies = $wpdb->get_row( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
foreach ( (array) $shared_term_taxonomies as $shared_term_taxonomy ) {
clean_term_cache( $term_id, $shared_term_taxonomy );
}
/**
* Fires after a previously shared taxonomy term is split into two separate terms.
*
* @since 4.1.0
*
* @param int $term_id ID of the formerly shared term.
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
*/
do_action( 'split_shared_term', $term_id, $new_term_id, $term_taxonomy_id );
return $new_term_id;
}
/**
* Generate a permalink for a taxonomy term archive.
*

View File

@ -690,6 +690,88 @@ class Tests_Term extends WP_UnitTestCase {
$this->assertSame( 'duplicate_term_slug', $updated->get_error_code() );
}
/**
* @ticket 5809
*/
public function test_wp_update_term_should_split_shared_term() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
register_taxonomy( 'wptests_tax_2', 'post' );
$t1 = wp_insert_term( 'Foo', 'wptests_tax' );
$t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
// Manually modify because split terms shouldn't naturally occur.
$wpdb->update( $wpdb->term_taxonomy,
array( 'term_id' => $t1['term_id'] ),
array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
array( '%d' ),
array( '%d' )
);
$posts = $this->factory->post->create_many( 2 );
wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );
wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );
// Verify that the terms are shared.
$t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );
$t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );
$this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );
wp_update_term( $t2_terms[0]->term_id, 'wptests_tax_2', array(
'name' => 'New Foo',
) );
$t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );
$t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );
$this->assertNotEquals( $t1_terms[0]->term_id, $t2_terms[0]->term_id );
}
/**
* @ticket 5809
*/
public function test_wp_update_term_should_not_split_shared_term_before_410_schema_change() {
global $wpdb;
$db_version = get_option( 'db_version' );
update_option( 'db_version', 30055 );
register_taxonomy( 'wptests_tax', 'post' );
register_taxonomy( 'wptests_tax_2', 'post' );
$t1 = wp_insert_term( 'Foo', 'wptests_tax' );
$t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
// Manually modify because split terms shouldn't naturally occur.
$wpdb->update( $wpdb->term_taxonomy,
array( 'term_id' => $t1['term_id'] ),
array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
array( '%d' ),
array( '%d' )
);
$posts = $this->factory->post->create_many( 2 );
wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );
wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );
// Verify that the term is shared.
$t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );
$t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );
$this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );
wp_update_term( $t2_terms[0]->term_id, 'wptests_tax_2', array(
'name' => 'New Foo',
) );
// Term should still be shared.
$t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );
$t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );
$this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );
update_option( 'db_version', $db_version );
}
public function test_wp_update_term_alias_of_no_term_group() {
register_taxonomy( 'wptests_tax', 'post' );
$t1 = $this->factory->term->create( array(

View File

@ -0,0 +1,92 @@
<?php
/**
* @group taxonomy
*/
class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
protected $tt_ids = array();
public function setUp() {
global $wpdb;
parent::setUp();
register_taxonomy( 'wptests_tax', 'post' );
register_taxonomy( 'wptests_tax_2', 'post', array(
'hierarchical' => true,
) );
register_taxonomy( 'wptests_tax_3', 'post' );
$t1 = wp_insert_term( 'Foo', 'wptests_tax' );
$t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
$t3 = wp_insert_term( 'Foo', 'wptests_tax_3' );
// Manually modify because split terms shouldn't naturally occur.
$wpdb->update( $wpdb->term_taxonomy,
array( 'term_id' => $t1['term_id'] ),
array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
array( '%d' ),
array( '%d' )
);
$wpdb->update( $wpdb->term_taxonomy,
array( 'term_id' => $t1['term_id'] ),
array( 'term_taxonomy_id' => $t3['term_taxonomy_id'] ),
array( '%d' ),
array( '%d' )
);
$t2_child = wp_insert_term( 'Foo Child', 'wptests_tax_2', array(
'parent' => $t1['term_id'],
) );
$this->tt_ids = array(
't1' => $t1['term_taxonomy_id'],
't2' => $t2['term_taxonomy_id'],
't3' => $t3['term_taxonomy_id'],
't2_child' => $t2_child['term_taxonomy_id'],
);
_split_shared_term( $t1['term_id'], $t2['term_taxonomy_id'] );
_split_shared_term( $t1['term_id'], $t3['term_taxonomy_id'] );
}
/**
* @ticket 5809
*/
public function test_should_create_new_term_ids() {
$t1_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t1'], 'wptests_tax' );
$t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
$t3_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t3'], 'wptests_tax_3' );
$this->assertNotEquals( $t1_term->term_id, $t2_term->term_id );
$this->assertNotEquals( $t1_term->term_id, $t3_term->term_id );
$this->assertNotEquals( $t2_term->term_id, $t3_term->term_id );
}
/**
* @ticket 5809
*/
public function test_should_retain_child_terms_when_using_get_terms_parent() {
$t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
$children = get_terms( 'wptests_tax_2', array(
'parent' => $t2_term->term_id,
'hide_empty' => false,
) );
$this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
}
/**
* @ticket 5809
*/
public function test_should_retain_child_terms_when_using_get_terms_child_of() {
$t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
$children = get_terms( 'wptests_tax_2', array(
'child_of' => $t2_term->term_id,
'hide_empty' => false,
) );
$this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
}
}