Make WordPress Core


Ignore:
Timestamp:
10/10/2015 03:38:41 AM (11 years ago)
Author:
boonebgorges
Message:

Return WP_Post objects from wp_get_object_terms().

A side effect of this change is that terms stored in the cache no longer have
an object_id associated with them. Previously, object_id had always been
cached when the term cache was populated via wp_get_object_terms(), a
strategy that was mostly harmless but still incorrect.

See #14162.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy-functions.php

    r34998 r34999  
    21952195 * @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`.
    21962196 *              Introduced `$parent` argument.
    2197  * @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments.
     2197 * @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments. When `$fields` is 'all' or
     2198 *              'all_with_object_id', an array of `WP_Term` objects will be returned.
    21982199 *
    21992200 * @global wpdb $wpdb WordPress database abstraction object.
    … …  
    23372338        if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
    23382339                $_terms = $wpdb->get_results( $query );
     2340                $object_id_index = array();
    23392341                foreach ( $_terms as $key => $term ) {
    2340                         $_terms[$key] = sanitize_term( $term, $taxonomy, 'raw' );
    2341                 }
     2342                        $term = sanitize_term( $term, $taxonomy, 'raw' );
     2343                        $_terms[ $key ] = $term;
     2344
     2345                        if ( isset( $term->object_id ) ) {
     2346                                $object_id_index[ $key ] = $term->object_id;
     2347                        }
     2348                }
     2349
     2350                update_term_cache( $_terms );
     2351                $_terms = array_map( 'get_term', $_terms );
     2352
     2353                // Re-add the object_id data, which is lost when fetching terms from cache.
     2354                if ( 'all_with_object_id' === $fields ) {
     2355                        foreach ( $_terms as $key => $_term ) {
     2356                                if ( isset( $object_id_index[ $key ] ) ) {
     2357                                        $_term->object_id = $object_id_index[ $key ];
     2358                                }
     2359                        }
     2360                }
     2361
    23422362                $terms = array_merge( $terms, $_terms );
    2343                 update_term_cache( $terms );
    23442363                $objects = true;
     2364
    23452365        } elseif ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) {
    23462366                $_terms = $wpdb->get_col( $query );
    … …  
    35563576function update_term_cache( $terms, $taxonomy = '' ) {
    35573577        foreach ( (array) $terms as $term ) {
    3558                 $term_taxonomy = $taxonomy;
    3559                 if ( empty($term_taxonomy) )
    3560                         $term_taxonomy = $term->taxonomy;
    3561 
    3562                 wp_cache_add( $term->term_id, $term, 'terms' );
     3578                // Create a copy in case the array was passed by reference.
     3579                $_term = $term;
     3580
     3581                // Object ID should not be cached.
     3582                unset( $_term->object_id );
     3583
     3584                wp_cache_add( $term->term_id, $_term, 'terms' );
    35633585        }
    35643586}
Note: See TracChangeset for help on using the changeset viewer.