| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Abilities API 4 * 5 * Defines WP_Abilities_Registry class. 6 * 7 * @package WordPress 8 * @subpackage Abilities API 9 * @since 6.9.0 10 */ 11 12 declare( strict_types = 1 ); 13 14 /** 15 * Manages the registration and lookup of abilities. 16 * 17 * @since 6.9.0 18 * @access private 19 */ 20 final class WP_Abilities_Registry { 21 /** 22 * The singleton instance of the registry. 23 * 24 * @since 6.9.0 25 * @var self|null 26 */ 27 private static $instance = null; 28 29 /** 30 * Holds the registered abilities. 31 * 32 * @since 6.9.0 33 * @var WP_Ability[] 34 */ 35 private $registered_abilities = array(); 36 37 /** 38 * Registers a new ability. 39 * 40 * Do not use this method directly. Instead, use the `wp_register_ability()` function. 41 * 42 * @since 6.9.0 43 * @since 7.1.0 Added the `public` meta argument. 44 * @since 7.2.0 Added the `required_capabilities` argument. 45 * 46 * @see wp_register_ability() 47 * 48 * @param string $name The name of the ability. The name must be a string containing a namespace 49 * prefix, i.e. `my-plugin/my-ability`. It can only contain lowercase 50 * alphanumeric characters, dashes and the forward slash. 51 * @param array<string, mixed> $args { 52 * An associative array of arguments for the ability. 53 * 54 * @type string $label The human-readable label for the ability. 55 * @type string $description A detailed description of what the ability does. 56 * @type string $category The registered ability category slug this ability belongs to. 57 * @type callable $execute_callback A callback function to execute when the ability is invoked. 58 * Receives optional mixed input and returns mixed result or WP_Error. 59 * @type callable $permission_callback A callback function to check permissions before execution. 60 * Receives optional mixed input and returns bool or WP_Error. 61 * Required unless `$required_capabilities` lists at least one capability. 62 * @type list<string> $required_capabilities Optional. Minimum capabilities required for every execution of the 63 * ability, regardless of input. They are checked before 64 * `$permission_callback`, which handles input-dependent requirements. 65 * @type array<string, mixed> $input_schema Optional. JSON Schema definition for the ability's input. 66 * @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output. 67 * @type array<string, mixed> $meta { 68 * Optional. Additional metadata for the ability. 69 * 70 * @type array<string, bool|null> $annotations { 71 * Optional. Semantic annotations describing the ability's behavioral characteristics. 72 * These annotations are hints for tooling and documentation. 73 * 74 * @type bool|null $readonly Optional. If true, the ability does not modify its environment. 75 * @type bool|null $destructive Optional. If true, the ability may perform destructive updates to its environment. 76 * If false, the ability performs only additive updates. 77 * @type bool|null $idempotent Optional. If true, calling the ability repeatedly with the same arguments 78 * will have no additional effect on its environment. 79 * } 80 * @type bool $public Optional. Whether the ability is meant to be available 81 * to clients such as the REST API, MCP, or AI agents. 82 * Seeds the default for per-channel flags like 83 * `$show_in_rest`. Defaults to false. 84 * @type bool $show_in_rest Optional. Whether to expose this ability in the REST API. 85 * Default is the value of `$public` when set, false otherwise. 86 * } 87 * @type string $ability_class Optional. Custom class to instantiate instead of WP_Ability. 88 * } 89 * @return WP_Ability|null The registered ability instance on success, null on failure. 90 * 91 * @phpstan-param lowercase-string&non-falsy-string $name 92 */ 93 public function register( string $name, array $args ): ?WP_Ability { 94 if ( ! preg_match( '/^[a-z0-9-]+\/[a-z0-9-]+$/', $name ) ) { 95 _doing_it_wrong( 96 __METHOD__, 97 __( 98 'Ability name must be a string containing a namespace prefix, i.e. "my-plugin/my-ability". It can only contain lowercase alphanumeric characters, dashes and the forward slash.' 99 ), 100 '6.9.0' 101 ); 102 return null; 103 } 104 105 if ( $this->is_registered( $name ) ) { 106 _doing_it_wrong( 107 __METHOD__, 108 /* translators: %s: Ability name. */ 109 sprintf( __( 'Ability "%s" is already registered.' ), esc_html( $name ) ), 110 '6.9.0' 111 ); 112 return null; 113 } 114 115 /** 116 * Filters the ability arguments before they are validated and used to instantiate the ability. 117 * 118 * @since 6.9.0 119 * @since 7.1.0 Added the `public` meta argument. 120 * @since 7.2.0 Added the `required_capabilities` argument. 121 * 122 * @param array<string, mixed> $args { 123 * An associative array of arguments for the ability. 124 * 125 * @type string $label The human-readable label for the ability. 126 * @type string $description A detailed description of what the ability does. 127 * @type string $category The registered ability category slug this ability belongs to. 128 * @type callable $execute_callback A callback function to execute when the ability is invoked. 129 * Receives optional mixed input and returns mixed result or WP_Error. 130 * @type callable $permission_callback A callback function to check permissions before execution. 131 * Receives optional mixed input and returns bool or WP_Error. 132 * Required unless `$required_capabilities` lists at least one capability. 133 * @type list<string> $required_capabilities Optional. Minimum capabilities required for every execution of the 134 * ability, regardless of input. They are checked before 135 * `$permission_callback`, which handles input-dependent requirements. 136 * @type array<string, mixed> $input_schema Optional. JSON Schema definition for the ability's input. 137 * @type array<string, mixed> $output_schema Optional. JSON Schema definition for the ability's output. 138 * @type array<string, mixed> $meta { 139 * Optional. Additional metadata for the ability. 140 * 141 * @type array<string, bool|string> $annotations Optional. Annotation metadata for the ability. 142 * @type bool $public Optional. Whether the ability is meant to be 143 * available to clients such as the REST API, MCP, or AI 144 * agents. Seeds the default for per-channel flags like 145 * `$show_in_rest`. Defaults to false. 146 * @type bool $show_in_rest Optional. Whether to expose this ability in the REST API. 147 * Default is the value of `$public` when set, false otherwise. 148 * } 149 * @type string $ability_class Optional. Custom class to instantiate instead of WP_Ability. 150 * } 151 * @param string $name The name of the ability, with its namespace. 152 */ 153 $args = apply_filters( 'wp_register_ability_args', $args, $name ); 154 155 if ( ! isset( $args['category'] ) || ! is_string( $args['category'] ) ) { 156 _doing_it_wrong( 157 __METHOD__, 158 __( 'The ability properties must contain a `category` string.' ), 159 '6.9.0' 160 ); 161 return null; 162 } 163 164 if ( ! wp_has_ability_category( $args['category'] ) ) { 165 _doing_it_wrong( 166 __METHOD__, 167 sprintf( 168 /* translators: %1$s: ability category slug, %2$s: ability name */ 169 __( 'Ability category "%1$s" is not registered. Please register the ability category before assigning it to ability "%2$s".' ), 170 esc_html( $args['category'] ), 171 esc_html( $name ) 172 ), 173 '6.9.0' 174 ); 175 return null; 176 } 177 178 // The class is only used to instantiate the ability, and is not a property of the ability itself. 179 if ( isset( $args['ability_class'] ) && ! is_a( $args['ability_class'], WP_Ability::class, true ) ) { 180 _doing_it_wrong( 181 __METHOD__, 182 __( 'The ability args should provide a valid `ability_class` that extends WP_Ability.' ), 183 '6.9.0' 184 ); 185 return null; 186 } 187 188 /** @var class-string<WP_Ability> */ 189 $ability_class = $args['ability_class'] ?? WP_Ability::class; 190 unset( $args['ability_class'] ); 191 192 try { 193 // WP_Ability::prepare_properties() will throw an exception if the properties are invalid. 194 $ability = new $ability_class( $name, $args ); 195 } catch ( InvalidArgumentException $e ) { 196 _doing_it_wrong( 197 __METHOD__, 198 $e->getMessage(), 199 '6.9.0' 200 ); 201 return null; 202 } 203 204 $this->registered_abilities[ $name ] = $ability; 205 return $ability; 206 } 207 208 /** 209 * Unregisters an ability. 210 * 211 * Do not use this method directly. Instead, use the `wp_unregister_ability()` function. 212 * 213 * @since 6.9.0 214 * 215 * @see wp_unregister_ability() 216 * 217 * @param string $name The name of the registered ability, with its namespace. 218 * @return WP_Ability|null The unregistered ability instance on success, null on failure. 219 */ 220 public function unregister( string $name ): ?WP_Ability { 221 if ( ! $this->is_registered( $name ) ) { 222 _doing_it_wrong( 223 __METHOD__, 224 /* translators: %s: Ability name. */ 225 sprintf( __( 'Ability "%s" not found.' ), esc_html( $name ) ), 226 '6.9.0' 227 ); 228 return null; 229 } 230 231 $unregistered_ability = $this->registered_abilities[ $name ]; 232 unset( $this->registered_abilities[ $name ] ); 233 234 return $unregistered_ability; 235 } 236 237 /** 238 * Retrieves the list of all registered abilities. 239 * 240 * Do not use this method directly. Instead, use the `wp_get_abilities()` function. 241 * 242 * @since 6.9.0 243 * 244 * @see wp_get_abilities() 245 * 246 * @return WP_Ability[] The array of registered abilities. 247 */ 248 public function get_all_registered(): array { 249 return $this->registered_abilities; 250 } 251 252 /** 253 * Checks if an ability is registered. 254 * 255 * Do not use this method directly. Instead, use the `wp_has_ability()` function. 256 * 257 * @since 6.9.0 258 * 259 * @see wp_has_ability() 260 * 261 * @param string $name The name of the registered ability, with its namespace. 262 * @return bool True if the ability is registered, false otherwise. 263 */ 264 public function is_registered( string $name ): bool { 265 return isset( $this->registered_abilities[ $name ] ); 266 } 267 268 /** 269 * Retrieves a registered ability. 270 * 271 * Do not use this method directly. Instead, use the `wp_get_ability()` function. 272 * 273 * @since 6.9.0 274 * 275 * @see wp_get_ability() 276 * 277 * @param string $name The name of the registered ability, with its namespace. 278 * @return WP_Ability|null The registered ability instance, or null if it is not registered. 279 */ 280 public function get_registered( string $name ): ?WP_Ability { 281 if ( ! $this->is_registered( $name ) ) { 282 _doing_it_wrong( 283 __METHOD__, 284 /* translators: %s: Ability name. */ 285 sprintf( __( 'Ability "%s" not found.' ), esc_html( $name ) ), 286 '6.9.0' 287 ); 288 return null; 289 } 290 return $this->registered_abilities[ $name ]; 291 } 292 293 /** 294 * Utility method to retrieve the main instance of the registry class. 295 * 296 * The instance will be created if it does not exist yet. 297 * 298 * @since 6.9.0 299 * 300 * @return WP_Abilities_Registry|null The main registry instance, or null when `init` action has not fired. 301 */ 302 public static function get_instance(): ?self { 303 if ( ! did_action( 'init' ) ) { 304 _doing_it_wrong( 305 __METHOD__, 306 sprintf( 307 // translators: %s: init action. 308 __( 'Ability API should not be initialized before the %s action has fired.' ), 309 '<code>init</code>' 310 ), 311 '6.9.0' 312 ); 313 return null; 314 } 315 316 if ( null === self::$instance ) { 317 self::$instance = new self(); 318 319 // Ensure ability category registry is initialized first to allow categories to be registered 320 // before abilities that depend on them. 321 WP_Ability_Categories_Registry::get_instance(); 322 323 /** 324 * Fires when preparing abilities registry. 325 * 326 * Abilities should be created and register their hooks on this action rather 327 * than another action to ensure they're only loaded when needed. 328 * 329 * @since 6.9.0 330 * 331 * @param WP_Abilities_Registry $instance Abilities registry object. 332 */ 333 do_action( 'wp_abilities_api_init', self::$instance ); 334 } 335 336 return self::$instance; 337 } 338 339 /** 340 * Wakeup magic method. 341 * 342 * @since 6.9.0 343 * @throws LogicException If the registry object is unserialized. 344 * This is a security hardening measure to prevent unserialization of the registry. 345 */ 346 public function __wakeup(): void { 347 throw new LogicException( __CLASS__ . ' should never be unserialized.' ); 348 } 349 350 /** 351 * Sleep magic method. 352 * 353 * @since 6.9.0 354 * @throws LogicException If the registry object is serialized. 355 * This is a security hardening measure to prevent serialization of the registry. 356 */ 357 public function __sleep(): array { 358 throw new LogicException( __CLASS__ . ' should never be serialized.' ); 359 } 360 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Nov 24 08:20:01 2026 | Cross-referenced by PHPXref |