wp/wp-admin/includes/revision.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    37 	$compare_to = get_post( $compare_to );
    37 	$compare_to = get_post( $compare_to );
    38 	if ( ! $compare_to ) {
    38 	if ( ! $compare_to ) {
    39 		return false;
    39 		return false;
    40 	}
    40 	}
    41 
    41 
    42 	// If comparing revisions, make sure we're dealing with the right post parent.
    42 	/*
    43 	// The parent post may be a 'revision' when revisions are disabled and we're looking at autosaves.
    43 	 * If comparing revisions, make sure we are dealing with the right post parent.
       
    44 	 * The parent post may be a 'revision' when revisions are disabled and we're looking at autosaves.
       
    45 	 */
    44 	if ( $compare_from && $compare_from->post_parent !== $post->ID && $compare_from->ID !== $post->ID ) {
    46 	if ( $compare_from && $compare_from->post_parent !== $post->ID && $compare_from->ID !== $post->ID ) {
    45 		return false;
    47 		return false;
    46 	}
    48 	}
    47 	if ( $compare_to->post_parent !== $post->ID && $compare_to->ID !== $post->ID ) {
    49 	if ( $compare_to->post_parent !== $post->ID && $compare_to->ID !== $post->ID ) {
    48 		return false;
    50 		return false;
    81 		 *
    83 		 *
    82 		 * @param string  $revision_field The current revision field to compare to or from.
    84 		 * @param string  $revision_field The current revision field to compare to or from.
    83 		 * @param string  $field          The current revision field.
    85 		 * @param string  $field          The current revision field.
    84 		 * @param WP_Post $compare_from   The revision post object to compare to or from.
    86 		 * @param WP_Post $compare_from   The revision post object to compare to or from.
    85 		 * @param string  $context        The context of whether the current revision is the old
    87 		 * @param string  $context        The context of whether the current revision is the old
    86 		 *                                or the new one. Values are 'to' or 'from'.
    88 		 *                                or the new one. Either 'to' or 'from'.
    87 		 */
    89 		 */
    88 		$content_from = $compare_from ? apply_filters( "_wp_post_revision_field_{$field}", $compare_from->$field, $field, $compare_from, 'from' ) : '';
    90 		$content_from = $compare_from ? apply_filters( "_wp_post_revision_field_{$field}", $compare_from->$field, $field, $compare_from, 'from' ) : '';
    89 
    91 
    90 		/** This filter is documented in wp-admin/includes/revision.php */
    92 		/** This filter is documented in wp-admin/includes/revision.php */
    91 		$content_to = apply_filters( "_wp_post_revision_field_{$field}", $compare_to->$field, $field, $compare_to, 'to' );
    93 		$content_to = apply_filters( "_wp_post_revision_field_{$field}", $compare_to->$field, $field, $compare_to, 'to' );
   116 		$args = apply_filters( 'revision_text_diff_options', $args, $field, $compare_from, $compare_to );
   118 		$args = apply_filters( 'revision_text_diff_options', $args, $field, $compare_from, $compare_to );
   117 
   119 
   118 		$diff = wp_text_diff( $content_from, $content_to, $args );
   120 		$diff = wp_text_diff( $content_from, $content_to, $args );
   119 
   121 
   120 		if ( ! $diff && 'post_title' === $field ) {
   122 		if ( ! $diff && 'post_title' === $field ) {
   121 			// It's a better user experience to still show the Title, even if it didn't change.
   123 			/*
   122 			// No, you didn't see this.
   124 			 * It's a better user experience to still show the Title, even if it didn't change.
       
   125 			 * No, you didn't see this.
       
   126 			 */
   123 			$diff = '<table class="diff"><colgroup><col class="content diffsplit left"><col class="content diffsplit middle"><col class="content diffsplit right"></colgroup><tbody><tr>';
   127 			$diff = '<table class="diff"><colgroup><col class="content diffsplit left"><col class="content diffsplit middle"><col class="content diffsplit right"></colgroup><tbody><tr>';
   124 
   128 
   125 			// In split screen mode, show the title before/after side by side.
   129 			// In split screen mode, show the title before/after side by side.
   126 			if ( true === $args['show_split_view'] ) {
   130 			if ( true === $args['show_split_view'] ) {
   127 				$diff .= '<td>' . esc_html( $compare_from->post_title ) . '</td><td></td><td>' . esc_html( $compare_to->post_title ) . '</td>';
   131 				$diff .= '<td>' . esc_html( $compare_from->post_title ) . '</td><td></td><td>' . esc_html( $compare_to->post_title ) . '</td>';
   155 	 * @param array[] $return       Array of revision UI fields. Each item is an array of id, name, and diff.
   159 	 * @param array[] $return       Array of revision UI fields. Each item is an array of id, name, and diff.
   156 	 * @param WP_Post $compare_from The revision post to compare from.
   160 	 * @param WP_Post $compare_from The revision post to compare from.
   157 	 * @param WP_Post $compare_to   The revision post to compare to.
   161 	 * @param WP_Post $compare_to   The revision post to compare to.
   158 	 */
   162 	 */
   159 	return apply_filters( 'wp_get_revision_ui_diff', $return, $compare_from, $compare_to );
   163 	return apply_filters( 'wp_get_revision_ui_diff', $return, $compare_from, $compare_to );
   160 
       
   161 }
   164 }
   162 
   165 
   163 /**
   166 /**
   164  * Prepare revisions for JavaScript.
   167  * Prepare revisions for JavaScript.
   165  *
   168  *
   192 		$revisions = array( $post->ID => $post ) + $revisions;
   195 		$revisions = array( $post->ID => $post ) + $revisions;
   193 	}
   196 	}
   194 
   197 
   195 	$show_avatars = get_option( 'show_avatars' );
   198 	$show_avatars = get_option( 'show_avatars' );
   196 
   199 
   197 	cache_users( wp_list_pluck( $revisions, 'post_author' ) );
   200 	update_post_author_caches( $revisions );
   198 
   201 
   199 	$can_restore = current_user_can( 'edit_post', $post->ID );
   202 	$can_restore = current_user_can( 'edit_post', $post->ID );
   200 	$current_id  = false;
   203 	$current_id  = false;
   201 
   204 
   202 	foreach ( $revisions as $revision ) {
   205 	foreach ( $revisions as $revision ) {
   277 		 * @param WP_Post $post           The revision's parent WP_Post object.
   280 		 * @param WP_Post $post           The revision's parent WP_Post object.
   278 		 */
   281 		 */
   279 		$revisions[ $revision->ID ] = apply_filters( 'wp_prepare_revision_for_js', $revisions_data, $revision, $post );
   282 		$revisions[ $revision->ID ] = apply_filters( 'wp_prepare_revision_for_js', $revisions_data, $revision, $post );
   280 	}
   283 	}
   281 
   284 
   282 	/**
   285 	/*
   283 	 * If we only have one revision, the initial revision is missing; This happens
   286 	 * If we only have one revision, the initial revision is missing. This happens
   284 	 * when we have an autsosave and the user has clicked 'View the Autosave'
   287 	 * when we have an autosave and the user has clicked 'View the Autosave'.
   285 	 */
   288 	 */
   286 	if ( 1 === count( $revisions ) ) {
   289 	if ( 1 === count( $revisions ) ) {
   287 		$revisions[ $post->ID ] = array(
   290 		$revisions[ $post->ID ] = array(
   288 			'id'         => $post->ID,
   291 			'id'         => $post->ID,
   289 			'title'      => get_the_title( $post->ID ),
   292 			'title'      => get_the_title( $post->ID ),
   298 		);
   301 		);
   299 		$current_id             = $post->ID;
   302 		$current_id             = $post->ID;
   300 	}
   303 	}
   301 
   304 
   302 	/*
   305 	/*
   303 	 * If a post has been saved since the last revision (no revisioned fields
   306 	 * If a post has been saved since the latest revision (no revisioned fields
   304 	 * were changed), we may not have a "current" revision. Mark the latest
   307 	 * were changed), we may not have a "current" revision. Mark the latest
   305 	 * revision as "current".
   308 	 * revision as "current".
   306 	 */
   309 	 */
   307 	if ( empty( $current_id ) ) {
   310 	if ( empty( $current_id ) ) {
   308 		if ( $revisions[ $revision->ID ]['autosave'] ) {
   311 		if ( $revisions[ $revision->ID ]['autosave'] ) {