Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Get_Post_Endpoint
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
56
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Endpoints: /sites/%s/posts/%d      -> $blog_id, $post_id
4 *            /sites/%s/posts/name:%s -> $blog_id, $post_id // not documented
5 *            /sites/%s/posts/slug:%s -> $blog_id, $post_id
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12new WPCOM_JSON_API_Get_Post_Endpoint(
13    array(
14        'description'                          => 'Get a single post (by ID).',
15        'group'                                => 'posts',
16        'stat'                                 => 'posts:1',
17        'new_version'                          => '1.1',
18        'max_version'                          => '1',
19        'method'                               => 'GET',
20        'path'                                 => '/sites/%s/posts/%d',
21        'path_labels'                          => array(
22            '$site'    => '(int|string) Site ID or domain',
23            '$post_ID' => '(int) The post ID',
24        ),
25
26        'allow_fallback_to_jetpack_blog_token' => true,
27
28        'example_request'                      => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/7',
29    )
30);
31
32new WPCOM_JSON_API_Get_Post_Endpoint(
33    array(
34        'description'     => 'Get a single post (by name)',
35        'group'           => '__do_not_document',
36        'stat'            => 'posts:name',
37        'method'          => 'GET',
38        'path'            => '/sites/%s/posts/name:%s',
39        'path_labels'     => array(
40            '$site'      => '(int|string) Site ID or domain',
41            '$post_name' => '(string) The post name (a.k.a. slug)',
42        ),
43
44        'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/name:blogging-and-stuff',
45    )
46);
47
48new WPCOM_JSON_API_Get_Post_Endpoint(
49    array(
50        'description'                          => 'Get a single post (by slug).',
51        'group'                                => 'posts',
52        'stat'                                 => 'posts:slug',
53        'new_version'                          => '1.1',
54        'max_version'                          => '1',
55        'method'                               => 'GET',
56        'path'                                 => '/sites/%s/posts/slug:%s',
57        'path_labels'                          => array(
58            '$site'      => '(int|string) Site ID or domain',
59            '$post_slug' => '(string) The post slug (a.k.a. sanitized name)',
60        ),
61
62        'allow_fallback_to_jetpack_blog_token' => true,
63
64        'example_request'                      => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/slug:blogging-and-stuff',
65    )
66);
67
68/**
69 * Get post endpoint class.
70 *
71 * @phan-constructor-used-for-side-effects
72 */
73class WPCOM_JSON_API_Get_Post_Endpoint extends WPCOM_JSON_API_Post_Endpoint {
74    /**
75     *
76     * API callback.
77     *
78     * @param string $path - the path.
79     * @param int    $blog_id - the blog ID.
80     * @param int    $post_id - the post ID.
81     */
82    public function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
83        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
84        if ( is_wp_error( $blog_id ) ) {
85            return $blog_id;
86        }
87
88        $args = $this->query_args();
89
90        if ( ! str_contains( $path, '/posts/slug:' ) && ! str_contains( $path, '/posts/name:' ) ) {
91            $get_by = 'ID';
92        } else {
93            $get_by = 'name';
94        }
95
96        $return = $this->get_post_by( $get_by, $post_id, $args['context'] );
97        if ( ! $return || is_wp_error( $return ) ) {
98            return $return;
99        }
100
101        if ( ! $this->current_user_can_access_post_type( $return['type'], $args['context'] ) ) {
102            return new WP_Error( 'unknown_post', 'Unknown post', 404 );
103        }
104
105        /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
106        do_action( 'wpcom_json_api_objects', 'posts' );
107
108        return $return;
109    }
110}