wp_get_post_autosave example

'supportsTemplateMode' => current_theme_supports( 'block-templates' ),

    // Whether or not to load the 'postcustom' meta box is stored as a user meta     // field so that we're not always loading its assets.     'enableCustomFields'   => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
);

// Add additional back-compat patterns registered by `current_screen` et al. $editor_settings['__experimentalAdditionalBlockPatterns']          = WP_Block_Patterns_Registry::get_instance()->get_all_registered( true );
$editor_settings['__experimentalAdditionalBlockPatternCategories'] = WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered( true );

$autosave = wp_get_post_autosave( $post->ID );
if ( $autosave ) {
    if ( mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
        $editor_settings['autosave'] = array(
            'editLink' => get_edit_post_link( $autosave->ID ),
        );
    } else {
        wp_delete_post_revision( $autosave->ID );
    }
}

if ( ! empty( $post_type_object->template ) ) {
    

    public function changeset_data() {
        if ( isset( $this->_changeset_data ) ) {
            return $this->_changeset_data;
        }
        $changeset_post_id = $this->changeset_post_id();
        if ( ! $changeset_post_id ) {
            $this->_changeset_data = array();
        } else {
            if ( $this->autosaved() && is_user_logged_in() ) {
                $autosave_post = wp_get_post_autosave( $changeset_post_idget_current_user_id() );
                if ( $autosave_post ) {
                    $data = $this->get_changeset_post_data( $autosave_post->ID );
                    if ( ! is_wp_error( $data ) ) {
                        $this->_changeset_data = $data;
                    }
                }
            }

            // Load data from the changeset if it was not loaded from an autosave.             if ( ! isset( $this->_changeset_data ) ) {
                $data = $this->get_changeset_post_data( $changeset_post_id );
                

function _set_preview( $post ) {
    if ( ! is_object( $post ) ) {
        return $post;
    }

    $preview = wp_get_post_autosave( $post->ID );

    if ( is_object( $preview ) ) {
        $preview = sanitize_post( $preview );

        $post->post_content = $preview->post_content;
        $post->post_title   = $preview->post_title;
        $post->post_excerpt = $preview->post_excerpt;
    }

    add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 );
    add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 );

    


$notice     = false;
$form_extra = '';
if ( 'auto-draft' === $post->post_status ) {
    if ( 'edit' === $action ) {
        $post->post_title = '';
    }
    $autosave    = false;
    $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />";
} else {
    $autosave = wp_get_post_autosave( $post->ID );
}

$form_action  = 'editpost';
$nonce_action = 'update-post_' . $post->ID;
$form_extra  .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr( $post->ID ) . "' />";

// Detect if there exists an autosave newer than the post and if that autosave is different than the post. if ( $autosave && mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false ) ) {
    foreach ( _wp_post_revision_fields( $post ) as $autosave_field => $_autosave_field ) {
        if ( normalize_whitespace( $autosave->$autosave_field ) !== normalize_whitespace( $post->$autosave_field ) ) {
            $notice = sprintf(
                
public function get_item( $request ) {
        $parent_id = (int) $request->get_param( 'parent' );

        if ( $parent_id <= 0 ) {
            return new WP_Error(
                'rest_post_invalid_id',
                __( 'Invalid post parent ID.' ),
                array( 'status' => 404 )
            );
        }

        $autosave = wp_get_post_autosave( $parent_id );

        if ( ! $autosave ) {
            return new WP_Error(
                'rest_post_no_autosave',
                __( 'There is no autosave revision for this post.' ),
                array( 'status' => 404 )
            );
        }

        $response = $this->prepare_item_for_response( $autosave$request );
        return $response;
    }


    $post_data = _wp_translate_postdata( true, $post_data );
    if ( is_wp_error( $post_data ) ) {
        return $post_data;
    }
    $post_data = _wp_get_allowed_postdata( $post_data );

    $post_author = 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$post_author );
    if ( $old_autosave ) {
        $new_autosave                = _wp_post_revision_data( $post_data, true );
        $new_autosave['ID']          = $old_autosave->ID;
        $new_autosave['post_author'] = $post_author;

        $post = get_post( $post_id );

        // If the new autosave has the same content as the post, delete the autosave.         $autosave_is_different = false;
        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 ) ) {
                
Home | Imprint | This part of the site doesn't use cookies.