This repository was archived by the owner on Feb 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Add serialization methods to WP_Ability and WP_Ability_Category #109
Open
bordoni
wants to merge
15
commits into
WordPress:trunk
Choose a base branch
from
bordoni:feature/add-to-array-and-to-json-schema
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5a4d0da
feat: Add to_array() and to_json_schema() methods to WP_Ability
bordoni 1b11c60
refactor: Use public getter methods instead of direct property access
bordoni d2bc425
feat: Add filters to to_array() and to_json_schema() methods
bordoni 1684e1d
feat: Implement JsonSerializable interface for WP_Ability
bordoni c6b6dc9
Improved to_array() method documentation
bordoni e35000a
Merge remote-tracking branch 'origin/trunk' into feature/add-to-array…
bordoni 25d2e4c
feat: Add to_array() and JsonSerializable to WP_Ability_Category
bordoni 87d6989
refactor: Update to_json_schema() to use JSON Schema Draft 4
bordoni 766f5a6
Merge branch 'trunk' into feature/add-to-array-and-to-json-schema
bordoni 426993c
Merge branch 'trunk' into feature/add-to-array-and-to-json-schema
bordoni 5552d68
Merge branch 'trunk' into feature/add-to-array-and-to-json-schema
bordoni 5e2b1fc
Merge branch 'trunk' into feature/add-to-array-and-to-json-schema
bordoni 74ebf3a
Merge branch 'trunk' into feature/add-to-array-and-to-json-schema
bordoni 203c29f
Merge branch 'trunk' of github.com:WordPress/abilities-api into featu…
bordoni 63e3b5f
Merge branch 'trunk' into feature/add-to-array-and-to-json-schema
bordoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| * | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.