| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Tracking; |
| 5 |
|
| 6 |
use Imagify\Dependencies\WPMedia\Mixpanel\Optin; |
| 7 |
use Imagify\Dependencies\WPMedia\Mixpanel\TrackingPlugin; |
| 8 |
|
| 9 |
/** |
| 10 |
* Abstract base class for Imagify tracking. |
| 11 |
* |
| 12 |
* @since 2.3.0 |
| 13 |
*/ |
| 14 |
abstract class BaseTracking { |
| 15 |
|
| 16 |
/** |
| 17 |
* The Mixpanel opt-in service. |
| 18 |
* |
| 19 |
* @var Optin |
| 20 |
*/ |
| 21 |
protected $optin; |
| 22 |
|
| 23 |
/** |
| 24 |
* The Mixpanel tracking plugin service. |
| 25 |
* |
| 26 |
* @var TrackingPlugin |
| 27 |
*/ |
| 28 |
protected $mixpanel; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor. |
| 32 |
* |
| 33 |
* @param Optin $optin The Mixpanel opt-in service. |
| 34 |
* @param TrackingPlugin $mixpanel The Mixpanel tracking plugin service. |
| 35 |
*/ |
| 36 |
public function __construct( Optin $optin, TrackingPlugin $mixpanel ) { |
| 37 |
$this->optin = $optin; |
| 38 |
$this->mixpanel = $mixpanel; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Check if tracking is allowed. |
| 43 |
* |
| 44 |
* @return bool True if tracking is allowed, false otherwise. |
| 45 |
*/ |
| 46 |
public function can_track(): bool { |
| 47 |
return $this->optin->can_track(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the default event properties shared by every tracked event. |
| 52 |
* |
| 53 |
* IMPORTANT: do NOT add `domain`, `wp_version`, `php_version`, `plugin`, |
| 54 |
* `brand`, or `application` here. `TrackingPlugin::track_direct()` injects |
| 55 |
* those automatically and any value set here is silently overwritten. |
| 56 |
* |
| 57 |
* @return array<string, mixed> |
| 58 |
*/ |
| 59 |
protected function get_default_event_properties(): array { |
| 60 |
$user = get_imagify_user(); |
| 61 |
$license_owner = ''; |
| 62 |
|
| 63 |
if ( ! is_wp_error( $user ) && ! empty( $user->email ) ) { |
| 64 |
$license_owner = hash( 'sha256', $user->email ); |
| 65 |
} |
| 66 |
|
| 67 |
return [ |
| 68 |
'context' => 'wp_plugin', |
| 69 |
'license_owner' => $license_owner, |
| 70 |
'user_id' => (int) get_current_user_id(), |
| 71 |
]; |
| 72 |
} |
| 73 |
} |
| 74 |
|