]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/revision.php
WordPress 3.6.1
[autoinstalls/wordpress.git] / wp-admin / includes / revision.php
1 <?php
2 /**
3  * WordPress Administration Revisions API.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * Get the revision UI diff.
11  *
12  * @since 3.6.0
13  *
14  * @param object $post The post object.
15  * @param int $compare_from The revision id to compare from.
16  * @param int $compare_to The revision id to come to.
17  *
18  * @return array|bool Associative array of a post's revisioned fields and their diffs.
19  *      Or, false on failure.
20  */
21 function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {
22         if ( ! $post = get_post( $post ) )
23                 return false;
24
25         if ( $compare_from ) {
26                 if ( ! $compare_from = get_post( $compare_from ) )
27                         return false;
28         } else {
29                 // If we're dealing with the first revision...
30                 $compare_from = false;
31         }
32
33         if ( ! $compare_to = get_post( $compare_to ) )
34                 return false;
35
36         // If comparing revisions, make sure we're dealing with the right post parent.
37         // The parent post may be a 'revision' when revisions are disabled and we're looking at autosaves.
38         if ( $compare_from && $compare_from->post_parent !== $post->ID && $compare_from->ID !== $post->ID )
39                 return false;
40         if ( $compare_to->post_parent !== $post->ID && $compare_to->ID !== $post->ID )
41                 return false;
42
43         if ( $compare_from && strtotime( $compare_from->post_date_gmt ) > strtotime( $compare_to->post_date_gmt ) ) {
44                 $temp = $compare_from;
45                 $compare_from = $compare_to;
46                 $compare_to = $temp;
47         }
48
49         // Add default title if title field is empty
50         if ( $compare_from && empty( $compare_from->post_title ) )
51                 $compare_from->post_title = __( '(no title)' );
52         if ( empty( $compare_to->post_title ) )
53                 $compare_to->post_title = __( '(no title)' );
54
55         $return = array();
56
57         foreach ( _wp_post_revision_fields() as $field => $name ) {
58                 $content_from = $compare_from ? apply_filters( "_wp_post_revision_field_$field", $compare_from->$field, $field, $compare_from, 'from' ) : '';
59                 $content_to = apply_filters( "_wp_post_revision_field_$field", $compare_to->$field, $field, $compare_to, 'to' );
60
61                 $diff = wp_text_diff( $content_from, $content_to, array( 'show_split_view' => true ) );
62
63                 if ( ! $diff && 'post_title' === $field ) {
64                         // It's a better user experience to still show the Title, even if it didn't change.
65                         // No, you didn't see this.
66                         $diff = '<table class="diff"><colgroup><col class="content diffsplit left"><col class="content diffsplit middle"><col class="content diffsplit right"></colgroup><tbody><tr>';
67                         $diff .= '<td>' . esc_html( $compare_from->post_title ) . '</td><td></td><td>' . esc_html( $compare_to->post_title ) . '</td>';
68                         $diff .= '</tr></tbody>';
69                         $diff .= '</table>';
70                 }
71
72                 if ( $diff ) {
73                         $return[] = array(
74                                 'id' => $field,
75                                 'name' => $name,
76                                 'diff' => $diff,
77                         );
78                 }
79         }
80         return $return;
81 }
82
83 /**
84  * Prepare revisions for JavaScript.
85  *
86  * @since 3.6.0
87  *
88  * @param object $post The post object.
89  * @param int $selected_revision_id The selected revision id.
90  * @param int $from (optional) The revision id to compare from.
91  *
92  * @return array An associative array of revision data and related settings.
93  */
94 function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null ) {
95         $post = get_post( $post );
96         $revisions = $authors = array();
97         $now_gmt = time();
98
99         $revisions = wp_get_post_revisions( $post->ID, array( 'order' => 'ASC', 'check_enabled' => false ) );
100         // If revisions are disabled, we only want autosaves and the current post.
101         if ( ! wp_revisions_enabled( $post ) ) {
102                 foreach ( $revisions as $revision_id => $revision ) {
103                         if ( ! wp_is_post_autosave( $revision ) )
104                                 unset( $revisions[ $revision_id ] );
105                 }
106                 $revisions = array( $post->ID => $post ) + $revisions;
107         }
108
109         $show_avatars = get_option( 'show_avatars' );
110
111         cache_users( wp_list_pluck( $revisions, 'post_author' ) );
112
113         $can_restore = current_user_can( 'edit_post', $post->ID );
114
115         foreach ( $revisions as $revision ) {
116                 $modified = strtotime( $revision->post_modified );
117                 $modified_gmt = strtotime( $revision->post_modified_gmt );
118                 if ( $can_restore ) {
119                         $restore_link = str_replace( '&amp;', '&', wp_nonce_url(
120                                 add_query_arg(
121                                         array( 'revision' => $revision->ID,
122                                                 'action' => 'restore' ),
123                                                 admin_url( 'revision.php' )
124                                 ),
125                                 "restore-post_{$revision->ID}"
126                         ) );
127                 }
128
129                 if ( ! isset( $authors[ $revision->post_author ] ) ) {
130                         $authors[ $revision->post_author ] = array(
131                                 'id' => (int) $revision->post_author,
132                                 'avatar' => $show_avatars ? get_avatar( $revision->post_author, 32 ) : '',
133                                 'name' => get_the_author_meta( 'display_name', $revision->post_author ),
134                         );
135                 }
136
137                 $autosave = (bool) wp_is_post_autosave( $revision );
138                 $current = ! $autosave && $revision->post_modified_gmt === $post->post_modified_gmt;
139                 if ( $current && ! empty( $current_id ) ) {
140                         // If multiple revisions have the same post_modified_gmt, highest ID is current.
141                         if ( $current_id < $revision->ID ) {
142                                 $revisions[ $current_id ]['current'] = false;
143                                 $current_id = $revision->ID;
144                         } else {
145                                 $current = false;
146                         }
147                 } elseif ( $current ) {
148                         $current_id = $revision->ID;
149                 }
150
151                 $revisions[ $revision->ID ] = array(
152                         'id'         => $revision->ID,
153                         'title'      => get_the_title( $post->ID ),
154                         'author'     => $authors[ $revision->post_author ],
155                         'date'       => date_i18n( __( 'M j, Y @ G:i' ), $modified ),
156                         'dateShort'  => date_i18n( _x( 'j M @ G:i', 'revision date short format' ), $modified ),
157                         'timeAgo'    => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ),
158                         'autosave'   => $autosave,
159                         'current'    => $current,
160                         'restoreUrl' => $can_restore ? $restore_link : false,
161                 );
162         }
163
164         // If a post has been saved since the last revision (no revisioned fields were changed)
165         // we may not have a "current" revision. Mark the latest revision as "current".
166         if ( empty( $current_id ) ) {
167                 if ( $revisions[ $revision->ID ]['autosave'] ) {
168                         $revision = end( $revisions );
169                         while ( $revision['autosave'] ) {
170                                 $revision = prev( $revisions );
171                         }
172                         $current_id = $revision['id'];
173                 } else {
174                         $current_id = $revision->ID;
175                 }
176                 $revisions[ $current_id ]['current'] = true;
177         }
178
179         // Now, grab the initial diff
180         $compare_two_mode = is_numeric( $from );
181         if ( ! $compare_two_mode ) {
182                 $found = array_search( $selected_revision_id, array_keys( $revisions ) );
183                 if ( $found ) {
184                         $from = array_keys( array_slice( $revisions, $found - 1, 1, true ) );
185                         $from = reset( $from );
186                 } else {
187                         $from = 0;
188                 }
189         }
190
191         $from = absint( $from );
192
193         $diffs = array( array(
194                 'id' => $from . ':' . $selected_revision_id,
195                 'fields' => wp_get_revision_ui_diff( $post->ID, $from, $selected_revision_id ),
196         ));
197
198         return array(
199                 'postId'           => $post->ID,
200                 'nonce'            => wp_create_nonce( 'revisions-ajax-nonce' ),
201                 'revisionData'     => array_values( $revisions ),
202                 'to'               => $selected_revision_id,
203                 'from'             => $from,
204                 'diffData'         => $diffs,
205                 'baseUrl'          => parse_url( admin_url( 'revision.php' ), PHP_URL_PATH ),
206                 'compareTwoMode'   => absint( $compare_two_mode ), // Apparently booleans are not allowed
207                 'revisionIds'      => array_keys( $revisions ),
208         );
209 }