Nav apraksta

class.wpcom-json-api-get-autosave-v1-1-endpoint.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. /**
  3. * WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint
  4. *
  5. * @package automattic/jetpack
  6. */
  7. new WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint(
  8. array(
  9. 'description' => 'Get the most recent autosave for a post.',
  10. 'group' => '__do_not_document',
  11. 'stat' => 'posts:autosave',
  12. 'min_version' => '1.1',
  13. 'method' => 'GET',
  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. 'response_format' => array(
  20. 'ID' => '(int) autodraft post ID',
  21. 'post_ID' => '(int) post ID',
  22. 'author_ID' => '(int) author ID',
  23. 'title' => '(HTML) The post title.',
  24. 'content' => '(HTML) The post content.',
  25. 'excerpt' => '(HTML) The post excerpt.',
  26. 'preview_URL' => '(string) preview URL for the post',
  27. 'modified' => '(ISO 8601 datetime) modified time',
  28. ),
  29. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/1/autosave',
  30. )
  31. );
  32. // phpcs:disable PEAR.NamingConventions.ValidClassName.Invalid
  33. /**
  34. * Class WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint
  35. */
  36. class WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint {
  37. /**
  38. * Get Autosave callback
  39. * /sites/%s/posts/%d/autosave -> $blog_id, $post_id
  40. *
  41. * @param string $path Path.
  42. * @param int $blog_id Blog ID.
  43. * @param int $post_id Post ID.
  44. *
  45. * @return array|int|mixed|WP_Error
  46. */
  47. public function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
  48. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  49. if ( is_wp_error( $blog_id ) ) {
  50. return $blog_id;
  51. }
  52. $post = get_post( $post_id );
  53. if ( ! $post || is_wp_error( $post ) ) {
  54. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  55. }
  56. if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  57. return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
  58. }
  59. $autosave = wp_get_post_autosave( $post->ID );
  60. if ( $autosave ) {
  61. $preview_url = add_query_arg( 'preview', 'true', get_permalink( $post->ID ) );
  62. $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
  63. $preview_url = add_query_arg(
  64. array(
  65. 'preview_id' => $post->ID,
  66. 'preview_nonce' => $nonce,
  67. ),
  68. $preview_url
  69. );
  70. return array(
  71. 'ID' => $autosave->ID,
  72. 'author_ID' => $autosave->post_author,
  73. 'post_ID' => $autosave->post_parent,
  74. 'title' => $autosave->post_title,
  75. 'content' => $autosave->post_content,
  76. 'excerpt' => $autosave->post_excerpt,
  77. 'preview_URL' => $preview_url,
  78. 'modified' => $this->format_date( $autosave->post_modified_gmt, $autosave->post_modified ),
  79. );
  80. } else {
  81. return new WP_Error( 'not_found', 'No autosaves exist for this post', 404 );
  82. }
  83. }
  84. }