Improve 'alias_of' handling in wp_insert_term() and wp_update_term().
Using get_term_by() rather than direct SQL queries to fetch the alias term fixes a number of issues: * Object cache for aliased term is properly cleared after update. * If the aliased term is in the object cache, it's served from there, saving a database query. * Duplicate 'edit_terms' and 'edited_terms' hooks can be removed. * Fix a PHP notice when the 'alias_of' term is not found. * Prevent the incorrect creation of a new term group for the primary term when the 'alias_of' term is not found. Adds unit tests for 'alias_of' functionality in both functions. Fixes #29848. git-svn-id: https://develop.svn.wordpress.org/trunk@29862 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
c8b53c2d1a
commit
01c529e062
|
@ -2448,34 +2448,20 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) {
|
||||||
|
|
||||||
$term_group = 0;
|
$term_group = 0;
|
||||||
if ( $args['alias_of'] ) {
|
if ( $args['alias_of'] ) {
|
||||||
$alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ) );
|
$alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
|
||||||
if ( $alias->term_group ) {
|
if ( ! empty( $alias->term_group ) ) {
|
||||||
// The alias we want is already in a group, so let's use that one.
|
// The alias we want is already in a group, so let's use that one.
|
||||||
$term_group = $alias->term_group;
|
$term_group = $alias->term_group;
|
||||||
} else {
|
} else if ( ! empty( $alias->term_id ) ) {
|
||||||
// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
|
/*
|
||||||
|
* The alias is not in a group, so we create a new one
|
||||||
|
* and add the alias to it.
|
||||||
|
*/
|
||||||
$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
|
$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
|
||||||
|
|
||||||
/**
|
wp_update_term( $alias->term_id, $taxonomy, array(
|
||||||
* Fires immediately before the given terms are edited.
|
'term_group' => $term_group,
|
||||||
*
|
) );
|
||||||
* @since 2.9.0
|
|
||||||
*
|
|
||||||
* @param int $term_id Term ID.
|
|
||||||
* @param string $taxonomy Taxonomy slug.
|
|
||||||
*/
|
|
||||||
do_action( 'edit_terms', $alias->term_id, $taxonomy );
|
|
||||||
$wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fires immediately after the given terms are edited.
|
|
||||||
*
|
|
||||||
* @since 2.9.0
|
|
||||||
*
|
|
||||||
* @param int $term_id Term ID
|
|
||||||
* @param string $taxonomy Taxonomy slug.
|
|
||||||
*/
|
|
||||||
do_action( 'edited_terms', $alias->term_id, $taxonomy );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2960,20 +2946,20 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) {
|
||||||
|
|
||||||
$term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0;
|
$term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0;
|
||||||
if ( $args['alias_of'] ) {
|
if ( $args['alias_of'] ) {
|
||||||
$alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ) );
|
$alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
|
||||||
if ( $alias->term_group ) {
|
if ( ! empty( $alias->term_group ) ) {
|
||||||
// The alias we want is already in a group, so let's use that one.
|
// The alias we want is already in a group, so let's use that one.
|
||||||
$term_group = $alias->term_group;
|
$term_group = $alias->term_group;
|
||||||
} else {
|
} else if ( ! empty( $alias->term_id ) ) {
|
||||||
// The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
|
/*
|
||||||
|
* The alias is not in a group, so we create a new one
|
||||||
|
* and add the alias to it.
|
||||||
|
*/
|
||||||
$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
|
$term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
|
||||||
|
|
||||||
/** This action is documented in wp-includes/taxonomy.php */
|
wp_update_term( $alias->term_id, $taxonomy, array(
|
||||||
do_action( 'edit_terms', $alias->term_id, $taxonomy );
|
'term_group' => $term_group,
|
||||||
$wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
|
) );
|
||||||
|
|
||||||
/** This action is documented in wp-includes/taxonomy.php */
|
|
||||||
do_action( 'edited_terms', $alias->term_id, $taxonomy );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$parsed_args['term_group'] = $term_group;
|
$parsed_args['term_group'] = $term_group;
|
||||||
|
@ -3005,7 +2991,14 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) {
|
||||||
return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug));
|
return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** This action is documented in wp-includes/taxonomy.php */
|
/**
|
||||||
|
* Fires immediately before the given terms are edited.
|
||||||
|
*
|
||||||
|
* @since 2.9.0
|
||||||
|
*
|
||||||
|
* @param int $term_id Term ID.
|
||||||
|
* @param string $taxonomy Taxonomy slug.
|
||||||
|
*/
|
||||||
do_action( 'edit_terms', $term_id, $taxonomy );
|
do_action( 'edit_terms', $term_id, $taxonomy );
|
||||||
$wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
|
$wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
|
||||||
if ( empty($slug) ) {
|
if ( empty($slug) ) {
|
||||||
|
@ -3013,7 +3006,14 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) {
|
||||||
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
|
$wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/** This action is documented in wp-includes/taxonomy.php */
|
/**
|
||||||
|
* Fires immediately after the given terms are edited.
|
||||||
|
*
|
||||||
|
* @since 2.9.0
|
||||||
|
*
|
||||||
|
* @param int $term_id Term ID
|
||||||
|
* @param string $taxonomy Taxonomy slug.
|
||||||
|
*/
|
||||||
do_action( 'edited_terms', $term_id, $taxonomy );
|
do_action( 'edited_terms', $term_id, $taxonomy );
|
||||||
|
|
||||||
$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) );
|
$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) );
|
||||||
|
|
|
@ -417,6 +417,62 @@ class Tests_Term extends WP_UnitTestCase {
|
||||||
$this->assertFalse( is_wp_error( $term20 ) );
|
$this->assertFalse( is_wp_error( $term20 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_wp_insert_term_alias_of_no_term_group() {
|
||||||
|
register_taxonomy( 'wptests_tax', 'post' );
|
||||||
|
$t1 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
) );
|
||||||
|
$term_1 = get_term( $t1, 'wptests_tax' );
|
||||||
|
|
||||||
|
$created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
|
||||||
|
'alias_of' => $term_1->slug,
|
||||||
|
) );
|
||||||
|
$created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
|
||||||
|
|
||||||
|
$updated_term_1 = get_term( $term_1->term_id, 'wptests_tax' );
|
||||||
|
|
||||||
|
$term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
|
||||||
|
_unregister_taxonomy( 'wptests_tax' );
|
||||||
|
|
||||||
|
$this->assertSame( 0, $term_1->term_group );
|
||||||
|
$this->assertNotEmpty( $created_term->term_group );
|
||||||
|
$this->assertSame( $created_term->term_group, $updated_term_1->term_group );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_wp_insert_term_alias_of_existing_term_group() {
|
||||||
|
register_taxonomy( 'wptests_tax', 'post' );
|
||||||
|
$t1 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
) );
|
||||||
|
$term_1 = get_term( $t1, 'wptests_tax' );
|
||||||
|
|
||||||
|
$t2 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
'alias_of' => $term_1->slug,
|
||||||
|
) );
|
||||||
|
$term_2 = get_term( $t2, 'wptests_tax' );
|
||||||
|
|
||||||
|
$created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
|
||||||
|
'alias_of' => $term_2->slug,
|
||||||
|
) );
|
||||||
|
$created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
|
||||||
|
_unregister_taxonomy( 'wptests_tax' );
|
||||||
|
|
||||||
|
$this->assertNotEmpty( $created_term->term_group );
|
||||||
|
$this->assertSame( $created_term->term_group, $term_2->term_group );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_wp_insert_term_alias_of_nonexistent_term() {
|
||||||
|
register_taxonomy( 'wptests_tax', 'post' );
|
||||||
|
$created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
|
||||||
|
'alias_of' => 'foo',
|
||||||
|
) );
|
||||||
|
$created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
|
||||||
|
_unregister_taxonomy( 'wptests_tax' );
|
||||||
|
|
||||||
|
$this->assertSame( 0, $created_term->term_group );
|
||||||
|
}
|
||||||
|
|
||||||
public function test_wp_insert_term_duplicate_name_slug_non_hierarchical() {
|
public function test_wp_insert_term_duplicate_name_slug_non_hierarchical() {
|
||||||
register_taxonomy( 'foo', 'post', array() );
|
register_taxonomy( 'foo', 'post', array() );
|
||||||
|
|
||||||
|
@ -537,6 +593,62 @@ class Tests_Term extends WP_UnitTestCase {
|
||||||
$this->assertTrue( in_array( $found['term_id'], $cached_children[ $t ] ) );
|
$this->assertTrue( in_array( $found['term_id'], $cached_children[ $t ] ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_wp_update_term_alias_of_no_term_group() {
|
||||||
|
register_taxonomy( 'wptests_tax', 'post' );
|
||||||
|
$t1 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
) );
|
||||||
|
$term_1 = get_term( $t1, 'wptests_tax' );
|
||||||
|
|
||||||
|
$created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );
|
||||||
|
wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(
|
||||||
|
'alias_of' => $term_1->slug,
|
||||||
|
) );
|
||||||
|
$created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
|
||||||
|
|
||||||
|
$updated_term_1 = get_term( $t1, 'wptests_tax' );
|
||||||
|
_unregister_taxonomy( 'wptests_tax' );
|
||||||
|
|
||||||
|
$this->assertSame( 0, $term_1->term_group );
|
||||||
|
$this->assertNotEmpty( $created_term->term_group );
|
||||||
|
$this->assertSame( $created_term->term_group, $updated_term_1->term_group );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_wp_update_term_alias_of_existing_term_group() {
|
||||||
|
register_taxonomy( 'wptests_tax', 'post' );
|
||||||
|
$t1 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
) );
|
||||||
|
$term_1 = get_term( $t1, 'wptests_tax' );
|
||||||
|
|
||||||
|
$t2 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
'alias_of' => $term_1->slug,
|
||||||
|
) );
|
||||||
|
$term_2 = get_term( $t2, 'wptests_tax' );
|
||||||
|
|
||||||
|
$created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );
|
||||||
|
wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(
|
||||||
|
'alias_of' => $term_2->slug,
|
||||||
|
) );
|
||||||
|
$created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
|
||||||
|
_unregister_taxonomy( 'wptests_tax' );
|
||||||
|
|
||||||
|
$this->assertNotEmpty( $created_term->term_group );
|
||||||
|
$this->assertSame( $created_term->term_group, $term_2->term_group );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_wp_update_term_alias_of_nonexistent_term() {
|
||||||
|
register_taxonomy( 'wptests_tax', 'post' );
|
||||||
|
$created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );
|
||||||
|
wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(
|
||||||
|
'alias_of' => 'bar',
|
||||||
|
) );
|
||||||
|
$created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
|
||||||
|
_unregister_taxonomy( 'wptests_tax' );
|
||||||
|
|
||||||
|
$this->assertSame( 0, $created_term->term_group );
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @ticket 5381
|
* @ticket 5381
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue