diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 908207ff3e..7216ca0f53 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -2668,7 +2668,7 @@ class WP_Query { $orderby_array[] = $orderby; } - $orderby = implode( ',', $orderby_array ); + $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array ); if ( empty( $orderby ) ) $orderby = "$wpdb->posts.post_date ".$q['order']; diff --git a/src/wp-includes/revision.php b/src/wp-includes/revision.php index 1203d78105..52412cdb51 100644 --- a/src/wp-includes/revision.php +++ b/src/wp-includes/revision.php @@ -417,7 +417,7 @@ function wp_get_post_revisions( $post_id = 0, $args = null ) { if ( ! $post || empty( $post->ID ) ) return array(); - $defaults = array( 'order' => 'DESC', 'orderby' => 'date', 'check_enabled' => true ); + $defaults = array( 'order' => 'ASC', 'orderby' => 'date ID', 'check_enabled' => true ); $args = wp_parse_args( $args, $defaults ); if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) @@ -428,6 +428,8 @@ function wp_get_post_revisions( $post_id = 0, $args = null ) { if ( ! $revisions = get_children( $args ) ) return array(); + $revisions = array_reverse( $revisions ); + return $revisions; } diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php index 49e4f84948..ce53784b0d 100644 --- a/tests/phpunit/tests/post/revisions.php +++ b/tests/phpunit/tests/post/revisions.php @@ -338,4 +338,51 @@ class Tests_Post_Revisions extends WP_UnitTestCase { $this->assertTrue( user_can( $author_user_id, 'read_post', $revision->ID ) ); } } + + /** + * @ticket 26042 + */ + function test_wp_get_posts_revisions_sql() { + $post = get_default_post_to_edit( 'post', true ); + + add_filter( 'query', array( $this, '_filter_query' ) ); + + wp_get_post_revisions( $post->ID ); + } + + function _filter_query( $sql ) { + remove_filter( 'query', array( $this, '_filter_query' ) ); + global $wpdb; + $this->assertContains( "ORDER BY $wpdb->posts.post_date ASC, $wpdb->posts.ID ASC", $sql ); + return $sql; + } + + /** + * @ticket 26042 + */ + function test_revision_order() { + $ok = 0; + $reversed = 0; + + for ( $i = 0; $i < 100; $i++ ) { + $post_id = $this->factory->post->create( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) ); + + for ( $j = 1; $j < 3; $j++ ) { + wp_update_post( array( 'post_content' => 'updated post' . $j , 'ID' => $post_id ) ); + } + + $revisions = wp_get_post_revisions( $post_id ); + $first = array_shift( $revisions ); + $last = array_pop( $revisions ); + + if ( $first->ID < $last->ID ) { + $reversed++; + } else { + $ok++; + } + } + + $this->assertEquals( 100, $ok ); + $this->assertEquals( 0, $reversed ); + } }