PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Tracking / McpTrackingSubscriber.php

McpTrackingSubscriber.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at classes/Tracking/McpTrackingSubscriber.php

78 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $this->mcp_tracking->track_ability_executed( $ability_id, $ability_name, $start_time );
60
61 if ( 'imagify/optimize-media' === $ability_id && is_array( $result ) ) {
62 $this->mcp_tracking->track_media_optimized( $ability_id, $result, $start_time, $input_params );
63 }
64 }
65
66 /**
67 * Fired when an MCP ability's check_permissions() returns false.
68 *
69 * @param string $ability_id Ability slug.
70 * @param string $ability_name Human-readable ability label.
71 * @param string $required_capability The capability that was missing.
72 * @return void
73 */
74 public function on_permission_denied( string $ability_id, string $ability_name, string $required_capability ): void {
75 $this->mcp_tracking->track_permission_denied( $ability_id, $ability_name, $required_capability );
76 }
77 }
78