WPCOM_JSON_API_Autosave_Post_v1_1_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Autosave Post v1 1 Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-autosave-post-v1-1-endpoint.php  
  1. class WPCOM_JSON_API_Autosave_Post_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint { 
  2. function __construct( $args ) { 
  3. parent::__construct( $args ); 
  4.  
  5. // /sites/%s/posts/%d/autosave -> $blog_id, $post_id 
  6. function callback( $path = '', $blog_id = 0, $post_id = 0 ) { 
  7.  
  8. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  9. if ( is_wp_error( $blog_id ) ) { 
  10. return $blog_id; 
  11.  
  12. $args = $this->query_args(); 
  13.  
  14. $input = $this->input( false ); 
  15.  
  16. if ( ! is_array( $input ) || ! $input ) { 
  17. return new WP_Error( 'invalid_input', 'Invalid request input', 400 ); 
  18.  
  19. $post = get_post( $post_id ); 
  20.  
  21. if ( ! $post || is_wp_error( $post ) ) { 
  22. return new WP_Error( 'unknown_post', 'Unknown post', 404 ); 
  23.  
  24. if ( ! current_user_can( 'edit_post', $post->ID ) ) { 
  25. return new WP_Error( 'unauthorized', 'User cannot edit post', 403 ); 
  26.  
  27. $post_data = array ( 
  28. 'post_ID' => $post_id,  
  29. 'post_title' => $input['title'],  
  30. 'post_content' => $input['content'],  
  31. 'post_excerpt' => $input['excerpt'],  
  32. ); 
  33.  
  34. $preview_url = add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ); 
  35.  
  36. if ( ! wp_check_post_lock( $post->ID ) && 
  37. get_current_user_id() == $post->post_author && 
  38. ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) 
  39. ) { 
  40. // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked 
  41. $auto_ID = edit_post( wp_slash( $post_data ) ); 
  42. } else { 
  43. // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user. 
  44. $auto_ID = wp_create_post_autosave( wp_slash( $post_data ) ); 
  45. $nonce = wp_create_nonce( 'post_preview_' . $post->ID ); 
  46. $preview_url = add_query_arg( array( 'preview_id' => $auto_ID, 'preview_nonce' => $nonce ), $preview_url ); 
  47.  
  48. $updated_post = get_post( $auto_ID ); 
  49.  
  50. if ( $updated_post && $updated_post->ID && $updated_post->post_modified ) { 
  51. return array( 
  52. 'ID' => $auto_ID,  
  53. 'post_ID' => $post->ID,  
  54. 'modified' => $this->format_date( $updated_post->post_modified ),  
  55. 'preview_URL' => $preview_url 
  56. ); 
  57. } else { 
  58. return new WP_Error( 'autosave_error', __( 'Autosave encountered an unexpected error', 'jetpack' ), 500 );