36 /* |
36 /* |
37 * Non-namespaced routes are not allowed, with the exception of the main |
37 * Non-namespaced routes are not allowed, with the exception of the main |
38 * and namespace indexes. If you really need to register a |
38 * and namespace indexes. If you really need to register a |
39 * non-namespaced route, call `WP_REST_Server::register_route` directly. |
39 * non-namespaced route, call `WP_REST_Server::register_route` directly. |
40 */ |
40 */ |
41 _doing_it_wrong( 'register_rest_route', __( 'Routes must be namespaced with plugin or theme name and version.' ), '4.4.0' ); |
41 _doing_it_wrong( |
|
42 __FUNCTION__, |
|
43 sprintf( |
|
44 /* translators: 1: string value of the namespace, 2: string value of the route. */ |
|
45 __( 'Routes must be namespaced with plugin or theme name and version. Instead there seems to be an empty namespace \'%1$s\' for route \'%2$s\'.' ), |
|
46 '<code>' . $route_namespace . '</code>', |
|
47 '<code>' . $route . '</code>' |
|
48 ), |
|
49 '4.4.0' |
|
50 ); |
42 return false; |
51 return false; |
43 } elseif ( empty( $route ) ) { |
52 } elseif ( empty( $route ) ) { |
44 _doing_it_wrong( 'register_rest_route', __( 'Route must be specified.' ), '4.4.0' ); |
53 _doing_it_wrong( |
|
54 __FUNCTION__, |
|
55 sprintf( |
|
56 /* translators: 1: string value of the namespace, 2: string value of the route. */ |
|
57 __( 'Route must be specified. Instead within the namespace \'%1$s\', there seems to be an empty route \'%2$s\'.' ), |
|
58 '<code>' . $route_namespace . '</code>', |
|
59 '<code>' . $route . '</code>' |
|
60 ), |
|
61 '4.4.0' |
|
62 ); |
45 return false; |
63 return false; |
46 } |
64 } |
47 |
65 |
48 $clean_namespace = trim( $route_namespace, '/' ); |
66 $clean_namespace = trim( $route_namespace, '/' ); |
49 |
67 |
50 if ( $clean_namespace !== $route_namespace ) { |
68 if ( $clean_namespace !== $route_namespace ) { |
51 _doing_it_wrong( __FUNCTION__, __( 'Namespace must not start or end with a slash.' ), '5.4.2' ); |
69 _doing_it_wrong( |
|
70 __FUNCTION__, |
|
71 sprintf( |
|
72 /* translators: 1: string value of the namespace, 2: string value of the route. */ |
|
73 __( 'Namespace must not start or end with a slash. Instead namespace \'%1$s\' for route \'%2$s\' seems to contain a slash.' ), |
|
74 '<code>' . $route_namespace . '</code>', |
|
75 '<code>' . $route . '</code>' |
|
76 ), |
|
77 '5.4.2' |
|
78 ); |
52 } |
79 } |
53 |
80 |
54 if ( ! did_action( 'rest_api_init' ) ) { |
81 if ( ! did_action( 'rest_api_init' ) ) { |
55 _doing_it_wrong( |
82 _doing_it_wrong( |
56 'register_rest_route', |
83 __FUNCTION__, |
57 sprintf( |
84 sprintf( |
58 /* translators: %s: rest_api_init */ |
85 /* translators: 1: rest_api_init, 2: string value of the route, 3: string value of the namespace. */ |
59 __( 'REST API routes must be registered on the %s action.' ), |
86 __( 'REST API routes must be registered on the %1$s action. Instead route \'%2$s\' with namespace \'%3$s\' was not registered on this action.' ), |
60 '<code>rest_api_init</code>' |
87 '<code>rest_api_init</code>', |
|
88 '<code>' . $route . '</code>', |
|
89 '<code>' . $route_namespace . '</code>' |
61 ), |
90 ), |
62 '5.1.0' |
91 '5.1.0' |
63 ); |
92 ); |
64 } |
93 } |
65 |
94 |
399 function rest_api_loaded() { |
428 function rest_api_loaded() { |
400 if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) { |
429 if ( empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) { |
401 return; |
430 return; |
402 } |
431 } |
403 |
432 |
|
433 // Return an error message if query_var is not a string. |
|
434 if ( ! is_string( $GLOBALS['wp']->query_vars['rest_route'] ) ) { |
|
435 $rest_type_error = new WP_Error( |
|
436 'rest_path_invalid_type', |
|
437 __( 'The REST route parameter must be a string.' ), |
|
438 array( 'status' => 400 ) |
|
439 ); |
|
440 wp_die( $rest_type_error ); |
|
441 } |
|
442 |
404 /** |
443 /** |
405 * Whether this is a REST Request. |
444 * Whether this is a REST Request. |
406 * |
445 * |
407 * @since 4.4.0 |
446 * @since 4.4.0 |
408 * @var bool |
447 * @var bool |
787 $args[ $param ] = $value; |
826 $args[ $param ] = $value; |
788 } |
827 } |
789 } |
828 } |
790 |
829 |
791 foreach ( $endpoints as $endpoint ) { |
830 foreach ( $endpoints as $endpoint ) { |
792 // Remove the redundant preg_match() argument. |
|
793 unset( $args[0] ); |
|
794 |
|
795 $request->set_url_params( $args ); |
831 $request->set_url_params( $args ); |
796 $request->set_attributes( $endpoint ); |
832 $request->set_attributes( $endpoint ); |
797 } |
833 } |
798 |
834 |
799 $data = $handler->get_data_for_route( $route, $endpoints, 'help' ); |
835 $data = $handler->get_data_for_route( $route, $endpoints, 'help' ); |
2904 if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) { |
2943 if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) { |
2905 $method = 'GET'; |
2944 $method = 'GET'; |
2906 } |
2945 } |
2907 } |
2946 } |
2908 |
2947 |
|
2948 // Remove trailing slashes at the end of the REST API path (query part). |
2909 $path = untrailingslashit( $path ); |
2949 $path = untrailingslashit( $path ); |
2910 if ( empty( $path ) ) { |
2950 if ( empty( $path ) ) { |
2911 $path = '/'; |
2951 $path = '/'; |
2912 } |
2952 } |
2913 |
2953 |
2914 $path_parts = parse_url( $path ); |
2954 $path_parts = parse_url( $path ); |
2915 if ( false === $path_parts ) { |
2955 if ( false === $path_parts ) { |
2916 return $memo; |
2956 return $memo; |
|
2957 } |
|
2958 |
|
2959 if ( isset( $path_parts['path'] ) && '/' !== $path_parts['path'] ) { |
|
2960 // Remove trailing slashes from the "path" part of the REST API path. |
|
2961 $path_parts['path'] = untrailingslashit( $path_parts['path'] ); |
|
2962 $path = str_contains( $path, '?' ) ? |
|
2963 $path_parts['path'] . '?' . ( $path_parts['query'] ?? '' ) : |
|
2964 $path_parts['path']; |
2917 } |
2965 } |
2918 |
2966 |
2919 $request = new WP_REST_Request( $method, $path_parts['path'] ); |
2967 $request = new WP_REST_Request( $method, $path_parts['path'] ); |
2920 if ( ! empty( $path_parts['query'] ) ) { |
2968 if ( ! empty( $path_parts['query'] ) ) { |
2921 parse_str( $path_parts['query'], $query_params ); |
2969 parse_str( $path_parts['query'], $query_params ); |