wp/wp-includes/revision.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
--- a/wp/wp-includes/revision.php	Mon Oct 14 18:06:33 2019 +0200
+++ b/wp/wp-includes/revision.php	Mon Oct 14 18:28:13 2019 +0200
@@ -31,7 +31,7 @@
 	if ( is_null( $fields ) ) {
 		// Allow these to be versioned
 		$fields = array(
-			'post_title' => __( 'Title' ),
+			'post_title'   => __( 'Title' ),
 			'post_content' => __( 'Content' ),
 			'post_excerpt' => __( 'Excerpt' ),
 		);
@@ -60,7 +60,6 @@
 		unset( $fields[ $protect ] );
 	}
 
-
 	return $fields;
 }
 
@@ -110,20 +109,25 @@
  * @return int|WP_Error|void Void or 0 if error, new revision ID, if success.
  */
 function wp_save_post_revision( $post_id ) {
-	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
+	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
 		return;
+	}
 
-	if ( ! $post = get_post( $post_id ) )
+	if ( ! $post = get_post( $post_id ) ) {
 		return;
+	}
 
-	if ( ! post_type_supports( $post->post_type, 'revisions' ) )
+	if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
 		return;
+	}
 
-	if ( 'auto-draft' == $post->post_status )
+	if ( 'auto-draft' == $post->post_status ) {
 		return;
+	}
 
-	if ( ! wp_revisions_enabled( $post ) )
+	if ( ! wp_revisions_enabled( $post ) ) {
 		return;
+	}
 
 	// Compare the proposed update with the last stored revision verifying that
 	// they are different, unless a plugin tells us to always save regardless.
@@ -149,7 +153,6 @@
 		 *                                   Default true.
 		 * @param WP_Post $last_revision     The last revision post object.
 		 * @param WP_Post $post              The post object.
-		 *
 		 */
 		if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post ) ) {
 			$post_has_changed = false;
@@ -172,7 +175,6 @@
 			 * @param bool    $post_has_changed Whether the post has changed.
 			 * @param WP_Post $last_revision    The last revision post object.
 			 * @param WP_Post $post             The post object.
-			 *
 			 */
 			$post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post );
 
@@ -189,21 +191,24 @@
 	// delete the oldest ones.
 	$revisions_to_keep = wp_revisions_to_keep( $post );
 
-	if ( $revisions_to_keep < 0 )
+	if ( $revisions_to_keep < 0 ) {
 		return $return;
+	}
 
 	$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );
 
-	$delete = count($revisions) - $revisions_to_keep;
+	$delete = count( $revisions ) - $revisions_to_keep;
 
-	if ( $delete < 1 )
+	if ( $delete < 1 ) {
 		return $return;
+	}
 
 	$revisions = array_slice( $revisions, 0, $delete );
 
-	for ( $i = 0; isset( $revisions[$i] ); $i++ ) {
-		if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) )
+	for ( $i = 0; isset( $revisions[ $i ] ); $i++ ) {
+		if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) {
 			continue;
+		}
 
 		wp_delete_post_revision( $revisions[ $i ]->ID );
 	}
@@ -229,8 +234,9 @@
 
 	foreach ( $revisions as $revision ) {
 		if ( false !== strpos( $revision->post_name, "{$post_id}-autosave" ) ) {
-			if ( $user_id && $user_id != $revision->post_author )
+			if ( $user_id && $user_id != $revision->post_author ) {
 				continue;
+			}
 
 			return $revision;
 		}
@@ -248,8 +254,9 @@
  * @return false|int False if not a revision, ID of revision's parent otherwise.
  */
 function wp_is_post_revision( $post ) {
-	if ( !$post = wp_get_post_revision( $post ) )
+	if ( ! $post = wp_get_post_revision( $post ) ) {
 		return false;
+	}
 
 	return (int) $post->post_parent;
 }
@@ -263,11 +270,13 @@
  * @return false|int False if not a revision, ID of autosave's parent otherwise
  */
 function wp_is_post_autosave( $post ) {
-	if ( !$post = wp_get_post_revision( $post ) )
+	if ( ! $post = wp_get_post_revision( $post ) ) {
 		return false;
+	}
 
-	if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) )
+	if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) {
 		return (int) $post->post_parent;
+	}
 
 	return false;
 }
