Skip to content

Commit 355672f

Browse files
committed
Taxonomy: Avoid type error in wp_delete_object_term_relationships() when invalid taxonomy supplied.
Developed in #10621 Props owolter, westonruter. Fixes #64406. git-svn-id: https://develop.svn.wordpress.org/trunk@61376 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 048ea74 commit 355672f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/wp-includes/taxonomy.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,10 @@ function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
19991999

20002000
foreach ( (array) $taxonomies as $taxonomy ) {
20012001
$term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) );
2002+
if ( ! is_array( $term_ids ) ) {
2003+
// Skip return value in the case of an error or the 'wp_get_object_terms' filter returning an invalid value.
2004+
continue;
2005+
}
20022006
$term_ids = array_map( 'intval', $term_ids );
20032007
wp_remove_object_terms( $object_id, $term_ids, $taxonomy );
20042008
}

tests/phpunit/tests/term/wpDeleteObjectTermRelationships.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,24 @@ public function test_array_of_taxonomies() {
5353

5454
$this->assertSameSets( array( $t2 ), $terms );
5555
}
56+
57+
/**
58+
* @ticket 64406
59+
*/
60+
public function test_delete_when_error() {
61+
$taxonomy_name = 'wptests_tax';
62+
register_taxonomy( $taxonomy_name, 'post' );
63+
$term_id = self::factory()->term->create( array( 'taxonomy' => $taxonomy_name ) );
64+
$object_id = 567;
65+
wp_set_object_terms( $object_id, array( $term_id ), $taxonomy_name );
66+
67+
// Confirm the setup.
68+
$terms = wp_get_object_terms( $object_id, array( $taxonomy_name ), array( 'fields' => 'ids' ) );
69+
$this->assertSame( array( $term_id ), $terms, 'Expected same object terms.' );
70+
71+
// Try wp_delete_object_term_relationships() when the taxonomy is invalid (no change expected).
72+
wp_delete_object_term_relationships( $object_id, 'wptests_taxation' );
73+
$terms = wp_get_object_terms( $object_id, array( $taxonomy_name ), array( 'fields' => 'ids' ) );
74+
$this->assertSame( array( $term_id ), $terms, 'Expected the object terms to be unchanged.' );
75+
}
5676
}

0 commit comments

Comments
 (0)