Make WordPress Core

Changeset 30959


Ignore:
Timestamp:
12/17/2014 05:35:48 PM (10 years ago)
Author:
nacin
Message:

Update individual term caches in get_terms().

Merges [30954] to the 4.1 branch.

props boonebgorges.
fixes #30749. see #21760.

Location:
branches/4.1
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1

  • branches/4.1/src/wp-includes/taxonomy.php

    r30914 r30959  
    19131913
    19141914    $terms = $wpdb->get_results($query);
     1915    if ( 'all' == $_fields ) {
     1916        update_term_cache( $terms );
     1917    }
    19151918
    19161919    if ( empty($terms) ) {
  • branches/4.1/tests/phpunit/tests/term/cache.php

    r30914 r30959  
    9494        _unregister_taxonomy( $tax );
    9595    }
     96
     97    public function test_get_term_should_update_term_cache_when_passed_an_object() {
     98        global $wpdb;
     99
     100        register_taxonomy( 'wptests_tax', 'post' );
     101        $term = $this->factory->term->create( array(
     102            'taxonomy' => 'wptests_tax',
     103        ) );
     104
     105        $term_object = get_term( $term, 'wptests_tax' );
     106        wp_cache_delete( $term, 'wptests_tax' );
     107
     108        // Affirm that the cache is empty.
     109        $this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     110
     111        $num_queries = $wpdb->num_queries;
     112
     113        // get_term() will only be update the cache if the 'filter' prop is unset.
     114        unset( $term_object->filter );
     115
     116        $term_object_2 = get_term( $term_object, 'wptests_tax' );
     117
     118        // No new queries should have fired.
     119        $this->assertSame( $num_queries, $wpdb->num_queries );
     120        $this->assertEquals( $term_object, $term_object_2 );
     121    }
     122
     123    public function test_get_term_should_update_term_cache_when_passed_a_valid_term_identifier() {
     124        global $wpdb;
     125
     126        register_taxonomy( 'wptests_tax', 'post' );
     127        $term = $this->factory->term->create( array(
     128            'taxonomy' => 'wptests_tax',
     129        ) );
     130
     131        wp_cache_delete( $term, 'wptests_tax' );
     132
     133        // Affirm that the cache is empty.
     134        $this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     135
     136        $num_queries = $wpdb->num_queries;
     137
     138        // Prime cache.
     139        $term_object = get_term( $term, 'wptests_tax' );
     140        $this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     141        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     142
     143        $term_object_2 = get_term( $term, 'wptests_tax' );
     144
     145        // No new queries should have fired.
     146        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     147        $this->assertEquals( $term_object, $term_object_2 );
     148    }
     149
     150    public function test_get_term_by_should_update_term_cache_when_passed_a_valid_term_identifier() {
     151        global $wpdb;
     152
     153        register_taxonomy( 'wptests_tax', 'post' );
     154        $term = $this->factory->term->create( array(
     155            'taxonomy' => 'wptests_tax',
     156        ) );
     157
     158        wp_cache_delete( $term, 'wptests_tax' );
     159
     160        // Affirm that the cache is empty.
     161        $this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     162
     163        $num_queries = $wpdb->num_queries;
     164
     165        // Prime cache.
     166        $term_object = get_term_by( 'id', $term, 'wptests_tax' );
     167        $this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     168        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     169
     170        $term_object_2 = get_term( $term, 'wptests_tax' );
     171
     172        // No new queries should have fired.
     173        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     174        $this->assertEquals( $term_object, $term_object_2 );
     175    }
     176
     177    /**
     178     * @ticket 30749
     179     */
     180    public function test_get_terms_should_update_cache_for_located_terms() {
     181        global $wpdb;
     182
     183        register_taxonomy( 'wptests_tax', 'post' );
     184
     185        $terms = $this->factory->term->create_many( 5, array(
     186            'taxonomy' => 'wptests_tax',
     187        ) );
     188
     189        $term_objects = get_terms( 'wptests_tax', array(
     190            'hide_empty' => false,
     191        ) );
     192
     193        $num_queries = $wpdb->num_queries;
     194
     195        foreach ( $terms as $term_id ) {
     196            get_term( $term_id, 'wptests_tax' );
     197        }
     198
     199        $this->assertSame( $num_queries, $wpdb->num_queries );
     200
     201        _unregister_taxonomy( 'wptests_tax' );
     202    }
    96203}
Note: See TracChangeset for help on using the changeset viewer.