From 01c529e062331ca7c2222b93514663aa822c2632 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 9 Oct 2014 02:31:35 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy.php | 72 +++++++++++----------- tests/phpunit/tests/term.php | 112 +++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 36 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 12cd91ca35..fe996b1f73 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2448,34 +2448,20 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { $term_group = 0; if ( $args['alias_of'] ) { - $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ) ); - if ( $alias->term_group ) { + $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy ); + if ( ! empty( $alias->term_group ) ) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; - } else { - // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. + } else if ( ! empty( $alias->term_id ) ) { + /* + * 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; - /** - * 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', $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 ); + wp_update_term( $alias->term_id, $taxonomy, array( + 'term_group' => $term_group, + ) ); } } @@ -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; if ( $args['alias_of'] ) { - $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ) ); - if ( $alias->term_group ) { + $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy ); + if ( ! empty( $alias->term_group ) ) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; - } else { - // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. + } else if ( ! empty( $alias->term_id ) ) { + /* + * 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; - /** This action is documented in wp-includes/taxonomy.php */ - do_action( 'edit_terms', $alias->term_id, $taxonomy ); - $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 ); + wp_update_term( $alias->term_id, $taxonomy, array( + '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)); } - /** 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 ); $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); 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' ) ); } - /** 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 ); $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) ); diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 180e87d549..f7d8615820 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -417,6 +417,62 @@ class Tests_Term extends WP_UnitTestCase { $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() { 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 ] ) ); } + 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 */