PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 14.5.1
Jetpack – WP Security, Backup, Speed, & Growth v14.5.1
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 All 500 releases
jetpack / json-endpoints / class.wpcom-json-api-get-post-endpoint.php
class.wpcom-json-api-get-post-endpoint.php
105 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
8 new WPCOM_JSON_API_Get_Post_Endpoint(
9 array(
10 'description' => 'Get a single post (by ID).',
11 'group' => 'posts',
12 'stat' => 'posts:1',
13 'new_version' => '1.1',
14 'max_version' => '1',
15 'method' => 'GET',
16 'path' => '/sites/%s/posts/%d',
17 'path_labels' => array(
18 '$site' => '(int|string) Site ID or domain',
19 '$post_ID' => '(int) The post ID',
20 ),
21
22 'allow_fallback_to_jetpack_blog_token' => true,
23
24 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/7',
25 )
26 );
27
28 new WPCOM_JSON_API_Get_Post_Endpoint(
29 array(
30 'description' => 'Get a single post (by name)',
31 'group' => '__do_not_document',
32 'stat' => 'posts:name',
33 'method' => 'GET',
34 'path' => '/sites/%s/posts/name:%s',
35 'path_labels' => array(
36 '$site' => '(int|string) Site ID or domain',
37 '$post_name' => '(string) The post name (a.k.a. slug)',
38 ),
39
40 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/name:blogging-and-stuff',
41 )
42 );
43
44 new WPCOM_JSON_API_Get_Post_Endpoint(
45 array(
46 'description' => 'Get a single post (by slug).',
47 'group' => 'posts',
48 'stat' => 'posts:slug',
49 'new_version' => '1.1',
50 'max_version' => '1',
51 'method' => 'GET',
52 'path' => '/sites/%s/posts/slug:%s',
53 'path_labels' => array(
54 '$site' => '(int|string) Site ID or domain',
55 '$post_slug' => '(string) The post slug (a.k.a. sanitized name)',
56 ),
57
58 'allow_fallback_to_jetpack_blog_token' => true,
59
60 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/slug:blogging-and-stuff',
61 )
62 );
63
64 /**
65 * Get post endpoint class.
66 */
67 class WPCOM_JSON_API_Get_Post_Endpoint extends WPCOM_JSON_API_Post_Endpoint {
68 /**
69 *
70 * API callback.
71 *
72 * @param string $path - the path.
73 * @param int $blog_id - the blog ID.
74 * @param int $post_id - the post ID.
75 */
76 public function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
77 $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
78 if ( is_wp_error( $blog_id ) ) {
79 return $blog_id;
80 }
81
82 $args = $this->query_args();
83
84 if ( ! str_contains( $path, '/posts/slug:' ) && ! str_contains( $path, '/posts/name:' ) ) {
85 $get_by = 'ID';
86 } else {
87 $get_by = 'name';
88 }
89
90 $return = $this->get_post_by( $get_by, $post_id, $args['context'] );
91 if ( ! $return || is_wp_error( $return ) ) {
92 return $return;
93 }
94
95 if ( ! $this->current_user_can_access_post_type( $return['type'], $args['context'] ) ) {
96 return new WP_Error( 'unknown_post', 'Unknown post', 404 );
97 }
98
99 /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
100 do_action( 'wpcom_json_api_objects', 'posts' );
101
102 return $return;
103 }
104 }
105