Make WordPress Core

Changeset 37567


Ignore:
Timestamp:
05/25/2016 06:44:00 PM (10 years ago)
Author:
boonebgorges
Message:

Fix termmeta pre-fetching in wp_get_object_terms().

[34529] introduced logic intended to prime the termmeta cache for certain
values of the fields parameter. There were a few bugs:

  • The all_with_object_id param was misspelled.
  • term_id was used instead of ids.
  • The values being passed to update_termmeta_cache() in the case where fields=ids was not correct.

All of these would result in a failure to pre-fetch termmeta in some cases.

Props dlh.
Fixes #36932.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r37544 r37567  
    26292629
    26302630        // Update termmeta cache, if necessary.
    2631         if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_ids' === $fields || 'term_id' === $fields ) ) {
    2632                 if ( 'term_id' === $fields ) {
    2633                         $term_ids = $fields;
     2631        if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_id' === $fields || 'ids' === $fields ) ) {
     2632                if ( 'ids' === $fields ) {
     2633                        $term_ids = $terms;
    26342634                } else {
    26352635                        $term_ids = wp_list_pluck( $terms, 'term_id' );
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r35242 r37567  
    474474
    475475        /**
     476         * @ticket 36932
     477         */
     478        public function test_termmeta_cache_should_be_primed_when_fields_is_all_with_object_id() {
     479                global $wpdb;
     480
     481                register_taxonomy( 'wptests_tax', 'post' );
     482                $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     483                add_term_meta( $terms[0], 'foo', 'bar' );
     484                add_term_meta( $terms[1], 'foo', 'bar' );
     485                add_term_meta( $terms[2], 'foo', 'bar' );
     486
     487                $p = self::factory()->post->create();
     488                wp_set_object_terms( $p, $terms, 'wptests_tax' );
     489
     490                $found = wp_get_object_terms( $p, 'wptests_tax', array(
     491                        'update_term_meta_cache' => true,
     492                        'fields' => 'all_with_object_id',
     493                ) );
     494
     495                $num_queries = $wpdb->num_queries;
     496
     497                foreach ( $terms as $t ) {
     498                        $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
     499                }
     500
     501                $this->assertSame( $num_queries, $wpdb->num_queries );
     502        }
     503
     504        /**
     505         * @ticket 36932
     506         */
     507        public function test_termmeta_cache_should_be_primed_when_fields_is_ids() {
     508                global $wpdb;
     509
     510                register_taxonomy( 'wptests_tax', 'post' );
     511                $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     512                add_term_meta( $terms[0], 'foo', 'bar' );
     513                add_term_meta( $terms[1], 'foo', 'bar' );
     514                add_term_meta( $terms[2], 'foo', 'bar' );
     515
     516                $p = self::factory()->post->create();
     517                wp_set_object_terms( $p, $terms, 'wptests_tax' );
     518
     519                $found = wp_get_object_terms( $p, 'wptests_tax', array(
     520                        'update_term_meta_cache' => true,
     521                        'fields' => 'ids',
     522                ) );
     523
     524                $num_queries = $wpdb->num_queries;
     525
     526                foreach ( $terms as $t ) {
     527                        $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
     528                }
     529
     530                $this->assertSame( $num_queries, $wpdb->num_queries );
     531        }
     532
     533        /**
    476534         * @ticket 10142
    477535         */
Note: See TracChangeset for help on using the changeset viewer.