Make WordPress Core

Ticket #34560: 34560.7.diff

File 34560.7.diff, 6.1 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 
    222220        // 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 ) );
     221        $revision_count = wp_get_post_revision_count( $post->ID );
     222        if ( $revision_count > 1 ) {
     223                $last_revision = wp_get_last_revision_id( $post->ID );
     224                $publish_callback_args = array(
     225                        'revisions_count' => $revision_count,
     226                        'revision_id'     => $last_revision,
     227                );
    226228                add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', null, 'normal', 'core');
    227229        }
    228230}
  • src/wp-includes/post-template.php

     
    16331633 * @return string|false gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'.
    16341634 */
    16351635function wp_post_revision_title_expanded( $revision, $link = true ) {
    1636         if ( !$revision = get_post( $revision ) )
    1637                 return $revision;
    16381636
    16391637        if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
    16401638                return false;
     
    16461644        $gravatar = get_avatar( $revision->post_author, 24 );
    16471645
    16481646        $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
    1649         if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
     1647        if ( $link && $link = admin_url( sprintf( 'revision.php?revision=%d&action=edit', $revision->ID ) ) )
    16501648                $date = "<a href='$link'>$date</a>";
    16511649
    16521650        $revision_date_author = sprintf(
     
    16941692        if ( ! $post = get_post( $post_id ) )
    16951693                return;
    16961694
     1695        if ( ! current_user_can( 'read_post', $post->ID ) )
     1696                return;
     1697
    16971698        // $args array with (parent, format, right, left, type) deprecated since 3.6
    16981699        if ( is_array( $type ) ) {
    16991700                $type = ! empty( $type['type'] ) ? $type['type']  : $type;
     
    17001701                _deprecated_argument( __FUNCTION__, '3.6' );
    17011702        }
    17021703
    1703         if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
     1704        if ( ! $revisions = wp_get_post_revisions_details( $post->ID ) )
    17041705                return;
    17051706
    17061707        $rows = '';
    17071708        foreach ( $revisions as $revision ) {
    1708                 if ( ! current_user_can( 'read_post', $revision->ID ) )
    1709                         continue;
    1710 
    17111709                $is_autosave = wp_is_post_autosave( $revision );
    17121710                if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) )
    17131711                        continue;
  • 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 $post_id The post id.
     460 * @return array An array of revision details.
     461 */
     462function wp_get_post_revisions_details( $post_id ) {
     463        global $wpdb;
     464
     465        $revision_details = $wpdb->get_results(
     466                $wpdb->prepare( "
     467                        SELECT   ID, post_author, post_date, post_date_gmt,
     468                                     post_title, post_status, post_parent, post_modified, post_type
     469                        FROM     $wpdb->posts
     470                        WHERE    post_parent = %d
     471                        AND      post_type   = 'revision'
     472                        AND      post_status = 'inherit'
     473                        ORDER BY post_date DESC, ID DESC",
     474                        $post_id
     475                )
     476        );
     477
     478        return $revision_details;
     479}
     480
     481/**
     482 * Returns the count of revisions for a given post.
     483 *
     484 * @since 4.5.0
     485 *
     486 * @param  int $post_id The post id.
     487 * @return int The count of revisions for this post.
     488 */
     489function wp_get_post_revision_count( $post_id ) {
     490        global $wpdb;
     491
     492        $revision_count = $wpdb->get_row(
     493                $wpdb->prepare( "
     494                        SELECT COUNT(*) as count
     495                        FROM   $wpdb->posts
     496                        WHERE  post_parent = %d
     497                        AND    post_type   = 'revision'
     498                        AND    post_status = 'inherit'",
     499                        $post_id
     500                )
     501        );
     502        return intval( $revision_count->count );
     503}
     504
     505/**
     506 * Returns the id of the last stored revision.
     507 *
     508 * @since 4.5.0
     509 *
     510 * @param  int $post_id The post id.
     511 * @return int The id of the last stored revision for this post.
     512 */
     513function wp_get_last_revision_id( $post_id ) {
     514        global $wpdb;
     515
     516        $last_revision_id = intval(
     517                $wpdb->get_var(
     518                        $wpdb->prepare( "
     519                                SELECT ID, post_date
     520                                FROM   $wpdb->posts
     521                                WHERE  post_parent = %d
     522                                AND    post_type   = 'revision'
     523                                AND    post_status = 'inherit'
     524                                ORDER  BY post_date DESC, ID DESC
     525                                LIMIT  1",
     526                                $post_id
     527                        )
     528                )
     529        );
     530        return $last_revision_id;
     531}
     532
     533/**
    437534 * Determine if revisions are enabled for a given post.
    438535 *
    439536 * @since 3.6.0