@@ -96,15 +96,15 @@ class WP_Ability {
9696 *
9797 * @see wp_register_ability()
9898 *
99- * @param string $name The name of the ability, with its namespace.
100- * @param array<string,mixed> $properties An associative array of properties for the ability. This should
101- * include `label`, `description`, `input_schema`, `output_schema`,
102- * `execute_callback`, `permission_callback`, and `meta`.
99+ * @param string $name The name of the ability, with its namespace.
100+ * @param array<string,mixed> $args An associative array of arguments for the ability. This should
101+ * include `label`, `description`, `input_schema`, `output_schema`,
102+ * `execute_callback`, `permission_callback`, and `meta`.
103103 */
104- public function __construct ( string $ name , array $ properties ) {
104+ public function __construct ( string $ name , array $ args ) {
105105 $ this ->name = $ name ;
106106
107- $ this ->validate_properties ( $ properties );
107+ $ properties = $ this ->prepare_properties ( $ args );
108108
109109 foreach ( $ properties as $ property_name => $ property_value ) {
110110 if ( ! property_exists ( $ this , $ property_name ) ) {
@@ -126,6 +126,77 @@ public function __construct( string $name, array $properties ) {
126126 }
127127 }
128128
129+ /**
130+ * Prepares and validates the properties used to instantiate the ability.
131+ *
132+ * Errors are thrown as exceptions instead of \WP_Errors to allow for simpler handling and overloading. They are then
133+ * caught and converted to a WP_Error when by WP_Abilities_Registry::register().
134+ *
135+ * @since 0.2.0
136+ *
137+ * @see WP_Abilities_Registry::register()
138+ *
139+ * @param array<string,mixed> $args An associative array of arguments used to instantiate the class.
140+ * @return array<string,mixed> The validated and prepared properties.
141+ * @throws \InvalidArgumentException if an argument is invalid.
142+ *
143+ * @phpstan-return array{
144+ * label: string,
145+ * description: string,
146+ * input_schema?: array<string,mixed>,
147+ * output_schema?: array<string,mixed>,
148+ * execute_callback: callable( array<string,mixed> $input): (mixed|\WP_Error),
149+ * permission_callback?: ?callable( array<string,mixed> $input ): (bool|\WP_Error),
150+ * meta?: array<string,mixed>,
151+ * ...<string, mixed>,
152+ * } $args
153+ */
154+ protected function prepare_properties ( array $ args ): array {
155+ if ( empty ( $ args ['label ' ] ) || ! is_string ( $ args ['label ' ] ) ) {
156+ throw new \InvalidArgumentException (
157+ esc_html__ ( 'The ability properties must contain a `label` string. ' )
158+ );
159+ }
160+
161+ if ( empty ( $ args ['description ' ] ) || ! is_string ( $ args ['description ' ] ) ) {
162+ throw new \InvalidArgumentException (
163+ esc_html__ ( 'The ability properties must contain a `description` string. ' )
164+ );
165+ }
166+
167+ if ( isset ( $ args ['input_schema ' ] ) && ! is_array ( $ args ['input_schema ' ] ) ) {
168+ throw new \InvalidArgumentException (
169+ esc_html__ ( 'The ability properties should provide a valid `input_schema` definition. ' )
170+ );
171+ }
172+
173+ if ( isset ( $ args ['output_schema ' ] ) && ! is_array ( $ args ['output_schema ' ] ) ) {
174+ throw new \InvalidArgumentException (
175+ esc_html__ ( 'The ability properties should provide a valid `output_schema` definition. ' )
176+ );
177+ }
178+
179+ if ( empty ( $ args ['execute_callback ' ] ) || ! is_callable ( $ args ['execute_callback ' ] ) ) {
180+ throw new \InvalidArgumentException (
181+ esc_html__ ( 'The ability properties must contain a valid `execute_callback` function. ' )
182+ );
183+ }
184+
185+ if ( isset ( $ args ['permission_callback ' ] ) && ! is_callable ( $ args ['permission_callback ' ] ) ) {
186+ throw new \InvalidArgumentException (
187+ esc_html__ ( 'The ability properties should provide a valid `permission_callback` function. ' )
188+ );
189+ }
190+
191+ if ( isset ( $ args ['meta ' ] ) && ! is_array ( $ args ['meta ' ] ) ) {
192+ throw new \InvalidArgumentException (
193+ esc_html__ ( 'The ability properties should provide a valid `meta` array. ' )
194+ );
195+ }
196+
197+ return $ args ;
198+ }
199+
129200 /**
130201 * Retrieves the name of the ability, with its namespace.
131202 * Example: `my-plugin/my-ability`.
@@ -193,76 +264,6 @@ public function get_meta(): array {
193264 return $ this ->meta ;
194265 }
195266
196- /**
197- * Validates the properties used to instantiate the ability.
198- *
199- * Errors are thrown as exceptions instead of \WP_Errors to allow for simpler handling and overloading. They are then
200- * caught and converted to a WP_Error when by WP_Abilities_Registry::register().
201- *
202- * @since n.e.x.t
203- *
204- * @see WP_Abilities_Registry::register()
205- *
206- * @param array<string,mixed> $properties An associative array of properties to validate.
207- *
208- * @return void
209- * @throws \InvalidArgumentException if the properties are invalid.
210- *
211- * @phpstan-assert array{
212- * label: string,
213- * description: string,
214- * input_schema?: array<string,mixed>,
215- * output_schema?: array<string,mixed>,
216- * execute_callback: callable( array<string,mixed> $input): (mixed|\WP_Error),
217- * permission_callback?: ?callable( array<string,mixed> $input ): (bool|\WP_Error),
218- * meta?: array<string,mixed>,
219- * ...<string, mixed>,
220- * } $properties
221- */
222- protected function validate_properties ( array $ properties ) {
223- if ( empty ( $ properties ['label ' ] ) || ! is_string ( $ properties ['label ' ] ) ) {
224- throw new \InvalidArgumentException (
225- esc_html__ ( 'The ability properties must contain a `label` string. ' )
226- );
227- }
228-
229- if ( empty ( $ properties ['description ' ] ) || ! is_string ( $ properties ['description ' ] ) ) {
230- throw new \InvalidArgumentException (
231- esc_html__ ( 'The ability properties must contain a `description` string. ' )
232- );
233- }
234-
235- if ( isset ( $ properties ['input_schema ' ] ) && ! is_array ( $ properties ['input_schema ' ] ) ) {
236- throw new \InvalidArgumentException (
237- esc_html__ ( 'The ability properties should provide a valid `input_schema` definition. ' )
238- );
239- }
240-
241- if ( isset ( $ properties ['output_schema ' ] ) && ! is_array ( $ properties ['output_schema ' ] ) ) {
242- throw new \InvalidArgumentException (
243- esc_html__ ( 'The ability properties should provide a valid `output_schema` definition. ' )
244- );
245- }
246-
247- if ( empty ( $ properties ['execute_callback ' ] ) || ! is_callable ( $ properties ['execute_callback ' ] ) ) {
248- throw new \InvalidArgumentException (
249- esc_html__ ( 'The ability properties must contain a valid `execute_callback` function. ' )
250- );
251- }
252-
253- if ( isset ( $ properties ['permission_callback ' ] ) && ! is_callable ( $ properties ['permission_callback ' ] ) ) {
254- throw new \InvalidArgumentException (
255- esc_html__ ( 'The ability properties should provide a valid `permission_callback` function. ' )
256- );
257- }
258-
259- if ( isset ( $ properties ['meta ' ] ) && ! is_array ( $ properties ['meta ' ] ) ) {
260- throw new \InvalidArgumentException (
261- esc_html__ ( 'The ability properties should provide a valid `meta` array. ' )
262- );
263- }
264- }
265-
266267 /**
267268 * Validates input data against the input schema.
268269 *
0 commit comments