Ticket #35803: 35803.1.diff
File 35803.1.diff, 2.9 KB (added by , 9 years ago) |
---|
-
src/wp-includes/rest-api.php
diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 0b2da7f..7048c3e 100644
a b function rest_get_date_with_gmt( $date, $force_utc = false ) { 648 648 649 649 return array( $local, $utc ); 650 650 } 651 652 /** 653 * Gets a WP_REST_Request object from a full URL. 654 * 655 * @since 4.5.0 656 * 657 * @param string $url URL with protocol, domain, path and query args. 658 * @return WP_REST_Request|false WP_REST_Request object on success, false on failure. 659 */ 660 function rest_get_request_from_url( $url ) { 661 $bits = parse_url( $url ); 662 $api_root = rest_url(); 663 if ( get_option( 'permalink_structure' ) ) { 664 $path_and_query_args = substr( $url, strlen( untrailingslashit( $api_root ) ) ); 665 $parsed = parse_url( $path_and_query_args ); 666 if ( empty( $parsed['path'] ) ) { 667 return false; 668 } 669 $route = $parsed['path']; 670 $request = new WP_REST_Request( 'GET', $route ); 671 if ( ! empty( $parsed['query'] ) ) { 672 parse_str( $parsed['query'], $query_params ); 673 674 // Ensure magic quotes are stripped. 675 if ( get_magic_quotes_gpc() ) { 676 $query_params = stripslashes_deep( $query_params ); 677 } 678 $request->set_query_params( $query_params ); 679 } 680 } else { 681 $parsed = parse_url( $url ); 682 if ( empty( $parsed['query'] ) ) { 683 return false; 684 } 685 parse_str( $parsed['query'], $query_params ); 686 if ( get_magic_quotes_gpc() ) { 687 $query_params = stripslashes_deep( $query_params ); 688 } 689 if ( empty( $query_params['rest_route'] ) ) { 690 return false; 691 } 692 $request = new WP_REST_Request( 'GET', $query_params['rest_route'] ); 693 unset( $query_params['rest_route'] ); 694 $request->set_query_params( $query_params ); 695 } 696 return $request; 697 } -
tests/phpunit/tests/rest-api.php
diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php index f417a93..514841e 100644
a b class Tests_REST_API extends WP_UnitTestCase { 332 332 333 333 } 334 334 335 /** 336 * @dataProvider data_rest_request_from_url 337 */ 338 public function test_rest_request_from_url( $permalink_structure, $original_url ) { 339 update_option( 'permalink_structure', $permalink_structure ); 340 $url = add_query_arg( 'foo', 'bar', rest_url( '/wp/v2/posts/1' ) ); 341 $this->assertEquals( $original_url, $url ); 342 $request = rest_get_request_from_url( $url ); 343 $this->assertInstanceOf( 'WP_REST_Request', $request ); 344 $this->assertEquals( '/wp/v2/posts/1', $request->get_route() ); 345 $this->assertEqualSets( array( 346 'foo' => 'bar', 347 ), $request->get_query_params() ); 348 } 349 350 public function data_rest_request_from_url() { 351 return array( 352 array( 353 'permalink_structure' => '/%post_name%/', 354 'original_url' => 'http://' . WP_TESTS_DOMAIN . '/wp-json/wp/v2/posts/1?foo=bar', 355 ), 356 array( 357 'permalink_structure' => '', 358 'original_url' => 'http://' . WP_TESTS_DOMAIN . '/?rest_route=%2Fwp%2Fv2%2Fposts%2F1&foo=bar', 359 ), 360 ); 361 } 362 335 363 }