• Resolved Zlatev

    (@entr)


    Hi there,

    We are experiencing fatal errors for unauthorized requests to WooCommerce endpoints. We tracked the issue down to the Endpoint_Api::save_cache() method.

    
    public function save_cache( $result, \WP_REST_Server $server, \WP_REST_Request $request ) {
    		
    		if ( ! empty( $result ) && is_array( $result ) && isset( $result['data']['status'] ) && 200 !== (int) $result['data']['status'] ) {
    			return $result;
    		}
    ...
    }
    

    Turns out $result['data'] is not guaranteed to be an Array and this will result in a fatal error:

    
    PHP Fatal error:  Uncaught Error: Cannot use object of type stdClass as array in .../wp-content/plugins/wp-rest-cache/includes/api/class-endpoint-api.php:232
    

    It might be due to the fact WC is returning WP_Error objects in their permission handlers but this should be further investigated.

    However, to more reliably retrieve the actual HTTP status code of the ongoing response you should use the http_response_code() php function. By the time rest_pre_serve_request filter is run that header is already set by WP_REST_Server::serve_request() method.

    Let us know if you need more information on the matter.

    • This topic was modified 4 years, 4 months ago by Zlatev.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Fatal error in Endpoint_Api::save_cache() method’ is closed to new replies.