PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
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 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
← All changes | json-endpoints/class.wpcom-json-api-get-post-v1-1-endpoint.php +44 -12 12.5.2 → 16.3-a.1 View file →
@@ -1,6 +1,10 @@
1 1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 2
3 +if ( ! defined( 'ABSPATH' ) ) {
4 + exit( 0 );
5 +}
6 +
3 7 new WPCOM_JSON_API_Get_Post_v1_1_Endpoint(
4 8 array(
5 9 'description' => 'Get a single post (by ID).',
6 10 'min_version' => '1.1',
@@ -13,8 +17,12 @@
13 17 '$site' => '(int|string) Site ID or domain',
14 18 '$post_ID' => '(int) The post ID',
15 19 ),
16 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 +
17 25 'allow_fallback_to_jetpack_blog_token' => true,
18 26
19 27 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts/7',
20 28 )
@@ -33,8 +41,12 @@
33 41 '$site' => '(int|string) Site ID or domain',
34 42 '$post_slug' => '(string) The post slug (a.k.a. sanitized name)',
35 43 ),
36 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 +
37 49 'allow_fallback_to_jetpack_blog_token' => true,
38 50
39 51 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts/slug:blogging-and-stuff',
40 52 )
@@ -41,8 +53,10 @@
41 53 );
42 54
43 55 /**
44 56 * Get Post v1_1 endpoint.
57 + *
58 + * @phan-constructor-used-for-side-effects
45 59 */
46 60 class WPCOM_JSON_API_Get_Post_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint { // phpcs:ignore
47 61 /**
48 62 *
@@ -50,11 +64,11 @@
50 64 *
51 65 * /sites/%s/posts/%d -> $blog_id, $post_id
52 66 * /sites/%s/posts/slug:%s -> $blog_id, $post_id
53 67 *
54 - * @param string $path - the path.
55 - * @param int $blog_id - the blog ID.
56 - * @param int $post_id - the post ID.
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.
57 71 */
58 72 public function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
59 73 $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
60 74 if ( is_wp_error( $blog_id ) ) {
@@ -62,11 +76,11 @@
62 76 }
63 77
64 78 $args = $this->query_args();
65 79
66 - $site = $this->get_platform()->get_site( $blog_id );
80 + if ( str_contains( $path, '/posts/slug:' ) ) {
81 + $site = $this->get_platform()->get_site( $blog_id );
67 82
68 - if ( false !== strpos( $path, '/posts/slug:' ) ) {
69 83 $post_id = $site->get_post_id_by_name( $post_id );
70 84 if ( is_wp_error( $post_id ) ) {
71 85 return $post_id;
72 86 }
@@ -71,20 +85,38 @@
71 85 return $post_id;
72 86 }
73 87 }
74 88
75 - if ( defined( 'IS_WPCOM' ) && IS_WPCOM &&
76 - ! in_array( get_post_type( $post_id ), array( false, 'post', 'revision' ), true ) ) {
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 + ) {
77 109 $this->load_theme_functions();
78 110 }
79 111
80 - $return = $this->get_post_by( 'ID', $post_id, $args['context'] );
112 + $post = $this->get_post_by( 'ID', $post_id, $context );
81 113
82 - if ( ! $return || is_wp_error( $return ) ) {
83 - return $return;
114 + if ( ! $post || is_wp_error( $post ) ) {
115 + return $post;
84 116 }
85 117
86 - if ( ! $site->current_user_can_access_post_type( $return['type'], $args['context'] ) ) {
118 + if ( ! $site->current_user_can_access_post_type( $post['type'], $context ) ) {
87 119 return new WP_Error( 'unknown_post', 'Unknown post', 404 );
88 120 }
89 121
90 122 /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
@@ -89,7 +121,7 @@
89 121
90 122 /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
91 123 do_action( 'wpcom_json_api_objects', 'posts' );
92 124
93 - return $return;
125 + return $post;
94 126 }
95 127 }