Avoid a race condition when multiple windows are open so that orphaned terms cannot be created by accident.

Adds a unit test.

Props dlh.
Fixes #19205.


git-svn-id: https://develop.svn.wordpress.org/trunk@29196 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2014-07-16 21:51:40 +00:00
parent 0e3aac5016
commit 722930611c
2 changed files with 17 additions and 1 deletions

View File

@ -2418,7 +2418,11 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) {
return new WP_Error('empty_term_name', __('A name is required for this term')); return new WP_Error('empty_term_name', __('A name is required for this term'));
} }
$defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
$args = wp_parse_args($args, $defaults); $args = wp_parse_args( $args, $defaults );
if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) {
return new WP_Error( 'missing_parent', __( 'The selected parent term no longer exists' ) );
}
$args['name'] = $term; $args['name'] = $term;
$args['taxonomy'] = $taxonomy; $args['taxonomy'] = $taxonomy;
$args = sanitize_term($args, $taxonomy, 'db'); $args = sanitize_term($args, $taxonomy, 'db');

View File

@ -690,4 +690,16 @@ class Tests_Term extends WP_UnitTestCase {
$this->assertEquals( $tag_id, $terms[0]->term_id ); $this->assertEquals( $tag_id, $terms[0]->term_id );
$this->assertEquals( 'This description is even more amazing!', $terms[0]->description ); $this->assertEquals( 'This description is even more amazing!', $terms[0]->description );
} }
/**
* @ticket 19205
*/
function test_orphan_category() {
$cat_id1 = $this->factory->category->create();
wp_delete_category( $cat_id1 );
$cat_id2 = $this->factory->category->create( array( 'parent' => $cat_id1 ) );
$this->assertWPError( $cat_id2 );
}
} }