class.wpcom-json-api-get-post-v1-1-endpoint.php 2.3 KB

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