Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions includes/abilities-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,16 @@
* @type array<string, mixed> $meta {
* Optional. Additional metadata for the ability.
*
* @type array<string, bool|null> $annotations Optional. Annotation metadata for the ability. Provides
* additional semantic information about the ability's
* characteristics and behavior.
* @type array<string, bool|null> $annotations {
* Optional. Semantic annotations describing the ability's behavioral characteristics.
* These annotations are hints for tooling and documentation.
*
* @type bool|null $readonly Optional. If true, the ability does not modify its environment.
* @type bool|null $destructive Optional. If true, the ability may perform destructive updates to its environment.
* If false, the ability performs only additive updates.
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
* will have no additional effect on its environment.
* }
* @type bool $show_in_rest Optional. Whether to expose this ability in the REST API.
* When true, the ability can be invoked via HTTP requests.
* Default false.
Expand All @@ -269,7 +276,7 @@
* @return WP_Ability|null The registered ability instance on success, `null` on failure.
*/
function wp_register_ability( string $name, array $args ): ?WP_Ability {
if ( ! did_action( 'wp_abilities_api_init' ) ) {
if ( ! doing_action( 'wp_abilities_api_init' ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
Expand Down Expand Up @@ -458,7 +465,7 @@ function wp_get_abilities(): array {
* @return WP_Ability_Category|null The registered ability category instance on success, `null` on failure.
*/
function wp_register_ability_category( string $slug, array $args ): ?WP_Ability_Category {
if ( ! did_action( 'wp_abilities_api_categories_init' ) ) {
if ( ! doing_action( 'wp_abilities_api_categories_init' ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
Expand Down
11 changes: 10 additions & 1 deletion includes/abilities-api/class-wp-abilities-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ final class WP_Abilities_Registry {
* @type array<string, mixed> $meta {
* Optional. Additional metadata for the ability.
*
* @type array<string, null|bool> $annotations Optional. Annotation metadata for the ability.
* @type array<string, bool|null> $annotations {
* Optional. Semantic annotations describing the ability's behavioral characteristics.
* These annotations are hints for tooling and documentation.
*
* @type bool|null $readonly Optional. If true, the ability does not modify its environment.
* @type bool|null $destructive Optional. If true, the ability may perform destructive updates to its environment.
* If false, the ability performs only additive updates.
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
* will have no additional effect on its environment.
* }
* @type bool $show_in_rest Optional. Whether to expose this ability in the REST API. Default false.
* }
* @type string $ability_class Optional. Custom class to instantiate instead of WP_Ability.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static function get_instance(): ?self {
__METHOD__,
sprintf(
// translators: %s: init action.
__( 'Ability API should not be initialized before the %s action has fired' ),
__( 'Ability API should not be initialized before the %s action has fired.' ),
'<code>init</code>'
),
'6.9.0'
Expand Down
43 changes: 39 additions & 4 deletions includes/abilities-api/class-wp-ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class WP_Ability {
* They are not guaranteed to provide a faithful description of ability behavior.
*
* @since 6.9.0
* @var array<string, (bool|null)>
* @var array<string, bool|null>
*/
protected static $default_annotations = array(
// If true, the ability does not modify its environment.
Expand Down Expand Up @@ -150,7 +150,16 @@ class WP_Ability {
* @type array<string, mixed> $meta {
* Optional. Additional metadata for the ability.
*
* @type array<string, null|bool> $annotations Optional. Annotation metadata for the ability.
* @type array<string, bool|null> $annotations {
* Optional. Semantic annotations describing the ability's behavioral characteristics.
* These annotations are hints for tooling and documentation.
*
* @type bool|null $readonly Optional. If true, the ability does not modify its environment.
* @type bool|null $destructive Optional. If true, the ability may perform destructive updates to its environment.
* If false, the ability performs only additive updates.
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
* will have no additional effect on its environment.
* }
* @type bool $show_in_rest Optional. Whether to expose this ability in the REST API. Default false.
* }
* }
Expand Down Expand Up @@ -205,7 +214,16 @@ public function __construct( string $name, array $args ) {
* @type array<string, mixed> $meta {
* Optional. Additional metadata for the ability.
*
* @type array<string, null|bool> $annotations Optional. Annotation metadata for the ability.
* @type array<string, bool|null> $annotations {
* Optional. Semantic annotations describing the ability's behavioral characteristics.
* These annotations are hints for tooling and documentation.
*
* @type bool|null $readonly Optional. If true, the ability does not modify its environment.
* @type bool|null $destructive Optional. If true, the ability may perform destructive updates to its environment.
* If false, the ability performs only additive updates.
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
* will have no additional effect on its environment.
* }
* @type bool $show_in_rest Optional. Whether to expose this ability in the REST API. Default false.
* }
* }
Expand All @@ -224,7 +242,16 @@ public function __construct( string $name, array $args ) {
* @type array<string, mixed> $meta {
* Additional metadata for the ability.
*
* @type array<string, null|bool> $annotations Optional. Annotation metadata for the ability.
* @type array<string, bool|null> $annotations {
* Semantic annotations describing the ability's behavioral characteristics.
* These annotations are hints for tooling and documentation.
*
* @type bool|null $readonly If true, the ability does not modify its environment.
* @type bool|null $destructive If true, the ability may perform destructive updates to its environment.
* If false, the ability performs only additive updates.
* @type bool|null $idempotent If true, calling the ability repeatedly with the same arguments
* will have no additional effect on its environment.
* }
* @type bool $show_in_rest Whether to expose this ability in the REST API. Default false.
* }
* }
Expand Down Expand Up @@ -498,6 +525,14 @@ protected function invoke_callback( callable $callback, $input = null ) {
* @return bool|WP_Error Whether the ability has the necessary permission.
*/
public function check_permissions( $input = null ) {
if ( ! is_callable( $this->permission_callback ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this redundant/contrary to the behavior in ::prepare_properties()?

( I see this was added in https://github.com/WordPress/wordpress-develop/pull/10431/files, but I can't find the discussion that prompted it. )

IMO the fatal error thrown from ::invoke_callback() is desired behavior, instead of hiding it inside a WP_Error. This error can only happen if a user is extending this class and overloading both the ::$permission_callback property type and either ::prepare_properties() or the constructor to avoid calling ::prepare_proerties() altogether, so the immediacy provides better DX.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be redundant, but it doesn't break anything. Feel free to open a patch against WordPress trunk proposing the removal of both checks. There is still time to adjust that logic before the final release.

As for this PR, as noted in the description, the code is copied from WordPress core as is, without modifications.

return new WP_Error(
'ability_invalid_permission_callback',
/* translators: %s ability name. */
sprintf( __( 'Ability "%s" does not have a valid permission callback.' ), esc_html( $this->name ) )
);
}

return $this->invoke_callback( $this->permission_callback, $input );
}

Expand Down
6 changes: 4 additions & 2 deletions tests/unit/abilities-api/wpAbilitiesRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ public function set_up(): void {

remove_all_filters( 'wp_register_ability_args' );

// Fire the init hook to allow test ability category registration.
do_action( 'wp_abilities_api_categories_init' );
// Simulates the Abilities API init hook to allow test ability category registration.
global $wp_current_filter;
$wp_current_filter[] = 'wp_abilities_api_categories_init';
wp_register_ability_category(
'math',
array(
'label' => 'Math',
'description' => 'Mathematical operations and calculations.',
)
);
array_pop( $wp_current_filter );

self::$test_ability_args = array(
'label' => 'Add numbers',
Expand Down
13 changes: 0 additions & 13 deletions tests/unit/abilities-api/wpAbility.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ class Tests_Abilities_API_WpAbility extends WP_UnitTestCase {
public function set_up(): void {
parent::set_up();

// Fire the init hook to allow test ability category registration.
do_action( 'wp_abilities_api_categories_init' );
wp_register_ability_category(
'math',
array(
'label' => 'Math',
'description' => 'Mathematical operations and calculations.',
)
);

self::$test_ability_properties = array(
'label' => 'Calculator',
'description' => 'Calculates the result of math operations.',
Expand Down Expand Up @@ -56,9 +46,6 @@ public function set_up(): void {
* Tear down after each test.
*/
public function tear_down(): void {
// Clean up registered test ability category.
wp_unregister_ability_category( 'math' );

parent::tear_down();
}

Expand Down
180 changes: 0 additions & 180 deletions tests/unit/abilities-api/wpCoreAbilities.php

This file was deleted.

Loading
Loading