From 636464857e2e4f7e73eec507f7d8022d946a4063 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 10 Oct 2015 03:39:26 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@34999 git-svn-id: http://core.svn.wordpress.org/trunk@34964 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/taxonomy-functions.php | 36 ++++++++++++++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/wp-includes/taxonomy-functions.php b/wp-includes/taxonomy-functions.php index 53435c0bc6..02e0499e84 100644 --- a/wp-includes/taxonomy-functions.php +++ b/wp-includes/taxonomy-functions.php @@ -2194,7 +2194,8 @@ function wp_delete_category( $cat_ID ) { * @since 2.3.0 * @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`. * Introduced `$parent` argument. - * @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments. + * @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments. When `$fields` is 'all' or + * 'all_with_object_id', an array of `WP_Term` objects will be returned. * * @global wpdb $wpdb WordPress database abstraction object. * @@ -2336,12 +2337,31 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { $objects = false; if ( 'all' == $fields || 'all_with_object_id' == $fields ) { $_terms = $wpdb->get_results( $query ); + $object_id_index = array(); foreach ( $_terms as $key => $term ) { - $_terms[$key] = sanitize_term( $term, $taxonomy, 'raw' ); + $term = sanitize_term( $term, $taxonomy, 'raw' ); + $_terms[ $key ] = $term; + + if ( isset( $term->object_id ) ) { + $object_id_index[ $key ] = $term->object_id; + } } + + update_term_cache( $_terms ); + $_terms = array_map( 'get_term', $_terms ); + + // Re-add the object_id data, which is lost when fetching terms from cache. + if ( 'all_with_object_id' === $fields ) { + foreach ( $_terms as $key => $_term ) { + if ( isset( $object_id_index[ $key ] ) ) { + $_term->object_id = $object_id_index[ $key ]; + } + } + } + $terms = array_merge( $terms, $_terms ); - update_term_cache( $terms ); $objects = true; + } elseif ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) { $_terms = $wpdb->get_col( $query ); $_field = ( 'ids' == $fields ) ? 'term_id' : 'name'; @@ -3555,11 +3575,13 @@ function update_object_term_cache($object_ids, $object_type) { */ function update_term_cache( $terms, $taxonomy = '' ) { foreach ( (array) $terms as $term ) { - $term_taxonomy = $taxonomy; - if ( empty($term_taxonomy) ) - $term_taxonomy = $term->taxonomy; + // Create a copy in case the array was passed by reference. + $_term = $term; - wp_cache_add( $term->term_id, $term, 'terms' ); + // Object ID should not be cached. + unset( $_term->object_id ); + + wp_cache_add( $term->term_id, $_term, 'terms' ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index a983951311..c6e650192f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-34998'; +$wp_version = '4.4-alpha-34999'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.