Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_JSON_API_Get_Post_v1_1_Endpoint | |
0.00% |
0 / 21 |
|
0.00% |
0 / 2 |
132 | |
0.00% |
0 / 1 |
| callback | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| fetch_post | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit( 0 ); |
| 5 | } |
| 6 | |
| 7 | new WPCOM_JSON_API_Get_Post_v1_1_Endpoint( |
| 8 | array( |
| 9 | 'description' => 'Get a single post (by ID).', |
| 10 | 'min_version' => '1.1', |
| 11 | 'max_version' => '1.1', |
| 12 | 'group' => 'posts', |
| 13 | 'stat' => 'posts:1', |
| 14 | 'method' => 'GET', |
| 15 | 'path' => '/sites/%s/posts/%d', |
| 16 | 'path_labels' => array( |
| 17 | '$site' => '(int|string) Site ID or domain', |
| 18 | '$post_ID' => '(int) The post ID', |
| 19 | ), |
| 20 | |
| 21 | 'allow_fallback_to_jetpack_blog_token' => true, |
| 22 | |
| 23 | 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts/7', |
| 24 | ) |
| 25 | ); |
| 26 | |
| 27 | new WPCOM_JSON_API_Get_Post_v1_1_Endpoint( |
| 28 | array( |
| 29 | 'description' => 'Get a single post (by slug).', |
| 30 | 'min_version' => '1.1', |
| 31 | 'max_version' => '1.1', |
| 32 | 'group' => 'posts', |
| 33 | 'stat' => 'posts:slug', |
| 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 | |
| 41 | 'allow_fallback_to_jetpack_blog_token' => true, |
| 42 | |
| 43 | 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts/slug:blogging-and-stuff', |
| 44 | ) |
| 45 | ); |
| 46 | |
| 47 | /** |
| 48 | * Get Post v1_1 endpoint. |
| 49 | * |
| 50 | * @phan-constructor-used-for-side-effects |
| 51 | */ |
| 52 | class WPCOM_JSON_API_Get_Post_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint { // phpcs:ignore |
| 53 | /** |
| 54 | * |
| 55 | * API callback. |
| 56 | * |
| 57 | * /sites/%s/posts/%d -> $blog_id, $post_id |
| 58 | * /sites/%s/posts/slug:%s -> $blog_id, $post_id |
| 59 | * |
| 60 | * @param string $path - the path. |
| 61 | * @param int $blog_id - the blog ID. |
| 62 | * @param int $post_id - the post ID. |
| 63 | */ |
| 64 | public function callback( $path = '', $blog_id = 0, $post_id = 0 ) { |
| 65 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
| 66 | if ( is_wp_error( $blog_id ) ) { |
| 67 | return $blog_id; |
| 68 | } |
| 69 | |
| 70 | $args = $this->query_args(); |
| 71 | |
| 72 | if ( str_contains( $path, '/posts/slug:' ) ) { |
| 73 | $site = $this->get_platform()->get_site( $blog_id ); |
| 74 | |
| 75 | $post_id = $site->get_post_id_by_name( $post_id ); |
| 76 | if ( is_wp_error( $post_id ) ) { |
| 77 | return $post_id; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return $this->fetch_post( $blog_id, $post_id, $args['context'] ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Helper function to fetch the content of a post. User validation |
| 86 | * should be handled by the caller. |
| 87 | * |
| 88 | * @param int $blog_id The blog ID for the post. |
| 89 | * @param int $post_id The post ID. |
| 90 | * @param string $context The context we're fetching for. |
| 91 | * @return array|SAL_Post|WP_Error |
| 92 | */ |
| 93 | public function fetch_post( $blog_id, $post_id, $context ) { |
| 94 | $site = $this->get_platform()->get_site( $blog_id ); |
| 95 | |
| 96 | if ( |
| 97 | defined( 'IS_WPCOM' ) |
| 98 | && IS_WPCOM |
| 99 | && ! in_array( get_post_type( $post_id ), array( false, 'post', 'revision' ), true ) |
| 100 | ) { |
| 101 | $this->load_theme_functions(); |
| 102 | } |
| 103 | |
| 104 | $post = $this->get_post_by( 'ID', $post_id, $context ); |
| 105 | |
| 106 | if ( ! $post || is_wp_error( $post ) ) { |
| 107 | return $post; |
| 108 | } |
| 109 | |
| 110 | if ( ! $site->current_user_can_access_post_type( $post['type'], $context ) ) { |
| 111 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
| 112 | } |
| 113 | |
| 114 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
| 115 | do_action( 'wpcom_json_api_objects', 'posts' ); |
| 116 | |
| 117 | return $post; |
| 118 | } |
| 119 | } |