Make WordPress Core

Changeset 28541


Ignore:
Timestamp:
05/22/2014 06:18:11 PM (12 years ago)
Author:
wonderboymusic
Message:

Apply order to each passed value for orderby in WP_Query:

  • Since orderby in WP_Query can accept space-delimited sets, yet only one order value: when multiple values are passed (and DESC is the order), the default sort order ASC is being applied to all values before the last in the set.
  • There is a unit test that sporadically fails since 3.6 in tests/post/revision due to multiple posts having the same post_date from being added so rapidly
  • When ordering revisions in wp_get_post_revisions(), order by post_date ID
  • Change the order value in wp_get_post_revisions() to ASC. This will produce SQL like: ORDER BY $wpdb->posts.post_date ASC, $wpdb->posts.ID ASC. Previously, this would have produced SQL like: ORDER BY $wpdb->posts.post_date DESC, and with the addition of ID: ORDER BY $wpdb->posts.post_date ASC, $wpdb->posts.ID DESC. Clearly, wrong. The original SQL produced: ORDER BY $wpdb->posts.post_date DESC. As such, return the reversions in reverse order using array_reverse(). Not doing so would break "Preview Changes."
  • Add unit tests to assert that all of this works.
  • All existing unit tests pass with the change to ordering multiple orderbys in WP_Query.
  • In the future, we should support independent order for each orderby, see #17065.

Props SergeyBiryukov, wonderboymusic.
Fixes #26042.

Location:
trunk
Files:
3 edited

Legend:

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

    r28533 r28541  
    26692669                                $orderby_array[] = $orderby;
    26702670                        }
    2671                         $orderby = implode( ',', $orderby_array );
     2671                        $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
    26722672
    26732673                        if ( empty( $orderby ) )
  • trunk/src/wp-includes/revision.php

    r28334 r28541  
    418418                return array();
    419419
    420         $defaults = array( 'order' => 'DESC', 'orderby' => 'date', 'check_enabled' => true );
     420        $defaults = array( 'order' => 'ASC', 'orderby' => 'date ID', 'check_enabled' => true );
    421421        $args = wp_parse_args( $args, $defaults );
    422422
     
    428428        if ( ! $revisions = get_children( $args ) )
    429429                return array();
     430
     431        $revisions = array_reverse( $revisions );
    430432
    431433        return $revisions;
  • trunk/tests/phpunit/tests/post/revisions.php

    r25002 r28541  
    339339                }
    340340        }
     341
     342        /**
     343         * @ticket 26042
     344         */
     345        function test_wp_get_posts_revisions_sql() {
     346                $post = get_default_post_to_edit( 'post', true );
     347
     348                add_filter( 'query', array( $this, '_filter_query' ) );
     349
     350                wp_get_post_revisions( $post->ID );
     351        }
     352
     353        function _filter_query( $sql ) {
     354                remove_filter( 'query', array( $this, '_filter_query' ) );
     355                global $wpdb;
     356                $this->assertContains( "ORDER BY $wpdb->posts.post_date ASC, $wpdb->posts.ID ASC", $sql );
     357                return $sql;
     358        }
     359
     360        /**
     361         * @ticket 26042
     362         */
     363        function test_revision_order() {
     364                $ok = 0;
     365                $reversed = 0;
     366
     367                for ( $i = 0; $i < 100; $i++ ) {
     368                        $post_id = $this->factory->post->create( array( 'post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content' ) );
     369
     370                        for ( $j = 1; $j < 3; $j++ ) {
     371                                wp_update_post( array( 'post_content' => 'updated post' . $j , 'ID' => $post_id ) );
     372                        }
     373
     374                        $revisions = wp_get_post_revisions( $post_id );
     375                        $first = array_shift( $revisions );
     376                        $last = array_pop( $revisions );
     377
     378                        if ( $first->ID < $last->ID ) {
     379                                $reversed++;
     380                        } else {
     381                                $ok++;
     382                        }
     383                }
     384
     385                $this->assertEquals( 100, $ok );
     386                $this->assertEquals( 0, $reversed );
     387        }
    341388}
Note: See TracChangeset for help on using the changeset viewer.