-
Notifications
You must be signed in to change notification settings - Fork 50
Add serialization methods to WP_Ability and WP_Ability_Category #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
5a4d0da
1b11c60
d2bc425
1684e1d
c6b6dc9
e35000a
25d2e4c
87d6989
766f5a6
426993c
5552d68
5e2b1fc
74ebf3a
203c29f
63e3b5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
| * | ||
| * @see WP_Abilities_Registry | ||
| */ | ||
| class WP_Ability { | ||
| class WP_Ability implements \JsonSerializable { | ||
|
|
||
| /** | ||
| * The default value for the `show_in_rest` meta. | ||
|
|
@@ -370,17 +370,6 @@ | |
| return $this->description; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the ability category for the ability. | ||
| * | ||
| * @since 6.9.0 | ||
| * | ||
| * @return string The ability category for the ability. | ||
| */ | ||
| public function get_category(): string { | ||
| return $this->category; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the input schema for the ability. | ||
| * | ||
|
|
@@ -452,6 +441,134 @@ | |
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the category for the ability. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return string The category for the ability. | ||
| */ | ||
| public function get_category(): string { | ||
| return $this->category; | ||
| } | ||
|
|
||
| /** | ||
| * Converts the ability to an array representation. | ||
| * | ||
| * Returns a complete array representation of the ability including name, label, | ||
| * description, schemas, and metadata. Callbacks are excluded as they are not serializable. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return array<string,mixed> { | ||
| * The ability as an associative array. | ||
| * | ||
| * @type string $name The ability name with namespace. | ||
| * @type string $label The human-readable label. | ||
| * @type string $description The detailed description. | ||
| * @type array $input_schema The input validation schema. | ||
| * @type array $output_schema The output validation schema. | ||
| * @type array $meta { | ||
| * Metadata for the ability. May contain additional custom keys beyond those documented below. | ||
| * | ||
| * @type array $annotations { | ||
| * Behavior annotations. | ||
| * | ||
| * @type string $instructions Usage instructions. | ||
| * @type bool $readonly Whether the ability is read-only. | ||
| * @type bool $destructive Whether the ability is destructive. | ||
| * @type bool $idempotent Whether the ability is idempotent. | ||
| * } | ||
| * @type bool $show_in_rest Whether the ability is exposed in REST API. | ||
| * @type mixed ...$0 Additional custom metadata keys. | ||
| * } | ||
| * } | ||
| */ | ||
| public function to_array(): array { | ||
| $array = array( | ||
| 'name' => $this->get_name(), | ||
| 'label' => $this->get_label(), | ||
| 'description' => $this->get_description(), | ||
| 'input_schema' => $this->get_input_schema(), | ||
| 'output_schema' => $this->get_output_schema(), | ||
| 'meta' => $this->get_meta(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, to make the PHPDoc correct, you would have to account for the callback in any custom meta provided or limit that object to
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point I will change the docblock, as I feel like the |
||
| ); | ||
|
|
||
| return $array; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes the ability to a value that can be serialized natively by json_encode(). | ||
| * | ||
| * Implements the JsonSerializable interface to allow the ability to be passed | ||
| * directly to json_encode() without manually calling to_array(). | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return array<string,mixed> The ability as an associative array. | ||
| */ | ||
| public function jsonSerialize(): array { | ||
| return $this->to_array(); | ||
| } | ||
|
|
||
| /** | ||
| * Converts the ability to a JSON Schema representation. | ||
| * | ||
| * Generates a JSON Schema Draft 4 compliant schema describing the ability's | ||
| * structure, including input/output schemas and metadata. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return array<string,mixed> A JSON Schema representation of the ability. | ||
| */ | ||
| public function to_json_schema(): array { | ||
| $input_schema = $this->get_input_schema(); | ||
| $output_schema = $this->get_output_schema(); | ||
|
|
||
| $schema = array( | ||
| '$schema' => 'http://json-schema.org/draft-04/schema#', | ||
| 'type' => 'object', | ||
| 'title' => $this->get_label(), | ||
| 'description' => $this->get_description(), | ||
| 'properties' => array( | ||
| 'name' => array( | ||
| 'type' => 'string', | ||
| 'enum' => array( $this->get_name() ), | ||
| ), | ||
| 'meta' => array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'annotations' => array( | ||
| 'type' => 'object', | ||
| 'properties' => array( | ||
| 'instructions' => array( 'type' => 'string' ), | ||
| 'readonly' => array( 'type' => 'boolean' ), | ||
| 'destructive' => array( 'type' => 'boolean' ), | ||
| 'idempotent' => array( 'type' => 'boolean' ), | ||
| ), | ||
| ), | ||
| 'show_in_rest' => array( | ||
| 'type' => 'boolean', | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| 'required' => array( 'name', 'meta' ), | ||
| ); | ||
|
|
||
| if ( ! empty( $input_schema ) ) { | ||
| $schema['properties']['input_schema'] = $input_schema; | ||
| $schema['required'][] = 'input_schema'; | ||
| } | ||
|
|
||
| if ( ! empty( $output_schema ) ) { | ||
| $schema['properties']['output_schema'] = $output_schema; | ||
| $schema['required'][] = 'output_schema'; | ||
| } | ||
|
|
||
| return $schema; | ||
| } | ||
|
|
||
| /** | ||
| * Validates input data against the input schema. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.