@@ -283,23 +292,27 @@
  * @return int|WP_Error WP_Error or 0 if error, new revision ID if success.
  */
 function _wp_put_post_revision( $post = null, $autosave = false ) {
-	if ( is_object($post) )
+	if ( is_object( $post ) ) {
 		$post = get_object_vars( $post );
-	elseif ( !is_array($post) )
-		$post = get_post($post, ARRAY_A);
+	} elseif ( ! is_array( $post ) ) {
+		$post = get_post( $post, ARRAY_A );
+	}
 
-	if ( ! $post || empty($post['ID']) )
+	if ( ! $post || empty( $post['ID'] ) ) {
 		return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
+	}
 
-	if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
+	if ( isset( $post['post_type'] ) && 'revision' == $post['post_type'] ) {
 		return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
+	}
 
 	$post = _wp_post_revision_data( $post, $autosave );
-	$post = wp_slash($post); //since data is from db
+	$post = wp_slash( $post ); //since data is from db
 
 	$revision_id = wp_insert_post( $post );
-	if ( is_wp_error($revision_id) )
+	if ( is_wp_error( $revision_id ) ) {
 		return $revision_id;
+	}
 
 	if ( $revision_id ) {
 		/**
@@ -326,19 +339,21 @@
  * @param string      $filter Optional sanitation filter. See sanitize_post().
  * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
  */
-function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
-	if ( !$revision = get_post( $post, OBJECT, $filter ) )
+function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) {
+	if ( ! $revision = get_post( $post, OBJECT, $filter ) ) {
 		return $revision;
-	if ( 'revision' !== $revision->post_type )
+	}
+	if ( 'revision' !== $revision->post_type ) {
 		return null;
+	}
 
 	if ( $output == OBJECT ) {
 		return $revision;
 	} elseif ( $output == ARRAY_A ) {
-		$_revision = get_object_vars($revision);
+		$_revision = get_object_vars( $revision );
 		return $_revision;
 	} elseif ( $output == ARRAY_N ) {
-		$_revision = array_values(get_object_vars($revision));
+		$_revision = array_values( get_object_vars( $revision ) );
 		return $_revision;
 	}
 
@@ -357,27 +372,31 @@
  * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success.
  */
 function wp_restore_post_revision( $revision_id, $fields = null ) {
-	if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )
+	if ( ! $revision = wp_get_post_revision( $revision_id, ARRAY_A ) ) {
 		return $revision;
+	}
 
-	if ( !is_array( $fields ) )
+	if ( ! is_array( $fields ) ) {
 		$fields = array_keys( _wp_post_revision_fields( $revision ) );
+	}
 
 	$update = array();
 	foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) {
-		$update[$field] = $revision[$field];
+		$update[ $field ] = $revision[ $field ];
 	}
 
-	if ( !$update )
+	if ( ! $update ) {
 		return false;
+	}
 
 	$update['ID'] = $revision['post_parent'];
 
 	$update = wp_slash( $update ); //since data is from db
 
 	$post_id = wp_update_post( $update );
-	if ( ! $post_id || is_wp_error( $post_id ) )
+	if ( ! $post_id || is_wp_error( $post_id ) ) {
 		return $post_id;
+	}
 
 	// Update last edit user
 	update_post_meta( $post_id, '_edit_last', get_current_user_id() );
@@ -439,19 +458,33 @@
  */
 function wp_get_post_revisions( $post_id = 0, $args = null ) {
 	$post = get_post( $post_id );
-	if ( ! $post || empty( $post->ID ) )
+	if ( ! $post || empty( $post->ID ) ) {
 		return array();
+	}
 
-	$defaults = array( 'order' => 'DESC', 'orderby' => 'date ID', 'check_enabled' => true );
-	$args = wp_parse_args( $args, $defaults );
+	$defaults = array(
+		'order'         => 'DESC',
+		'orderby'       => 'date ID',
+		'check_enabled' => true,
+	);
+	$args     = wp_parse_args( $args, $defaults );
 
-	if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) )
+	if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) {
 		return array();
+	}
 
-	$args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );
+	$args = array_merge(
+		$args,
+		array(
+			'post_parent' => $post->ID,
+			'post_type'   => 'revision',
+			'post_status' => 'inherit',
+		)
+	);
 
-	if ( ! $revisions = get_children( $args ) )
+	if ( ! $revisions = get_children( $args ) ) {
 		return array();
+	}
 
 	return $revisions;
 }
