WP_Ability::prepare_properties( array $args ): array<string,
- Since
- 6.9.0
- Source
wp-includes/abilities-api/class-wp-ability.php:281
Description
Errors are thrown as exceptions instead of WP_Errors to allow for simpler handling and overloading. They are then caught and converted to a WP_Error by WP_Abilities_Registry::register().
Compatibility
- WordPress
- since 6.9.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$argsarray
Return value
array<string,- mixed> { An associative array of arguments with validated and prepared properties for the ability class.
$labelstringThe human-readable label for the ability.$descriptionstringA detailed description of what the ability does.$categorystringThe ability category slug this ability belongs to.$execute_callbackcallableA callback function to execute when the ability is invoked. Receives optional mixed input and returns mixed result or WP_Error.$permission_callbackcallableA callback function to check permissions before execution. Receives optional mixed input and returns bool or WP_Error.mixedarray<string,> $input_schema Optional. JSON Schema definition for the ability's input.mixedarray<string,> $output_schema Optional. JSON Schema definition for the ability's output.mixedarray<string,> $meta { Additional metadata for the ability.boolarray<string,|null> $annotations { Semantic annotations describing the ability's behavioral characteristics. These annotations are hints for tooling and documentation.$readonlybool|nullIf true, the ability does not modify its environment.$destructivebool|nullIf true, the ability may perform destructive updates to its environment. If false, the ability performs only additive updates.$idempotentbool|nullIf true, calling the ability repeatedly with the same arguments will have no additional effect on its environment. } @type bool $public Whether the ability is meant to be available to clients such as the REST API, MCP, or AI agents. Defaults to false.$show_in_restboolWhether to expose this ability in the REST API.
Performance profile
How much work a call to WP_Ability::prepare_properties() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 10–116
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 193.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Ability::prepare_properties() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10–67 | __() |
!empty($args) | 34–74 | is_callable(), __() |
!empty($args) && is_callable() | 45–81 | is_callable(), is_callable(), __() |
!empty($args) | 74–102 | annotations(), wp_parse_args() |
!empty($args) && is_callable() | 81–109 | is_callable(), annotations(), wp_parse_args() |
!empty($args) && is_callable() | 88–116 | is_callable(), is_callable(), annotations(), wp_parse_args() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 193 | 10–116 | 28 | |
| 8.5 | 193 | 10–116 | 28 | |
| 8.4 | 193 | 10–116 | 28 | |
| 8.3 | 193 | 10–116 | 28 | |
| 8.2 | 193 | 10–116 | 28 | |
| 8.1 | 193 | 10–116 | 28 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 195 | 10–118 | 28 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- __()Retrieves the translation of $text.
- wp_parse_args()Merges user defined arguments into defaults array.
- InvalidArgumentException::__construct()
Used by · 1
- WP_Ability::__construct()Constructor.
Source code
protected function prepare_properties( array $args ): array { // Required args must be present and of the correct type. if ( empty( $args['label'] ) || ! is_string( $args['label'] ) ) { throw new InvalidArgumentException( __( 'The ability properties must contain a `label` string.' ) ); } if ( empty( $args['description'] ) || ! is_string( $args['description'] ) ) { throw new InvalidArgumentException( __( 'The ability properties must contain a `description` string.' ) ); } if ( empty( $args['category'] ) || ! is_string( $args['category'] ) ) { throw new InvalidArgumentException( __( 'The ability properties must contain a `category` string.' ) ); } // If we are not overriding `ability_class` parameter during instantiation, then we need to validate the execute_callback. if ( get_class( $this ) === self::class && ( empty( $args['execute_callback'] ) || ! is_callable( $args['execute_callback'] ) ) ) { throw new InvalidArgumentException( __( 'The ability properties must contain a valid `execute_callback` function.' ) ); } // If we are not overriding `ability_class` parameter during instantiation, then we need to validate the permission_callback. if ( get_class( $this ) === self::class && ( empty( $args['permission_callback'] ) || ! is_callable( $args['permission_callback'] ) ) ) { throw new InvalidArgumentException( __( 'The ability properties must provide a valid `permission_callback` function.' ) ); } // Optional args only need to be of the correct type if they are present. if ( isset( $args['input_schema'] ) && ! is_array( $args['input_schema'] ) ) { throw new InvalidArgumentException( __( 'The ability properties should provide a valid `input_schema` definition.' ) ); } if ( isset( $args['output_schema'] ) && ! is_array( $args['output_schema'] ) ) { throw new InvalidArgumentException( __( 'The ability properties should provide a valid `output_schema` definition.' ) ); } if ( isset( $args['meta'] ) && ! is_array( $args['meta'] ) ) { throw new InvalidArgumentException( __( 'The ability properties should provide a valid `meta` array.' ) ); } if ( isset( $args['meta']['annotations'] ) && ! is_array( $args['meta']['annotations'] ) ) { throw new InvalidArgumentException( __( 'The ability meta should provide a valid `annotations` array.' ) ); } if ( isset( $args['meta']['show_in_rest'] ) && ! is_bool( $args['meta']['show_in_rest'] ) ) { throw new InvalidArgumentException( __( 'The ability meta should provide a valid `show_in_rest` boolean.' ) ); } if ( isset( $args['meta']['public'] ) && ! is_bool( $args['meta']['public'] ) ) { throw new InvalidArgumentException( __( 'The ability meta should provide a valid `public` boolean.' ) ); } // Set defaults for optional meta. $args['meta'] = wp_parse_args( $args['meta'] ?? array(), array( 'annotations' => static::$default_annotations, ) ); $args['meta']['annotations'] = wp_parse_args(Changelog
Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.
Signature, return type and hooks compared across 3 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/abilities-api/class-wp-ability.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.