From 52384c2e6bb80a8a96d43dc483f254ca417df618 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 10 Dec 2015 03:34:51 +0000 Subject: [PATCH] Improve handling for `WP_Error` objects in `get_the_terms()`. `wp_get_object_terms()` can return a `WP_Error` object. As such, the `get_the_terms()` cache wrapper should handle them properly. To wit: * Don't try to map an error object to `get_term()`. Introduced in [35032]. * Don't cache an error object as taxonomy relationships. Introduced in at least [16487], maybe earlier. Props stephenharris. Fixes #34723. git-svn-id: https://develop.svn.wordpress.org/trunk@35850 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/category-template.php | 14 +++++++++----- tests/phpunit/tests/term.php | 9 +++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/category-template.php b/src/wp-includes/category-template.php index 9b685edb11..68cd74c8e5 100644 --- a/src/wp-includes/category-template.php +++ b/src/wp-includes/category-template.php @@ -1148,14 +1148,18 @@ function get_the_terms( $post, $taxonomy ) { $terms = get_object_term_cache( $post->ID, $taxonomy ); if ( false === $terms ) { $terms = wp_get_object_terms( $post->ID, $taxonomy ); - $to_cache = array(); - foreach ( $terms as $key => $term ) { - $to_cache[ $key ] = $term->data; + if ( ! is_wp_error( $terms ) ) { + $to_cache = array(); + foreach ( $terms as $key => $term ) { + $to_cache[ $key ] = $term->data; + } + wp_cache_add( $post->ID, $to_cache, $taxonomy . '_relationships' ); } - wp_cache_add( $post->ID, $to_cache, $taxonomy . '_relationships' ); } - $terms = array_map( 'get_term', $terms ); + if ( ! is_wp_error( $terms ) ) { + $terms = array_map( 'get_term', $terms ); + } /** * Filter the list of terms attached to the given post. diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 0d005f38d3..9c6e9655a4 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -627,4 +627,13 @@ class Tests_Term extends WP_UnitTestCase { $cat_id2 = self::factory()->category->create( array( 'parent' => $cat_id1 ) ); $this->assertWPError( $cat_id2 ); } + + /** + * @ticket 34723 + */ + function test_get_the_terms_should_return_wp_error_when_taxonomy_is_unregistered() { + $p = self::$post_ids[0]; + $terms = get_the_terms( $p, 'this-taxonomy-does-not-exist' ); + $this->assertTrue( is_wp_error( $terms ) ); + } }