From 3a9bc329497dc5875372f25deb78ddc12d7fcb29 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 7 Jan 2016 03:31:48 +0000 Subject: [PATCH] Ensure 'description' is a string in `wp_insert_term()`. Passing `'description' => null` when creating a term can cause MySQL notices, as the description column in the terms table does not allow for null values. We correct this by intepreting a `null` description as an empty string. Props TimothyBlynJacobs. Fixes #35321. git-svn-id: https://develop.svn.wordpress.org/trunk@36214 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 5 +++++ tests/phpunit/tests/term/wpInsertTerm.php | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 9865f42be7..78dd52ced9 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2579,8 +2579,13 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) { return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) ); } + $args['name'] = $term; $args['taxonomy'] = $taxonomy; + + // Coerce null description to strings, to avoid database errors. + $args['description'] = (string) $args['description']; + $args = sanitize_term($args, $taxonomy, 'db'); // expected_slashed ($name) diff --git a/tests/phpunit/tests/term/wpInsertTerm.php b/tests/phpunit/tests/term/wpInsertTerm.php index c3dea31247..386b66fa15 100644 --- a/tests/phpunit/tests/term/wpInsertTerm.php +++ b/tests/phpunit/tests/term/wpInsertTerm.php @@ -642,6 +642,23 @@ class Tests_Term_WpInsertTerm extends WP_UnitTestCase { } + /** + * @ticket 35321 + */ + public function test_wp_insert_term_with_null_description() { + + register_taxonomy( 'wptests_tax', 'post' ); + + $term = wp_insert_term( 'foo', 'wptests_tax', array( + 'description' => null + ) ); + + $term_object = get_term( $term['term_id'] ); + + $this->assertInstanceOf( 'WP_Term', $term_object ); + $this->assertSame( '', $term_object->description ); + } + /** Helpers **********************************************************/ public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term, $object_ids ) {