Aucune description

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

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