WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint

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

Defined (1)

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

/json-endpoints/class.wpcom-json-api-get-autosave-v1-1-endpoint.php  
  1. class WPCOM_JSON_API_Get_Autosave_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. $post = get_post( $post_id ); 
  13.  
  14. if ( ! $post || is_wp_error( $post ) ) { 
  15. return new WP_Error( 'unknown_post', 'Unknown post', 404 ); 
  16.  
  17. if ( ! current_user_can( 'edit_post', $post->ID ) ) { 
  18. return new WP_Error( 'unauthorized', 'User cannot edit post', 403 ); 
  19.  
  20. $autosave = wp_get_post_autosave( $post->ID ); 
  21.  
  22. if ( $autosave ) { 
  23. $preview_url = add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ); 
  24. $nonce = wp_create_nonce( 'post_preview_' . $post->ID ); 
  25. $preview_url = add_query_arg( array( 'preview_id' => $auto_ID, 'preview_nonce' => $nonce ), $preview_url ); 
  26.  
  27. return array( 
  28. 'ID' => $autosave->ID,  
  29. 'author_ID' => $autosave->post_author,  
  30. 'post_ID' => $autosave->post_parent,  
  31. 'title' => $autosave->post_title,  
  32. 'content' => $autosave->post_content,  
  33. 'excerpt' => $autosave->post_excerpt,  
  34. 'preview_URL' => $preview_url,  
  35. 'modified' => $this->format_date( $autosave->post_modified ) 
  36. ); 
  37. } else { 
  38. return new WP_Error( 'not_found', 'No autosaves exist for this post', 404 );