@@ -81,8 +81,12 @@ private function get_args_from_rest_api(): void {
8181 );
8282
8383 foreach ( $ rest_api ['args ' ] as $ arg_name => $ arg_schema ) {
84+ $ type = $ arg_schema ['type ' ];
85+ if ( is_array ( $ type ) ) {
86+ $ type = reset ( $ type );
87+ }
8488 $ input_schema ['properties ' ][ $ arg_name ] = array (
85- 'type ' => $ arg_schema [ ' type ' ] ,
89+ 'type ' => $ type ,
8690 'description ' => $ arg_schema ['description ' ],
8791 );
8892
@@ -120,6 +124,12 @@ private function get_args_from_rest_api(): void {
120124 }
121125 }
122126
127+ // Apply modifications if provided in rest_alias['modifications'] .
128+ if ( isset ( $ this ->args ['rest_alias ' ]['inputSchemaReplacements ' ] ) ) {
129+ $ modifications = $ this ->args ['rest_alias ' ]['inputSchemaReplacements ' ];
130+ $ input_schema = $ this ->apply_modifications ( $ input_schema , $ modifications );
131+ }
132+
123133 // Update the args with the converted schema.
124134 $ this ->args ['inputSchema ' ] = $ input_schema ;
125135 $ this ->args ['callback ' ] = $ rest_api ['callback ' ];
@@ -268,4 +278,36 @@ private function validate_input_schema(): void {
268278 }
269279 }
270280 }
281+
282+ /**
283+ * Recursively remove all null values from an array.
284+ *
285+ * @param array $array The array to clean.
286+ * @return array The cleaned array.
287+ */
288+ private function remove_null_recursive ( array $ array ): array {
289+ foreach ( $ array as $ key => &$ value ) {
290+ if ( is_array ( $ value ) ) {
291+ $ value = $ this ->remove_null_recursive ( $ value );
292+ } elseif ( is_null ( $ value ) ) {
293+ unset( $ array [ $ key ] );
294+ }
295+ }
296+ unset( $ value ); // break reference.
297+ return $ array ;
298+ }
299+
300+ /**
301+ * Apply modifications to the input schema.
302+ *
303+ * @param array $input_schema The input schema.
304+ * @param array $modifications The modifications to apply.
305+ * @return array The modified input schema.
306+ */
307+ private function apply_modifications ( array $ input_schema , array $ modifications ): array {
308+
309+ $ modifications = array_replace_recursive ( $ input_schema , $ modifications );
310+
311+ return $ this ->remove_null_recursive ( $ modifications );
312+ }
271313}
0 commit comments