Make WordPress Core

Changeset 42209


Ignore:
Timestamp:
11/20/2017 10:45:29 PM (7 years ago)
Author:
boonebgorges
Message:

Improve data types returned from empty hierarchical term queries.

When querying for 'count', ensure that 0 is returned. Otherwise,
ensure that it's an array.

Props xParham, birgire.
Fixes #42327.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r41881 r42209  
    380380
    381381            if ( ! $in_hierarchy ) {
    382                 return array();
     382                if ( 'count' == $args['fields'] ) {
     383                    return 0;
     384                } else {
     385                    $this->terms = array();
     386                    return $this->terms;
     387                }
    383388            }
    384389        }
  • trunk/tests/phpunit/tests/term/query.php

    r41880 r42209  
    472472        $this->assertSame( $expected, $found3 );
    473473    }
     474
     475    /**
     476     * The query method should return zero for field as count and parent set.
     477     *
     478     * @ticket 42327
     479     */
     480    public function test_query_should_return_zero_for_field_count_and_parent_set() {
     481        $post_id = self::factory()->post->create();
     482        register_taxonomy( 'wptests_tax', 'post' );
     483
     484        $term_id = self::factory()->term->create( array(
     485            'taxonomy' => 'wptests_tax',
     486        ) );
     487        wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
     488
     489        $q = new WP_Term_Query();
     490        $args = array(
     491            'taxonomy' => 'wptests_tax',
     492            'parent'   => $term_id,
     493            'fields'   => 'count',
     494        );
     495        $this->assertSame( 0, $q->query( $args ) );
     496    }
     497
     498    /**
     499     * The query method should return zero for field as count and child_of set.
     500     *
     501     * @ticket 42327
     502     */
     503    public function test_query_should_return_zero_for_field_as_count_and_child_of_set() {
     504        $post_id = self::factory()->post->create();
     505        register_taxonomy( 'wptests_tax', 'post' );
     506
     507        $term_id = self::factory()->term->create( array(
     508            'taxonomy' => 'wptests_tax',
     509        ) );
     510        wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
     511
     512        $q = new WP_Term_Query();
     513        $args = array(
     514            'taxonomy' => 'wptests_tax',
     515            'child_of' => $term_id,
     516            'fields'   => 'count',
     517        );
     518        $this->assertSame( 0, $q->query( $args ) );
     519    }
     520
     521    /**
     522     * The terms property should be an empty array for fields not as count and parent set.
     523     *
     524     * @ticket 42327
     525     */
     526    public function test_terms_property_should_be_empty_array_for_field_not_as_count_and_parent_set() {
     527        $post_id = self::factory()->post->create();
     528        register_taxonomy( 'wptests_tax', 'post' );
     529
     530        $term_id = self::factory()->term->create( array(
     531            'taxonomy' => 'wptests_tax',
     532        ) );
     533        wp_set_object_terms( $post_id, array( $term_id ), 'wptests_tax' );
     534
     535        $q = new WP_Term_Query( array(
     536            'taxonomy' => 'wptests_tax',
     537            'parent'   => $term_id,
     538        ) );
     539        $this->assertSame( array(), $q->terms );
     540    }
    474541}
Note: See TracChangeset for help on using the changeset viewer.