Skip to content

Commit

Permalink
Taxonomy: Check for empty term after DB sanitization in wp_insert_term
Browse files Browse the repository at this point in the history
  • Loading branch information
fgiannar committed Dec 1, 2023
1 parent 0ce733c commit 14861c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
10 changes: 10 additions & 0 deletions tests/phpunit/tests/term/wpInsertTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<script>Hello</script>',
'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 ) {
Expand Down

0 comments on commit 14861c9

Please sign in to comment.