wp/wp-admin/includes/revision.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    10 /**
    10 /**
    11  * Get the revision UI diff.
    11  * Get the revision UI diff.
    12  *
    12  *
    13  * @since 3.6.0
    13  * @since 3.6.0
    14  *
    14  *
    15  * @param object|int $post         The post object. Also accepts a post ID.
    15  * @param WP_Post|int $post         The post object or post ID.
    16  * @param int        $compare_from The revision ID to compare from.
    16  * @param int         $compare_from The revision ID to compare from.
    17  * @param int        $compare_to   The revision ID to come to.
    17  * @param int         $compare_to   The revision ID to come to.
    18  *
       
    19  * @return array|bool Associative array of a post's revisioned fields and their diffs.
    18  * @return array|bool Associative array of a post's revisioned fields and their diffs.
    20  *                    Or, false on failure.
    19  *                    Or, false on failure.
    21  */
    20  */
    22 function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {
    21 function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {
    23 	if ( ! $post = get_post( $post ) ) {
    22 	$post = get_post( $post );
       
    23 	if ( ! $post ) {
    24 		return false;
    24 		return false;
    25 	}
    25 	}
    26 
    26 
    27 	if ( $compare_from ) {
    27 	if ( $compare_from ) {
    28 		if ( ! $compare_from = get_post( $compare_from ) ) {
    28 		$compare_from = get_post( $compare_from );
       
    29 		if ( ! $compare_from ) {
    29 			return false;
    30 			return false;
    30 		}
    31 		}
    31 	} else {
    32 	} else {
    32 		// If we're dealing with the first revision...
    33 		// If we're dealing with the first revision...
    33 		$compare_from = false;
    34 		$compare_from = false;
    34 	}
    35 	}
    35 
    36 
    36 	if ( ! $compare_to = get_post( $compare_to ) ) {
    37 	$compare_to = get_post( $compare_to );
       
    38 	if ( ! $compare_to ) {
    37 		return false;
    39 		return false;
    38 	}
    40 	}
    39 
    41 
    40 	// If comparing revisions, make sure we're dealing with the right post parent.
    42 	// If comparing revisions, make sure we're dealing with the right post parent.
    41 	// The parent post may be a 'revision' when revisions are disabled and we're looking at autosaves.
    43 	// The parent post may be a 'revision' when revisions are disabled and we're looking at autosaves.
    50 		$temp         = $compare_from;
    52 		$temp         = $compare_from;
    51 		$compare_from = $compare_to;
    53 		$compare_from = $compare_to;
    52 		$compare_to   = $temp;
    54 		$compare_to   = $temp;
    53 	}
    55 	}
    54 
    56 
    55 	// Add default title if title field is empty
    57 	// Add default title if title field is empty.
    56 	if ( $compare_from && empty( $compare_from->post_title ) ) {
    58 	if ( $compare_from && empty( $compare_from->post_title ) ) {
    57 		$compare_from->post_title = __( '(no title)' );
    59 		$compare_from->post_title = __( '(no title)' );
    58 	}
    60 	}
    59 	if ( empty( $compare_to->post_title ) ) {
    61 	if ( empty( $compare_to->post_title ) ) {
    60 		$compare_to->post_title = __( '(no title)' );
    62 		$compare_to->post_title = __( '(no title)' );
    69 		 * The dynamic portion of the hook name, `$field`, corresponds to each of the post
    71 		 * The dynamic portion of the hook name, `$field`, corresponds to each of the post
    70 		 * fields of the revision object being iterated over in a foreach statement.
    72 		 * fields of the revision object being iterated over in a foreach statement.
    71 		 *
    73 		 *
    72 		 * @since 3.6.0
    74 		 * @since 3.6.0
    73 		 *
    75 		 *
    74 		 * @param string  $compare_from->$field The current revision field to compare to or from.
    76 		 * @param string  $revision_field The current revision field to compare to or from.
    75 		 * @param string  $field                The current revision field.
    77 		 * @param string  $field          The current revision field.
    76 		 * @param WP_Post $compare_from         The revision post object to compare to or from.
    78 		 * @param WP_Post $compare_from   The revision post object to compare to or from.
    77 		 * @param string  null                  The context of whether the current revision is the old
    79 		 * @param string  $context        The context of whether the current revision is the old
    78 		 *                                      or the new one. Values are 'to' or 'from'.
    80 		 *                                or the new one. Values are 'to' or 'from'.
    79 		 */
    81 		 */
    80 		$content_from = $compare_from ? apply_filters( "_wp_post_revision_field_{$field}", $compare_from->$field, $field, $compare_from, 'from' ) : '';
    82 		$content_from = $compare_from ? apply_filters( "_wp_post_revision_field_{$field}", $compare_from->$field, $field, $compare_from, 'from' ) : '';
    81 
    83 
    82 		/** This filter is documented in wp-admin/includes/revision.php */
    84 		/** This filter is documented in wp-admin/includes/revision.php */
    83 		$content_to = apply_filters( "_wp_post_revision_field_{$field}", $compare_to->$field, $field, $compare_to, 'to' );
    85 		$content_to = apply_filters( "_wp_post_revision_field_{$field}", $compare_to->$field, $field, $compare_to, 'to' );
   153 /**
   155 /**
   154  * Prepare revisions for JavaScript.
   156  * Prepare revisions for JavaScript.
   155  *
   157  *
   156  * @since 3.6.0
   158  * @since 3.6.0
   157  *
   159  *
   158  * @param object|int $post                 The post object. Also accepts a post ID.
   160  * @param WP_Post|int $post                 The post object or post ID.
   159  * @param int        $selected_revision_id The selected revision ID.
   161  * @param int         $selected_revision_id The selected revision ID.
   160  * @param int        $from                 Optional. The revision ID to compare from.
   162  * @param int         $from                 Optional. The revision ID to compare from.
   161  *
       
   162  * @return array An associative array of revision data and related settings.
   163  * @return array An associative array of revision data and related settings.
   163  */
   164  */
   164 function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null ) {
   165 function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null ) {
   165 	$post    = get_post( $post );
   166 	$post    = get_post( $post );
   166 	$authors = array();
   167 	$authors = array();
   236 			'id'         => $revision->ID,
   237 			'id'         => $revision->ID,
   237 			'title'      => get_the_title( $post->ID ),
   238 			'title'      => get_the_title( $post->ID ),
   238 			'author'     => $authors[ $revision->post_author ],
   239 			'author'     => $authors[ $revision->post_author ],
   239 			'date'       => date_i18n( __( 'M j, Y @ H:i' ), $modified ),
   240 			'date'       => date_i18n( __( 'M j, Y @ H:i' ), $modified ),
   240 			'dateShort'  => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), $modified ),
   241 			'dateShort'  => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), $modified ),
       
   242 			/* translators: %s: Human-readable time difference. */
   241 			'timeAgo'    => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ),
   243 			'timeAgo'    => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ),
   242 			'autosave'   => $autosave,
   244 			'autosave'   => $autosave,
   243 			'current'    => $current,
   245 			'current'    => $current,
   244 			'restoreUrl' => $can_restore ? $restore_link : false,
   246 			'restoreUrl' => $can_restore ? $restore_link : false,
   245 		);
   247 		);
   278 			'id'         => $post->ID,
   280 			'id'         => $post->ID,
   279 			'title'      => get_the_title( $post->ID ),
   281 			'title'      => get_the_title( $post->ID ),
   280 			'author'     => $authors[ $post->post_author ],
   282 			'author'     => $authors[ $post->post_author ],
   281 			'date'       => date_i18n( __( 'M j, Y @ H:i' ), strtotime( $post->post_modified ) ),
   283 			'date'       => date_i18n( __( 'M j, Y @ H:i' ), strtotime( $post->post_modified ) ),
   282 			'dateShort'  => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), strtotime( $post->post_modified ) ),
   284 			'dateShort'  => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), strtotime( $post->post_modified ) ),
       
   285 			/* translators: %s: Human-readable time difference. */
   283 			'timeAgo'    => sprintf( __( '%s ago' ), human_time_diff( strtotime( $post->post_modified_gmt ), $now_gmt ) ),
   286 			'timeAgo'    => sprintf( __( '%s ago' ), human_time_diff( strtotime( $post->post_modified_gmt ), $now_gmt ) ),
   284 			'autosave'   => false,
   287 			'autosave'   => false,
   285 			'current'    => true,
   288 			'current'    => true,
   286 			'restoreUrl' => false,
   289 			'restoreUrl' => false,
   287 		);
   290 		);
   307 	}
   310 	}
   308 
   311 
   309 	// Now, grab the initial diff.
   312 	// Now, grab the initial diff.
   310 	$compare_two_mode = is_numeric( $from );
   313 	$compare_two_mode = is_numeric( $from );
   311 	if ( ! $compare_two_mode ) {
   314 	if ( ! $compare_two_mode ) {
   312 		$found = array_search( $selected_revision_id, array_keys( $revisions ) );
   315 		$found = array_search( $selected_revision_id, array_keys( $revisions ), true );
   313 		if ( $found ) {
   316 		if ( $found ) {
   314 			$from = array_keys( array_slice( $revisions, $found - 1, 1, true ) );
   317 			$from = array_keys( array_slice( $revisions, $found - 1, 1, true ) );
   315 			$from = reset( $from );
   318 			$from = reset( $from );
   316 		} else {
   319 		} else {
   317 			$from = 0;
   320 			$from = 0;
   333 		'revisionData'   => array_values( $revisions ),
   336 		'revisionData'   => array_values( $revisions ),
   334 		'to'             => $selected_revision_id,
   337 		'to'             => $selected_revision_id,
   335 		'from'           => $from,
   338 		'from'           => $from,
   336 		'diffData'       => $diffs,
   339 		'diffData'       => $diffs,
   337 		'baseUrl'        => parse_url( admin_url( 'revision.php' ), PHP_URL_PATH ),
   340 		'baseUrl'        => parse_url( admin_url( 'revision.php' ), PHP_URL_PATH ),
   338 		'compareTwoMode' => absint( $compare_two_mode ), // Apparently booleans are not allowed
   341 		'compareTwoMode' => absint( $compare_two_mode ), // Apparently booleans are not allowed.
   339 		'revisionIds'    => array_keys( $revisions ),
   342 		'revisionIds'    => array_keys( $revisions ),
   340 	);
   343 	);
   341 }
   344 }
   342 
   345 
   343 /**
   346 /**
   344  * Print JavaScript templates required for the revisions experience.
   347  * Print JavaScript templates required for the revisions experience.
   345  *
   348  *
   346  * @since 4.1.0
   349  * @since 4.1.0
   347  *
   350  *
   348  * @global WP_Post $post The global `$post` object.
   351  * @global WP_Post $post Global post object.
   349  */
   352  */
   350 function wp_print_revision_templates() {
   353 function wp_print_revision_templates() {
   351 	global $post;
   354 	global $post;
   352 	?><script id="tmpl-revisions-frame" type="text/html">
   355 	?><script id="tmpl-revisions-frame" type="text/html">
   353 		<div class="revisions-control-frame"></div>
   356 		<div class="revisions-control-frame"></div>
   392 					<div class="author-info">
   395 					<div class="author-info">
   393 					<# if ( data.attributes.autosave ) { #>
   396 					<# if ( data.attributes.autosave ) { #>
   394 						<span class="byline">
   397 						<span class="byline">
   395 						<?php
   398 						<?php
   396 						printf(
   399 						printf(
       
   400 							/* translators: %s: User's display name. */
   397 							__( 'Autosave by %s' ),
   401 							__( 'Autosave by %s' ),
   398 							'<span class="author-name">{{ data.attributes.author.name }}</span>'
   402 							'<span class="author-name">{{ data.attributes.author.name }}</span>'
   399 						);
   403 						);
   400 						?>
   404 						?>
   401 							</span>
   405 							</span>
   402 					<# } else if ( data.attributes.current ) { #>
   406 					<# } else if ( data.attributes.current ) { #>
   403 						<span class="byline">
   407 						<span class="byline">
   404 						<?php
   408 						<?php
   405 						printf(
   409 						printf(
       
   410 							/* translators: %s: User's display name. */
   406 							__( 'Current Revision by %s' ),
   411 							__( 'Current Revision by %s' ),
   407 							'<span class="author-name">{{ data.attributes.author.name }}</span>'
   412 							'<span class="author-name">{{ data.attributes.author.name }}</span>'
   408 						);
   413 						);
   409 						?>
   414 						?>
   410 							</span>
   415 							</span>
   411 					<# } else { #>
   416 					<# } else { #>
   412 						<span class="byline">
   417 						<span class="byline">
   413 						<?php
   418 						<?php
   414 						printf(
   419 						printf(
       
   420 							/* translators: %s: User's display name. */
   415 							__( 'Revision by %s' ),
   421 							__( 'Revision by %s' ),
   416 							'<span class="author-name">{{ data.attributes.author.name }}</span>'
   422 							'<span class="author-name">{{ data.attributes.author.name }}</span>'
   417 						);
   423 						);
   418 						?>
   424 						?>
   419 							</span>
   425 							</span>