diff --git a/projects/packages/jetpack-mu-wpcom/changelog/update-help-center-endpoint b/projects/packages/jetpack-mu-wpcom/changelog/update-help-center-endpoint new file mode 100644 index 0000000000000..e3b3f173ae89f --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/update-help-center-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Help Center: extended post fetch endpoint to accept URLs diff --git a/projects/packages/jetpack-mu-wpcom/src/features/help-center/class-wp-rest-help-center-fetch-post.php b/projects/packages/jetpack-mu-wpcom/src/features/help-center/class-wp-rest-help-center-fetch-post.php index bf25cbae87c92..87f530ed1db89 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/help-center/class-wp-rest-help-center-fetch-post.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/help-center/class-wp-rest-help-center-fetch-post.php @@ -33,8 +33,20 @@ public function register_rest_route() { 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_post' ), 'permission_callback' => 'is_user_logged_in', + 'args' => array( + 'blog_id' => array( + 'type' => 'number', + ), + 'post_id' => array( + 'type' => 'number', + ), + 'post_url' => array( + 'type' => 'string', + ), + ), ) ); + register_rest_route( $this->namespace, '/articles', @@ -60,9 +72,9 @@ public function register_rest_route() { } /** - * Should return blog post articles + * Should return blog post articles. * - * @param \WP_REST_Request $request The request sent to the API. + * @param \WP_REST_Request $request The request sent to the API. */ public function get_blog_post_articles( \WP_REST_Request $request ) { $query_parameters = array( @@ -83,22 +95,30 @@ public function get_blog_post_articles( \WP_REST_Request $request ) { } /** - * Should return the search results + * Should return the post. * - * @param \WP_REST_Request $request The request sent to the API. + * @param \WP_REST_Request $request The request sent to the API. */ public function get_post( \WP_REST_Request $request ) { - $alternate_data = $this->get_post_alternate_data( $request['blog_id'], $request['post_id'] ); - if ( is_wp_error( $alternate_data ) ) { - return $alternate_data; + if ( isset( $request['post_url'] ) ) { + $body = Client::wpcom_json_api_request_as_user( + '/help/article?post_url=' . $request['post_url'] + ); + } else { + $alternate_data = $this->get_post_alternate_data( $request['blog_id'], $request['post_id'] ); + if ( is_wp_error( $alternate_data ) ) { + return $alternate_data; + } + + $body = Client::wpcom_json_api_request_as_user( + '/help/article/' . $alternate_data['blog_id'] . '/' . $alternate_data['post_id'] + ); } - $body = Client::wpcom_json_api_request_as_user( - '/help/article/' . $alternate_data['blog_id'] . '/' . $alternate_data['post_id'] - ); if ( is_wp_error( $body ) ) { return $body; } + $response = json_decode( wp_remote_retrieve_body( $body ) ); return rest_ensure_response( $response );