Make WordPress Core

Ticket #35692: 35692.2.diff

File 35692.2.diff, 2.1 KB (added by boonebgorges, 9 years ago)
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index 053f4e9..b8c245f 100644
    class WP_Query { 
    23322332                        $allowed_keys   = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
    23332333                }
    23342334
     2335                // Sanitize RAND() with a seed value and add to allowed keys.
     2336                $rand_with_seed = false;
     2337                if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) {
     2338                        $orderby = sprintf( 'RAND(%s)', intval( $matches[1] ) );
     2339                        $allowed_keys[] = $orderby;
     2340                        $rand_with_seed = true;
     2341                }
     2342
    23352343                if ( ! in_array( $orderby, $allowed_keys, true ) ) {
    23362344                        return false;
    23372345                }
    class WP_Query { 
    23682376                                        // $orderby corresponds to a meta_query clause.
    23692377                                        $meta_clause = $meta_clauses[ $orderby ];
    23702378                                        $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
     2379                                } elseif ( $rand_with_seed ) {
     2380                                        $orderby_clause = $orderby;
    23712381                                } else {
    23722382                                        // Default: order by post field.
    23732383                                        $orderby_clause = "$wpdb->posts.post_" . sanitize_key( $orderby );
  • tests/phpunit/tests/post/query.php

    diff --git tests/phpunit/tests/post/query.php tests/phpunit/tests/post/query.php
    index bc0e865..3571538 100644
    class Tests_Post_Query extends WP_UnitTestCase { 
    304304        }
    305305
    306306        /**
     307         * @ticket 35692
     308         */
     309        public function test_orderby_rand_with_seed() {
     310                $q = new WP_Query( array(
     311                        'orderby' => 'RAND(5)',
     312                ) );
     313
     314                $this->assertContains( 'ORDER BY RAND(5)', $q->request );
     315        }
     316
     317        /**
     318         * @ticket 35692
     319         */
     320        public function test_orderby_rand_should_ignore_invalid_seed() {
     321                $q = new WP_Query( array(
     322                        'orderby' => 'RAND(foo)',
     323                ) );
     324
     325                $this->assertNotContains( 'ORDER BY RAND', $q->request );
     326        }
     327
     328        /**
     329         * @ticket 35692
     330         */
     331        public function test_orderby_rand_with_seed_should_be_case_insensitive() {
     332                $q = new WP_Query( array(
     333                        'orderby' => 'rand(5)',
     334                ) );
     335
     336                $this->assertContains( 'ORDER BY RAND(5)', $q->request );
     337        }
     338
     339        /**
    307340         * Tests the post_name__in attribute of WP_Query.
    308341         *
    309342         * @ticket 33065