From 0b0eb4fb20586aeb58a104959d02ce91bf07a018 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 23 Sep 2014 03:51:24 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@29760 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 8 +------ src/wp-includes/query.php | 9 ++++---- tests/phpunit/tests/post/query.php | 35 ++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index b9d38fcf0a..73877bfb1d 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -959,9 +959,6 @@ function gallery_shortcode( $attr ) { ), $attr, 'gallery' ); $id = intval( $atts['id'] ); - if ( 'RAND' == $atts['order'] ) { - $atts['orderby'] = 'none'; - } if ( ! empty( $atts['include'] ) ) { $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); @@ -1166,7 +1163,7 @@ add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' ); * * @type string $type Type of playlist to display. Accepts 'audio' or 'video'. Default 'audio'. * @type string $order Designates ascending or descending order of items in the playlist. - * Accepts 'ASC', 'DESC', or 'RAND'. Default 'ASC'. + * Accepts 'ASC', 'DESC'. Default 'ASC'. * @type string $orderby Any column, or columns, to sort the playlist. If $ids are * passed, this defaults to the order of the $ids array ('post__in'). * Otherwise default is 'menu_order ID'. @@ -1243,9 +1240,6 @@ function wp_playlist_shortcode( $attr ) { ), $attr, 'playlist' ); $id = intval( $atts['id'] ); - if ( 'RAND' == $atts['order'] ) { - $atts['orderby'] = 'none'; - } $args = array( 'post_status' => 'inherit', diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 39a22c7dc5..a832e6d515 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2795,10 +2795,11 @@ class WP_Query { $where .= $search . $whichauthor . $whichmimetype; + $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] ); if ( ! isset( $q['order'] ) ) { - $q['order'] = 'DESC'; + $q['order'] = $rand ? '' : 'DESC'; } else { - $q['order'] = $this->parse_order( $q['order'] ); + $q['order'] = $rand ? '' : $this->parse_order( $q['order'] ); } // Order by. @@ -2849,8 +2850,8 @@ class WP_Query { $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array ); if ( empty( $orderby ) ) { - $orderby = "$wpdb->posts.post_date ".$q['order']; - } else { + $orderby = "$wpdb->posts.post_date " . $q['order']; + } elseif ( ! empty( $q['order'] ) ) { $orderby .= " {$q['order']}"; } } diff --git a/tests/phpunit/tests/post/query.php b/tests/phpunit/tests/post/query.php index 004e710f25..d66e5339ec 100644 --- a/tests/phpunit/tests/post/query.php +++ b/tests/phpunit/tests/post/query.php @@ -831,4 +831,39 @@ class Tests_Post_Query extends WP_UnitTestCase { $q3->request ); } + + /** + * @ticket 29629 + */ + function test_orderby() { + // 'rand' is a valid value + $q = new WP_Query( array( 'orderby' => 'rand' ) ); + $this->assertContains( 'ORDER BY RAND()', $q->request ); + $this->assertNotContains( 'ASC', $q->request ); + $this->assertNotContains( 'DESC', $q->request ); + + // This isn't allowed + $q2 = new WP_Query( array( 'order' => 'rand' ) ); + $this->assertContains( 'ORDER BY', $q2->request ); + $this->assertNotContains( 'RAND()', $q2->request ); + $this->assertContains( 'DESC', $q2->request ); + + // 'none' is a valid value + $q3 = new WP_Query( array( 'orderby' => 'none' ) ); + $this->assertNotContains( 'ORDER BY', $q3->request ); + $this->assertNotContains( 'DESC', $q3->request ); + $this->assertNotContains( 'ASC', $q3->request ); + + // false is a valid value + $q4 = new WP_Query( array( 'orderby' => false ) ); + $this->assertNotContains( 'ORDER BY', $q4->request ); + $this->assertNotContains( 'DESC', $q4->request ); + $this->assertNotContains( 'ASC', $q4->request ); + + // empty array() is a valid value + $q5 = new WP_Query( array( 'orderby' => array() ) ); + $this->assertNotContains( 'ORDER BY', $q5->request ); + $this->assertNotContains( 'DESC', $q5->request ); + $this->assertNotContains( 'ASC', $q5->request ); + } } \ No newline at end of file