class.wpcom-json-api-get-post-endpoint.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. new WPCOM_JSON_API_Get_Post_Endpoint( array(
  3. 'description' => 'Get a single post (by ID).',
  4. 'group' => 'posts',
  5. 'stat' => 'posts:1',
  6. 'new_version' => '1.1',
  7. 'max_version' => '1',
  8. 'method' => 'GET',
  9. 'path' => '/sites/%s/posts/%d',
  10. 'path_labels' => array(
  11. '$site' => '(int|string) Site ID or domain',
  12. '$post_ID' => '(int) The post ID',
  13. ),
  14. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/7'
  15. ) );
  16. new WPCOM_JSON_API_Get_Post_Endpoint( array(
  17. 'description' => 'Get a single post (by name)',
  18. 'group' => '__do_not_document',
  19. 'stat' => 'posts:name',
  20. 'method' => 'GET',
  21. 'path' => '/sites/%s/posts/name:%s',
  22. 'path_labels' => array(
  23. '$site' => '(int|string) Site ID or domain',
  24. '$post_name' => '(string) The post name (a.k.a. slug)',
  25. ),
  26. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/name:blogging-and-stuff',
  27. ) );
  28. new WPCOM_JSON_API_Get_Post_Endpoint( array(
  29. 'description' => 'Get a single post (by slug).',
  30. 'group' => 'posts',
  31. 'stat' => 'posts:slug',
  32. 'new_version' => '1.1',
  33. 'max_version' => '1',
  34. 'method' => 'GET',
  35. 'path' => '/sites/%s/posts/slug:%s',
  36. 'path_labels' => array(
  37. '$site' => '(int|string) Site ID or domain',
  38. '$post_slug' => '(string) The post slug (a.k.a. sanitized name)',
  39. ),
  40. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/slug:blogging-and-stuff',
  41. ) );
  42. class WPCOM_JSON_API_Get_Post_Endpoint extends WPCOM_JSON_API_Post_Endpoint {
  43. // /sites/%s/posts/%d -> $blog_id, $post_id
  44. // /sites/%s/posts/name:%s -> $blog_id, $post_id // not documented
  45. // /sites/%s/posts/slug:%s -> $blog_id, $post_id
  46. function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
  47. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  48. if ( is_wp_error( $blog_id ) ) {
  49. return $blog_id;
  50. }
  51. $args = $this->query_args();
  52. if ( false === strpos( $path, '/posts/slug:' ) && false === strpos( $path, '/posts/name:' ) ) {
  53. $get_by = 'ID';
  54. } else {
  55. $get_by = 'name';
  56. }
  57. $return = $this->get_post_by( $get_by, $post_id, $args['context'] );
  58. if ( !$return || is_wp_error( $return ) ) {
  59. return $return;
  60. }
  61. if ( ! $this->current_user_can_access_post_type( $return['type'], $args['context'] ) ) {
  62. return new WP_Error( 'unknown_post', 'Unknown post', 404 );
  63. }
  64. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  65. do_action( 'wpcom_json_api_objects', 'posts' );
  66. return $return;
  67. }
  68. }