wp_ajax_autosave

The timeline below displays how wordpress function wp_ajax_autosave has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 3.7

function wp_ajax_autosave()
{
    define('DOING_AUTOSAVE', true);
    check_ajax_referer('autosave', 'autosavenonce');
    if (!empty($_POST['catslist'])) {
        $_POST['post_category'] = explode(',', $_POST['catslist']);
    }
    if ($_POST['post_type'] == 'page' || empty($_POST['post_category'])) {
        unset($_POST['post_category']);
    }
    $data = '';
    $supplemental = array();
    $id = $revision_id = 0;
    $post_id = (int) $_POST['post_id'];
    $_POST['ID'] = $_POST['post_ID'] = $post_id;
    $post = get_post($post_id);
    if (empty($post->ID) || !current_user_can('edit_post', $post->ID)) {
        wp_die(__('You are not allowed to edit this post.'));
    }
    if ('page' == $post->post_type && !current_user_can('edit_page', $post->ID)) {
        wp_die(__('You are not allowed to edit this page.'));
    }
    if ('auto-draft' == $post->post_status) {
        $_POST['post_status'] = 'draft';
    }
    if (!empty($_POST['autosave'])) {
        if (!wp_check_post_lock($post->ID) && get_current_user_id() == $post->post_author && ('auto-draft' == $post->post_status || 'draft' == $post->post_status)) {
            // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
            $id = edit_post();
        } else {
            // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
            $revision_id = wp_create_post_autosave($post->ID);
            if (is_wp_error($revision_id)) {
                $id = $revision_id;
            } else {
                $id = $post->ID;
            }
        }
        if (!is_wp_error($id)) {
            /* translators: draft saved date format, see http://php.net/date */
            $draft_saved_date_format = __('g:i:s a');
            /* translators: %s: date and time */
            $data = sprintf(__('Draft saved at %s.'), date_i18n($draft_saved_date_format));
        }
    } else if (!empty($_POST['auto_draft'])) {
        $id = 0;
    } else {
        $id = $post->ID;
    }
    // @todo Consider exposing any errors, rather than having 'Saving draft...'
    $x = new WP_Ajax_Response(array('what' => 'autosave', 'id' => $id, 'data' => $data, 'supplemental' => $supplemental));
    $x->send();
}