WordPress.org

Make WordPress Core

Ticket #36814: 36814.diff

File 36814.diff, 8.4 KB (added by boonebgorges, 3 years ago)
  • src/wp-admin/includes/taxonomy.php

    diff --git src/wp-admin/includes/taxonomy.php src/wp-admin/includes/taxonomy.php
    index 19f7ebe..aee240b 100644
    function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) { 
    242242        $terms = get_object_term_cache( $post_id, $taxonomy );
    243243        if ( false === $terms ) {
    244244                $terms = wp_get_object_terms( $post_id, $taxonomy );
    245                 wp_cache_add( $post_id, $terms, $taxonomy . '_relationships' );
     245                wp_cache_add( $post_id, wp_list_pluck( $terms, 'term_id' ), $taxonomy . '_relationships' );
    246246        }
    247247
    248248        if ( ! $terms ) {
  • src/wp-admin/includes/template.php

    diff --git src/wp-admin/includes/template.php src/wp-admin/includes/template.php
    index 8518a05..3497e3f 100644
    function get_inline_data($post) { 
    311311                        $terms = get_object_term_cache( $post->ID, $taxonomy_name );
    312312                        if ( false === $terms ) {
    313313                                $terms = wp_get_object_terms( $post->ID, $taxonomy_name );
    314                                 wp_cache_add( $post->ID, $terms, $taxonomy_name . '_relationships' );
     314                                wp_cache_add( $post->ID, wp_list_pluck( $terms, 'term_id' ), $taxonomy_name . '_relationships' );
    315315                        }
    316316                        $term_ids = empty( $terms ) ? array() : wp_list_pluck( $terms, 'term_id' );
    317317
  • src/wp-includes/category-template.php

    diff --git src/wp-includes/category-template.php src/wp-includes/category-template.php
    index 232f147..0c9bd3f 100644
    function get_the_terms( $post, $taxonomy ) { 
    11851185        if ( false === $terms ) {
    11861186                $terms = wp_get_object_terms( $post->ID, $taxonomy );
    11871187                if ( ! is_wp_error( $terms ) ) {
    1188                         $to_cache = array();
    1189                         foreach ( $terms as $key => $term ) {
    1190                                 $to_cache[ $key ] = $term->data;
    1191                         }
    1192                         wp_cache_add( $post->ID, $to_cache, $taxonomy . '_relationships' );
     1188                        $term_ids = wp_list_pluck( $terms, 'term_id' );
     1189                        wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
    11931190                }
    11941191        }
    11951192
    1196         if ( ! is_wp_error( $terms ) ) {
    1197                 $terms = array_map( 'get_term', $terms );
    1198         }
    1199 
    12001193        /**
    12011194         * Filter the list of terms attached to the given post.
    12021195         *
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index aa19e35..a89f66a 100644
    function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { 
    37583758/**
    37593759 * Retrieves the taxonomy relationship to the term object id.
    37603760 *
     3761 * Upstream functions (like `get_the_terms()` and `is_object_in_term()`) are responsible for populating the
     3762 * object-term relationship cache. The current function only fetches relationship data that is already in the cache.
     3763 *
    37613764 * @since 2.3.0
    37623765 *
    37633766 * @param int    $id       Term object ID.
    37643767 * @param string $taxonomy Taxonomy name.
    3765  * @return bool|mixed Empty array if $terms found, but not `$taxonomy`. False if nothing is in cache
    3766  *                    for `$taxonomy` and `$id`.
     3768 * @return bool|array Array of `WP_Term` objects, if cached False if cache is empty for `$taxonomy` and `$id`.
    37673769 */
    37683770function get_object_term_cache( $id, $taxonomy ) {
    3769         return wp_cache_get( $id, "{$taxonomy}_relationships" );
     3771        $_term_ids = wp_cache_get( $id, "{$taxonomy}_relationships" );
     3772
     3773        // We leave the priming of relationship caches to upstream functions.
     3774        if ( false === $_term_ids ) {
     3775                return false;
     3776        }
     3777
     3778        // Backward compatibility for if a plugin is putting objects into the cache, rather than IDs.
     3779        $term_ids = array();
     3780        foreach ( $_term_ids as $term_id ) {
     3781                if ( is_numeric( $term_id ) ) {
     3782                        $term_ids[] = intval( $term_id );
     3783                } elseif ( isset( $term_id->term_id ) ) {
     3784                        $term_ids[] = intval( $term_id->term_id );
     3785                }
     3786        }
     3787
     3788        // Fill the term objects.
     3789        _prime_term_caches( $term_ids );
     3790
     3791        $terms = array();
     3792        foreach ( $term_ids as $term_id ) {
     3793                $terms[] = wp_cache_get( $term_id, 'terms' );
     3794        }
     3795
     3796        return array_map( 'get_term', $terms );
    37703797}
    37713798
    37723799/**
    function update_object_term_cache($object_ids, $object_type) { 
    38173844
    38183845        $object_terms = array();
    38193846        foreach ( (array) $terms as $term )
    3820                 $object_terms[$term->object_id][$term->taxonomy][] = $term;
     3847                $object_terms[$term->object_id][$term->taxonomy][] = $term->term_id;
    38213848
    38223849        foreach ( $ids as $id ) {
    38233850                foreach ( $taxonomies as $taxonomy ) {
    function _pad_term_counts( &$terms, $taxonomy ) { 
    40234050                        $terms_by_id[$id]->count = count($items);
    40244051}
    40254052
     4053/**
     4054 * Adds any terms from the given IDs to the cache that do not already exist in cache.
     4055 *
     4056 * @since 4.6.0
     4057 * @access private
     4058 *
     4059 * @global wpdb $wpdb WordPress database abstraction object.
     4060 *
     4061 * @param array $term_ids          Array of term IDs.
     4062 * @param bool  $update_meta_cache Optional. Whether to update the meta cache. Default true.
     4063 */
     4064function _prime_term_caches( $term_ids, $update_meta_cache = true ) {
     4065        global $wpdb;
     4066
     4067        $non_cached_ids = _get_non_cached_ids( $term_ids, 'terms' );
     4068        if ( ! empty( $non_cached_ids ) ) {
     4069                $fresh_terms = $wpdb->get_results( sprintf( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
     4070
     4071                update_term_cache( $fresh_terms, $update_meta_cache );
     4072
     4073                if ( $update_meta_cache ) {
     4074                        update_termmeta_cache( $non_cached_ids );
     4075                }
     4076        }
     4077}
     4078
    40264079//
    40274080// Default callbacks
    40284081//
    function is_object_in_term( $object_id, $taxonomy, $terms = null ) { 
    47304783        $object_terms = get_object_term_cache( $object_id, $taxonomy );
    47314784        if ( false === $object_terms ) {
    47324785                $object_terms = wp_get_object_terms( $object_id, $taxonomy, array( 'update_term_meta_cache' => false ) );
    4733                 wp_cache_set( $object_id, $object_terms, "{$taxonomy}_relationships" );
     4786                wp_cache_set( $object_id, wp_list_pluck( $object_terms, 'term_id' ), "{$taxonomy}_relationships" );
    47344787        }
    47354788
    47364789        if ( is_wp_error( $object_terms ) )
  • tests/phpunit/tests/term/getTheTerms.php

    diff --git tests/phpunit/tests/term/getTheTerms.php tests/phpunit/tests/term/getTheTerms.php
    index d7bb7fc..a9bcf78 100644
    class Tests_Term_GetTheTerms extends WP_UnitTestCase { 
    7878        /**
    7979         * @ticket 34262
    8080         */
    81         public function test_get_the_terms_should_not_cache_wp_term_objects() {
    82                 $p = self::$post_ids[0];
    83                 register_taxonomy( 'wptests_tax', 'post' );
    84                 $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
    85                 wp_set_object_terms( $p, $t, 'wptests_tax' );
    86 
    87                 // Prime the cache.
    88                 $terms = get_the_terms( $p, 'wptests_tax' );
    89 
    90                 $cached = get_object_term_cache( $p, 'wptests_tax' );
    91 
    92                 $this->assertNotEmpty( $cached );
    93                 $this->assertSame( $t, (int) $cached[0]->term_id );
    94                 $this->assertNotInstanceOf( 'WP_Term', $cached[0] );
    95         }
    96 
    97         /**
    98          * @ticket 34262
    99          */
    10081        public function test_get_the_terms_should_return_wp_term_objects_from_cache() {
    10182                $p = self::$post_ids[0];
    10283                register_taxonomy( 'wptests_tax', 'post' );
    class Tests_Term_GetTheTerms extends WP_UnitTestCase { 
    170151                $terms = get_the_terms( $p, 'this-taxonomy-does-not-exist' );
    171152                $this->assertTrue( is_wp_error( $terms ) );
    172153        }
     154
     155        /**
     156         * @ticket 36814
     157         */
     158        public function test_count_should_not_be_improperly_cached() {
     159                register_taxonomy( 'wptests_tax', 'post' );
     160
     161                $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     162
     163                wp_set_object_terms( self::$post_ids[0], $t, 'wptests_tax' );
     164
     165                $terms = get_the_terms( self::$post_ids[0], 'wptests_tax' );
     166                $this->assertSame( 1, $terms[0]->count );
     167
     168                wp_set_object_terms( self::$post_ids[1], $t, 'wptests_tax' );
     169
     170                $terms = get_the_terms( self::$post_ids[0], 'wptests_tax' );
     171                $this->assertSame( 2, $terms[0]->count );
     172        }
     173
     174        /**
     175         * @ticket 36814
     176         */
     177        public function test_uncached_terms_should_be_primed_with_a_single_query() {
     178                global $wpdb;
     179
     180                register_taxonomy( 'wptests_tax', 'post' );
     181
     182                $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     183
     184                wp_set_object_terms( self::$post_ids[0], $terms, 'wptests_tax' );
     185
     186                get_the_terms( self::$post_ids[0], 'wptests_tax' );
     187
     188                // Clean cache for two of the terms.
     189                clean_term_cache( array( $terms[0], $terms[1] ), 'wptests_tax', false );
     190
     191                $num_queries = $wpdb->num_queries;
     192                $found = get_the_terms( self::$post_ids[0], 'wptests_tax' );
     193
     194                $this->assertEqualSets( $terms, wp_list_pluck( $found, 'term_id' ) );
     195
     196                $num_queries++;
     197                $this->assertSame( $num_queries, $wpdb->num_queries );
     198
     199        }
    173200}