| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Abilities API: core functions for registering and managing abilities. 4 * 5 * The Abilities API provides a unified, extensible framework for registering 6 * and executing discrete capabilities within WordPress. An "ability" is a 7 * self-contained unit of functionality with defined inputs, outputs, permissions, 8 * and execution logic. 9 * 10 * ## Overview 11 * 12 * The Abilities API enables developers to: 13 * 14 * - Register custom abilities with standardized interfaces. 15 * - Define permission checks and execution callbacks. 16 * - Organize abilities into logical categories. 17 * - Validate inputs and outputs using JSON Schema. 18 * - Expose abilities to clients such as the REST API. 19 * 20 * ## Working with Abilities 21 * 22 * Abilities must be registered on the `wp_abilities_api_init` action hook. 23 * Attempting to register an ability outside of this hook will fail and 24 * trigger a `_doing_it_wrong()` notice. 25 26 * Example: 27 * 28 * function my_plugin_register_abilities(): void { 29 * wp_register_ability( 30 * 'my-plugin/export-users', 31 * array( 32 * 'label' => __( 'Export Users', 'my-plugin' ), 33 * 'description' => __( 'Exports user data to CSV format.', 'my-plugin' ), 34 * 'category' => 'data-export', 35 * 'execute_callback' => 'my_plugin_export_users', 36 * 'permission_callback' => function(): bool { 37 * return current_user_can( 'export' ); 38 * }, 39 * 'input_schema' => array( 40 * 'type' => 'string', 41 * 'enum' => array( 'subscriber', 'contributor', 'author', 'editor', 'administrator' ), 42 * 'description' => __( 'Limits the export to users with this role.', 'my-plugin' ), 43 * 'required' => false, 44 * ), 45 * 'output_schema' => array( 46 * 'type' => 'string', 47 * 'description' => __( 'User data in CSV format.', 'my-plugin' ), 48 * 'required' => true, 49 * ), 50 * 'meta' => array( 51 * 'public' => true, 52 * ), 53 * ) 54 * ); 55 * } 56 * add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' ); 57 * 58 * Once registered, abilities can be checked, retrieved, and managed: 59 * 60 * // Checks if an ability is registered, and prints its label. 61 * if ( wp_has_ability( 'my-plugin/export-users' ) ) { 62 * $ability = wp_get_ability( 'my-plugin/export-users' ); 63 * 64 * echo $ability->get_label(); 65 * } 66 * 67 * // Gets all registered abilities. 68 * $all_abilities = wp_get_abilities(); 69 * 70 * // Unregisters when no longer needed. 71 * wp_unregister_ability( 'my-plugin/export-users' ); 72 * 73 * ## Best Practices 74 * 75 * - Always register abilities on the `wp_abilities_api_init` hook. 76 * - Use namespaced ability names to prevent conflicts. 77 * - Implement robust permission checks in permission callbacks. 78 * - Provide an `input_schema` to ensure data integrity and document expected inputs. 79 * - Define an `output_schema` to describe return values and validate responses. 80 * - Return `WP_Error` objects for failures rather than throwing exceptions. 81 * - Use internationalization functions for all user-facing strings. 82 * 83 * @package WordPress 84 * @subpackage Abilities_API 85 * @since 6.9.0 86 */ 87 88 declare( strict_types = 1 ); 89 90 /** 91 * Registers a new ability using the Abilities API. It requires three steps: 92 * 93 * 1. Hook into the `wp_abilities_api_init` action. 94 * 2. Call `wp_register_ability()` with a namespaced name and configuration. 95 * 3. Provide execute and permission callbacks. 96 * 97 * Example: 98 * 99 * function my_plugin_register_abilities(): void { 100 * wp_register_ability( 101 * 'my-plugin/analyze-text', 102 * array( 103 * 'label' => __( 'Analyze Text', 'my-plugin' ), 104 * 'description' => __( 'Performs sentiment analysis on provided text.', 'my-plugin' ), 105 * 'category' => 'text-processing', 106 * 'input_schema' => array( 107 * 'type' => 'string', 108 * 'description' => __( 'The text to be analyzed.', 'my-plugin' ), 109 * 'minLength' => 10, 110 * 'required' => true, 111 * ), 112 * 'output_schema' => array( 113 * 'type' => 'string', 114 * 'enum' => array( 'positive', 'negative', 'neutral' ), 115 * 'description' => __( 'The sentiment result: positive, negative, or neutral.', 'my-plugin' ), 116 * 'required' => true, 117 * ), 118 * 'execute_callback' => 'my_plugin_analyze_text', 119 * 'permission_callback' => 'my_plugin_can_analyze_text', 120 * 'meta' => array( 121 * 'annotations' => array( 122 * 'readonly' => true, 123 * ), 124 * 'public' => true, 125 * ), 126 * ) 127 * ); 128 * } 129 * add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' ); 130 * 131 * On failure, this function returns `null` and calls `_doing_it_wrong()` with the reason. 132 * By default, the resulting notice is displayed when `WP_DEBUG` is enabled. 133 * 134 * ### Naming Conventions 135 * 136 * Ability names must follow these rules: 137 * 138 * - Include a namespace prefix (e.g., `my-plugin/my-ability`). 139 * - Use only lowercase alphanumeric characters, dashes, and forward slashes. 140 * - Use descriptive, action-oriented names (e.g., `process-payment`, `generate-report`). 141 * 142 * ### Categories 143 * 144 * Abilities can be organized into categories. If no category is provided, the ability is 145 * assigned to the built-in `uncategorized` category. Custom categories must be registered 146 * before the abilities that reference them: 147 * 148 * function my_plugin_register_categories(): void { 149 * wp_register_ability_category( 150 * 'text-processing', 151 * array( 152 * 'label' => __( 'Text Processing', 'my-plugin' ), 153 * 'description' => __( 'Abilities for analyzing and transforming text.', 'my-plugin' ), 154 * ) 155 * ); 156 * } 157 * add_action( 'wp_abilities_api_categories_init', 'my_plugin_register_categories' ); 158 * 159 * ### Input and Output Schemas 160 * 161 * Schemas define the expected structure, type, and constraints for ability inputs 162 * and outputs using JSON Schema syntax. They serve two critical purposes: automatic 163 * validation of data passed to and returned from abilities, and self-documenting 164 * API contracts for developers. 165 * 166 * WordPress implements a validator based on a subset of the JSON Schema Version 4 167 * specification (https://json-schema.org/specification-links.html#draft-4). 168 * For details on supported JSON Schema properties and syntax, see the 169 * related WordPress REST API Schema documentation: 170 * https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#json-schema-basics 171 * 172 * Defining schemas is mandatory when there is a value to pass or return. 173 * They ensure data integrity, improve developer experience, and enable 174 * better documentation: 175 * 176 * 'input_schema' => array( 177 * 'type' => 'string', 178 * 'description' => __( 'The text to be analyzed.', 'my-plugin' ), 179 * 'minLength' => 10, 180 * 'required' => true, 181 * ), 182 * 'output_schema' => array( 183 * 'type' => 'string', 184 * 'enum' => array( 'positive', 'negative', 'neutral' ), 185 * 'description' => __( 'The sentiment result: positive, negative, or neutral.', 'my-plugin' ), 186 * 'required' => true, 187 * ), 188 * 189 * ### Callbacks 190 * 191 * #### Execute Callback 192 * 193 * The execute callback performs the ability's core functionality. It receives 194 * optional input data and returns either a result or `WP_Error` on failure. 195 * 196 * function my_plugin_analyze_text( string $input ): string|WP_Error { 197 * $score = My_Plugin::perform_sentiment_analysis( $input ); 198 * if ( is_wp_error( $score ) ) { 199 * return $score; 200 * } 201 * return My_Plugin::interpret_sentiment_score( $score ); 202 * } 203 * 204 * #### Permission Callback 205 * 206 * The permission callback determines whether the ability can be executed. 207 * It receives the same input as the execute callback and must return a 208 * boolean or `WP_Error`. Common use cases include checking user capabilities, 209 * validating API keys, or verifying system state: 210 * 211 * function my_plugin_can_analyze_text( string $input ): bool|WP_Error { 212 * return current_user_can( 'edit_posts' ); 213 * } 214 * 215 * ### Client Exposure 216 * 217 * Set the high-level `public` flag to make an ability available to clients 218 * such as the REST API, MCP, or AI agents: 219 * 220 * 'meta' => array( 221 * 'public' => true, 222 * ), 223 * 224 * The `public` flag seeds the default for each per-channel flag. For the REST 225 * API it seeds `show_in_rest`, which lets the ability be invoked via HTTP 226 * requests. Set a per-channel flag directly to override that default. For 227 * example, keep a public ability out of the REST API: 228 * 229 * 'meta' => array( 230 * 'public' => true, 231 * 'show_in_rest' => false, 232 * ), 233 * 234 * @since 6.9.0 235 * @since 7.1.0 Added the `public` meta argument. 236 * @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`. 237 * 238 * @see WP_Abilities_Registry::register() 239 * @see wp_register_ability_category() 240 * @see wp_unregister_ability() 241 * 242 * @param string $name The name of the ability. Must be a namespaced string containing 243 * a prefix, e.g., `my-plugin/my-ability`. Can only contain lowercase 244 * alphanumeric characters, dashes, and forward slashes. 245 * @param array<string, mixed> $args { 246 * An associative array of arguments for configuring the ability. 247 * 248 * @type string $label Required. The human-readable label for the ability. 249 * @type string $description Required. A detailed description of what the ability does 250 * and when it should be used. 251 * @type string $category Optional. The ability category slug this ability belongs to. 252 * Defaults to `uncategorized`. Custom categories must be registered 253 * via `wp_register_ability_category()` before registering the ability. 254 * @type callable $execute_callback Required. A callback function to execute when the ability is invoked. 255 * Receives optional mixed input data and must return either a result 256 * value (any type) or a `WP_Error` object on failure. 257 * @type callable $permission_callback Required. A callback function to check permissions before execution. 258 * Receives optional mixed input data (same as `execute_callback`) and 259 * must return `true`/`false` for simple checks, or `WP_Error` for 260 * detailed error responses. 261 * @type array<string, mixed> $input_schema Optional. JSON Schema definition for validating the ability's input. 262 * Must be a valid JSON Schema object defining the structure and 263 * constraints for input data. Used for automatic validation and 264 * API documentation. 265 * @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output. 266 * Describes the structure of successful return values from 267 * `execute_callback`. Used for documentation and validation. 268 * @type array<string, mixed> $meta { 269 * Optional. Additional metadata for the ability. 270 * 271 * @type array<string, bool|null> $annotations { 272 * Optional. Semantic annotations describing the ability's behavioral characteristics. 273 * These annotations are hints for tooling and documentation. 274 * 275 * @type bool|null $readonly Optional. If true, the ability does not modify its environment. 276 * @type bool|null $destructive Optional. If true, the ability may perform destructive updates to its environment. 277 * If false, the ability performs only additive updates. 278 * @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments 279 * will have no additional effect on its environment. 280 * } 281 * @type bool $public Optional. Whether the ability is meant to be available to 282 * clients such as the REST API, MCP, or AI agents. Seeds 283 * the default for per-channel flags like `$show_in_rest`. 284 * Defaults to false. 285 * @type bool $show_in_rest Optional. Whether to expose this ability in the REST API. 286 * When true, the ability can be invoked via HTTP requests. 287 * Default is the value of `$public` when set, false otherwise. 288 * } 289 * @type string $ability_class Optional. Fully-qualified custom class name to instantiate 290 * instead of the default `WP_Ability` class. The custom class 291 * must extend `WP_Ability`. Useful for advanced customization 292 * of ability behavior. 293 * } 294 * @return WP_Ability|null The registered ability instance on success, `null` on failure. 295 */ 296 function wp_register_ability( string $name, array $args ): ?WP_Ability { 297 if ( ! doing_action( 'wp_abilities_api_init' ) ) { 298 _doing_it_wrong( 299 __FUNCTION__, 300 sprintf( 301 /* translators: 1: wp_abilities_api_init, 2: string value of the ability name. */ 302 __( 'Abilities must be registered on the %1$s action. The ability %2$s was not registered.' ), 303 '<code>wp_abilities_api_init</code>', 304 '<code>' . esc_html( $name ) . '</code>' 305 ), 306 '6.9.0' 307 ); 308 return null; 309 } 310 311 $registry = WP_Abilities_Registry::get_instance(); 312 if ( null === $registry ) { 313 return null; 314 } 315 316 return $registry->register( $name, $args ); 317 } 318 319 /** 320 * Unregisters an ability from the Abilities API. 321 * 322 * Removes a previously registered ability from the global registry. Use this to 323 * disable abilities provided by other plugins or when an ability is no longer needed. 324 * 325 * Can be called at any time after the ability has been registered. 326 * 327 * Example: 328 * 329 * if ( wp_has_ability( 'other-plugin/some-ability' ) ) { 330 * wp_unregister_ability( 'other-plugin/some-ability' ); 331 * } 332 * 333 * @since 6.9.0 334 * 335 * @see WP_Abilities_Registry::unregister() 336 * @see wp_register_ability() 337 * 338 * @param string $name The name of the ability to unregister, including namespace prefix 339 * (e.g., 'my-plugin/my-ability'). 340 * @return WP_Ability|null The unregistered ability instance on success, `null` on failure. 341 */ 342 function wp_unregister_ability( string $name ): ?WP_Ability { 343 $registry = WP_Abilities_Registry::get_instance(); 344 if ( null === $registry ) { 345 return null; 346 } 347 348 return $registry->unregister( $name ); 349 } 350 351 /** 352 * Checks if an ability is registered. 353 * 354 * Use this for conditional logic and feature detection before attempting to 355 * retrieve or use an ability. 356 * 357 * Example: 358 * 359 * // Displays different UI based on available abilities. 360 * if ( wp_has_ability( 'premium-plugin/advanced-export' ) ) { 361 * echo 'Export with Premium Features'; 362 * } else { 363 * echo 'Basic Export'; 364 * } 365 * 366 * @since 6.9.0 367 * 368 * @see WP_Abilities_Registry::is_registered() 369 * @see wp_get_ability() 370 * 371 * @param string $name The name of the ability to check, including namespace prefix 372 * (e.g., 'my-plugin/my-ability'). 373 * @return bool `true` if the ability is registered, `false` otherwise. 374 */ 375 function wp_has_ability( string $name ): bool { 376 $registry = WP_Abilities_Registry::get_instance(); 377 if ( null === $registry ) { 378 return false; 379 } 380 381 return $registry->is_registered( $name ); 382 } 383 384 /** 385 * Retrieves a registered ability. 386 * 387 * Returns the ability instance for inspection or use. The instance provides access 388 * to the ability's configuration, metadata, and execution methods. 389 * 390 * Example: 391 * 392 * // Prints information about a registered ability. 393 * $ability = wp_get_ability( 'my-plugin/export-data' ); 394 * if ( $ability ) { 395 * echo $ability->get_label() . ': ' . $ability->get_description(); 396 * } 397 * 398 * @since 6.9.0 399 * 400 * @see WP_Abilities_Registry::get_registered() 401 * @see wp_has_ability() 402 * 403 * @param string $name The name of the ability, including namespace prefix 404 * (e.g., 'my-plugin/my-ability'). 405 * @return WP_Ability|null The registered ability instance, or `null` if not registered. 406 */ 407 function wp_get_ability( string $name ): ?WP_Ability { 408 $registry = WP_Abilities_Registry::get_instance(); 409 if ( null === $registry ) { 410 return null; 411 } 412 413 return $registry->get_registered( $name ); 414 } 415 416 /** 417 * Retrieves registered abilities, optionally filtered by the given arguments. 418 * 419 * When called without arguments, returns all registered abilities. When called 420 * with an $args array, returns only abilities that match every specified condition. 421 * 422 * Filtering pipeline (executed in order): 423 * 424 * 1. Declarative filters (`category`, `namespace`, `meta`) — per-item, AND logic between 425 * arg types. 426 * 2. `item_include_callback` — per-item, caller-scoped. Return true to include, false to exclude. 427 * 3. `wp_get_abilities_item_include` filter — per-item, ecosystem-scoped. Plugins can enforce 428 * universal inclusion rules regardless of what the caller passed. 429 * 4. `result_callback` — on the full matched array, caller-scoped. Sort, slice, or reshape. 430 * 5. `wp_get_abilities_result` filter — on the full array, ecosystem-scoped. 431 * 432 * Steps 1–3 run inside a single loop over the registry — no extra iteration. 433 * 434 * Examples: 435 * 436 * // All abilities (unchanged behaviour). 437 * $abilities = wp_get_abilities(); 438 * 439 * // Filter by category. 440 * $abilities = wp_get_abilities( array( 'category' => 'content' ) ); 441 * 442 * // Filter by namespace. 443 * $abilities = wp_get_abilities( array( 'namespace' => 'woocommerce' ) ); 444 * 445 * // Filter by meta. 446 * $abilities = wp_get_abilities( array( 'meta' => array( 'show_in_rest' => true ) ) ); 447 * 448 * // Combine filters (AND logic between arg types). 449 * $abilities = wp_get_abilities( array( 450 * 'category' => 'content', 451 * 'namespace' => 'core', 452 * 'meta' => array( 'show_in_rest' => true ), 453 * ) ); 454 * 455 * // Caller-scoped per-item callback. 456 * $abilities = wp_get_abilities( array( 457 * 'item_include_callback' => function ( WP_Ability $ability ) { 458 * return current_user_can( 'manage_options' ); 459 * }, 460 * ) ); 461 * 462 * // Caller-scoped result callback (sort + paginate). 463 * $abilities = wp_get_abilities( array( 464 * 'result_callback' => function ( array $abilities ) { 465 * usort( $abilities, fn( $a, $b ) => strcasecmp( $a->get_label(), $b->get_label() ) ); 466 * return array_slice( $abilities, 0, 10 ); 467 * }, 468 * ) ); 469 * 470 * The pipeline always runs, even when called with no arguments. This ensures that the 471 * `wp_get_abilities_item_include` and `wp_get_abilities_result` filters always fire, 472 * giving plugins a reliable place to enforce universal inclusion or shaping rules. 473 * For raw, unfiltered registry data that bypasses the filter pipeline entirely, use 474 * {@see WP_Abilities_Registry::get_all_registered()} directly. 475 * 476 * @since 6.9.0 477 * @since 7.1.0 Added the `$args` parameter for filtering support. 478 * 479 * @see WP_Abilities_Registry::get_all_registered() 480 * 481 * @param array $args { 482 * Optional. Arguments to filter the returned abilities. Default empty array (returns all). 483 * 484 * @type string $category Filter by category slug. Only abilities whose category 485 * exactly matches the given slug are included. 486 * @type string $namespace Filter by ability namespace prefix. Pass the namespace 487 * without a trailing slash, e.g. `'woocommerce'` matches 488 * `'woocommerce/create-order'`. 489 * @type array $meta Filter by meta key/value pairs. All conditions must 490 * match (AND logic). Supports nested arrays for structured 491 * meta, e.g. `array( 'mcp' => array( 'public' => true ) )`. 492 * @type callable $item_include_callback Optional. A callback invoked per ability after declarative 493 * filters. Receives a WP_Ability instance, returns bool. 494 * Return true to include, false to exclude. 495 * @type callable $result_callback Optional. A callback invoked once on the full matched 496 * array. Receives WP_Ability[], must return WP_Ability[]. 497 * Use for sorting, slicing, or reshaping the result. 498 * } 499 * @return WP_Ability[] An array of registered WP_Ability instances matching the given args, 500 * keyed by ability name. Returns an empty array if no abilities are 501 * registered, the registry is unavailable, or no abilities match the 502 * given args. 503 */ 504 function wp_get_abilities( array $args = array() ): array { 505 $registry = WP_Abilities_Registry::get_instance(); 506 if ( null === $registry ) { 507 return array(); 508 } 509 510 $abilities = $registry->get_all_registered(); 511 512 $category = isset( $args['category'] ) && is_string( $args['category'] ) ? $args['category'] : ''; 513 $namespace = isset( $args['namespace'] ) && is_string( $args['namespace'] ) ? rtrim( $args['namespace'], '/' ) . '/' : ''; 514 $meta = isset( $args['meta'] ) && is_array( $args['meta'] ) ? $args['meta'] : array(); 515 $item_include_callback = isset( $args['item_include_callback'] ) && is_callable( $args['item_include_callback'] ) ? $args['item_include_callback'] : null; 516 $result_callback = isset( $args['result_callback'] ) && is_callable( $args['result_callback'] ) ? $args['result_callback'] : null; 517 518 $matched = array(); 519 520 foreach ( $abilities as $name => $ability ) { 521 // Step 1a: Filter by category. 522 if ( '' !== $category && $ability->get_category() !== $category ) { 523 continue; 524 } 525 526 // Step 1b: Filter by namespace prefix. 527 if ( '' !== $namespace && ! str_starts_with( $ability->get_name(), $namespace ) ) { 528 continue; 529 } 530 531 // Step 1c: Filter by meta key/value pairs (AND logic, supports nested arrays). 532 if ( ! empty( $meta ) && ! _wp_get_abilities_match_meta( $ability->get_meta(), $meta ) ) { 533 continue; 534 } 535 536 // Step 2: Caller-scoped per-item callback. 537 $include = true; 538 if ( null !== $item_include_callback ) { 539 $include = (bool) call_user_func( $item_include_callback, $ability ); 540 } 541 542 /** 543 * Filters whether an individual ability should be included in the result set. 544 * 545 * Fires after the declarative filters and the caller-scoped item_include_callback. 546 * Plugins can use this to enforce universal inclusion rules regardless of 547 * what the caller passed in $args. 548 * 549 * @since 7.1.0 550 * 551 * @param bool $include Whether to include the ability. Default true (after declarative filters pass). 552 * @param WP_Ability $ability The ability instance being evaluated. 553 * @param array $args The full $args array passed to wp_get_abilities(). 554 */ 555 $include = (bool) apply_filters( 'wp_get_abilities_item_include', $include, $ability, $args ); 556 557 if ( $include ) { 558 $matched[ $name ] = $ability; 559 } 560 } 561 562 // Step 4: Caller-scoped result callback. 563 if ( null !== $result_callback ) { 564 $matched = (array) call_user_func( $result_callback, $matched ); 565 } 566 567 /** 568 * Filters the full list of matched abilities after all per-item filtering is complete. 569 * 570 * Fires after the caller-scoped result_callback. Plugins can use this to sort, 571 * paginate, or reshape the final result set universally. 572 * 573 * @since 7.1.0 574 * 575 * @param WP_Ability[] $matched The matched abilities after all filtering. 576 * @param array $args The full $args array passed to wp_get_abilities(). 577 */ 578 return (array) apply_filters( 'wp_get_abilities_result', $matched, $args ); 579 } 580 581 /** 582 * Checks whether an ability's meta array matches a set of required key/value conditions. 583 * 584 * All conditions must match (AND logic). Supports nested arrays for structured meta, 585 * e.g. `array( 'mcp' => array( 'public' => true ) )`. 586 * 587 * @since 7.1.0 588 * @access private 589 * 590 * @param array $meta The ability's meta array. 591 * @param array $conditions The required key/value conditions to match against. 592 * @return bool True if all conditions match, false otherwise. 593 */ 594 function _wp_get_abilities_match_meta( array $meta, array $conditions ): bool { 595 foreach ( $conditions as $key => $value ) { 596 if ( ! array_key_exists( $key, $meta ) ) { 597 return false; 598 } 599 600 if ( is_array( $value ) ) { 601 if ( ! is_array( $meta[ $key ] ) || ! _wp_get_abilities_match_meta( $meta[ $key ], $value ) ) { 602 return false; 603 } 604 } elseif ( $meta[ $key ] !== $value ) { 605 return false; 606 } 607 } 608 609 return true; 610 } 611 612 /** 613 * Registers a new ability category. 614 * 615 * Ability categories provide a way to organize and group related abilities for better 616 * discoverability and management. Custom categories must be registered before abilities 617 * that reference them. Abilities that omit a category are assigned to the built-in 618 * `uncategorized` category, which is intended as an escape hatch for simple or transitional 619 * registrations. 620 * 621 * Ability categories must be registered on the `wp_abilities_api_categories_init` action hook. 622 * 623 * Example: 624 * 625 * function my_plugin_register_categories() { 626 * wp_register_ability_category( 627 * 'content-management', 628 * array( 629 * 'label' => __( 'Content Management', 'my-plugin' ), 630 * 'description' => __( 'Abilities for managing and organizing content.', 'my-plugin' ), 631 * ) 632 * ); 633 * } 634 * add_action( 'wp_abilities_api_categories_init', 'my_plugin_register_categories' ); 635 * 636 * On failure, this function returns `null` and calls `_doing_it_wrong()` with the reason. 637 * By default, the resulting notice is displayed when `WP_DEBUG` is enabled. 638 * 639 * @since 6.9.0 640 * 641 * @see WP_Ability_Categories_Registry::register() 642 * @see wp_register_ability() 643 * @see wp_unregister_ability_category() 644 * 645 * @param string $slug The unique slug for the ability category. Must contain only lowercase 646 * alphanumeric characters and dashes (e.g., 'data-export'). 647 * @param array<string, mixed> $args { 648 * An associative array of arguments for the ability category. 649 * 650 * @type string $label Required. The human-readable label for the ability category. 651 * @type string $description Required. A description of what abilities in this category do. 652 * @type array<string, mixed> $meta Optional. Additional metadata for the ability category. 653 * } 654 * @return WP_Ability_Category|null The registered ability category instance on success, `null` on failure. 655 */ 656 function wp_register_ability_category( string $slug, array $args ): ?WP_Ability_Category { 657 if ( ! doing_action( 'wp_abilities_api_categories_init' ) ) { 658 _doing_it_wrong( 659 __FUNCTION__, 660 sprintf( 661 /* translators: 1: wp_abilities_api_categories_init, 2: ability category slug. */ 662 __( 'Ability categories must be registered on the %1$s action. The ability category %2$s was not registered.' ), 663 '<code>wp_abilities_api_categories_init</code>', 664 '<code>' . esc_html( $slug ) . '</code>' 665 ), 666 '6.9.0' 667 ); 668 return null; 669 } 670 671 $registry = WP_Ability_Categories_Registry::get_instance(); 672 if ( null === $registry ) { 673 return null; 674 } 675 676 return $registry->register( $slug, $args ); 677 } 678 679 /** 680 * Unregisters an ability category. 681 * 682 * Removes a previously registered ability category from the global registry. Use this to 683 * disable ability categories that are no longer needed. 684 * 685 * Can be called at any time after the ability category has been registered. 686 * 687 * Example: 688 * 689 * if ( wp_has_ability_category( 'deprecated-category' ) ) { 690 * wp_unregister_ability_category( 'deprecated-category' ); 691 * } 692 * 693 * @since 6.9.0 694 * 695 * @see WP_Ability_Categories_Registry::unregister() 696 * @see wp_register_ability_category() 697 * 698 * @param string $slug The slug of the ability category to unregister. 699 * @return WP_Ability_Category|null The unregistered ability category instance on success, `null` on failure. 700 */ 701 function wp_unregister_ability_category( string $slug ): ?WP_Ability_Category { 702 $registry = WP_Ability_Categories_Registry::get_instance(); 703 if ( null === $registry ) { 704 return null; 705 } 706 707 return $registry->unregister( $slug ); 708 } 709 710 /** 711 * Checks if an ability category is registered. 712 * 713 * Use this for conditional logic and feature detection before attempting to 714 * retrieve or use an ability category. 715 * 716 * Example: 717 * 718 * // Displays different UI based on available ability categories. 719 * if ( wp_has_ability_category( 'premium-features' ) ) { 720 * echo 'Premium Features Available'; 721 * } else { 722 * echo 'Standard Features'; 723 * } 724 * 725 * @since 6.9.0 726 * 727 * @see WP_Ability_Categories_Registry::is_registered() 728 * @see wp_get_ability_category() 729 * 730 * @param string $slug The slug of the ability category to check. 731 * @return bool `true` if the ability category is registered, `false` otherwise. 732 */ 733 function wp_has_ability_category( string $slug ): bool { 734 $registry = WP_Ability_Categories_Registry::get_instance(); 735 if ( null === $registry ) { 736 return false; 737 } 738 739 return $registry->is_registered( $slug ); 740 } 741 742 /** 743 * Retrieves a registered ability category. 744 * 745 * Returns the ability category instance for inspection or use. The instance provides access 746 * to the ability category's configuration and metadata. 747 * 748 * Example: 749 * 750 * // Prints information about a registered ability category. 751 * $ability_category = wp_get_ability_category( 'content-management' ); 752 * if ( $ability_category ) { 753 * echo $ability_category->get_label() . ': ' . $ability_category->get_description(); 754 * } 755 * 756 * @since 6.9.0 757 * 758 * @see WP_Ability_Categories_Registry::get_registered() 759 * @see wp_has_ability_category() 760 * @see wp_get_ability_categories() 761 * 762 * @param string $slug The slug of the ability category. 763 * @return WP_Ability_Category|null The ability category instance, or `null` if not registered. 764 */ 765 function wp_get_ability_category( string $slug ): ?WP_Ability_Category { 766 $registry = WP_Ability_Categories_Registry::get_instance(); 767 if ( null === $registry ) { 768 return null; 769 } 770 771 return $registry->get_registered( $slug ); 772 } 773 774 /** 775 * Retrieves all registered ability categories. 776 * 777 * Returns an array of all ability category instances currently registered in the system. 778 * Use this for discovery, debugging, or building administrative interfaces. 779 * 780 * Example: 781 * 782 * // Prints information about all available ability categories. 783 * $ability_categories = wp_get_ability_categories(); 784 * foreach ( $ability_categories as $ability_category ) { 785 * echo $ability_category->get_label() . ': ' . $ability_category->get_description() . "\n"; 786 * } 787 * 788 * @since 6.9.0 789 * 790 * @see WP_Ability_Categories_Registry::get_all_registered() 791 * @see wp_get_ability_category() 792 * 793 * @return WP_Ability_Category[] An array of registered ability category instances. Returns an empty array 794 * if no ability categories are registered or if the registry is unavailable. 795 */ 796 function wp_get_ability_categories(): array { 797 $registry = WP_Ability_Categories_Registry::get_instance(); 798 if ( null === $registry ) { 799 return array(); 800 } 801 802 return $registry->get_all_registered(); 803 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Sep 5 08:20:28 2026 | Cross-referenced by PHPXref |