From 0dc6bd7c04491daf3d859f05b3b700ff335194cc Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 30 Apr 2018 21:08:22 +0000 Subject: [PATCH] Taxonomy: Ensure that invalid term objects are discarded in `WP_Term_Query`. The `get_term()` mapping may result in term objects that are `null` or `WP_Error` when plugins use `get_term` or a related filter. Since `null` and error objects are not valid results for a term query, we discard them. Props GM_Alex. See #42691. Built from https://develop.svn.wordpress.org/trunk@43049 git-svn-id: http://core.svn.wordpress.org/trunk@42878 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-term-query.php | 31 +++++++++++++++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/wp-includes/class-wp-term-query.php b/wp-includes/class-wp-term-query.php index b7c3b42a2b..7431d1f5f0 100644 --- a/wp-includes/class-wp-term-query.php +++ b/wp-includes/class-wp-term-query.php @@ -678,7 +678,7 @@ class WP_Term_Query { $cache = wp_cache_get( $cache_key, 'terms' ); if ( false !== $cache ) { if ( 'all' === $_fields ) { - $cache = array_map( 'get_term', $cache ); + $cache = $this->populate_terms( $cache ); } $this->terms = $cache; @@ -810,7 +810,7 @@ class WP_Term_Query { wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) { - $terms = array_map( 'get_term', $terms ); + $terms = $this->populate_terms( $terms ); } $this->terms = $terms; @@ -972,4 +972,31 @@ class WP_Term_Query { return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like ); } + + /** + * Creates an array of term objects from an array of term IDs. + * + * Also discards invalid term objects. + * + * @since 5.0.0 + * + * @param array $term_ids Term IDs. + * @return array + */ + protected function populate_terms( $term_ids ) { + $terms = array(); + + if ( ! is_array( $term_ids ) ) { + return $terms; + } + + foreach ( $term_ids as $key => $term_id ) { + $term = get_term( $term_id ); + if ( $term instanceof WP_Term ) { + $terms[ $key ] = $term; + } + } + + return $terms; + } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 826679f10b..d1fa280c38 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '5.0-alpha-43048'; +$wp_version = '5.0-alpha-43049'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.