WordPress.org

Make WordPress Core

Ticket #36251: 36251.diff

File 36251.diff, 4.8 KB (added by boonebgorges, 5 years ago)
  • src/wp-includes/category-template.php

    diff --git src/wp-includes/category-template.php src/wp-includes/category-template.php
    index 35d6c82..d0c8e02 100644
    function get_the_terms( $post, $taxonomy ) { 
    11711171        if ( false === $terms ) {
    11721172                $terms = wp_get_object_terms( $post->ID, $taxonomy );
    11731173                if ( ! is_wp_error( $terms ) ) {
    1174                         $to_cache = array();
    1175                         foreach ( $terms as $key => $term ) {
    1176                                 $to_cache[ $key ] = $term->data;
    1177                         }
     1174                        $to_cache = wp_list_pluck( $terms, 'term_id' );
    11781175                        wp_cache_add( $post->ID, $to_cache, $taxonomy . '_relationships' );
    11791176                }
    11801177        }
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index 7bc2d40..86bec6e 100644
    function wp_update_term( $term_id, $taxonomy, $args = array() ) { 
    34693469         */
    34703470        do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );
    34713471
    3472         // Clean the relationship caches for all object types using this term.
    3473         $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
    3474         $tax_object = get_taxonomy( $taxonomy );
    3475         foreach ( $tax_object->object_type as $object_type ) {
    3476                 clean_object_term_cache( $objects, $object_type );
    3477         }
    3478 
    34793472        /**
    34803473         * Fires after a term has been updated, but before the term cache has been cleaned.
    34813474         *
    function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { 
    37663759 *                    for `$taxonomy` and `$id`.
    37673760 */
    37683761function get_object_term_cache( $id, $taxonomy ) {
    3769         return wp_cache_get( $id, "{$taxonomy}_relationships" );
     3762        $cached_ids = wp_cache_get( $id, "{$taxonomy}_relationships" );
     3763
     3764        if ( false !== $cached_ids ) {
     3765                $terms = array_map( 'get_term', $cached_ids );
     3766
     3767                $term_data = array();
     3768                foreach ( $terms as $term ) {
     3769                        $term_data[] = $term->data;
     3770                }
     3771                return $term_data;
     3772        } else {
     3773                return $cached_ids;
     3774        }
    37703775}
    37713776
    37723777/**
    function update_object_term_cache($object_ids, $object_type) { 
    38163821        ) );
    38173822
    38183823        $object_terms = array();
    3819         foreach ( (array) $terms as $term )
    3820                 $object_terms[$term->object_id][$term->taxonomy][] = $term;
     3824        foreach ( (array) $terms as $term ) {
     3825                $object_terms[ $term->object_id ][ $term->taxonomy ][] = $term->term_id;
     3826        }
    38213827
    38223828        foreach ( $ids as $id ) {
    38233829                foreach ( $taxonomies as $taxonomy ) {
    function is_object_in_term( $object_id, $taxonomy, $terms = null ) { 
    47304736        $object_terms = get_object_term_cache( $object_id, $taxonomy );
    47314737        if ( false === $object_terms ) {
    47324738                $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" );
     4739                $object_term_ids = wp_list_pluck( $object_terms, 'term_id' );
     4740                wp_cache_set( $object_id, $object_term_ids, "{$taxonomy}_relationships" );
    47344741        }
    47354742
    47364743        if ( is_wp_error( $object_terms ) )
  • tests/phpunit/tests/term/wpUpdateTerm.php

    diff --git tests/phpunit/tests/term/wpUpdateTerm.php tests/phpunit/tests/term/wpUpdateTerm.php
    index 4d9d17b..b4c88a7 100644
    class Tests_Term_WpUpdateTerm extends WP_UnitTestCase { 
    488488                $this->assertInternalType( 'int', $found['term_taxonomy_id'] );
    489489        }
    490490
    491         public function test_wp_update_term_should_clean_object_term_cache() {
    492                 register_taxonomy( 'wptests_tax_for_post', 'post' );
    493                 register_taxonomy( 'wptests_tax_for_page', 'page' );
    494                 $post = self::factory()->post->create();
    495                 $page = self::factory()->post->create( array(
    496                         'post_type' => 'page',
    497                 ) );
    498 
    499                 $t_for_post = self::factory()->term->create( array(
    500                         'taxonomy' => 'wptests_tax_for_post',
    501                 ) );
    502                 $t_for_page = self::factory()->term->create( array(
    503                         'taxonomy' => 'wptests_tax_for_page',
    504                 ) );
    505 
    506                 wp_set_post_terms( $post, array( $t_for_post ), 'wptests_tax_for_post' );
    507                 wp_set_post_terms( $page, array( $t_for_page ), 'wptests_tax_for_page' );
    508 
    509                 // Prime caches and verify.
    510                 update_object_term_cache( array( $post ), 'post' );
    511                 update_object_term_cache( array( $page ), 'page' );
    512                 $this->assertNotEmpty( wp_cache_get( $post, 'wptests_tax_for_post_relationships' ) );
    513                 $this->assertNotEmpty( wp_cache_get( $page, 'wptests_tax_for_page_relationships' ) );
    514 
    515                 // Update a term in just one of the taxonomies.
    516                 $found = wp_update_term( $t_for_post, 'wptests_tax_for_post', array(
    517                         'slug' => 'foo',
    518                 ) );
    519 
    520                 // Only the relevant cache should have been cleared.
    521                 $this->assertFalse( wp_cache_get( $post, 'wptests_tax_for_post_relationships' ) );
    522                 $this->assertNotEmpty( wp_cache_get( $page, 'wptests_tax_for_page_relationships' ) );
    523         }
    524 
    525491        public function test_wp_update_term_should_clean_term_cache() {
    526492                register_taxonomy( 'wptests_tax', 'post', array(
    527493                        'hierarchical' => true,