Edit File by line
/home/zeestwma/redstone.../wp-inclu...
File: abilities-api.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Abilities API: core functions for registering and managing abilities.
[2] Fix | Delete
*
[3] Fix | Delete
* The Abilities API provides a unified, extensible framework for registering
[4] Fix | Delete
* and executing discrete capabilities within WordPress. An "ability" is a
[5] Fix | Delete
* self-contained unit of functionality with defined inputs, outputs, permissions,
[6] Fix | Delete
* and execution logic.
[7] Fix | Delete
*
[8] Fix | Delete
* ## Overview
[9] Fix | Delete
*
[10] Fix | Delete
* The Abilities API enables developers to:
[11] Fix | Delete
*
[12] Fix | Delete
* - Register custom abilities with standardized interfaces.
[13] Fix | Delete
* - Define permission checks and execution callbacks.
[14] Fix | Delete
* - Organize abilities into logical categories.
[15] Fix | Delete
* - Validate inputs and outputs using JSON Schema.
[16] Fix | Delete
* - Expose abilities through the REST API.
[17] Fix | Delete
*
[18] Fix | Delete
* ## Working with Abilities
[19] Fix | Delete
*
[20] Fix | Delete
* Abilities must be registered on the `wp_abilities_api_init` action hook.
[21] Fix | Delete
* Attempting to register an ability outside of this hook will fail and
[22] Fix | Delete
* trigger a `_doing_it_wrong()` notice.
[23] Fix | Delete
[24] Fix | Delete
* Example:
[25] Fix | Delete
*
[26] Fix | Delete
* function my_plugin_register_abilities(): void {
[27] Fix | Delete
* wp_register_ability(
[28] Fix | Delete
* 'my-plugin/export-users',
[29] Fix | Delete
* array(
[30] Fix | Delete
* 'label' => __( 'Export Users', 'my-plugin' ),
[31] Fix | Delete
* 'description' => __( 'Exports user data to CSV format.', 'my-plugin' ),
[32] Fix | Delete
* 'category' => 'data-export',
[33] Fix | Delete
* 'execute_callback' => 'my_plugin_export_users',
[34] Fix | Delete
* 'permission_callback' => function(): bool {
[35] Fix | Delete
* return current_user_can( 'export' );
[36] Fix | Delete
* },
[37] Fix | Delete
* 'input_schema' => array(
[38] Fix | Delete
* 'type' => 'string',
[39] Fix | Delete
* 'enum' => array( 'subscriber', 'contributor', 'author', 'editor', 'administrator' ),
[40] Fix | Delete
* 'description' => __( 'Limits the export to users with this role.', 'my-plugin' ),
[41] Fix | Delete
* 'required' => false,
[42] Fix | Delete
* ),
[43] Fix | Delete
* 'output_schema' => array(
[44] Fix | Delete
* 'type' => 'string',
[45] Fix | Delete
* 'description' => __( 'User data in CSV format.', 'my-plugin' ),
[46] Fix | Delete
* 'required' => true,
[47] Fix | Delete
* ),
[48] Fix | Delete
* 'meta' => array(
[49] Fix | Delete
* 'show_in_rest' => true,
[50] Fix | Delete
* ),
[51] Fix | Delete
* )
[52] Fix | Delete
* );
[53] Fix | Delete
* }
[54] Fix | Delete
* add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' );
[55] Fix | Delete
*
[56] Fix | Delete
* Once registered, abilities can be checked, retrieved, and managed:
[57] Fix | Delete
*
[58] Fix | Delete
* // Checks if an ability is registered, and prints its label.
[59] Fix | Delete
* if ( wp_has_ability( 'my-plugin/export-users' ) ) {
[60] Fix | Delete
* $ability = wp_get_ability( 'my-plugin/export-users' );
[61] Fix | Delete
*
[62] Fix | Delete
* echo $ability->get_label();
[63] Fix | Delete
* }
[64] Fix | Delete
*
[65] Fix | Delete
* // Gets all registered abilities.
[66] Fix | Delete
* $all_abilities = wp_get_abilities();
[67] Fix | Delete
*
[68] Fix | Delete
* // Unregisters when no longer needed.
[69] Fix | Delete
* wp_unregister_ability( 'my-plugin/export-users' );
[70] Fix | Delete
*
[71] Fix | Delete
* ## Best Practices
[72] Fix | Delete
*
[73] Fix | Delete
* - Always register abilities on the `wp_abilities_api_init` hook.
[74] Fix | Delete
* - Use namespaced ability names to prevent conflicts.
[75] Fix | Delete
* - Implement robust permission checks in permission callbacks.
[76] Fix | Delete
* - Provide an `input_schema` to ensure data integrity and document expected inputs.
[77] Fix | Delete
* - Define an `output_schema` to describe return values and validate responses.
[78] Fix | Delete
* - Return `WP_Error` objects for failures rather than throwing exceptions.
[79] Fix | Delete
* - Use internationalization functions for all user-facing strings.
[80] Fix | Delete
*
[81] Fix | Delete
* @package WordPress
[82] Fix | Delete
* @subpackage Abilities_API
[83] Fix | Delete
* @since 6.9.0
[84] Fix | Delete
*/
[85] Fix | Delete
[86] Fix | Delete
declare( strict_types = 1 );
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* Registers a new ability using the Abilities API. It requires three steps:
[90] Fix | Delete
*
[91] Fix | Delete
* 1. Hook into the `wp_abilities_api_init` action.
[92] Fix | Delete
* 2. Call `wp_register_ability()` with a namespaced name and configuration.
[93] Fix | Delete
* 3. Provide execute and permission callbacks.
[94] Fix | Delete
*
[95] Fix | Delete
* Example:
[96] Fix | Delete
*
[97] Fix | Delete
* function my_plugin_register_abilities(): void {
[98] Fix | Delete
* wp_register_ability(
[99] Fix | Delete
* 'my-plugin/analyze-text',
[100] Fix | Delete
* array(
[101] Fix | Delete
* 'label' => __( 'Analyze Text', 'my-plugin' ),
[102] Fix | Delete
* 'description' => __( 'Performs sentiment analysis on provided text.', 'my-plugin' ),
[103] Fix | Delete
* 'category' => 'text-processing',
[104] Fix | Delete
* 'input_schema' => array(
[105] Fix | Delete
* 'type' => 'string',
[106] Fix | Delete
* 'description' => __( 'The text to be analyzed.', 'my-plugin' ),
[107] Fix | Delete
* 'minLength' => 10,
[108] Fix | Delete
* 'required' => true,
[109] Fix | Delete
* ),
[110] Fix | Delete
* 'output_schema' => array(
[111] Fix | Delete
* 'type' => 'string',
[112] Fix | Delete
* 'enum' => array( 'positive', 'negative', 'neutral' ),
[113] Fix | Delete
* 'description' => __( 'The sentiment result: positive, negative, or neutral.', 'my-plugin' ),
[114] Fix | Delete
* 'required' => true,
[115] Fix | Delete
* ),
[116] Fix | Delete
* 'execute_callback' => 'my_plugin_analyze_text',
[117] Fix | Delete
* 'permission_callback' => 'my_plugin_can_analyze_text',
[118] Fix | Delete
* 'meta' => array(
[119] Fix | Delete
* 'annotations' => array(
[120] Fix | Delete
* 'readonly' => true,
[121] Fix | Delete
* ),
[122] Fix | Delete
* 'show_in_rest' => true,
[123] Fix | Delete
* ),
[124] Fix | Delete
* )
[125] Fix | Delete
* );
[126] Fix | Delete
* }
[127] Fix | Delete
* add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' );
[128] Fix | Delete
*
[129] Fix | Delete
* ### Naming Conventions
[130] Fix | Delete
*
[131] Fix | Delete
* Ability names must follow these rules:
[132] Fix | Delete
*
[133] Fix | Delete
* - Include a namespace prefix (e.g., `my-plugin/my-ability`).
[134] Fix | Delete
* - Use only lowercase alphanumeric characters, dashes, and forward slashes.
[135] Fix | Delete
* - Use descriptive, action-oriented names (e.g., `process-payment`, `generate-report`).
[136] Fix | Delete
*
[137] Fix | Delete
* ### Categories
[138] Fix | Delete
*
[139] Fix | Delete
* Abilities must be organized into categories. Ability categories provide better
[140] Fix | Delete
* discoverability and must be registered before the abilities that reference them:
[141] Fix | Delete
*
[142] Fix | Delete
* function my_plugin_register_categories(): void {
[143] Fix | Delete
* wp_register_ability_category(
[144] Fix | Delete
* 'text-processing',
[145] Fix | Delete
* array(
[146] Fix | Delete
* 'label' => __( 'Text Processing', 'my-plugin' ),
[147] Fix | Delete
* 'description' => __( 'Abilities for analyzing and transforming text.', 'my-plugin' ),
[148] Fix | Delete
* )
[149] Fix | Delete
* );
[150] Fix | Delete
* }
[151] Fix | Delete
* add_action( 'wp_abilities_api_categories_init', 'my_plugin_register_categories' );
[152] Fix | Delete
*
[153] Fix | Delete
* ### Input and Output Schemas
[154] Fix | Delete
*
[155] Fix | Delete
* Schemas define the expected structure, type, and constraints for ability inputs
[156] Fix | Delete
* and outputs using JSON Schema syntax. They serve two critical purposes: automatic
[157] Fix | Delete
* validation of data passed to and returned from abilities, and self-documenting
[158] Fix | Delete
* API contracts for developers.
[159] Fix | Delete
*
[160] Fix | Delete
* WordPress implements a validator based on a subset of the JSON Schema Version 4
[161] Fix | Delete
* specification (https://json-schema.org/specification-links.html#draft-4).
[162] Fix | Delete
* For details on supported JSON Schema properties and syntax, see the
[163] Fix | Delete
* related WordPress REST API Schema documentation:
[164] Fix | Delete
* https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#json-schema-basics
[165] Fix | Delete
*
[166] Fix | Delete
* Defining schemas is mandatory when there is a value to pass or return.
[167] Fix | Delete
* They ensure data integrity, improve developer experience, and enable
[168] Fix | Delete
* better documentation:
[169] Fix | Delete
*
[170] Fix | Delete
* 'input_schema' => array(
[171] Fix | Delete
* 'type' => 'string',
[172] Fix | Delete
* 'description' => __( 'The text to be analyzed.', 'my-plugin' ),
[173] Fix | Delete
* 'minLength' => 10,
[174] Fix | Delete
* 'required' => true,
[175] Fix | Delete
* ),
[176] Fix | Delete
* 'output_schema' => array(
[177] Fix | Delete
* 'type' => 'string',
[178] Fix | Delete
* 'enum' => array( 'positive', 'negative', 'neutral' ),
[179] Fix | Delete
* 'description' => __( 'The sentiment result: positive, negative, or neutral.', 'my-plugin' ),
[180] Fix | Delete
* 'required' => true,
[181] Fix | Delete
* ),
[182] Fix | Delete
*
[183] Fix | Delete
* ### Callbacks
[184] Fix | Delete
*
[185] Fix | Delete
* #### Execute Callback
[186] Fix | Delete
*
[187] Fix | Delete
* The execute callback performs the ability's core functionality. It receives
[188] Fix | Delete
* optional input data and returns either a result or `WP_Error` on failure.
[189] Fix | Delete
*
[190] Fix | Delete
* function my_plugin_analyze_text( string $input ): string|WP_Error {
[191] Fix | Delete
* $score = My_Plugin::perform_sentiment_analysis( $input );
[192] Fix | Delete
* if ( is_wp_error( $score ) ) {
[193] Fix | Delete
* return $score;
[194] Fix | Delete
* }
[195] Fix | Delete
* return My_Plugin::interpret_sentiment_score( $score );
[196] Fix | Delete
* }
[197] Fix | Delete
*
[198] Fix | Delete
* #### Permission Callback
[199] Fix | Delete
*
[200] Fix | Delete
* The permission callback determines whether the ability can be executed.
[201] Fix | Delete
* It receives the same input as the execute callback and must return a
[202] Fix | Delete
* boolean or `WP_Error`. Common use cases include checking user capabilities,
[203] Fix | Delete
* validating API keys, or verifying system state:
[204] Fix | Delete
*
[205] Fix | Delete
* function my_plugin_can_analyze_text( string $input ): bool|WP_Error {
[206] Fix | Delete
* return current_user_can( 'edit_posts' );
[207] Fix | Delete
* }
[208] Fix | Delete
*
[209] Fix | Delete
* ### REST API Integration
[210] Fix | Delete
*
[211] Fix | Delete
* Abilities can be exposed through the REST API by setting `show_in_rest`
[212] Fix | Delete
* to `true` in the meta configuration:
[213] Fix | Delete
*
[214] Fix | Delete
* 'meta' => array(
[215] Fix | Delete
* 'show_in_rest' => true,
[216] Fix | Delete
* ),
[217] Fix | Delete
*
[218] Fix | Delete
* This allows abilities to be invoked via HTTP requests to the WordPress REST API.
[219] Fix | Delete
*
[220] Fix | Delete
* @since 6.9.0
[221] Fix | Delete
*
[222] Fix | Delete
* @see WP_Abilities_Registry::register()
[223] Fix | Delete
* @see wp_register_ability_category()
[224] Fix | Delete
* @see wp_unregister_ability()
[225] Fix | Delete
*
[226] Fix | Delete
* @param string $name The name of the ability. Must be a namespaced string containing
[227] Fix | Delete
* a prefix, e.g., `my-plugin/my-ability`. Can only contain lowercase
[228] Fix | Delete
* alphanumeric characters, dashes, and forward slashes.
[229] Fix | Delete
* @param array<string, mixed> $args {
[230] Fix | Delete
* An associative array of arguments for configuring the ability.
[231] Fix | Delete
*
[232] Fix | Delete
* @type string $label Required. The human-readable label for the ability.
[233] Fix | Delete
* @type string $description Required. A detailed description of what the ability does
[234] Fix | Delete
* and when it should be used.
[235] Fix | Delete
* @type string $category Required. The ability category slug this ability belongs to.
[236] Fix | Delete
* The ability category must be registered via `wp_register_ability_category()`
[237] Fix | Delete
* before registering the ability.
[238] Fix | Delete
* @type callable $execute_callback Required. A callback function to execute when the ability is invoked.
[239] Fix | Delete
* Receives optional mixed input data and must return either a result
[240] Fix | Delete
* value (any type) or a `WP_Error` object on failure.
[241] Fix | Delete
* @type callable $permission_callback Required. A callback function to check permissions before execution.
[242] Fix | Delete
* Receives optional mixed input data (same as `execute_callback`) and
[243] Fix | Delete
* must return `true`/`false` for simple checks, or `WP_Error` for
[244] Fix | Delete
* detailed error responses.
[245] Fix | Delete
* @type array<string, mixed> $input_schema Optional. JSON Schema definition for validating the ability's input.
[246] Fix | Delete
* Must be a valid JSON Schema object defining the structure and
[247] Fix | Delete
* constraints for input data. Used for automatic validation and
[248] Fix | Delete
* API documentation.
[249] Fix | Delete
* @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output.
[250] Fix | Delete
* Describes the structure of successful return values from
[251] Fix | Delete
* `execute_callback`. Used for documentation and validation.
[252] Fix | Delete
* @type array<string, mixed> $meta {
[253] Fix | Delete
* Optional. Additional metadata for the ability.
[254] Fix | Delete
*
[255] Fix | Delete
* @type array<string, bool|null> $annotations {
[256] Fix | Delete
* Optional. Semantic annotations describing the ability's behavioral characteristics.
[257] Fix | Delete
* These annotations are hints for tooling and documentation.
[258] Fix | Delete
*
[259] Fix | Delete
* @type bool|null $readonly Optional. If true, the ability does not modify its environment.
[260] Fix | Delete
* @type bool|null $destructive Optional. If true, the ability may perform destructive updates to its environment.
[261] Fix | Delete
* If false, the ability performs only additive updates.
[262] Fix | Delete
* @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments
[263] Fix | Delete
* will have no additional effect on its environment.
[264] Fix | Delete
* }
[265] Fix | Delete
* @type bool $show_in_rest Optional. Whether to expose this ability in the REST API.
[266] Fix | Delete
* When true, the ability can be invoked via HTTP requests.
[267] Fix | Delete
* Default false.
[268] Fix | Delete
* }
[269] Fix | Delete
* @type string $ability_class Optional. Fully-qualified custom class name to instantiate
[270] Fix | Delete
* instead of the default `WP_Ability` class. The custom class
[271] Fix | Delete
* must extend `WP_Ability`. Useful for advanced customization
[272] Fix | Delete
* of ability behavior.
[273] Fix | Delete
* }
[274] Fix | Delete
* @return WP_Ability|null The registered ability instance on success, `null` on failure.
[275] Fix | Delete
*/
[276] Fix | Delete
function wp_register_ability( string $name, array $args ): ?WP_Ability {
[277] Fix | Delete
if ( ! doing_action( 'wp_abilities_api_init' ) ) {
[278] Fix | Delete
_doing_it_wrong(
[279] Fix | Delete
__FUNCTION__,
[280] Fix | Delete
sprintf(
[281] Fix | Delete
/* translators: 1: wp_abilities_api_init, 2: string value of the ability name. */
[282] Fix | Delete
__( 'Abilities must be registered on the %1$s action. The ability %2$s was not registered.' ),
[283] Fix | Delete
'<code>wp_abilities_api_init</code>',
[284] Fix | Delete
'<code>' . esc_html( $name ) . '</code>'
[285] Fix | Delete
),
[286] Fix | Delete
'6.9.0'
[287] Fix | Delete
);
[288] Fix | Delete
return null;
[289] Fix | Delete
}
[290] Fix | Delete
[291] Fix | Delete
$registry = WP_Abilities_Registry::get_instance();
[292] Fix | Delete
if ( null === $registry ) {
[293] Fix | Delete
return null;
[294] Fix | Delete
}
[295] Fix | Delete
[296] Fix | Delete
return $registry->register( $name, $args );
[297] Fix | Delete
}
[298] Fix | Delete
[299] Fix | Delete
/**
[300] Fix | Delete
* Unregisters an ability from the Abilities API.
[301] Fix | Delete
*
[302] Fix | Delete
* Removes a previously registered ability from the global registry. Use this to
[303] Fix | Delete
* disable abilities provided by other plugins or when an ability is no longer needed.
[304] Fix | Delete
*
[305] Fix | Delete
* Can be called at any time after the ability has been registered.
[306] Fix | Delete
*
[307] Fix | Delete
* Example:
[308] Fix | Delete
*
[309] Fix | Delete
* if ( wp_has_ability( 'other-plugin/some-ability' ) ) {
[310] Fix | Delete
* wp_unregister_ability( 'other-plugin/some-ability' );
[311] Fix | Delete
* }
[312] Fix | Delete
*
[313] Fix | Delete
* @since 6.9.0
[314] Fix | Delete
*
[315] Fix | Delete
* @see WP_Abilities_Registry::unregister()
[316] Fix | Delete
* @see wp_register_ability()
[317] Fix | Delete
*
[318] Fix | Delete
* @param string $name The name of the ability to unregister, including namespace prefix
[319] Fix | Delete
* (e.g., 'my-plugin/my-ability').
[320] Fix | Delete
* @return WP_Ability|null The unregistered ability instance on success, `null` on failure.
[321] Fix | Delete
*/
[322] Fix | Delete
function wp_unregister_ability( string $name ): ?WP_Ability {
[323] Fix | Delete
$registry = WP_Abilities_Registry::get_instance();
[324] Fix | Delete
if ( null === $registry ) {
[325] Fix | Delete
return null;
[326] Fix | Delete
}
[327] Fix | Delete
[328] Fix | Delete
return $registry->unregister( $name );
[329] Fix | Delete
}
[330] Fix | Delete
[331] Fix | Delete
/**
[332] Fix | Delete
* Checks if an ability is registered.
[333] Fix | Delete
*
[334] Fix | Delete
* Use this for conditional logic and feature detection before attempting to
[335] Fix | Delete
* retrieve or use an ability.
[336] Fix | Delete
*
[337] Fix | Delete
* Example:
[338] Fix | Delete
*
[339] Fix | Delete
* // Displays different UI based on available abilities.
[340] Fix | Delete
* if ( wp_has_ability( 'premium-plugin/advanced-export' ) ) {
[341] Fix | Delete
* echo 'Export with Premium Features';
[342] Fix | Delete
* } else {
[343] Fix | Delete
* echo 'Basic Export';
[344] Fix | Delete
* }
[345] Fix | Delete
*
[346] Fix | Delete
* @since 6.9.0
[347] Fix | Delete
*
[348] Fix | Delete
* @see WP_Abilities_Registry::is_registered()
[349] Fix | Delete
* @see wp_get_ability()
[350] Fix | Delete
*
[351] Fix | Delete
* @param string $name The name of the ability to check, including namespace prefix
[352] Fix | Delete
* (e.g., 'my-plugin/my-ability').
[353] Fix | Delete
* @return bool `true` if the ability is registered, `false` otherwise.
[354] Fix | Delete
*/
[355] Fix | Delete
function wp_has_ability( string $name ): bool {
[356] Fix | Delete
$registry = WP_Abilities_Registry::get_instance();
[357] Fix | Delete
if ( null === $registry ) {
[358] Fix | Delete
return false;
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
return $registry->is_registered( $name );
[362] Fix | Delete
}
[363] Fix | Delete
[364] Fix | Delete
/**
[365] Fix | Delete
* Retrieves a registered ability.
[366] Fix | Delete
*
[367] Fix | Delete
* Returns the ability instance for inspection or use. The instance provides access
[368] Fix | Delete
* to the ability's configuration, metadata, and execution methods.
[369] Fix | Delete
*
[370] Fix | Delete
* Example:
[371] Fix | Delete
*
[372] Fix | Delete
* // Prints information about a registered ability.
[373] Fix | Delete
* $ability = wp_get_ability( 'my-plugin/export-data' );
[374] Fix | Delete
* if ( $ability ) {
[375] Fix | Delete
* echo $ability->get_label() . ': ' . $ability->get_description();
[376] Fix | Delete
* }
[377] Fix | Delete
*
[378] Fix | Delete
* @since 6.9.0
[379] Fix | Delete
*
[380] Fix | Delete
* @see WP_Abilities_Registry::get_registered()
[381] Fix | Delete
* @see wp_has_ability()
[382] Fix | Delete
*
[383] Fix | Delete
* @param string $name The name of the ability, including namespace prefix
[384] Fix | Delete
* (e.g., 'my-plugin/my-ability').
[385] Fix | Delete
* @return WP_Ability|null The registered ability instance, or `null` if not registered.
[386] Fix | Delete
*/
[387] Fix | Delete
function wp_get_ability( string $name ): ?WP_Ability {
[388] Fix | Delete
$registry = WP_Abilities_Registry::get_instance();
[389] Fix | Delete
if ( null === $registry ) {
[390] Fix | Delete
return null;
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
return $registry->get_registered( $name );
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
/**
[397] Fix | Delete
* Retrieves all registered abilities.
[398] Fix | Delete
*
[399] Fix | Delete
* Returns an array of all ability instances currently registered in the system.
[400] Fix | Delete
* Use this for discovery, debugging, or building administrative interfaces.
[401] Fix | Delete
*
[402] Fix | Delete
* Example:
[403] Fix | Delete
*
[404] Fix | Delete
* // Prints information about all available abilities.
[405] Fix | Delete
* $abilities = wp_get_abilities();
[406] Fix | Delete
* foreach ( $abilities as $ability ) {
[407] Fix | Delete
* echo $ability->get_label() . ': ' . $ability->get_description() . "\n";
[408] Fix | Delete
* }
[409] Fix | Delete
*
[410] Fix | Delete
* @since 6.9.0
[411] Fix | Delete
*
[412] Fix | Delete
* @see WP_Abilities_Registry::get_all_registered()
[413] Fix | Delete
*
[414] Fix | Delete
* @return WP_Ability[] An array of registered WP_Ability instances. Returns an empty
[415] Fix | Delete
* array if no abilities are registered or if the registry is unavailable.
[416] Fix | Delete
*/
[417] Fix | Delete
function wp_get_abilities(): array {
[418] Fix | Delete
$registry = WP_Abilities_Registry::get_instance();
[419] Fix | Delete
if ( null === $registry ) {
[420] Fix | Delete
return array();
[421] Fix | Delete
}
[422] Fix | Delete
[423] Fix | Delete
return $registry->get_all_registered();
[424] Fix | Delete
}
[425] Fix | Delete
[426] Fix | Delete
/**
[427] Fix | Delete
* Registers a new ability category.
[428] Fix | Delete
*
[429] Fix | Delete
* Ability categories provide a way to organize and group related abilities for better
[430] Fix | Delete
* discoverability and management. Ability categories must be registered before abilities
[431] Fix | Delete
* that reference them.
[432] Fix | Delete
*
[433] Fix | Delete
* Ability categories must be registered on the `wp_abilities_api_categories_init` action hook.
[434] Fix | Delete
*
[435] Fix | Delete
* Example:
[436] Fix | Delete
*
[437] Fix | Delete
* function my_plugin_register_categories() {
[438] Fix | Delete
* wp_register_ability_category(
[439] Fix | Delete
* 'content-management',
[440] Fix | Delete
* array(
[441] Fix | Delete
* 'label' => __( 'Content Management', 'my-plugin' ),
[442] Fix | Delete
* 'description' => __( 'Abilities for managing and organizing content.', 'my-plugin' ),
[443] Fix | Delete
* )
[444] Fix | Delete
* );
[445] Fix | Delete
* }
[446] Fix | Delete
* add_action( 'wp_abilities_api_categories_init', 'my_plugin_register_categories' );
[447] Fix | Delete
*
[448] Fix | Delete
* @since 6.9.0
[449] Fix | Delete
*
[450] Fix | Delete
* @see WP_Ability_Categories_Registry::register()
[451] Fix | Delete
* @see wp_register_ability()
[452] Fix | Delete
* @see wp_unregister_ability_category()
[453] Fix | Delete
*
[454] Fix | Delete
* @param string $slug The unique slug for the ability category. Must contain only lowercase
[455] Fix | Delete
* alphanumeric characters and dashes (e.g., 'data-export').
[456] Fix | Delete
* @param array<string, mixed> $args {
[457] Fix | Delete
* An associative array of arguments for the ability category.
[458] Fix | Delete
*
[459] Fix | Delete
* @type string $label Required. The human-readable label for the ability category.
[460] Fix | Delete
* @type string $description Required. A description of what abilities in this category do.
[461] Fix | Delete
* @type array<string, mixed> $meta Optional. Additional metadata for the ability category.
[462] Fix | Delete
* }
[463] Fix | Delete
* @return WP_Ability_Category|null The registered ability category instance on success, `null` on failure.
[464] Fix | Delete
*/
[465] Fix | Delete
function wp_register_ability_category( string $slug, array $args ): ?WP_Ability_Category {
[466] Fix | Delete
if ( ! doing_action( 'wp_abilities_api_categories_init' ) ) {
[467] Fix | Delete
_doing_it_wrong(
[468] Fix | Delete
__FUNCTION__,
[469] Fix | Delete
sprintf(
[470] Fix | Delete
/* translators: 1: wp_abilities_api_categories_init, 2: ability category slug. */
[471] Fix | Delete
__( 'Ability categories must be registered on the %1$s action. The ability category %2$s was not registered.' ),
[472] Fix | Delete
'<code>wp_abilities_api_categories_init</code>',
[473] Fix | Delete
'<code>' . esc_html( $slug ) . '</code>'
[474] Fix | Delete
),
[475] Fix | Delete
'6.9.0'
[476] Fix | Delete
);
[477] Fix | Delete
return null;
[478] Fix | Delete
}
[479] Fix | Delete
[480] Fix | Delete
$registry = WP_Ability_Categories_Registry::get_instance();
[481] Fix | Delete
if ( null === $registry ) {
[482] Fix | Delete
return null;
[483] Fix | Delete
}
[484] Fix | Delete
[485] Fix | Delete
return $registry->register( $slug, $args );
[486] Fix | Delete
}
[487] Fix | Delete
[488] Fix | Delete
/**
[489] Fix | Delete
* Unregisters an ability category.
[490] Fix | Delete
*
[491] Fix | Delete
* Removes a previously registered ability category from the global registry. Use this to
[492] Fix | Delete
* disable ability categories that are no longer needed.
[493] Fix | Delete
*
[494] Fix | Delete
* Can be called at any time after the ability category has been registered.
[495] Fix | Delete
*
[496] Fix | Delete
* Example:
[497] Fix | Delete
*
[498] Fix | Delete
* if ( wp_has_ability_category( 'deprecated-category' ) ) {
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function