Definitions awaiting native registration. */ private $pending = []; /** @var array Names reserved by this instance, including failures. */ private $names = []; /** * Queue a definition without invoking WordPress or adding hooks. * * Both schemas are required by AO, even for abilities with no input/output * constraints (use an empty array). Metadata is passed through unchanged. * * @param mixed $name Ability name, in namespace/ability form. * @param mixed $configuration Native wp_register_ability() arguments. * @return true|\WP_Error True when queued; otherwise a validation error. */ public function register( $name, $configuration ) { if ( ! is_string( $name ) || ! preg_match( '/^[a-z0-9-]+\/[a-z0-9-]+$/', $name ) ) { return new \WP_Error( 'ao_ability_invalid_name', 'Ability names must use lowercase namespace/ability form.' ); } if ( isset( $this->names[ $name ] ) ) { return new \WP_Error( 'ao_ability_duplicate', 'This ability name has already been submitted.', $name ); } if ( ! is_array( $configuration ) ) { return new \WP_Error( 'ao_ability_invalid_configuration', 'Ability configuration must be an array.' ); } foreach ( [ 'label', 'description', 'category' ] as $key ) { if ( ! isset( $configuration[ $key ] ) || ! is_string( $configuration[ $key ] ) || '' === trim( $configuration[ $key ] ) ) { return new \WP_Error( 'ao_ability_invalid_configuration', 'A nonempty string is required for ' . $key . '.' ); } } if ( ! preg_match( '/^[a-z0-9-]+$/', $configuration['category'] ) ) { return new \WP_Error( 'ao_ability_invalid_configuration', 'The ability category must be a lowercase slug.' ); } foreach ( [ 'execute_callback', 'permission_callback' ] as $key ) { if ( ! isset( $configuration[ $key ] ) || ! is_callable( $configuration[ $key ] ) ) { return new \WP_Error( 'ao_ability_invalid_configuration', 'A callable is required for ' . $key . '.' ); } } foreach ( [ 'input_schema', 'output_schema' ] as $key ) { if ( ! isset( $configuration[ $key ] ) || ! is_array( $configuration[ $key ] ) ) { return new \WP_Error( 'ao_ability_invalid_configuration', 'A schema array is required for ' . $key . '.' ); } } if ( array_key_exists( 'meta', $configuration ) ) { if ( ! is_array( $configuration['meta'] ) ) { return new \WP_Error( 'ao_ability_invalid_configuration', 'Ability metadata must be an array.' ); } if ( array_key_exists( 'annotations', $configuration['meta'] ) ) { $annotations = $configuration['meta']['annotations']; if ( ! is_array( $annotations ) ) { return new \WP_Error( 'ao_ability_invalid_configuration', 'Ability annotations must be an array.' ); } foreach ( $annotations as $key => $value ) { if ( ! in_array( $key, [ 'readonly', 'destructive', 'idempotent' ], true ) || ( null !== $value && ! is_bool( $value ) ) ) { return new \WP_Error( 'ao_ability_invalid_configuration', 'Use native boolean or null ability annotations.' ); } } } } $this->names[ $name ] = true; $this->pending[ $name ] = $configuration; return true; } /** @return bool Whether the native API is available. */ public function is_available(): bool { return function_exists( 'wp_register_ability' ); } /** * Flush during wp_abilities_api_init only. Unavailable API or wrong lifecycle * leaves the queue intact. Each attempted definition is consumed, including * failures, so repeated or reentrant calls never process an ability twice. * Native validation (including category existence) remains authoritative. * * @return array|\WP_Error Results by name, * or a precondition error when no registrations were attempted. */ public function register_pending() { if ( ! $this->is_available() ) { return new \WP_Error( 'ao_abilities_unavailable', 'The native WordPress Abilities API is unavailable.' ); } if ( ! function_exists( 'doing_action' ) || ! doing_action( 'wp_abilities_api_init' ) ) { return new \WP_Error( 'ao_abilities_wrong_lifecycle', 'Abilities must be registered during wp_abilities_api_init.' ); } $results = []; foreach ( array_keys( $this->pending ) as $name ) { // A native filter may have reentered and already consumed this name. if ( ! isset( $this->pending[ $name ] ) ) { continue; } $configuration = $this->pending[ $name ]; unset( $this->pending[ $name ] ); try { $result = \wp_register_ability( $name, $configuration ); $results[ $name ] = $result instanceof \WP_Ability || $result instanceof \WP_Error ? $result : new \WP_Error( 'ao_ability_registration_failed', 'Native ability registration failed.', $name ); } catch ( \Throwable $error ) { $results[ $name ] = new \WP_Error( 'ao_ability_registration_failed', $error->getMessage(), $name ); } } return $results; } }