@@ -100,21 +100,12 @@ class WP_Ability {
100100	 * @param array<string,mixed> $properties An associative array of properties for the ability. This should 
101101	 *                                        include `label`, `description`, `input_schema`, `output_schema`, 
102102	 *                                        `execute_callback`, `permission_callback`, and `meta`. 
103- 	 * 
104- 	 * @phpstan-param array{ 
105- 	 *   label: string, 
106- 	 *   description: string, 
107- 	 *   input_schema?: array<string,mixed>, 
108- 	 *   output_schema?: array<string,mixed>, 
109- 	 *   execute_callback: callable( array<string,mixed> $input): (mixed|\WP_Error), 
110- 	 *   permission_callback?: ?callable( array<string,mixed> $input ): (bool|\WP_Error), 
111- 	 *   meta?: array<string,mixed>, 
112- 	 *   ...<string, mixed>, 
113- 	 * } $properties 
114103	 */ 
115104	public  function  __construct ( string  $ name , array  $ properties  ) {
116105		$ this  ->name  = $ name ;
117106
107+ 		$ this  ->validate_properties ( $ properties  );
108+ 
118109		foreach  ( $ properties  as  $ property_name  => $ property_value  ) {
119110			if  ( ! property_exists ( $ this  , $ property_name  ) ) {
120111				_doing_it_wrong (
@@ -202,6 +193,76 @@ public function get_meta(): array {
202193		return  $ this  ->meta ;
203194	}
204195
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+ 
205266	/** 
206267	 * Validates input data against the input schema. 
207268	 * 
0 commit comments