Make WordPress Core

Ticket #34560: 34560.4.diff

File 34560.4.diff, 5.7 KB (added by adamsilverstein, 8 years ago)
  • src/wp-admin/edit-form-advanced.php

     
    217217
    218218$publish_callback_args = null;
    219219if ( post_type_supports($post_type, 'revisions') && 'auto-draft' != $post->post_status ) {
    220         $revisions = wp_get_post_revisions( $post_ID );
    221 
     220        $revision_count = wp_get_post_revision_count( $post_id );
    222221        // We should aim to show the revisions metabox only when there are revisions.
    223         if ( count( $revisions ) > 1 ) {
    224                 reset( $revisions ); // Reset pointer for key()
    225                 $publish_callback_args = array( 'revisions_count' => count( $revisions ), 'revision_id' => key( $revisions ) );
     222        if ( $revision_count > 1 ) {
     223                $last_revision = wp_get_last_revision_id( $post_id );
     224                $publish_callback_args = array( 'revisions_count' => $revision_count, 'revision_id' => $last_revision ? $last_revision: 0 );
    226225                add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', null, 'normal', 'core');
    227226        }
    228227}
  • src/wp-includes/post-template.php

     
    16611661        $autosavef = __( '%1$s [Autosave]' );
    16621662        $currentf  = __( '%1$s [Current Revision]' );
    16631663
    1664         if ( !wp_is_post_revision( $revision ) )
     1664        if ( !wp_is_post_revision( $revision->ID ) )
    16651665                $revision_date_author = sprintf( $currentf, $revision_date_author );
    1666         elseif ( wp_is_post_autosave( $revision ) )
     1666        elseif ( wp_is_post_autosave( $revision->ID ) )
    16671667                $revision_date_author = sprintf( $autosavef, $revision_date_author );
    16681668
    16691669        /**
     
    17001700                _deprecated_argument( __FUNCTION__, '3.6' );
    17011701        }
    17021702
    1703         if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
     1703        if ( ! $revisions = wp_get_post_revisions_details( $post->ID ) )
    17041704                return;
    17051705
    17061706        $rows = '';
  • src/wp-includes/revision.php

     
    198198 * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
    199199 */
    200200function wp_get_post_autosave( $post_id, $user_id = 0 ) {
    201         $revisions = wp_get_post_revisions( $post_id, array( 'check_enabled' => false ) );
     201        global $wpdb;
    202202
    203         foreach ( $revisions as $revision ) {
    204                 if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
    205                         if ( $user_id && $user_id != $revision->post_author )
    206                                 continue;
     203        // Contruct the autosave query.
     204        $autosave_query = "
     205                SELECT *
     206                FROM $wpdb->posts
     207                WHERE post_parent = %d
     208                AND   post_type   = 'revision'
     209                AND   post_status = 'inherit'
     210                AND   post_name   = %s
     211                AND   post_author = %s
     212                LIMIT 1";
    207213
    208                         return $revision;
    209                 }
     214        $autosave_details = $wpdb->get_results(
     215                $wpdb->prepare(
     216                        $autosave_query,
     217                        $post_id,
     218                        $post_id . '-autosave-v1',
     219                        ( 0 !== $user_id ) ? $user_id : get_current_user_id()
     220                )
     221        );
     222
     223        if ( empty( $autosave_details ) ) {
     224                return false;
    210225        }
    211226
    212         return false;
     227        return $autosave_details[0];
    213228}
    214229
    215230/**
     
    434449}
    435450
    436451/**
     452 * Returns the revision details of specified post.
     453 *
     454 * Data includes details about each revision on a post. Includes ID, post_author,
     455 * post_date, post_date_gmt, post_title, post_status, post_parent, and post_modified.
     456 *
     457 * @since 4.5.0
     458 *
     459 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
     460 * @return array An array of revision details, or an empty array if post invalid.
     461 */
     462function wp_get_post_revisions_details( $post_id = 0, $args = null ) {
     463        global $wpdb;
     464
     465        $post = get_post( $post_id );
     466        if ( ! $post || empty( $post->ID ) ) {
     467                return array();
     468        }
     469
     470        $revision_details = $wpdb->get_results(
     471                $wpdb->prepare( "
     472                        SELECT ID, post_author, post_date, post_date_gmt, post_title, post_status, post_parent, post_modified
     473                        FROM $wpdb->posts
     474                        WHERE post_parent = %d
     475                        AND   post_type   = 'revision'
     476                        AND   post_status = 'inherit'
     477                        ORDER BY post_date DESC, ID DESC",
     478                        $post->ID
     479                )
     480        );
     481
     482        return $revision_details;
     483}
     484
     485/**
     486 * Return the count of revisions for a given post.
     487 *
     488 * @since 4.5.0
     489 *
     490 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
     491 * @return int The count of revisions for this post, false if post invalid.
     492 */
     493function wp_get_post_revision_count( $post_id = 0 ) {
     494        global $wpdb;
     495
     496        $post = get_post( $post_id );
     497        if ( ! $post || empty( $post->ID ) ) {
     498                return false;
     499        }
     500
     501        $revision_count = intval(
     502                $wpdb->get_var(
     503                        $wpdb->prepare( "
     504                                SELECT COUNT(*)
     505                                FROM $wpdb->posts
     506                                WHERE post_parent = %d
     507                                AND   post_type   = 'revision'
     508                                AND   post_status = 'inherit'",
     509                        $post->ID
     510                        )
     511                )
     512        );
     513        return $revision_count;
     514}
     515
     516/**
     517 * Return the id of the last stored revision.
     518 *
     519 * @since 4.5.0
     520 *
     521 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
     522 * @return int The id of the last stored revision for this post, false if post invalid.
     523 */
     524function wp_get_last_revision_id( $post_id = 0 ) {
     525        global $wpdb;
     526
     527        $post = get_post( $post_id );
     528        if ( ! $post || empty( $post->ID ) ) {
     529                return false;
     530        }
     531
     532        $last_revision_id = intval(
     533                $wpdb->get_var(
     534                        $wpdb->prepare( "
     535                                SELECT ID, post_date
     536                                FROM $wpdb->posts
     537                                WHERE post_parent = %d
     538                                AND   post_type   = 'revision'
     539                                AND   post_status = 'inherit'
     540                                ORDER BY post_date DESC, ID DESC
     541                                LIMIT 1",
     542                        $post->ID
     543                        )
     544                )
     545        );
     546        return $last_revision_id;
     547}
     548
     549/**
    437550 * Determine if revisions are enabled for a given post.
    438551 *
    439552 * @since 3.6.0