WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/30/2018 09:07:16 PM (3 years ago)
Author:
boonebgorges
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r42876 r43049  
    679679        if ( false !== $cache ) {
    680680            if ( 'all' === $_fields ) {
    681                 $cache = array_map( 'get_term', $cache );
     681                $cache = $this->populate_terms( $cache );
    682682            }
    683683
     
    811811
    812812        if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) {
    813             $terms = array_map( 'get_term', $terms );
     813            $terms = $this->populate_terms( $terms );
    814814        }
    815815
     
    973973        return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
    974974    }
     975
     976    /**
     977     * Creates an array of term objects from an array of term IDs.
     978     *
     979     * Also discards invalid term objects.
     980     *
     981     * @since 5.0.0
     982     *
     983     * @param array $term_ids Term IDs.
     984     * @return array
     985     */
     986    protected function populate_terms( $term_ids ) {
     987        $terms = array();
     988
     989        if ( ! is_array( $term_ids ) ) {
     990            return $terms;
     991        }
     992
     993        foreach ( $term_ids as $key => $term_id ) {
     994            $term = get_term( $term_id );
     995            if ( $term instanceof WP_Term ) {
     996                $terms[ $key ] = $term;
     997            }
     998        }
     999
     1000        return $terms;
     1001    }
    9751002}
Note: See TracChangeset for help on using the changeset viewer.