wp/wp-admin/includes/revision.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     2 /**
     2 /**
     3  * WordPress Administration Revisions API
     3  * WordPress Administration Revisions API
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Administration
     6  * @subpackage Administration
       
     7  * @since 3.6.0
     7  */
     8  */
     8 
     9 
     9 /**
    10 /**
    10  * Get the revision UI diff.
    11  * Get the revision UI diff.
    11  *
    12  *
    52 	if ( empty( $compare_to->post_title ) )
    53 	if ( empty( $compare_to->post_title ) )
    53 		$compare_to->post_title = __( '(no title)' );
    54 		$compare_to->post_title = __( '(no title)' );
    54 
    55 
    55 	$return = array();
    56 	$return = array();
    56 
    57 
    57 	foreach ( _wp_post_revision_fields() as $field => $name ) {
    58 	foreach ( _wp_post_revision_fields( $post ) as $field => $name ) {
    58 		/**
    59 		/**
    59 		 * Contextually filter a post revision field.
    60 		 * Contextually filter a post revision field.
    60 		 *
    61 		 *
    61 		 * The dynamic portion of the hook name, `$field`, corresponds to each of the post
    62 		 * The dynamic portion of the hook name, `$field`, corresponds to each of the post
    62 		 * fields of the revision object being iterated over in a foreach statement.
    63 		 * fields of the revision object being iterated over in a foreach statement.
    67 		 * @param string  $field                The current revision field.
    68 		 * @param string  $field                The current revision field.
    68 		 * @param WP_Post $compare_from         The revision post object to compare to or from.
    69 		 * @param WP_Post $compare_from         The revision post object to compare to or from.
    69 		 * @param string  null                  The context of whether the current revision is the old
    70 		 * @param string  null                  The context of whether the current revision is the old
    70 		 *                                      or the new one. Values are 'to' or 'from'.
    71 		 *                                      or the new one. Values are 'to' or 'from'.
    71 		 */
    72 		 */
    72 		$content_from = $compare_from ? apply_filters( "_wp_post_revision_field_$field", $compare_from->$field, $field, $compare_from, 'from' ) : '';
    73 		$content_from = $compare_from ? apply_filters( "_wp_post_revision_field_{$field}", $compare_from->$field, $field, $compare_from, 'from' ) : '';
    73 
    74 
    74 		/** This filter is documented in wp-admin/includes/revision.php */
    75 		/** This filter is documented in wp-admin/includes/revision.php */
    75 		$content_to = apply_filters( "_wp_post_revision_field_$field", $compare_to->$field, $field, $compare_to, 'to' );
    76 		$content_to = apply_filters( "_wp_post_revision_field_{$field}", $compare_to->$field, $field, $compare_to, 'to' );
    76 
    77 
    77 		$args = array(
    78 		$args = array(
    78 			'show_split_view' => true
    79 			'show_split_view' => true
    79 		);
    80 		);
    80 
    81 
    81 		/**
    82 		/**
    82 		 * Filter revisions text diff options.
    83 		 * Filters revisions text diff options.
    83 		 *
    84 		 *
    84 		 * Filter the options passed to {@see wp_text_diff()} when viewing a post revision.
    85 		 * Filters the options passed to wp_text_diff() when viewing a post revision.
    85 		 *
    86 		 *
    86 		 * @since 4.1.0
    87 		 * @since 4.1.0
    87 		 *
    88 		 *
    88 		 * @param array   $args {
    89 		 * @param array   $args {
    89 		 *     Associative array of options to pass to {@see wp_text_diff()}.
    90 		 *     Associative array of options to pass to wp_text_diff().
    90 		 *
    91 		 *
    91 		 *     @type bool $show_split_view True for split view (two columns), false for
    92 		 *     @type bool $show_split_view True for split view (two columns), false for
    92 		 *                                 un-split view (single column). Default true.
    93 		 *                                 un-split view (single column). Default true.
    93 		 * }
    94 		 * }
    94 		 * @param string  $field        The current revision field.
    95 		 * @param string  $field        The current revision field.
   116 			);
   117 			);
   117 		}
   118 		}
   118 	}
   119 	}
   119 
   120 
   120 	/**
   121 	/**
   121 	 * Filter the fields displayed in the post revision diff UI.
   122 	 * Filters the fields displayed in the post revision diff UI.
   122 	 *
   123 	 *
   123 	 * @since 4.1.0
   124 	 * @since 4.1.0
   124 	 *
   125 	 *
   125 	 * @param array   $return       Revision UI fields. Each item is an array of id, name and diff.
   126 	 * @param array   $return       Revision UI fields. Each item is an array of id, name and diff.
   126 	 * @param WP_Post $compare_from The revision post to compare from.
   127 	 * @param WP_Post $compare_from The revision post to compare from.
   163 	$can_restore = current_user_can( 'edit_post', $post->ID );
   164 	$can_restore = current_user_can( 'edit_post', $post->ID );
   164 	$current_id = false;
   165 	$current_id = false;
   165 
   166 
   166 	foreach ( $revisions as $revision ) {
   167 	foreach ( $revisions as $revision ) {
   167 		$modified = strtotime( $revision->post_modified );
   168 		$modified = strtotime( $revision->post_modified );
   168 		$modified_gmt = strtotime( $revision->post_modified_gmt );
   169 		$modified_gmt = strtotime( $revision->post_modified_gmt . ' +0000' );
   169 		if ( $can_restore ) {
   170 		if ( $can_restore ) {
   170 			$restore_link = str_replace( '&', '&', wp_nonce_url(
   171 			$restore_link = str_replace( '&', '&', wp_nonce_url(
   171 				add_query_arg(
   172 				add_query_arg(
   172 					array( 'revision' => $revision->ID,
   173 					array( 'revision' => $revision->ID,
   173 						'action' => 'restore' ),
   174 						'action' => 'restore' ),
   197 			}
   198 			}
   198 		} elseif ( $current ) {
   199 		} elseif ( $current ) {
   199 			$current_id = $revision->ID;
   200 			$current_id = $revision->ID;
   200 		}
   201 		}
   201 
   202 
   202 		$revisions[ $revision->ID ] = array(
   203 		$revisions_data = array(
   203 			'id'         => $revision->ID,
   204 			'id'         => $revision->ID,
   204 			'title'      => get_the_title( $post->ID ),
   205 			'title'      => get_the_title( $post->ID ),
   205 			'author'     => $authors[ $revision->post_author ],
   206 			'author'     => $authors[ $revision->post_author ],
   206 			'date'       => date_i18n( __( 'M j, Y @ H:i' ), $modified ),
   207 			'date'       => date_i18n( __( 'M j, Y @ H:i' ), $modified ),
   207 			'dateShort'  => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), $modified ),
   208 			'dateShort'  => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), $modified ),
   208 			'timeAgo'    => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ),
   209 			'timeAgo'    => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ),
   209 			'autosave'   => $autosave,
   210 			'autosave'   => $autosave,
   210 			'current'    => $current,
   211 			'current'    => $current,
   211 			'restoreUrl' => $can_restore ? $restore_link : false,
   212 			'restoreUrl' => $can_restore ? $restore_link : false,
   212 		);
   213 		);
       
   214 
       
   215 		/**
       
   216 		 * Filters the array of revisions used on the revisions screen.
       
   217 		 *
       
   218 		 * @since 4.4.0
       
   219 		 *
       
   220 		 * @param array   $revisions_data {
       
   221 		 *     The bootstrapped data for the revisions screen.
       
   222 		 *
       
   223 		 *     @type int        $id         Revision ID.
       
   224 		 *     @type string     $title      Title for the revision's parent WP_Post object.
       
   225 		 *     @type int        $author     Revision post author ID.
       
   226 		 *     @type string     $date       Date the revision was modified.
       
   227 		 *     @type string     $dateShort  Short-form version of the date the revision was modified.
       
   228 		 *     @type string     $timeAgo    GMT-aware amount of time ago the revision was modified.
       
   229 		 *     @type bool       $autosave   Whether the revision is an autosave.
       
   230 		 *     @type bool       $current    Whether the revision is both not an autosave and the post
       
   231 		 *                                  modified date matches the revision modified date (GMT-aware).
       
   232 		 *     @type bool|false $restoreUrl URL if the revision can be restored, false otherwise.
       
   233 		 * }
       
   234 		 * @param WP_Post $revision       The revision's WP_Post object.
       
   235 		 * @param WP_Post $post           The revision's parent WP_Post object.
       
   236 		 */
       
   237 		$revisions[ $revision->ID ] = apply_filters( 'wp_prepare_revision_for_js', $revisions_data, $revision, $post );
   213 	}
   238 	}
   214 
   239 
   215 	/**
   240 	/**
   216 	 * If we only have one revision, the initial revision is missing; This happens
   241 	 * If we only have one revision, the initial revision is missing; This happens
   217 	 * when we have an autsosave and the user has clicked 'View the Autosave'
   242 	 * when we have an autsosave and the user has clicked 'View the Autosave'
   313 				if ( 'undefined' !== typeof data && data.model.attributes.compareTwoMode ) {
   338 				if ( 'undefined' !== typeof data && data.model.attributes.compareTwoMode ) {
   314 					#> checked="checked"<#
   339 					#> checked="checked"<#
   315 				}
   340 				}
   316 				#>
   341 				#>
   317 				/>
   342 				/>
   318 				<?php esc_attr_e( 'Compare any two revisions' ); ?>
   343 				<?php esc_html_e( 'Compare any two revisions' ); ?>
   319 			</label>
   344 			</label>
   320 		</div>
   345 		</div>
   321 	</script>
   346 	</script>
   322 
   347 
   323 	<script id="tmpl-revisions-meta" type="text/html">
   348 	<script id="tmpl-revisions-meta" type="text/html">