@@ -484,13 +517,15 @@
 function wp_revisions_to_keep( $post ) {
 	$num = WP_POST_REVISIONS;
 
-	if ( true === $num )
+	if ( true === $num ) {
 		$num = -1;
-	else
+	} else {
 		$num = intval( $num );
+	}
 
-	if ( ! post_type_supports( $post->post_type, 'revisions' ) )
+	if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
 		$num = 0;
+	}
 
 	/**
 	 * Filters the number of revisions to save for the given post.
@@ -527,7 +562,7 @@
 	$preview = sanitize_post( $preview );
 
 	$post->post_content = $preview->post_content;
-	$post->post_title = $preview->post_title;
+	$post->post_title   = $preview->post_title;
 	$post->post_excerpt = $preview->post_excerpt;
 
 	add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
@@ -543,13 +578,14 @@
  * @access private
  */
 function _show_post_preview() {
-	if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {
+	if ( isset( $_GET['preview_id'] ) && isset( $_GET['preview_nonce'] ) ) {
 		$id = (int) $_GET['preview_id'];
 
-		if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )
-			wp_die( __('Sorry, you are not allowed to preview drafts.') );
+		if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) {
+			wp_die( __( 'Sorry, you are not allowed to preview drafts.' ), 403 );
+		}
 
-		add_filter('the_preview', '_set_preview');
+		add_filter( 'the_preview', '_set_preview' );
 	}
 }
 
@@ -565,16 +601,19 @@
  * @return array
  */
 function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
-	if ( ! $post = get_post() )
+	if ( ! $post = get_post() ) {
 		return $terms;
+	}
 
-	if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type )
+	if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id || 'post_format' != $taxonomy || 'revision' == $post->post_type ) {
 		return $terms;
+	}
 
-	if ( 'standard' == $_REQUEST['post_format'] )
+	if ( 'standard' == $_REQUEST['post_format'] ) {
 		$terms = array();
-	elseif ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ) )
+	} elseif ( $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ) ) {
 		$terms = array( $term ); // Can only have one post format
+	}
 
 	return $terms;
 }
@@ -596,12 +635,12 @@
 	}
 
 	if ( empty( $_REQUEST['_thumbnail_id'] ) ||
-	     empty( $_REQUEST['preview_id'] ) ||
-	     $post->ID != $post_id ||
-	     '_thumbnail_id' != $meta_key ||
-	     'revision' == $post->post_type ||
-	     $post_id != $_REQUEST['preview_id']
-	) {
+		empty( $_REQUEST['preview_id'] ) ||
+		$post->ID != $post_id ||
+		'_thumbnail_id' != $meta_key ||
+		'revision' == $post->post_type ||
+		$post_id != $_REQUEST['preview_id'] ) {
+
 		return $value;
 	}
 
@@ -623,13 +662,15 @@
  * @return int|false
  */
 function _wp_get_post_revision_version( $revision ) {
-	if ( is_object( $revision ) )
+	if ( is_object( $revision ) ) {
 		$revision = get_object_vars( $revision );
-	elseif ( !is_array( $revision ) )
+	} elseif ( ! is_array( $revision ) ) {
 		return false;
+	}
 
-	if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) )
+	if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) {
 		return (int) $matches[1];
+	}
 
 	return 0;
 }
@@ -650,8 +691,8 @@
 	global $wpdb;
 
 	// Add post option exclusively
-	$lock = "revision-upgrade-{$post->ID}";
-	$now = time();
+	$lock   = "revision-upgrade-{$post->ID}";
+	$now    = time();
 	$result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
 	if ( ! $result ) {
 		// If we couldn't get a lock, see how old the previous lock is
@@ -683,8 +724,9 @@
 		$this_revision_version = _wp_get_post_revision_version( $this_revision );
 
 		// Something terrible happened
-		if ( false === $this_revision_version )
+		if ( false === $this_revision_version ) {
 			continue;
+		}
 
 		// 1 is the latest revision version, so we're already up to date.
 		// No need to add a copy of the post as latest revision.
@@ -705,23 +747,25 @@
 			$prev_revision_version = _wp_get_post_revision_version( $prev_revision );
 
 			// If the previous revision is already up to date, it no longer has the information we need :(
-			if ( $prev_revision_version < 1 )
+			if ( $prev_revision_version < 1 ) {
 				$update['post_author'] = $prev_revision->post_author;
+			}
 		}
 
 		// Upgrade this revision
 		$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
 
-		if ( $result )
+		if ( $result ) {
 			wp_cache_delete( $this_revision->ID, 'posts' );
-
+		}
 	} while ( $prev_revision );
 
 	delete_option( $lock );
 
 	// Add a copy of the post as latest revision.
-	if ( $add_last )
+	if ( $add_last ) {
 		wp_save_post_revision( $post->ID );
+	}
 
 	return true;
 }