Changeset 47849
- Timestamp:
- 05/23/2020 02:34:38 PM (5 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api.php
r47842 r47849 507 507 * Ensures a REST response is a response object (for consistency). 508 508 * 509 * This implements WP_ HTTP_Response, allowing usage of `set_status`/`header`/etc509 * This implements WP_REST_Response, allowing usage of `set_status`/`header`/etc 510 510 * without needing to double-check the object. Will also allow WP_Error to indicate error 511 511 * responses, so users should immediately check for this value. … … 513 513 * @since 4.4.0 514 514 * 515 * @param WP_ HTTP_Response|WP_Error|mixed $response Response to check.516 * @return WP_REST_Response| mixedIf response generated an error, WP_Error, if response517 * is already an instance, WP_HTTP_Response, otherwise518 * returns a new WP_REST_Response instance.515 * @param WP_REST_Response|WP_Error|WP_HTTP_Response|mixed $response Response to check. 516 * @return WP_REST_Response|WP_Error If response generated an error, WP_Error, if response 517 * is already an instance, WP_REST_Response, otherwise 518 * returns a new WP_REST_Response instance. 519 519 */ 520 520 function rest_ensure_response( $response ) { … … 523 523 } 524 524 525 if ( $response instanceof WP_REST_Response ) { 526 return $response; 527 } 528 529 // While WP_HTTP_Response is the base class of WP_REST_Response, it doesn't provide 530 // all the required methods used in WP_REST_Server::dispatch(). 525 531 if ( $response instanceof WP_HTTP_Response ) { 526 return $response; 532 return new WP_REST_Response( 533 $response->get_data(), 534 $response->get_status(), 535 $response->get_headers() 536 ); 527 537 } 528 538 -
trunk/src/wp-includes/rest-api/class-wp-rest-server.php
r47351 r47849 970 970 * @since 4.7.0 971 971 * 972 * @param WP_ HTTP_Response|WP_Error$response Result to send to the client. Usually a WP_REST_Response or WP_Error.973 * @param array $handler Route handler used for the request.974 * @param WP_REST_Request $request Request used to generate the response.972 * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. Usually a WP_REST_Response or WP_Error. 973 * @param array $handler Route handler used for the request. 974 * @param WP_REST_Request $request Request used to generate the response. 975 975 */ 976 976 $response = apply_filters( 'rest_request_before_callbacks', $response, $handler, $request ); … … 1033 1033 * @since 4.7.0 1034 1034 * 1035 * @param WP_ HTTP_Response|WP_Error$response Result to send to the client. Usually a WP_REST_Response or WP_Error.1036 * @param array $handler Route handler used for the request.1037 * @param WP_REST_Request $request Request used to generate the response.1035 * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. Usually a WP_REST_Response or WP_Error. 1036 * @param array $handler Route handler used for the request. 1037 * @param WP_REST_Request $request Request used to generate the response. 1038 1038 */ 1039 1039 $response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request ); -
trunk/tests/phpunit/tests/rest-api.php
r47842 r47849 943 943 * @ticket 40614 944 944 */ 945 function test_rest_ensure_re sponse_accepts_path_string() {945 function test_rest_ensure_request_accepts_path_string() { 946 946 $request = rest_ensure_request( '/wp/v2/posts' ); 947 947 $this->assertInstanceOf( 'WP_REST_Request', $request ); … … 1316 1316 ); 1317 1317 } 1318 1319 function test_rest_ensure_response_accepts_wp_error_and_returns_wp_error() { 1320 $response = rest_ensure_response( new WP_Error() ); 1321 $this->assertInstanceOf( 'WP_Error', $response ); 1322 } 1323 1324 /** 1325 * @dataProvider rest_ensure_response_data_provider 1326 * @group test1 1327 * 1328 * @param mixed $response The response passed to rest_ensure_response(). 1329 * @param mixed $expected_data The expected data a response should include. 1330 */ 1331 function test_rest_ensure_response_returns_instance_of_wp_rest_response( $response, $expected_data ) { 1332 $response_object = rest_ensure_response( $response ); 1333 $this->assertInstanceOf( 'WP_REST_Response', $response_object ); 1334 $this->assertSame( $expected_data, $response_object->get_data() ); 1335 } 1336 1337 /** 1338 * Data provider for test_rest_ensure_response_returns_instance_of_wp_rest_response(). 1339 * 1340 * @return array 1341 */ 1342 function rest_ensure_response_data_provider() { 1343 return array( 1344 array( null, null ), 1345 array( array( 'chocolate' => 'cookies' ), array( 'chocolate' => 'cookies' ) ), 1346 array( 123, 123 ), 1347 array( true, true ), 1348 array( 'chocolate', 'chocolate' ), 1349 array( new WP_HTTP_Response( 'http' ), 'http' ), 1350 array( new WP_REST_Response( 'rest' ), 'rest' ), 1351 ); 1352 } 1318 1353 }
Note: See TracChangeset
for help on using the changeset viewer.