WordPress.org

Make WordPress Core

Changeset 29760


Ignore:
Timestamp:
09/23/2014 03:51:24 AM (7 years ago)
Author:
wonderboymusic
Message:

Ordering by RAND():

The shortcode callbacks for gallery and playlist check for 'RAND' == $atts['order'], which isn't a valid value for order. Remove those checks and update the docs.

In WP_Query, if the value of orderby is rand, order is irrelevant and should be unset.

Adds unit tests.

Fixes #29629.

Location:
trunk
Files:
3 edited

Legend:

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

    r29750 r29760  
    960960
    961961    $id = intval( $atts['id'] );
    962     if ( 'RAND' == $atts['order'] ) {
    963         $atts['orderby'] = 'none';
    964     }
    965962
    966963    if ( ! empty( $atts['include'] ) ) {
     
    11671164 *     @type string  $type         Type of playlist to display. Accepts 'audio' or 'video'. Default 'audio'.
    11681165 *     @type string  $order        Designates ascending or descending order of items in the playlist.
    1169  *                                 Accepts 'ASC', 'DESC', or 'RAND'. Default 'ASC'.
     1166 *                                 Accepts 'ASC', 'DESC'. Default 'ASC'.
    11701167 *     @type string  $orderby      Any column, or columns, to sort the playlist. If $ids are
    11711168 *                                 passed, this defaults to the order of the $ids array ('post__in').
     
    12441241
    12451242    $id = intval( $atts['id'] );
    1246     if ( 'RAND' == $atts['order'] ) {
    1247         $atts['orderby'] = 'none';
    1248     }
    12491243
    12501244    $args = array(
  • trunk/src/wp-includes/query.php

    r29658 r29760  
    27962796        $where .= $search . $whichauthor . $whichmimetype;
    27972797
     2798        $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
    27982799        if ( ! isset( $q['order'] ) ) {
    2799             $q['order'] = 'DESC';
     2800            $q['order'] = $rand ? '' : 'DESC';
    28002801        } else {
    2801             $q['order'] = $this->parse_order( $q['order'] );
     2802            $q['order'] = $rand ? '' : $this->parse_order( $q['order'] );
    28022803        }
    28032804
     
    28502851
    28512852                if ( empty( $orderby ) ) {
    2852                     $orderby = "$wpdb->posts.post_date ".$q['order'];
    2853                 } else {
     2853                    $orderby = "$wpdb->posts.post_date " . $q['order'];
     2854                } elseif ( ! empty( $q['order'] ) ) {
    28542855                    $orderby .= " {$q['order']}";
    28552856                }
  • trunk/tests/phpunit/tests/post/query.php

    r29027 r29760  
    832832        );
    833833    }
     834
     835    /**
     836     * @ticket 29629
     837     */
     838    function test_orderby() {
     839        // 'rand' is a valid value
     840        $q = new WP_Query( array( 'orderby' => 'rand' ) );
     841        $this->assertContains( 'ORDER BY RAND()', $q->request );
     842        $this->assertNotContains( 'ASC', $q->request );
     843        $this->assertNotContains( 'DESC', $q->request );
     844
     845        // This isn't allowed
     846        $q2 = new WP_Query( array( 'order' => 'rand' ) );
     847        $this->assertContains( 'ORDER BY', $q2->request );
     848        $this->assertNotContains( 'RAND()', $q2->request );
     849        $this->assertContains( 'DESC', $q2->request );
     850
     851        // 'none' is a valid value
     852        $q3 = new WP_Query( array( 'orderby' => 'none' ) );
     853        $this->assertNotContains( 'ORDER BY', $q3->request );
     854        $this->assertNotContains( 'DESC', $q3->request );
     855        $this->assertNotContains( 'ASC', $q3->request );
     856
     857        // false is a valid value
     858        $q4 = new WP_Query( array( 'orderby' => false ) );
     859        $this->assertNotContains( 'ORDER BY', $q4->request );
     860        $this->assertNotContains( 'DESC', $q4->request );
     861        $this->assertNotContains( 'ASC', $q4->request );
     862
     863        // empty array() is a valid value
     864        $q5 = new WP_Query( array( 'orderby' => array() ) );
     865        $this->assertNotContains( 'ORDER BY', $q5->request );
     866        $this->assertNotContains( 'DESC', $q5->request );
     867        $this->assertNotContains( 'ASC', $q5->request );
     868    }
    834869}
Note: See TracChangeset for help on using the changeset viewer.