WP_REST_Autosaves_Controller::create_post_autosave() – Creates autosave for the specified post.

You appear to be a bot. Output may be restricted

Description

Creates autosave for the specified post.

From wp-admin/post.php.

Usage

$mixed = WP_REST_Autosaves_Controller::create_post_autosave( $post_data, $meta );

Parameters

$post_data
( array ) required – Associative array containing the post data.
$meta
( array ) optional – Associative array containing the post meta data.

Returns

mixed The autosave revision ID or WP_Error.

Source

File name: wordpress/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php
Lines:

1 to 74 of 74
  public function create_post_autosave( $post_data, array $meta = array() ) {

    $post_id = (int) $post_data['ID'];
    $post    = get_post( $post_id );

    if ( is_wp_error( $post ) ) {
      return $post;
    }

    // Only create an autosave when it is different from the saved post.
    $autosave_is_different = false;
    $new_autosave          = _wp_post_revision_data( $post_data, true );

    foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
      if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) {
        $autosave_is_different = true;
        break;
      }
    }

    // Check if meta values have changed.
    if ( ! empty( $meta ) ) {
      $revisioned_meta_keys = wp_post_revision_meta_keys( $post->post_type );
      foreach ( $revisioned_meta_keys as $meta_key ) {
        // get_metadata_raw is used to avoid retrieving the default value.
        $old_meta = get_metadata_raw( 'post', $post_id, $meta_key, true );
        $new_meta = isset( $meta[ $meta_key ] ) ? $meta[ $meta_key ] : '';

        if ( $new_meta !== $old_meta ) {
          $autosave_is_different = true;
          break;
        }
      }
    }

    $user_id = get_current_user_id();

    // Store one autosave per author. If there is already an autosave, overwrite it.
    $old_autosave = wp_get_post_autosave( $post_id, $user_id );

    if ( ! $autosave_is_different && $old_autosave ) {
      // Nothing to save, return the existing autosave.
      return $old_autosave->ID;
    }

    if ( $old_autosave ) {
      $new_autosave['ID']          = $old_autosave->ID;
      $new_autosave['post_author'] = $user_id;

      
/** This filter is documented in wp-admin/post.php */
      do_action( 'wp_creating_autosave', $new_autosave );

      // wp_update_post() expects escaped array.
      $revision_id = wp_update_post( wp_slash( $new_autosave ) );
    } else {
      // Create the new autosave as a special post revision.
      $revision_id = _wp_put_post_revision( $post_data, true );
    }

    if ( is_wp_error( $revision_id ) || 0 === $revision_id ) {
      return $revision_id;
    }

    // Attached any passed meta values that have revisions enabled.
    if ( ! empty( $meta ) ) {
      foreach ( $revisioned_meta_keys as $meta_key ) {
        if ( isset( $meta[ $meta_key ] ) ) {
          update_metadata( 'post', $revision_id, $meta_key, wp_slash( $meta[ $meta_key ] ) );
        }
      }
    }

    return $revision_id;
  }
 

 View on GitHub View on Trac