Make WordPress Core

Ticket #34560: 34560.9.diff

File 34560.9.diff, 5.9 KB (added by DBrumbaugh10Up, 7 years ago)

Adam, I took your code and added a bit more "oopness"

  • 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 );
    221220
     221
     222        require_once( ABSPATH . 'wp-admin/includes/class-wp-revisions.php' );
     223        $wp_revisions = new WP_PostRevisions( $post->ID );
     224
    222225        // 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 ) );
     226        if ( $wp_revisions->revisions_count > 1 ) {
     227                $publish_callback_args = array(
     228                        'revisions_count' => $wp_revisions->revisions_count,
     229                        'revision_id'     => $wp_revisions->last_revision_id,
     230                );
    226231                add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', null, 'normal', 'core');
    227232        }
    228233}
  • wp-admin/includes/class-wp-revisions.php

     
     1<?php
     2/**
     3 * WP_Revisions helper class.
     4 */
     5class WP_PostRevisions {
     6
     7
     8        /**
     9         * WP_PostRevisions constructor.
     10         * @param $post_id - ID of the post in question
     11         */
     12        public function __construct( $post_id ) {
     13                $this->revisions_details = false;
     14                $this->revisions_count = 0;
     15                $this->last_revision_id = false;
     16                $this->init( $post_id );
     17        }
     18
     19        /**
     20         * Initialize this instance of the revisions class
     21         * @param $post_id
     22         */
     23        public function init( $post_id ) {
     24                global  $wpdb;
     25
     26                // Grab all the revision details we need to the Post Edit screen.
     27                $this->revisions_details = $wpdb->get_results(
     28                        $wpdb->prepare( "
     29                                SELECT   ID, post_author, post_date, post_date_gmt,
     30                                             post_title, post_status, post_parent, post_modified, post_type
     31                                FROM     $wpdb->posts
     32                                WHERE    post_parent = %d
     33                                AND      post_type   = 'revision'
     34                                AND      post_status = 'inherit'
     35                                ORDER BY post_date DESC, ID DESC",
     36                                $post_id
     37                        )
     38                );
     39
     40                // Store the total revisions count.
     41                $this->revisions_count = $wpdb->num_rows;
     42
     43                // Store the id of the last revision.
     44                $this->last_revision_id = isset( $this->revisions_details[0] ) ? $this->revisions_details[0]->ID : false;
     45        }
     46
     47        /**
     48         * The number of revisions for the current object.
     49         * @var int
     50         */
     51        public $revisions_count;
     52
     53        /**
     54         * Details about the revisions.
     55         * @var Array
     56         */
     57        public $revisions_details;
     58
     59        /**
     60         * The id of the last revision.
     61         * @var int
     62         */
     63        public $last_revision_id;
     64}
     65
     66
  • wp-includes/post-template.php

     
    16491649 * @return string|false gravatar, user, i18n formatted datetimestamp or localized 'Current Revision'.
    16501650 */
    16511651function wp_post_revision_title_expanded( $revision, $link = true ) {
    1652         if ( !$revision = get_post( $revision ) )
    1653                 return $revision;
    16541652
    16551653        if ( !in_array( $revision->post_type, array( 'post', 'page', 'revision' ) ) )
    16561654                return false;
     
    16621660        $gravatar = get_avatar( $revision->post_author, 24 );
    16631661
    16641662        $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
    1665         if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
     1663        if ( $link && $link = admin_url( sprintf( 'revision.php?revision=%d&action=edit', $revision->ID ) ) )
    16661664                $date = "<a href='$link'>$date</a>";
    16671665
    16681666        $revision_date_author = sprintf(
     
    17101708        if ( ! $post = get_post( $post_id ) )
    17111709                return;
    17121710
     1711        if ( ! current_user_can( 'read_post', $post->ID ) )
     1712                return;
     1713
    17131714        // $args array with (parent, format, right, left, type) deprecated since 3.6
    17141715        if ( is_array( $type ) ) {
    17151716                $type = ! empty( $type['type'] ) ? $type['type']  : $type;
    17161717                _deprecated_argument( __FUNCTION__, '3.6' );
    17171718        }
    17181719
    1719         if ( ! $revisions = wp_get_post_revisions( $post->ID ) )
     1720        require_once( ABSPATH . 'wp-admin/includes/class-wp-revisions.php' );
     1721        $wp_revisions = new WP_PostRevisions( $post->ID );
     1722
     1723        if ( ! $revisions = $wp_revisions->revisions_details ) {
    17201724                return;
     1725        }
    17211726
    17221727        $rows = '';
    17231728        foreach ( $revisions as $revision ) {
    1724                 if ( ! current_user_can( 'read_post', $revision->ID ) )
    1725                         continue;
    1726 
    17271729                $is_autosave = wp_is_post_autosave( $revision );
    17281730                if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) )
    17291731                        continue;
  • 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/**