Make WordPress Core


Ignore:
Timestamp:
03/24/2026 05:38:12 PM (4 days ago)
Author:
westonruter
Message:

Code Modernization: Fix rest_convert_error_to_response() handling of non-array error data when obtaining status.

While the error data is normally an array, this is not guaranteed. This issue would have been detected by PHPStan rule level 9, since WP_Error::get_all_error_data() returns mixed[].

Developed in https://github.com/WordPress/wordpress-develop/pull/11307

Follow-up to r61429.

Props nateallen, desrosj, jorbin, mukesh27, westonruter.
See #63430.
Fixes #64901.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r60635 r62107  
    591591        $this->assertSame( 'more_data', $response->get_data()['data'] );
    592592        $this->assertSame( array( array( 'status' => 400 ) ), $response->get_data()['additional_data'] );
     593    }
     594
     595    /**
     596     * @ticket 64901
     597     */
     598    public function test_error_to_response_with_stdclass_data() {
     599        $error = new WP_Error( 'test', 'test', (object) array( 'status' => 400 ) );
     600
     601        $response = rest_convert_error_to_response( $error );
     602        $this->assertInstanceOf( WP_REST_Response::class, $response );
     603
     604        // stdClass data should not cause a fatal, status should default to 500.
     605        $this->assertSame( 500, $response->get_status() );
     606    }
     607
     608    /**
     609     * @ticket 64901
     610     */
     611    public function test_error_to_response_with_multi_status_non_numeric_status() {
     612        $error = new WP_Error( 'test', 'test', array( 'status' => array( 'feeling' => 'happy' ) ) );
     613        $error->add_data( array( 'status' => 400 ), 'test' );
     614        $error->add_data( array( 'status' => array( 'feeling' => 'bleh' ) ), 'test' );
     615
     616        $response = rest_convert_error_to_response( $error );
     617        $this->assertInstanceOf( WP_REST_Response::class, $response );
     618
     619        $this->assertSame( 400, $response->get_status() );
    593620    }
    594621
Note: See TracChangeset for help on using the changeset viewer.