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