Açıklama Yok

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

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