Skip to content

Commit e7249f2

Browse files
gzioloCopilot
andauthored
Implement server-side registry for Abilities API (#3)
* Add server-side registry * Update tests/unit/WPAbilitiesRegistryTest.php Co-authored-by: Copilot <[email protected]> * Update src/class-wp-abilities-registry.php Co-authored-by: Copilot <[email protected]> * Use type shorthand for null consistently * Fix test hints for incorrect usage * Backport changes for PHP 7.2 and coding standards compatibility with WordPress core --------- Co-authored-by: Copilot <[email protected]>
1 parent 4dd7d5c commit e7249f2

8 files changed

+1530
-0
lines changed

.editorconfig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
# WordPress Coding Standards
5+
# https://make.wordpress.org/core/handbook/coding-standards/
6+
7+
root = true
8+
9+
[*]
10+
charset = utf-8
11+
end_of_line = lf
12+
insert_final_newline = true
13+
trim_trailing_whitespace = true
14+
indent_style = tab
15+
16+
[*.yml]
17+
indent_style = space
18+
indent_size = 2
19+
20+
[*.md]
21+
trim_trailing_whitespace = false
22+
23+
[{*.txt,wp-config-sample.php}]
24+
end_of_line = crlf

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.2/phpunit.xsd"
5+
bootstrap="tests/bootstrap.php"
6+
backupGlobals="false"
7+
colors="true"
8+
beStrictAboutTestsThatDoNotTestAnything="true"
9+
beStrictAboutOutputDuringTests="true"
10+
convertErrorsToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
convertNoticesToExceptions="true"
13+
convertDeprecationsToExceptions="true"
14+
>
15+
<testsuites>
16+
<testsuite name="unit">
17+
<directory suffix="Test.php">tests/unit</directory>
18+
</testsuite>
19+
</testsuites>
20+
</phpunit>

src/abilities-api.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php declare( strict_types = 1 );
2+
3+
/**
4+
* Abilities API
5+
*
6+
* Defines functions for managing abilities in WordPress.
7+
*
8+
* @package WordPress
9+
* @subpackage Abilities API
10+
* @since 0.1.0
11+
*/
12+
13+
/**
14+
* Registers a new ability using Abilities API.
15+
*
16+
* Note: Do not use before the {@see 'abilities_api_init'} hook.
17+
*
18+
* @see WP_Abilities_Registry::register()
19+
*
20+
* @since 0.1.0
21+
*
22+
* @param string|WP_Ability $name The name of the ability, or WP_Ability instance. The name must be a string
23+
* containing a namespace prefix, i.e. `my-plugin/my-ability`. It can only
24+
* contain lowercase alphanumeric characters, dashes and the forward slash.
25+
* @param array $properties Optional. An associative array of properties for the ability. This should
26+
* include `label`, `description`, `input_schema`, `output_schema`,
27+
* `execute_callback`, `permission_callback`, and `meta`.
28+
* @return ?WP_Ability An instance of registered ability on success, null on failure.
29+
*/
30+
function wp_register_ability( $name, array $properties = array() ): ?WP_Ability {
31+
if ( ! did_action( 'abilities_api_init' ) ) {
32+
_doing_it_wrong(
33+
__FUNCTION__,
34+
sprintf(
35+
/* translators: 1: abilities_api_init, 2: string value of the ability name. */
36+
esc_html__( 'Abilities must be registered on the %1$s action. The ability %2$s was not registered.' ),
37+
'<code>abilities_api_init</code>',
38+
'<code>' . esc_attr( $name ) . '</code>'
39+
),
40+
'0.1.0'
41+
);
42+
return null;
43+
}
44+
45+
return WP_Abilities_Registry::get_instance()->register( $name, $properties );
46+
}
47+
48+
/**
49+
* Unregisters an ability using Abilities API.
50+
*
51+
* @see WP_Abilities_Registry::unregister()
52+
*
53+
* @since 0.1.0
54+
*
55+
* @param string $name The name of the registered ability, with its namespace.
56+
* @return ?WP_Ability The unregistered ability instance on success, null on failure.
57+
*/
58+
function wp_unregister_ability( string $name ): ?WP_Ability {
59+
return WP_Abilities_Registry::get_instance()->unregister( $name );
60+
}
61+
62+
/**
63+
* Retrieves a registered ability using Abilities API.
64+
*
65+
* @see WP_Abilities_Registry::get_registered()
66+
*
67+
* @since 0.1.0
68+
*
69+
* @param string $name The name of the registered ability, with its namespace.
70+
* @return ?WP_Ability The registered ability instance, or null if it is not registered.
71+
*/
72+
function wp_get_ability( string $name ): ?WP_Ability {
73+
return WP_Abilities_Registry::get_instance()->get_registered( $name );
74+
}
75+
76+
/**
77+
* Retrieves all registered abilities using Abilities API.
78+
*
79+
* @see WP_Abilities_Registry::get_all_registered()
80+
*
81+
* @since 0.1.0
82+
*
83+
* @return WP_Ability[] The array of registered abilities.
84+
*/
85+
function wp_get_abilities(): array {
86+
return WP_Abilities_Registry::get_instance()->get_all_registered();
87+
}

0 commit comments

Comments
 (0)