Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help Center: extend post fetch endpoint #38445

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading