class.wpcom-json-api-autosave-post-v1-1-endpoint.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. new WPCOM_JSON_API_Autosave_Post_v1_1_Endpoint( array(
  3. 'description' => 'Create a post autosave.',
  4. 'group' => '__do_not_document',
  5. 'stat' => 'posts:autosave',
  6. 'min_version' => '1.1',
  7. 'method' => 'POST',
  8. 'path' => '/sites/%s/posts/%d/autosave',
  9. 'path_labels' => array(
  10. '$site' => '(int|string) Site ID or domain',
  11. '$post_ID' => '(int) The post ID',
  12. ),
  13. 'request_format' => array(
  14. 'content' => '(HTML) The post content.',
  15. 'title' => '(HTML) The post title.',
  16. 'excerpt' => '(HTML) The post excerpt.',
  17. ),
  18. 'response_format' => array(
  19. 'ID' => '(int) autodraft post ID',
  20. 'post_ID' => '(int) post ID',
  21. 'preview_URL' => '(string) preview URL for the post',
  22. 'modified' => '(ISO 8601 datetime) modified time',
  23. ),
  24. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/1/autosave',
  25. 'example_request_data' => array(
  26. 'headers' => array(
  27. 'authorization' => 'Bearer YOUR_API_TOKEN'
  28. ),
  29. 'body' => array(
  30. 'title' => 'Howdy',
  31. 'content' => 'Hello. I am a test post. I was created by the API',
  32. )
  33. )
  34. ) );
  35. class WPCOM_JSON_API_Autosave_Post_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint {
  36. function __construct( $args ) {
  37. parent::__construct( $args );
  38. }
  39. // /sites/%s/posts/%d/autosave -> $blog_id, $post_id
  40. function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
  41. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  42. if ( is_wp_error( $blog_id ) ) {
  43. return $blog_id;
  44. }
  45. $args = $this->query_args();
  46. $input = $this->input( false );
  47. if ( ! is_array( $input ) || ! $input ) {
  48. return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
  49. }
  50. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  51. // Make sure Custom Post Types, etc. get registered.
  52. $this->load_theme_functions();
  53. }
  54. $post = get_post( $post_id );
  55. if ( ! $post || is_wp_error( $post ) ) {
  56. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  57. }
  58. if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  59. return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
  60. }
  61. $post_data = array (
  62. 'post_ID' => $post_id,
  63. 'post_title' => $input['title'],
  64. 'post_content' => $input['content'],
  65. 'post_excerpt' => $input['excerpt'],
  66. );
  67. $preview_url = add_query_arg( 'preview', 'true', get_permalink( $post->ID ) );
  68. if ( ! wp_check_post_lock( $post->ID ) &&
  69. get_current_user_id() == $post->post_author &&
  70. ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status )
  71. ) {
  72. // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
  73. $auto_ID = edit_post( wp_slash( $post_data ) );
  74. } else {
  75. // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
  76. $auto_ID = wp_create_post_autosave( wp_slash( $post_data ) );
  77. $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
  78. $preview_url = add_query_arg( array( 'preview_id' => $auto_ID, 'preview_nonce' => $nonce ), $preview_url );
  79. }
  80. $updated_post = get_post( $auto_ID );
  81. if ( $updated_post && $updated_post->ID && $updated_post->post_modified ) {
  82. return array(
  83. 'ID' => $auto_ID,
  84. 'post_ID' => $post->ID,
  85. 'modified' => $this->format_date( $updated_post->post_modified ),
  86. 'preview_URL' => $preview_url
  87. );
  88. } else {
  89. return new WP_Error( 'autosave_error', __( 'Autosave encountered an unexpected error', 'jetpack' ), 500 );
  90. }
  91. }
  92. }