| 1 |
<?php |
| 2 |
/** |
| 3 |
* Custom Abilities Experiment. |
| 4 |
* |
| 5 |
* @package WordPress\AI\Experiments\Custom_Abilities |
| 6 |
* @since 1.3.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare( strict_types=1 ); |
| 10 |
|
| 11 |
namespace WordPress\AI\Experiments\Custom_Abilities; |
| 12 |
|
| 13 |
use WordPress\AI\Abilities\Gated\Gated_Abilities; |
| 14 |
use WordPress\AI\Abilities\Show_In_Abilities; |
| 15 |
use WordPress\AI\Abstracts\Abstract_Feature; |
| 16 |
use WordPress\AI\Experiments\Experiment_Category; |
| 17 |
|
| 18 |
// Exit if accessed directly. |
| 19 |
defined( 'ABSPATH' ) || exit; |
| 20 |
|
| 21 |
/** |
| 22 |
* Gates the plugin's custom WordPress Abilities behind a single experiment. |
| 23 |
* |
| 24 |
* When the experiment is enabled, every gated ability (see the Gated_Abilities |
| 25 |
* registry) is registered together, so a site owner gains access to all custom |
| 26 |
* abilities without toggling anything else on. |
| 27 |
* |
| 28 |
* @since 1.3.0 |
| 29 |
*/ |
| 30 |
class Custom_Abilities extends Abstract_Feature { |
| 31 |
/** |
| 32 |
* {@inheritDoc} |
| 33 |
*/ |
| 34 |
public static function get_id(): string { |
| 35 |
return 'custom-abilities'; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* {@inheritDoc} |
| 40 |
*/ |
| 41 |
protected function load_metadata(): array { |
| 42 |
return array( |
| 43 |
'label' => __( 'Custom Abilities', 'ai' ), |
| 44 |
'description' => __( 'Register the plugin\'s custom WordPress Abilities for use via the Abilities API and MCP.', 'ai' ), |
| 45 |
'category' => Experiment_Category::ADMIN, |
| 46 |
'capability' => 'none', |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* {@inheritDoc} |
| 52 |
* |
| 53 |
* Registers every gated ability. Show_In_Abilities runs once beforehand when |
| 54 |
* any ability depends on core objects being exposed to the Abilities API. |
| 55 |
*/ |
| 56 |
public function register(): void { |
| 57 |
$abilities = Gated_Abilities::get_all(); |
| 58 |
|
| 59 |
if ( $this->requires_core_object_exposure( $abilities ) ) { |
| 60 |
( new Show_In_Abilities() )->register(); |
| 61 |
} |
| 62 |
|
| 63 |
foreach ( $abilities as $ability ) { |
| 64 |
$ability->register(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Whether any of the given abilities needs core objects exposed to the |
| 70 |
* Abilities API before it registers. |
| 71 |
* |
| 72 |
* @since 1.3.0 |
| 73 |
* |
| 74 |
* @param array<\WordPress\AI\Abstracts\Abstract_Gated_Ability> $abilities The gated abilities. |
| 75 |
* @return bool True if any ability requires core-object exposure. |
| 76 |
*/ |
| 77 |
private function requires_core_object_exposure( array $abilities ): bool { |
| 78 |
foreach ( $abilities as $ability ) { |
| 79 |
if ( $ability->requires_core_object_exposure() ) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
} |
| 87 |
|