Make WordPress Core

Ticket #44658: 44658.diff

File 44658.diff, 6.5 KB (added by soulseekah, 7 years ago)
  • src/wp-includes/class-wp-tax-query.php

    diff --git src/wp-includes/class-wp-tax-query.php src/wp-includes/class-wp-tax-query.php
    index 4b58635..cdefcd8 100644
    class WP_Tax_Query { 
    9090         *
    9191         * @since 3.1.0
    9292         * @since 4.1.0 Added support for `$operator` 'NOT EXISTS' and 'EXISTS' values.
     93         * @since 5.0.0 Added support for `$operator` 'BETWEEN' values.
    9394         *
    9495         * @param array $tax_query {
    9596         *     Array of taxonomy query clauses.
    class WP_Tax_Query { 
    104105         *         @type string           $field            Field to match $terms against. Accepts 'term_id', 'slug',
    105106         *                                                 'name', or 'term_taxonomy_id'. Default: 'term_id'.
    106107         *         @type string           $operator         MySQL operator to be used with $terms in the WHERE clause.
    107          *                                                  Accepts 'AND', 'IN', 'NOT IN', 'EXISTS', 'NOT EXISTS'.
     108         *                                                  Accepts 'AND', 'IN', 'NOT IN', 'EXISTS', 'NOT EXISTS', 'BETWEEN'.
    108109         *                                                  Default: 'IN'.
    109110         *         @type bool             $include_children Optional. Whether to include child terms.
    110111         *                                                  Requires a $taxonomy. Default: true.
    class WP_Tax_Query { 
    399400                $terms    = $clause['terms'];
    400401                $operator = strtoupper( $clause['operator'] );
    401402
    402                 if ( 'IN' == $operator ) {
     403                if ( 'IN' == $operator || 'BETWEEN' == $operator ) {
    403404
    404405                        if ( empty( $terms ) ) {
    405406                                return self::$no_results;
    class WP_Tax_Query { 
    427428                                $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)";
    428429                        }
    429430
     431                        $operator = 'IN';
     432
    430433                        $where = "$alias.term_taxonomy_id $operator ($terms)";
    431434
    432435                } elseif ( 'NOT IN' == $operator ) {
    class WP_Tax_Query { 
    621624                                $args['slug'] = $terms;
    622625                                break;
    623626                        case 'name':
    624                                 $args['name'] = $terms;
     627                                if ( 'BETWEEN' == strtoupper( $query['operator'] ) ) {
     628                                        $args['name__between'] = $terms;
     629                                } else {
     630                                        $args['name']          = $terms;
     631                                }
    625632                                break;
    626633                        case 'term_taxonomy_id':
    627634                                $args['term_taxonomy_id'] = $terms;
  • src/wp-includes/class-wp-term-query.php

    diff --git src/wp-includes/class-wp-term-query.php src/wp-includes/class-wp-term-query.php
    index 5890bf7..a3e7eda 100644
    class WP_Term_Query { 
    8888         * @since 4.6.0 Introduced 'term_taxonomy_id' parameter.
    8989         * @since 4.7.0 Introduced 'object_ids' parameter.
    9090         * @since 4.9.0 Added 'slug__in' support for 'orderby'.
     91         * @since 5.0.0 Added 'name__between' parameter.
    9192         *
    9293         * @param string|array $query {
    9394         *     Optional. Array or query string of term query parameters. Default empty.
    class WP_Term_Query { 
    148149         *                                                wildcards before and after. Default empty.
    149150         *     @type string       $name__like             Retrieve terms with criteria by which a term is LIKE
    150151         *                                                `$name__like`. Default empty.
     152         *     @type string       $name__between          Retrieve terms with criteria by which a term is BETWEEN
     153         *                                                the two array values supplied in `$name__between`. Default empty.
    151154         *     @type string       $description__like      Retrieve terms where the description is LIKE
    152155         *                                                `$description__like`. Default empty.
    153156         *     @type bool         $pad_counts             Whether to pad the quantity of a term's children in the
    class WP_Term_Query { 
    197200                        'hierarchical'           => true,
    198201                        'search'                 => '',
    199202                        'name__like'             => '',
     203                        'name__between'          => '',
    200204                        'description__like'      => '',
    201205                        'pad_counts'             => false,
    202206                        'get'                    => '',
    class WP_Term_Query { 
    513517                        $this->sql_clauses['where']['name__like'] = $wpdb->prepare( 't.name LIKE %s', '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
    514518                }
    515519
     520                if ( ! empty( $args['name__between'] ) ) {
     521                        $this->sql_clauses['where']['name__between'] = $wpdb->prepare( 'CAST(t.name AS SIGNED) BETWEEN %s AND %s', array_slice( $args['name__between'], 0, 2 ) );
     522                }
     523
    516524                if ( ! empty( $args['description__like'] ) ) {
    517525                        $this->sql_clauses['where']['description__like'] = $wpdb->prepare( 'tt.description LIKE %s', '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
    518526                }
  • tests/phpunit/tests/query/taxQuery.php

    diff --git tests/phpunit/tests/query/taxQuery.php tests/phpunit/tests/query/taxQuery.php
    index fea31ed..7add121 100644
    class Tests_Query_TaxQuery extends WP_UnitTestCase { 
    16201620
    16211621                $this->assertEqualSets( array( $p ), $q->posts );
    16221622        }
     1623
     1624        public function test_tax_terms_between() {
     1625                register_taxonomy( $tax = 'height', 'post' );
     1626
     1627                $posts = array();
     1628
     1629                foreach ( range( 0, 20 ) as $height ) {
     1630                        $t = self::factory()->term->create(
     1631                                array(
     1632                                        'name'     => strval( $height ),
     1633                                        'taxonomy' => $tax,
     1634                                )
     1635                        );
     1636
     1637                        $p = self::factory()->post->create();
     1638                        wp_set_object_terms( $p, array( $t ), $tax );
     1639                        $posts[] = $p;
     1640                }
     1641
     1642                $q = new WP_Query(
     1643                        array(
     1644                                'fields'    => 'ids',
     1645                                'tax_query' => array(
     1646                                        array(
     1647                                                'taxonomy' => $tax,
     1648                                                'operator' => 'between',
     1649                                                'field'    => 'name',
     1650                                                'terms'    => array( 7, 13 ),
     1651                                        ),
     1652                                ),
     1653                        )
     1654                );
     1655
     1656                $this->assertEqualSets( array_slice( $posts, 7, 7 ), $q->posts );
     1657        }
    16231658}
  • tests/phpunit/tests/term/getTerms.php

    diff --git tests/phpunit/tests/term/getTerms.php tests/phpunit/tests/term/getTerms.php
    index 6e38eea..dc2fa83 100644
    class Tests_Term_getTerms extends WP_UnitTestCase { 
    28412841                $this->assertEqualSets( array( $term_id1, $term_id2 ), wp_list_pluck( $found, 'term_id' ) );
    28422842        }
    28432843
     2844        public function test_get_terms_between() {
     2845                register_taxonomy( $tax = 'length', 'post' );
     2846
     2847                $term_ids = array();
     2848
     2849                foreach ( range( 0, 20 ) as $length ) {
     2850                        $term_ids[] = self::factory()->term->create(
     2851                                array(
     2852                                        'name'     => strval( $length ),
     2853                                        'taxonomy' => $tax,
     2854                                )
     2855                        );
     2856                }
     2857
     2858                $terms = get_terms(
     2859                        $tax, array(
     2860                                'hide_empty'    => false,
     2861                                'name__between' => array( 7, 13 ),
     2862                                'fields'        => 'ids',
     2863                        )
     2864                );
     2865                $this->assertEqualSets( array_slice( $term_ids, 7, 7 ), $terms );
     2866        }
     2867
    28442868        protected function create_hierarchical_terms_and_posts() {
    28452869                $terms = array();
    28462870