From c13a263830cede675ba69f2a0ab34ea625ad4f86 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 9 Nov 2015 03:35:56 +0000 Subject: [PATCH] When deleting a term, delete its metadata as well. Props barryceelen. Fixes #34626. git-svn-id: https://develop.svn.wordpress.org/trunk@35585 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/meta-functions.php | 4 ++-- src/wp-includes/taxonomy-functions.php | 7 +++++++ tests/phpunit/tests/term/meta.php | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/meta-functions.php b/src/wp-includes/meta-functions.php index be7ad5b65a..29cf291f92 100644 --- a/src/wp-includes/meta-functions.php +++ b/src/wp-includes/meta-functions.php @@ -558,7 +558,7 @@ function metadata_exists( $meta_type, $object_id, $meta_key ) { * * @global wpdb $wpdb WordPress database abstraction object. * - * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) + * @param string $meta_type Type of object metadata is for (e.g., comment, post, meta, or user). * @param int $meta_id ID for a specific meta row * @return object|false Meta object or false. */ @@ -692,7 +692,7 @@ function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = * * @global wpdb $wpdb WordPress database abstraction object. * - * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) + * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user). * @param int $meta_id ID for a specific meta row * @return bool True on successful delete, false on failure. */ diff --git a/src/wp-includes/taxonomy-functions.php b/src/wp-includes/taxonomy-functions.php index 174fe7427d..035f18b0d4 100644 --- a/src/wp-includes/taxonomy-functions.php +++ b/src/wp-includes/taxonomy-functions.php @@ -2054,6 +2054,8 @@ function wp_delete_object_term_relationships( $object_id, $taxonomies ) { * If the term is a parent of other terms, then the children will be updated to * that term's parent. * + * Metadata associated with the term will be deleted. + * * The `$args` 'default' will only override the terms found, if there is only one * term found. Any other and the found terms are used. * @@ -2172,6 +2174,11 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { foreach ( $tax_object->object_type as $object_type ) clean_object_term_cache( $objects, $object_type ); + $term_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->termmeta WHERE term_id = %d ", $term ) ); + foreach ( $term_meta_ids as $mid ) { + delete_metadata_by_mid( 'term', $mid ); + } + /** * Fires immediately before a term taxonomy ID is deleted. * diff --git a/tests/phpunit/tests/term/meta.php b/tests/phpunit/tests/term/meta.php index 6c34cab918..f6c7b903bf 100644 --- a/tests/phpunit/tests/term/meta.php +++ b/tests/phpunit/tests/term/meta.php @@ -381,6 +381,24 @@ class Tests_Term_Meta extends WP_UnitTestCase { $this->assertSame( 'ambiguous_term_id', $found->get_error_code() ); } + /** + * @ticket 34626 + */ + public function test_term_meta_should_be_deleted_when_term_is_deleted() { + $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + + add_term_meta( $t, 'foo', 'bar' ); + add_term_meta( $t, 'foo1', 'bar' ); + + $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); + $this->assertSame( 'bar', get_term_meta( $t, 'foo1', true ) ); + + $this->assertTrue( wp_delete_term( $t, 'wptests_tax' ) ); + + $this->assertSame( '', get_term_meta( $t, 'foo', true ) ); + $this->assertSame( '', get_term_meta( $t, 'foo1', true ) ); + } + public static function set_cache_results( $q ) { $q->set( 'cache_results', true ); }