From 14861c9245887569beae34419389709721365b64 Mon Sep 17 00:00:00 2001 From: Foteini Giannaropoulou Date: Fri, 1 Dec 2023 09:22:47 +0000 Subject: [PATCH] Taxonomy: Check for empty term after DB sanitization in wp_insert_term --- src/wp-includes/taxonomy.php | 4 ++++ tests/phpunit/tests/term/wpInsertTerm.php | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 59ec5345fe0cd..b0565c239ceeb 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2434,6 +2434,10 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { $description = wp_unslash( $args['description'] ); $parent = (int) $args['parent']; + if ( '' === $name ) { + return new WP_Error( 'invalid_term_name', __( 'Invalid term name.' ) ); + } + $slug_provided = ! empty( $args['slug'] ); if ( ! $slug_provided ) { $slug = sanitize_title( $name ); diff --git a/tests/phpunit/tests/term/wpInsertTerm.php b/tests/phpunit/tests/term/wpInsertTerm.php index d99593b1d5f2e..e506947589a26 100644 --- a/tests/phpunit/tests/term/wpInsertTerm.php +++ b/tests/phpunit/tests/term/wpInsertTerm.php @@ -895,6 +895,16 @@ public function test_wp_insert_term_with_null_description() { $this->assertSame( '', $term_object->description ); } + public function test_wp_insert_term_with_empty_name_after_db_sanitization() { + $term = wp_insert_term( + '', + 'post_tag' + ); + + $this->assertWPError( $term ); + $this->assertSame( 'invalid_term_name', $found->get_error_code() ); + } + /** Helpers */ public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term, $object_ids ) {