| @@ -33,10 +33,16 @@ | ||
| 33 | 33 | $this->permission_callback = $permission_callback; |
| 34 | 34 | $this->input_schema = $input_schema; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | + public static function empty_object_input_schema(): array { | |
| 38 | + return [ | |
| 39 | + 'type' => 'object', | |
| 40 | + ]; | |
| 41 | + } | |
| 42 | + | |
| 37 | 43 | public function to_array(): array { |
| 38 | - $definition = [ | |
| 44 | + return [ | |
| 39 | 45 | 'label' => $this->label, |
| 40 | 46 | 'description' => $this->description, |
| 41 | 47 | 'category' => $this->category, |
| 42 | 48 | 'output_schema' => $this->output_schema, |
| @@ -41,13 +47,30 @@ | ||
| 41 | 47 | 'category' => $this->category, |
| 42 | 48 | 'output_schema' => $this->output_schema, |
| 43 | 49 | 'meta' => $this->meta, |
| 44 | 50 | 'permission_callback' => $this->permission_callback, |
| 51 | + 'input_schema' => $this->normalized_input_schema(), | |
| 45 | 52 | ]; |
| 53 | + } | |
| 46 | 54 | |
| 47 | - if ( ! empty( $this->input_schema ) ) { | |
| 48 | - $definition['input_schema'] = $this->input_schema; | |
| 55 | + private function normalized_input_schema(): array { | |
| 56 | + if ( empty( $this->input_schema ) ) { | |
| 57 | + return self::empty_object_input_schema(); | |
| 49 | 58 | } |
| 50 | 59 | |
| 51 | - return $definition; | |
| 60 | + $schema = $this->input_schema; | |
| 61 | + | |
| 62 | + if ( | |
| 63 | + array_key_exists( 'properties', $schema ) | |
| 64 | + && is_array( $schema['properties'] ) | |
| 65 | + && empty( $schema['properties'] ) | |
| 66 | + ) { | |
| 67 | + unset( $schema['properties'] ); | |
| 68 | + } | |
| 69 | + | |
| 70 | + if ( ! isset( $schema['type'] ) ) { | |
| 71 | + $schema['type'] = 'object'; | |
| 72 | + } | |
| 73 | + | |
| 74 | + return $schema; | |
| 52 | 75 | } |
| 53 | 76 | } |