WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/17/2018 04:27:12 PM (2 years ago)
Author:
SergeyBiryukov
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.
Merges [43049] and [43491] to the 4.9 branch.
Fixes #42691.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/class-wp-term-query.php

    r43314 r43492  
    673673        if ( false !== $cache ) {
    674674            if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) {
    675                 $cache = array_map( 'get_term', $cache );
     675                $cache = $this->populate_terms( $cache );
    676676            }
    677677
     
    805805
    806806        if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) {
    807             $terms = array_map( 'get_term', $terms );
     807            $terms = $this->populate_terms( $terms );
    808808        }
    809809
     
    967967        return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
    968968    }
     969
     970    /**
     971     * Creates an array of term objects from an array of term IDs.
     972     *
     973     * Also discards invalid term objects.
     974     *
     975     * @since 4.9.8
     976     *
     977     * @param array $term_ids Term IDs.
     978     * @return array
     979     */
     980    protected function populate_terms( $term_ids ) {
     981        $terms = array();
     982
     983        if ( ! is_array( $term_ids ) ) {
     984            return $terms;
     985        }
     986
     987        foreach ( $term_ids as $key => $term_id ) {
     988            $term = get_term( $term_id );
     989            if ( $term instanceof WP_Term ) {
     990                $terms[ $key ] = $term;
     991            }
     992        }
     993
     994        return $terms;
     995    }
    969996}
Note: See TracChangeset for help on using the changeset viewer.