| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Tracking; |
| 5 |
|
| 6 |
use Imagify\EventManagement\SubscriberInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* WordPress hook listener that routes MCP ability lifecycle events to McpTracking. |
| 10 |
* |
| 11 |
* @since 2.3.0 |
| 12 |
*/ |
| 13 |
class McpTrackingSubscriber implements SubscriberInterface { |
| 14 |
|
| 15 |
/** |
| 16 |
* The MCP tracking service. |
| 17 |
* |
| 18 |
* @var McpTracking |
| 19 |
*/ |
| 20 |
private $mcp_tracking; |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor. |
| 24 |
* |
| 25 |
* @param McpTracking $mcp_tracking The MCP tracking service. |
| 26 |
*/ |
| 27 |
public function __construct( McpTracking $mcp_tracking ) { |
| 28 |
$this->mcp_tracking = $mcp_tracking; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns the list of events this subscriber wants to listen to. |
| 33 |
* |
| 34 |
* @return array<string, array<int, int|string>> |
| 35 |
*/ |
| 36 |
public static function get_subscribed_events(): array { |
| 37 |
return [ |
| 38 |
// @action imagify_mcp_ability_executed |
| 39 |
'imagify_mcp_ability_executed' => [ 'on_ability_executed', 10, 5 ], |
| 40 |
// @action imagify_mcp_permission_denied |
| 41 |
'imagify_mcp_permission_denied' => [ 'on_permission_denied', 10, 3 ], |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Fired after an MCP ability's execute() method returns (success or failure). |
| 47 |
* |
| 48 |
* Calls both the generic ability-executed tracker (all abilities) and the |
| 49 |
* media-optimized tracker (optimize-media success only). |
| 50 |
* |
| 51 |
* @param string $ability_id Ability slug, e.g. `imagify/optimize-media`. |
| 52 |
* @param string $ability_name Human-readable ability label. |
| 53 |
* @param mixed $result Return value of the ability's execute() method. |
| 54 |
* @param float $start_time microtime(true) captured before execute() ran. |
| 55 |
* @param array $input_params Raw input params passed to execute(). |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function on_ability_executed( string $ability_id, string $ability_name, $result, float $start_time, array $input_params ): void { |
| 59 |
$is_preview = is_array( $result ) && in_array( |
| 60 |
$result['status'] ?? '', |
| 61 |
[ 'confirmation_required', 'insufficient_quota', 'invalid_api_key' ], |
| 62 |
true |
| 63 |
); |
| 64 |
|
| 65 |
$this->mcp_tracking->track_ability_executed( $ability_id, $ability_name, $start_time, $is_preview ); |
| 66 |
|
| 67 |
if ( 'imagify/optimize-media' === $ability_id && is_array( $result ) ) { |
| 68 |
$this->mcp_tracking->track_media_optimized( $ability_id, $result, $start_time, $input_params ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Fired when an MCP ability's check_permissions() returns false. |
| 74 |
* |
| 75 |
* @param string $ability_id Ability slug. |
| 76 |
* @param string $ability_name Human-readable ability label. |
| 77 |
* @param string $required_capability The capability that was missing. |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function on_permission_denied( string $ability_id, string $ability_name, string $required_capability ): void { |
| 81 |
$this->mcp_tracking->track_permission_denied( $ability_id, $ability_name, $required_capability ); |
| 82 |
} |
| 83 |
} |
| 84 |
|