Don't cache `WP_Term` objects in `wp_get_object_cache()`.

The data stored in the cache should be raw database query results, not
`WP_Term` objects (which may be modified by plugins, and may contain additional
properties that shouldn't be cached).

If term relationships caches were handled in `wp_get_object_terms()` - where
a database query takes place - it would be straightforward to cache raw data.
See #34239. Since, in fact, `get_the_terms()` caches the value it gets from
`wp_get_object_terms()`, we need a technique that allows us to get raw data
from a `WP_Term` object. Mirroring `WP_User`, we introduce a `data` property
on term objects, which `get_the_terms()` uses to fetch cacheable term info.

Fixes #34262.
Built from https://develop.svn.wordpress.org/trunk@35032


git-svn-id: http://core.svn.wordpress.org/trunk@34997 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-10-12 15:13:48 +00:00
parent 6912d6fe64
commit 499d1b74ba
4 changed files with 39 additions and 4 deletions

View File

@ -1137,9 +1137,15 @@ function get_the_terms( $post, $taxonomy ) {
$terms = get_object_term_cache( $post->ID, $taxonomy );
if ( false === $terms ) {
$terms = wp_get_object_terms( $post->ID, $taxonomy );
wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
$to_cache = array();
foreach ( $terms as $key => $term ) {
$to_cache[ $key ] = $term->data;
}
wp_cache_add( $post->ID, $to_cache, $taxonomy . '_relationships' );
}
$terms = array_map( 'get_term', $terms );
/**
* Filter the list of terms attached to the given post.
*

View File

@ -95,6 +95,15 @@ final class WP_Term {
*/
public $count = 0;
/**
* Info about the term, as stored in the database.
*
* @since 4.4.0
* @access protected
* @var array
*/
protected $data = array();
/**
* Stores the term object's sanitization level.
*
@ -157,6 +166,8 @@ final class WP_Term {
foreach ( get_object_vars( $term ) as $key => $value ) {
$this->$key = $value;
}
$this->data = sanitize_term( $term, $this->taxonomy, $this->filter );
}
/**
@ -182,4 +193,20 @@ final class WP_Term {
public function to_array() {
return get_object_vars( $this );
}
/**
* Getter.
*
* @since 4.4.0
* @access public
*
* @return mixed
*/
public function __get( $key ) {
switch ( $key ) {
case 'data' :
return sanitize_term( $this->{$key}, $this->data->taxonomy, 'raw' );
break;
}
}
}

View File

@ -3062,7 +3062,7 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) {
$term_id = (int) $term_id;
// First, get all of the original args
$term = get_term( $term_id, $taxonomy, ARRAY_A );
$term = get_term( $term_id, $taxonomy );
if ( is_wp_error( $term ) ) {
return $term;
@ -3072,8 +3072,10 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) {
return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
}
$term = (array) $term->data;
// Escape data pulled from DB.
$term = wp_slash($term);
$term = wp_slash( $term );
// Merge old and new args with new args overwriting old ones.
$args = array_merge($term, $args);

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.4-alpha-35031';
$wp_version = '4.4-alpha-35032';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.