| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\MCP; |
| 5 |
|
| 6 |
use Imagify\Abilities\AbilitiesInterface; |
| 7 |
use Imagify\EventManagement\SubscriberInterface; |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers the Imagify ability category and all Imagify MCP abilities. |
| 11 |
* |
| 12 |
* Listens on `wp_abilities_api_categories_init` to register the `imagify` |
| 13 |
* category and on `wp_abilities_api_init` to register each concrete ability |
| 14 |
* that is injected via the constructor. |
| 15 |
* |
| 16 |
* All 7 ability instances are wired by `ServiceProvider` and injected at |
| 17 |
* construction time. See docs/api/mcp.md for the full list of abilities. |
| 18 |
* |
| 19 |
* @since 2.3.0 |
| 20 |
*/ |
| 21 |
class AbilitiesSubscriber implements SubscriberInterface { |
| 22 |
|
| 23 |
/** |
| 24 |
* Ability instances to register on `wp_abilities_api_init`. |
| 25 |
* |
| 26 |
* @var AbilitiesInterface[] |
| 27 |
*/ |
| 28 |
private $abilities; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor. |
| 32 |
* |
| 33 |
* @param AbilitiesInterface ...$abilities Ability instances to register. |
| 34 |
*/ |
| 35 |
public function __construct( AbilitiesInterface ...$abilities ) { |
| 36 |
$this->abilities = $abilities; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns the events this subscriber listens to. |
| 41 |
* |
| 42 |
* @return array<string, string> |
| 43 |
*/ |
| 44 |
public static function get_subscribed_events(): array { |
| 45 |
return [ |
| 46 |
// @action wp_abilities_api_categories_init |
| 47 |
'wp_abilities_api_categories_init' => 'register_categories', |
| 48 |
// @action wp_abilities_api_init |
| 49 |
'wp_abilities_api_init' => 'register_abilities', |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Registers the `imagify` ability category. |
| 55 |
* |
| 56 |
* No-ops gracefully when the WP Abilities API is not available (WP < 6.9). |
| 57 |
* |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function register_categories(): void { |
| 61 |
if ( ! function_exists( 'wp_register_ability_category' ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
wp_register_ability_category( |
| 66 |
'imagify', |
| 67 |
[ |
| 68 |
'label' => __( 'Imagify', 'imagify' ), |
| 69 |
'description' => __( 'Image optimization tools for WordPress.', 'imagify' ), |
| 70 |
] |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Registers all injected Imagify abilities. |
| 76 |
* |
| 77 |
* No-ops gracefully when the WP Abilities API is not available (WP < 6.9). |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function register_abilities(): void { |
| 82 |
if ( ! function_exists( 'wp_register_ability' ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
foreach ( $this->abilities as $ability ) { |
| 87 |
$ability->register(); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|