| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Tracking; |
| 5 |
|
| 6 |
use Imagify\EventManagement\SubscriberInterface; |
| 7 |
use Imagify\Optimization\Process\ProcessInterface; |
| 8 |
|
| 9 |
/** |
| 10 |
* Subscriber that hooks Imagify optimization events to tracking. |
| 11 |
* |
| 12 |
* @since 2.3.0 |
| 13 |
*/ |
| 14 |
class Subscriber implements SubscriberInterface { |
| 15 |
|
| 16 |
/** |
| 17 |
* The tracking service. |
| 18 |
* |
| 19 |
* @var Tracking |
| 20 |
*/ |
| 21 |
private $tracking; |
| 22 |
|
| 23 |
/** |
| 24 |
* Constructor. |
| 25 |
* |
| 26 |
* @param Tracking $tracking The tracking service. |
| 27 |
*/ |
| 28 |
public function __construct( Tracking $tracking ) { |
| 29 |
$this->tracking = $tracking; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Returns the list of events this subscriber wants to listen to. |
| 34 |
* |
| 35 |
* @return array<string, array<int, mixed>> |
| 36 |
*/ |
| 37 |
public static function get_subscribed_events(): array { |
| 38 |
return [ |
| 39 |
// @action imagify_after_optimize |
| 40 |
'imagify_after_optimize' => [ 'track_media_optimized', 10, 2 ], |
| 41 |
// @action update_option_imagify_settings fires on single-site saves. |
| 42 |
'update_option_imagify_settings' => [ 'track_settings_saved', 10, 2 ], |
| 43 |
// @action update_site_option_imagify_settings fires on multisite saves. |
| 44 |
'update_site_option_imagify_settings' => [ 'track_settings_saved', 10, 2 ], |
| 45 |
// @action imagify_after_reset_internal_state |
| 46 |
'imagify_after_reset_internal_state' => [ 'track_internal_state_reset', 10, 0 ], |
| 47 |
// @action imagify_after_restore_media |
| 48 |
'imagify_after_restore_media' => [ 'track_media_restored', 10, 4 ], |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Track a "Media Optimized" event after optimization completes. |
| 54 |
* |
| 55 |
* @param ProcessInterface $process The optimization process instance. |
| 56 |
* @param array $item The optimization item data. |
| 57 |
* |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function track_media_optimized( $process, $item ): void { |
| 61 |
$this->tracking->track_media_optimized( $process, $item ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Delegate to Tracking::track_settings_saved(). |
| 66 |
* |
| 67 |
* Both update_option_imagify_settings and update_site_option_imagify_settings fire |
| 68 |
* with different arg ordering (on multisite the first arg is the option name string, |
| 69 |
* not the old value). Both args are cast to array so the strict-typed downstream |
| 70 |
* signature is always satisfied. $old_value is unused downstream. |
| 71 |
* |
| 72 |
* @param mixed $old_value Old option value, or option name string on multisite. |
| 73 |
* @param mixed $new_value New option value. |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function track_settings_saved( $old_value, $new_value ): void { |
| 78 |
$this->tracking->track_settings_saved( (array) $old_value, (array) $new_value ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Track an "Internal State Reset" event after the internal state is reset. |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function track_internal_state_reset(): void { |
| 87 |
$this->tracking->track_internal_state_reset(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Track a "Media Restored" event after a media file is restored. |
| 92 |
* |
| 93 |
* @param ProcessInterface $process The optimization process instance. |
| 94 |
* @param bool|\WP_Error $response True on success, WP_Error on failure. |
| 95 |
* @param array $files The list of files before restoring. |
| 96 |
* @param array $data The optimization data captured before deletion. |
| 97 |
* |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
public function track_media_restored( $process, $response, array $files, array $data ): void { |
| 101 |
$this->tracking->track_media_restored( $process, $response, $files, $data ); |
| 102 |
} |
| 103 |
} |
| 104 |
|