@@ -92,67 +92,86 @@ function register_rest_route( $route_namespace, $route, $args = array(), $overri
9292 );
9393 }
9494
95- if ( isset ( $ args ['args ' ] ) ) {
95+ if ( ! is_callable ( $ args ) && isset ( $ args ['args ' ] ) ) {
9696 $ common_args = $ args ['args ' ];
9797 unset( $ args ['args ' ] );
9898 } else {
9999 $ common_args = array ();
100100 }
101101
102- if ( isset ( $ args ['callback ' ] ) ) {
102+ if ( is_callable ( $ args ) || isset ( $ args ['callback ' ] ) ) {
103103 // Upgrade a single set to multiple.
104104 $ args = array ( $ args );
105105 }
106106
107- $ defaults = array (
108- 'methods ' => 'GET ' ,
109- 'callback ' => null ,
110- 'args ' => array (),
111- );
112-
113107 foreach ( $ args as $ key => &$ arg_group ) {
114108 if ( ! is_numeric ( $ key ) ) {
115109 // Route option, skip here.
116110 continue ;
117111 }
118112
119- $ arg_group = array_merge ( $ defaults , $ arg_group );
120- $ arg_group ['args ' ] = array_merge ( $ common_args , $ arg_group ['args ' ] );
113+ if ( is_callable ( $ arg_group ) ) {
114+ // Just-in-time resolvable callback, we'll normalize it later.
115+ $ arg_group = new WP_REST_Resolvable_Route ( $ clean_namespace , $ route , $ arg_group );
116+ continue ;
117+ }
118+
119+ $ arg_group = normalize_rest_endpoint_options ( $ clean_namespace , $ route , $ arg_group , $ common_args );
120+ }
121+
122+ $ full_route = '/ ' . $ clean_namespace . '/ ' . trim ( $ route , '/ ' );
123+ rest_get_server ()->register_route ( $ clean_namespace , $ full_route , $ args , $ override );
124+ return true ;
125+ }
121126
122- if ( ! isset ( $ arg_group ['permission_callback ' ] ) ) {
127+ /**
128+ * Normalize the options for a single REST API endpoint.
129+ *
130+ * @param string $namespace The route namespace.
131+ * @param string $route The route.
132+ * @param array $endpoint The endpoint options.
133+ * @param array $common_args Common arguments to merge with endpoint-specific arguments.
134+ */
135+ function normalize_rest_endpoint_options ( string $ namespace , string $ route , array $ endpoint , array $ common_args = [] ) {
136+ $ defaults = array (
137+ 'methods ' => 'GET ' ,
138+ 'callback ' => null ,
139+ 'args ' => array (),
140+ );
141+ $ endpoint = array_merge ( $ defaults , $ endpoint );
142+ $ endpoint ['args ' ] = array_merge ( $ common_args , $ endpoint ['args ' ] );
143+
144+ if ( ! isset ( $ endpoint ['permission_callback ' ] ) ) {
145+ _doing_it_wrong (
146+ 'register_rest_route ' ,
147+ sprintf (
148+ /* translators: 1: The REST API route being registered, 2: The argument name, 3: The suggested function name. */
149+ __ ( 'The REST API route definition for %1$s is missing the required %2$s argument. For REST API routes that are intended to be public, use %3$s as the permission callback. ' ),
150+ '<code> ' . $ namespace . '/ ' . trim ( $ route , '/ ' ) . '</code> ' ,
151+ '<code>permission_callback</code> ' ,
152+ '<code>__return_true</code> '
153+ ),
154+ '5.5.0 '
155+ );
156+ }
157+
158+ foreach ( $ endpoint ['args ' ] as $ arg ) {
159+ if ( ! is_array ( $ arg ) ) {
123160 _doing_it_wrong (
124- __FUNCTION__ ,
161+ ' register_rest_route ' ,
125162 sprintf (
126- /* translators: 1: The REST API route being registered, 2: The argument name, 3: The suggested function name. */
127- __ ( 'The REST API route definition for %1$s is missing the required %2$s argument. For REST API routes that are intended to be public, use %3$s as the permission callback. ' ),
128- '<code> ' . $ clean_namespace . '/ ' . trim ( $ route , '/ ' ) . '</code> ' ,
129- '<code>permission_callback</code> ' ,
130- '<code>__return_true</code> '
163+ /* translators: 1: $args, 2: The REST API route being registered. */
164+ __ ( 'REST API %1$s should be an array of arrays. Non-array value detected for %2$s. ' ),
165+ '<code>$args</code> ' ,
166+ '<code> ' . $ namespace . '/ ' . trim ( $ route , '/ ' ) . '</code> '
131167 ),
132- '5.5 .0 '
168+ '6.1 .0 '
133169 );
134- }
135-
136- foreach ( $ arg_group ['args ' ] as $ arg ) {
137- if ( ! is_array ( $ arg ) ) {
138- _doing_it_wrong (
139- __FUNCTION__ ,
140- sprintf (
141- /* translators: 1: $args, 2: The REST API route being registered. */
142- __ ( 'REST API %1$s should be an array of arrays. Non-array value detected for %2$s. ' ),
143- '<code>$args</code> ' ,
144- '<code> ' . $ clean_namespace . '/ ' . trim ( $ route , '/ ' ) . '</code> '
145- ),
146- '6.1.0 '
147- );
148- break ; // Leave the foreach loop once a non-array argument was found.
149- }
170+ break ; // Leave the foreach loop once a non-array argument was found.
150171 }
151172 }
152173
153- $ full_route = '/ ' . $ clean_namespace . '/ ' . trim ( $ route , '/ ' );
154- rest_get_server ()->register_route ( $ clean_namespace , $ full_route , $ args , $ override );
155- return true ;
174+ return $ endpoint ;
156175}
157176
158177/**
0 commit comments