Skip to content

Commit

Permalink
Help Center: extend post fetch endpoint (#38445)
Browse files Browse the repository at this point in the history
  • Loading branch information
arcangelini authored Jul 30, 2024
1 parent d7a2ed7 commit d94211b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Help Center: extended post fetch endpoint to accept URLs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
Expand All @@ -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 );
Expand Down

0 comments on commit d94211b

Please sign in to comment.