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