Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Rest API - Namespace error handling (#5319)
Browse files Browse the repository at this point in the history
* get_routes_from_namespace needs to handle WP_Error responses

* is_request_to_store_api is not static
  • Loading branch information
mikejolley authored Dec 7, 2021
1 parent 22c77d3 commit 67a9aef
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/RestApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function register_rest_routes() {
* Get routes for a namespace.
*
* @param string $namespace Namespace to retrieve.
* @return array|null
* @return array
*/
public function get_routes_from_namespace( $namespace ) {
$rest_server = rest_get_server();
Expand All @@ -61,9 +61,13 @@ public function get_routes_from_namespace( $namespace ) {
]
);

if ( is_wp_error( $namespace_index ) ) {
return [];
}

$response_data = $namespace_index->get_data();

return isset( $response_data['routes'] ) ? $response_data['routes'] : null;
return $response_data['routes'] ?? [];
}

/**
Expand All @@ -74,7 +78,7 @@ public function get_routes_from_namespace( $namespace ) {
*/
public function store_api_authentication( $result ) {
// Pass through errors from other authentication methods used before this one.
if ( ! empty( $result ) || ! self::is_request_to_store_api() ) {
if ( ! empty( $result ) || ! $this->is_request_to_store_api() ) {
return $result;
}
return true;
Expand All @@ -87,7 +91,7 @@ public function store_api_authentication( $result ) {
* @param string $logged_in_cookie The value for the logged in cookie.
*/
public function store_api_logged_in_cookie( $logged_in_cookie ) {
if ( ! defined( 'LOGGED_IN_COOKIE' ) || ! self::is_request_to_store_api() ) {
if ( ! defined( 'LOGGED_IN_COOKIE' ) || ! $this->is_request_to_store_api() ) {
return;
}
$_COOKIE[ LOGGED_IN_COOKIE ] = $logged_in_cookie;
Expand Down

0 comments on commit 67a9aef

Please sign in to comment.