Make WordPress Core


Ignore:
Timestamp:
01/22/2014 04:55:37 AM (9 years ago)
Author:
azaozz
Message:

Autosave: refactor autosave.js, use heartbeat for transport and move all "Add/Edit Post" related functionality to post.js. See #25272.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/post.php

    r26169 r26995  
    8080    }
    8181
    82     if ( ! empty( $post_data['post_status'] ) )
     82    if ( ! empty( $post_data['post_status'] ) ) {
    8383        $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
     84
     85        // No longer an auto-draft
     86        if ( 'auto-draft' == $post_data['post_status'] )
     87            $post_data['post_status'] = 'draft';
     88    }
    8489
    8590    // What to do based on which button they pressed
     
    191196    if ( is_wp_error($post_data) )
    192197        wp_die( $post_data->get_error_message() );
    193     if ( ( empty( $post_data['action'] ) || 'autosave' != $post_data['action'] ) && 'auto-draft' == $post_data['post_status'] ) {
    194         $post_data['post_status'] = 'draft';
    195     }
    196198
    197199    if ( isset($post_data['visibility']) ) {
     
    13361338 * @uses _wp_post_revision_fields()
    13371339 *
    1338  * @return unknown
    1339  */
    1340 function wp_create_post_autosave( $post_id ) {
    1341     $translated = _wp_translate_postdata( true );
    1342     if ( is_wp_error( $translated ) )
    1343         return $translated;
     1340 * @param mixed $post_data Associative array containing the post data or int post ID.
     1341 * @return mixed The autosave revision ID. WP_Error or 0 on error.
     1342 */
     1343function wp_create_post_autosave( $post_data ) {
     1344    if ( is_numeric( $post_data ) ) {
     1345        $post_id = $post_data;
     1346        $post_data = &$_POST;
     1347    } else {
     1348        $post_id = (int) $post_data['post_ID'];
     1349    }
     1350
     1351    $post_data = _wp_translate_postdata( true, $post_data );
     1352    if ( is_wp_error( $post_data ) )
     1353        return $post_data;
    13441354
    13451355    $post_author = get_current_user_id();
     
    13471357    // Store one autosave per author. If there is already an autosave, overwrite it.
    13481358    if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
    1349         $new_autosave = _wp_post_revision_fields( $_POST, true );
     1359        $new_autosave = _wp_post_revision_fields( $post_data, true );
    13501360        $new_autosave['ID'] = $old_autosave->ID;
    13511361        $new_autosave['post_author'] = $post_author;
    13521362
    1353         // If the new autosave is the same content as the post, delete the old autosave.
     1363        // If the new autosave has the same content as the post, delete the autosave.
    13541364        $post = get_post( $post_id );
    13551365        $autosave_is_different = false;
     
    13631373        if ( ! $autosave_is_different ) {
    13641374            wp_delete_post_revision( $old_autosave->ID );
    1365             return;
     1375            return 0;
    13661376        }
    13671377
     
    13701380
    13711381    // _wp_put_post_revision() expects unescaped.
    1372     $post_data = wp_unslash( $_POST );
     1382    $post_data = wp_unslash( $post_data );
    13731383
    13741384    // Otherwise create the new autosave as a special post revision
     
    13961406
    13971407    $post_ID = (int) $_POST['post_ID'];
    1398     $status = get_post_status( $post_ID );
    1399     if ( 'auto-draft' == $status )
    1400         wp_die( __('Preview not available. Please save as a draft first.') );
    1401 
    1402     if ( isset($_POST['catslist']) )
    1403         $_POST['post_category'] = explode(",", $_POST['catslist']);
    1404 
    1405     if ( isset($_POST['tags_input']) )
    1406         $_POST['tags_input'] = explode(",", $_POST['tags_input']);
    1407 
    1408     if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
    1409         unset($_POST['post_category']);
    1410 
    14111408    $_POST['ID'] = $post_ID;
    1412     $post = get_post($post_ID);
    1413 
    1414     if ( 'page' == $post->post_type ) {
    1415         if ( ! current_user_can('edit_page', $post_ID) )
    1416             wp_die( __('You are not allowed to edit this page.') );
     1409
     1410    if ( ! $post = get_post( $post_ID ) )
     1411        wp_die( __('You attempted to preview a non existing item.') );
     1412
     1413    if ( ! current_user_can( 'edit_post', $post->ID ) )
     1414        wp_die( __('You are not allowed to preview this item.') );
     1415
     1416    $is_autosave = false;
     1417
     1418    if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) {
     1419        $saved_post_id = edit_post();
    14171420    } else {
    1418         if ( ! current_user_can('edit_post', $post_ID) )
    1419             wp_die( __('You are not allowed to edit this post.') );
    1420     }
    1421 
    1422     $user_id = get_current_user_id();
    1423     $locked = wp_check_post_lock( $post->ID );
    1424     if ( ! $locked && 'draft' == $post->post_status && $user_id == $post->post_author ) {
    1425         $id = edit_post();
    1426     } else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
    1427         $id = wp_create_post_autosave( $post->ID );
    1428         if ( ! is_wp_error($id) )
    1429             $id = $post->ID;
    1430     }
    1431 
    1432     if ( is_wp_error($id) )
    1433         wp_die( $id->get_error_message() );
    1434 
    1435     if ( ! $locked && $_POST['post_status'] == 'draft' && $user_id == $post->post_author ) {
    1436         $url = add_query_arg( 'preview', 'true', get_permalink($id) );
     1421        $is_autosave = true;
     1422
     1423        if ( 'auto-draft' == $_POST['post_status'] )
     1424            $_POST['post_status'] = 'draft';
     1425
     1426        $saved_post_id = wp_create_post_autosave( $post->ID );
     1427    }
     1428
     1429    if ( is_wp_error( $saved_post_id ) )
     1430        wp_die( $saved_post_id->get_error_message() );
     1431
     1432    $query_args = array( 'preview' => 'true' );
     1433
     1434    if ( $is_autosave && $saved_post_id ) {
     1435        $query_args['preview_id'] = $post->ID;
     1436        $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
     1437
     1438        if ( isset( $_POST['post_format'] ) )
     1439            $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
     1440    }
     1441
     1442    $url = add_query_arg( $query_args, get_permalink( $post->ID ) );
     1443    return apply_filters( 'preview_post_link', $url );
     1444}
     1445
     1446/**
     1447 * Save a post submitted with XHR
     1448 *
     1449 * Intended for use with heartbeat and autosave.js
     1450 *
     1451 * @since 3.9
     1452 *
     1453 * @param $post_data Associative array of the submitted post data.
     1454 * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
     1455 *               Te ID can be the draft post_id or the autosave revision post_id.
     1456 */
     1457function wp_autosave( $post_data ) {
     1458    // Back-compat
     1459    if ( ! defined( 'DOING_AUTOSAVE' ) )
     1460        define( 'DOING_AUTOSAVE', true );
     1461
     1462    $post_id = (int) $post_data['post_id'];
     1463    $post_data['ID'] = $post_data['post_ID'] = $post_id;
     1464
     1465    if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) )
     1466        return new WP_Error( 'invalid_nonce', __('ERROR: invalid post data.') );
     1467
     1468    $post = get_post( $post_id );
     1469
     1470    if ( ! current_user_can( 'edit_post', $post->ID ) )
     1471        return new WP_Error( 'edit_post', __('You are not allowed to edit this item.') );
     1472
     1473    if ( 'auto-draft' == $post->post_status )
     1474        $post_data['post_status'] = 'draft';
     1475
     1476    if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) )
     1477        $post_data['post_category'] = explode( ',', $post_data['catslist'] );
     1478
     1479    if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) {
     1480        // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
     1481        return edit_post( $post_data );
    14371482    } else {
    1438         $nonce = wp_create_nonce('post_preview_' . $id);
    1439         $args = array(
    1440             'preview' => 'true',
    1441             'preview_id' => $id,
    1442             'preview_nonce' => $nonce,
    1443         );
    1444 
    1445         if ( isset( $_POST['post_format'] ) )
    1446             $args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
    1447 
    1448         $url = add_query_arg( $args, get_permalink($id) );
    1449     }
    1450 
    1451     return apply_filters( 'preview_post_link', $url );
    1452 }
     1483        // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
     1484        return wp_create_post_autosave( $post_data );
     1485    }
     1486}
Note: See TracChangeset for help on using the changeset viewer.