Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3419,6 +3419,30 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) {
return new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
}

/**
* Filters a term before it is updated in the database.
*
* The filter only runs when the term exists. Returning a WP_Error object
* short-circuits the update and returns that error. Returning any other
* value that is not a WP_Term object short-circuits the update with an
* invalid_term error.
*
* @since 7.3.0
*
* @param WP_Term $term Term object.
* @param string $taxonomy Taxonomy slug.
* @param array|string $args Array or query string of arguments passed to wp_update_term().
*/
$term = apply_filters( 'pre_update_term', $term, $taxonomy, $args );

if ( is_wp_error( $term ) ) {
return $term;
}

if ( ! $term instanceof WP_Term ) {
return new WP_Error( 'invalid_term', __( 'Invalid term.' ) );
}

$term = (array) $term->data;

// Escape data pulled from DB.
Expand Down
155 changes: 155 additions & 0 deletions tests/phpunit/tests/term/wpUpdateTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -855,4 +855,159 @@ public function test_wp_update_term_child_with_long_encoded_slug_should_be_updat

_unregister_taxonomy( $taxonomy );
}

/**
* @ticket 58404
*/
public function test_pre_update_term_filter_should_not_fire_when_term_does_not_exist() {
register_taxonomy( 'wptests_tax', 'post' );

$fired = false;
add_filter(
'pre_update_term',
static function () use ( &$fired ) {
$fired = true;
return null;
}
);

$found = wp_update_term( 0, 'wptests_tax', array( 'name' => 'Updated' ) );

_unregister_taxonomy( 'wptests_tax' );

$this->assertWPError( $found );
$this->assertSame( 'invalid_term', $found->get_error_code() );
$this->assertFalse( $fired, 'pre_update_term fired for a term that does not exist.' );
}

/**
* @ticket 58404
*/
public function test_pre_update_term_filter_should_receive_term_taxonomy_and_args() {
register_taxonomy( 'wptests_tax', 'post' );

$t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$captured = array();

add_filter(
'pre_update_term',
static function ( $term, $taxonomy, $args ) use ( &$captured ) {
$captured = array( $term, $taxonomy, $args );
return $term;
},
10,
3
);

$found = wp_update_term( $t, 'wptests_tax', array( 'description' => 'New description' ) );

_unregister_taxonomy( 'wptests_tax' );

$this->assertNotWPError( $found );
$this->assertInstanceOf( WP_Term::class, $captured[0] );
$this->assertSame( $t, $captured[0]->term_id );
$this->assertSame( 'wptests_tax', $captured[1] );
$this->assertSame( array( 'description' => 'New description' ), $captured[2] );
}

/**
* @ticket 58404
*/
public function test_pre_update_term_filter_should_abort_update_with_wp_error() {
register_taxonomy( 'wptests_tax', 'post' );

$t = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'Original Name',
)
);

add_filter(
'pre_update_term',
static function () {
return new WP_Error( 'my_custom_error', 'My custom error.' );
}
);

$edit_terms_fired = false;
add_action(
'edit_terms',
static function () use ( &$edit_terms_fired ) {
$edit_terms_fired = true;
}
);

$found = wp_update_term( $t, 'wptests_tax', array( 'name' => 'Updated Name' ) );

$term = get_term( $t, 'wptests_tax' );

_unregister_taxonomy( 'wptests_tax' );

$this->assertWPError( $found );
$this->assertSame( 'my_custom_error', $found->get_error_code() );
$this->assertFalse( $edit_terms_fired, 'edit_terms fired after a pre_update_term short-circuit.' );
$this->assertSame( 'Original Name', $term->name, 'The term was updated despite the pre_update_term short-circuit.' );
}

/**
* @ticket 58404
*/
public function test_pre_update_term_filter_should_return_error_for_non_term_value() {
register_taxonomy( 'wptests_tax', 'post' );

$t = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'Original Name',
)
);

add_filter(
'pre_update_term',
static function () {
return 'not a term';
}
);

$found = wp_update_term( $t, 'wptests_tax', array( 'name' => 'Updated Name' ) );

$term = get_term( $t, 'wptests_tax' );

_unregister_taxonomy( 'wptests_tax' );

$this->assertWPError( $found );
$this->assertSame( 'invalid_term', $found->get_error_code() );
$this->assertSame( 'Original Name', $term->name, 'The term was updated despite an invalid pre_update_term return value.' );
}

/**
* @ticket 58404
*/
public function test_pre_update_term_filter_should_allow_update_to_proceed() {
register_taxonomy( 'wptests_tax', 'post' );

$t = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'Original Name',
)
);

add_filter(
'pre_update_term',
static function ( $term ) {
return $term;
}
);

$found = wp_update_term( $t, 'wptests_tax', array( 'name' => 'Updated Name' ) );

$term = get_term( $t, 'wptests_tax' );

_unregister_taxonomy( 'wptests_tax' );

$this->assertNotWPError( $found );
$this->assertSame( 'Updated Name', $term->name );
}
}
Loading