PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 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 All 508 releases
← All changes | json-endpoints/class.wpcom-json-api-get-post-v1-1-endpoint.php +127 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,127 @@
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 + // The %d token is substituted with the real post ID by the transport (build_concrete_rest_route).
22 + 'rest_route' => '/posts/%d',
23 + 'rest_min_jp_version' => '16.1-a.3',
24 +
25 + 'allow_fallback_to_jetpack_blog_token' => true,
26 +
27 + 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts/7',
28 + )
29 +);
30 +
31 +new WPCOM_JSON_API_Get_Post_v1_1_Endpoint(
32 + array(
33 + 'description' => 'Get a single post (by slug).',
34 + 'min_version' => '1.1',
35 + 'max_version' => '1.1',
36 + 'group' => 'posts',
37 + 'stat' => 'posts:slug',
38 + 'method' => 'GET',
39 + 'path' => '/sites/%s/posts/slug:%s',
40 + 'path_labels' => array(
41 + '$site' => '(int|string) Site ID or domain',
42 + '$post_slug' => '(string) The post slug (a.k.a. sanitized name)',
43 + ),
44 +
45 + // The slug:%s token is substituted with the real slug by the transport (build_concrete_rest_route).
46 + 'rest_route' => '/posts/slug:%s',
47 + 'rest_min_jp_version' => '16.1-a.3',
48 +
49 + 'allow_fallback_to_jetpack_blog_token' => true,
50 +
51 + 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts/slug:blogging-and-stuff',
52 + )
53 +);
54 +
55 +/**
56 + * Get Post v1_1 endpoint.
57 + *
58 + * @phan-constructor-used-for-side-effects
59 + */
60 +class WPCOM_JSON_API_Get_Post_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint { // phpcs:ignore
61 + /**
62 + *
63 + * API callback.
64 + *
65 + * /sites/%s/posts/%d -> $blog_id, $post_id
66 + * /sites/%s/posts/slug:%s -> $blog_id, $post_id
67 + *
68 + * @param string $path - the path.
69 + * @param int $blog_id - the blog ID.
70 + * @param int|string $post_id - the post ID, or the post slug for the `/posts/slug:` route.
71 + */
72 + public function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
73 + $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
74 + if ( is_wp_error( $blog_id ) ) {
75 + return $blog_id;
76 + }
77 +
78 + $args = $this->query_args();
79 +
80 + if ( str_contains( $path, '/posts/slug:' ) ) {
81 + $site = $this->get_platform()->get_site( $blog_id );
82 +
83 + $post_id = $site->get_post_id_by_name( $post_id );
84 + if ( is_wp_error( $post_id ) ) {
85 + return $post_id;
86 + }
87 + }
88 +
89 + return $this->fetch_post( $blog_id, $post_id, $args['context'] );
90 + }
91 +
92 + /**
93 + * Helper function to fetch the content of a post. User validation
94 + * should be handled by the caller.
95 + *
96 + * @param int $blog_id The blog ID for the post.
97 + * @param int $post_id The post ID.
98 + * @param string $context The context we're fetching for.
99 + * @return array|SAL_Post|WP_Error
100 + */
101 + public function fetch_post( $blog_id, $post_id, $context ) {
102 + $site = $this->get_platform()->get_site( $blog_id );
103 +
104 + if (
105 + defined( 'IS_WPCOM' )
106 + && IS_WPCOM
107 + && ! in_array( get_post_type( $post_id ), array( false, 'post', 'revision' ), true )
108 + ) {
109 + $this->load_theme_functions();
110 + }
111 +
112 + $post = $this->get_post_by( 'ID', $post_id, $context );
113 +
114 + if ( ! $post || is_wp_error( $post ) ) {
115 + return $post;
116 + }
117 +
118 + if ( ! $site->current_user_can_access_post_type( $post['type'], $context ) ) {
119 + return new WP_Error( 'unknown_post', 'Unknown post', 404 );
120 + }
121 +
122 + /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
123 + do_action( 'wpcom_json_api_objects', 'posts' );
124 +
125 + return $post;
126 + }
127 +}