Nav apraksta

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

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