_wp_post_revision_fields example


function _wp_post_revision_data( $post = array()$autosave = false ) {
    if ( ! is_array( $post ) ) {
        $post = get_post( $post, ARRAY_A );
    }

    $fields = _wp_post_revision_fields( $post );

    $revision_data = array();

    foreach ( array_intersect( array_keys( $post )array_keys( $fields ) ) as $field ) {
        $revision_data[ $field ] = $post[ $field ];
    }

    $revision_data['post_parent']   = $post['ID'];
    $revision_data['post_status']   = 'inherit';
    $revision_data['post_type']     = 'revision';
    $revision_data['post_name']     = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version.
// 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 ) ) {
                $autosave_is_different = true;
                break;
            }
        }

        if ( ! $autosave_is_different ) {
            wp_delete_post_revision( $old_autosave->ID );
            return 0;
        }

        
// Add default title if title field is empty.     if ( $compare_from && empty( $compare_from->post_title ) ) {
        $compare_from->post_title = __( '(no title)' );
    }
    if ( empty( $compare_to->post_title ) ) {
        $compare_to->post_title = __( '(no title)' );
    }

    $return = array();

    foreach ( _wp_post_revision_fields( $post ) as $field => $name ) {
        /** * Contextually filter a post revision field. * * The dynamic portion of the hook name, `$field`, corresponds to a name of a * field of the revision object. * * Possible hook names include: * * - `_wp_post_revision_field_post_title` * - `_wp_post_revision_field_post_content` * - `_wp_post_revision_field_post_excerpt` * * @since 3.6.0 * * @param string $revision_field The current revision field to compare to or from. * @param string $field The current revision field. * @param WP_Post $compare_from The revision post object to compare to or from. * @param string $context The context of whether the current revision is the old * or the new one. Values are 'to' or 'from'. */
$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;
            }
        }

        $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 );

        
$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(
                /* translators: %s: URL to view the autosave. */
                __( 'There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>' ),
                get_edit_post_link( $autosave->ID )
            );
            break;
        }
    }
    // If this autosave isn't different from the current post, begone.     if ( ! $notice ) {
        
Home | Imprint | This part of the site doesn't use cookies.