From 3b6ff031365b3ab34951682c8a63a4a102190901 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 13 Apr 2015 01:20:57 +0000 Subject: [PATCH] `wp_update_term()` should check if `get_term()` returned null. props dlh. fixes #31954. git-svn-id: https://develop.svn.wordpress.org/trunk@32117 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 14 ++++++++++---- tests/phpunit/tests/term/wpUpdateTerm.php | 12 ++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index c1c3f99679..cce486786b 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -3365,16 +3365,22 @@ function wp_unique_term_slug($slug, $term) { function wp_update_term( $term_id, $taxonomy, $args = array() ) { global $wpdb; - if ( ! taxonomy_exists($taxonomy) ) - return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); + if ( ! taxonomy_exists( $taxonomy ) ) { + return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); + } $term_id = (int) $term_id; // First, get all of the original args - $term = get_term ($term_id, $taxonomy, ARRAY_A); + $term = get_term( $term_id, $taxonomy, ARRAY_A ); - if ( is_wp_error( $term ) ) + if ( is_wp_error( $term ) ) { return $term; + } + + if ( ! $term ) { + return new WP_Error( 'invalid_term', __( 'Empty Term' ) ); + } // Escape data pulled from DB. $term = wp_slash($term); diff --git a/tests/phpunit/tests/term/wpUpdateTerm.php b/tests/phpunit/tests/term/wpUpdateTerm.php index 33d579b89c..b00bd67ffe 100644 --- a/tests/phpunit/tests/term/wpUpdateTerm.php +++ b/tests/phpunit/tests/term/wpUpdateTerm.php @@ -653,4 +653,16 @@ class Tests_Term_WpUpdateTerm extends WP_UnitTestCase { _unregister_taxonomy( 'wptests_tax' ); } + + /** + * @ticket 31954 + */ + public function test_wp_update_term_with_null_get_term() { + $t = $this->factory->term->create( array( 'taxonomy' => 'category' ) ); + $found = wp_update_term( $t, 'post_tag', array( 'slug' => 'foo' ) ); + + $this->assertWPError( $found ); + $this->assertSame( 'invalid_term', $found->get_error_code() ); + } + }