Make WordPress Core

Ticket #42691: 42691.2.diff

File 42691.2.diff, 3.3 KB (added by boonebgorges, 5 years ago)
  • src/wp-includes/class-wp-term-query.php

    diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php
    index 7bd537cef4..dec8ac0719 100644
    a b class WP_Term_Query { 
    678678                $cache        = wp_cache_get( $cache_key, 'terms' );
    679679                if ( false !== $cache ) {
    680680                        if ( 'all' === $_fields ) {
    681                                 $cache = array_map( 'get_term', $cache );
     681                                $cache = $this->populate_terms( $cache );
    682682                        }
    683683
    684684                        $this->terms = $cache;
    class WP_Term_Query { 
    810810                wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
    811811
    812812                if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) {
    813                         $terms = array_map( 'get_term', $terms );
     813                        $terms = $this->populate_terms( $terms );
    814814                }
    815815
    816816                $this->terms = $terms;
    class WP_Term_Query { 
    972972
    973973                return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
    974974        }
     975
     976        /**
     977         * Creates an array of term objects from an array of term IDs.
     978         *
     979         * Also discards invalid term objects.
     980         *
     981         * @since 5.0.0
     982         *
     983         * @param array $term_ids Term IDs.
     984         * @return array
     985         */
     986        protected function populate_terms( $term_ids ) {
     987                $terms = array();
     988                foreach ( $term_ids as $key => $term_id ) {
     989                        $term = get_term( $term_id );
     990                        if ( $term instanceof WP_Term ) {
     991                                $terms[ $key ] = $term;
     992                        }
     993                }
     994
     995                return $terms;
     996        }
    975997}
  • tests/phpunit/tests/term/query.php

    diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php
    index b517417f06..39496b16cb 100644
    a b class Tests_Term_Query extends WP_UnitTestCase { 
    614614                );
    615615                $this->assertSame( array(), $q->terms );
    616616        }
     617
     618        /**
     619         * @ticket 42691
     620         */
     621        public function test_null_term_object_should_be_discarded() {
     622                register_taxonomy( 'wptests_tax', 'post' );
     623
     624                $terms = self::factory()->term->create_many( 3, array(
     625                        'taxonomy' => 'wptests_tax',
     626                ) );
     627
     628                $this->term_id = $terms[1];
     629
     630                add_filter( 'get_term', array( $this, 'filter_term_to_null' ) );
     631                $found = get_terms( array(
     632                        'taxonomy'   => 'wptests_tax',
     633                        'hide_empty' => false,
     634                ) );
     635                remove_filter( 'get_term', array( $this, 'filter_term_to_null' ) );
     636
     637                $expected = array( $terms[0], $terms[2] );
     638
     639                $this->assertEqualSets( $expected, wp_list_pluck( $found, 'term_id' ) );
     640        }
     641
     642        public function filter_term_to_null( $term ) {
     643                if ( $this->term_id === $term->term_id ) {
     644                        return null;
     645                }
     646
     647                return $term;
     648        }
     649
     650        /**
     651         * @ticket 42691
     652         */
     653        public function test_error_term_object_should_be_discarded() {
     654                register_taxonomy( 'wptests_tax', 'post' );
     655
     656                $terms = self::factory()->term->create_many( 3, array(
     657                        'taxonomy' => 'wptests_tax',
     658                ) );
     659
     660                $this->term_id = $terms[1];
     661
     662                add_filter( 'get_term', array( $this, 'filter_term_to_wp_error' ) );
     663                $found = get_terms( array(
     664                        'taxonomy'   => 'wptests_tax',
     665                        'hide_empty' => false,
     666                ) );
     667                remove_filter( 'get_term', array( $this, 'filter_term_to_wp_error' ) );
     668
     669                $expected = array( $terms[0], $terms[2] );
     670
     671                $this->assertEqualSets( $expected, wp_list_pluck( $found, 'term_id' ) );
     672        }
     673
     674        public function filter_term_to_wp_error( $term ) {
     675                if ( $this->term_id === $term->term_id ) {
     676                        return new WP_Error( 'foo' );
     677                }
     678
     679                return $term;
     680        }
    617681}