| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to handle mixpanel events functionality. |
| 4 |
* |
| 5 |
* @since 4.2.0 |
| 6 |
* @package WP_Defender |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WP_Defender; |
| 10 |
|
| 11 |
use Calotes\Component\Request; |
| 12 |
use Calotes\Component\Response; |
| 13 |
use WP_Defender\Model\Setting\Main_Setting; |
| 14 |
use WP_Defender\Component\Product_Analytics; |
| 15 |
|
| 16 |
/** |
| 17 |
* Abstract class for Mixpanel Events. |
| 18 |
*/ |
| 19 |
abstract class Event extends Controller { |
| 20 |
|
| 21 |
/** |
| 22 |
* Location of the event |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected $location = ''; |
| 27 |
|
| 28 |
/** |
| 29 |
* Get mixpanel instance. |
| 30 |
*/ |
| 31 |
private function tracker() { |
| 32 |
return wd_di()->get( Product_Analytics::class )->get_mixpanel(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Check if usage tracking is active. |
| 37 |
* |
| 38 |
* @return bool |
| 39 |
*/ |
| 40 |
protected function is_tracking_active() { |
| 41 |
return wd_di()->get( Main_Setting::class )->usage_tracking; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Check if the current moment is right for tracking. |
| 46 |
* |
| 47 |
* @return bool |
| 48 |
*/ |
| 49 |
protected function maybe_track(): bool { |
| 50 |
return ! defender_is_wp_cli() && $this->is_tracking_active(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Has the data changed? |
| 55 |
* |
| 56 |
* @param array $old_data Old data to compare. |
| 57 |
* @param array $new_data New data to compare. |
| 58 |
* |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
protected function is_feature_state_changed( $old_data, $new_data ) { |
| 62 |
return ! empty( array_diff( $old_data, $new_data ) ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Track data tracking opt in and opt out. |
| 67 |
* |
| 68 |
* @param bool $active Toggle value. |
| 69 |
* @param string $from Triggered method. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
protected function track_opt_toggle( $active, $from ) { |
| 74 |
$this->tracker()->track( |
| 75 |
$active ? 'Opt In' : 'Opt Out', |
| 76 |
array( |
| 77 |
'Method' => $from, |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Tracks a feature event if tracking is active. |
| 84 |
* |
| 85 |
* @param mixed $event The event to track. |
| 86 |
* @param array $data The data associated with the event. |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function track_feature( $event, $data ) { |
| 91 |
if ( $this->is_tracking_active() ) { |
| 92 |
$this->tracker()->track( $event, $data ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Save tracking state. |
| 98 |
* |
| 99 |
* @param Request $request Request object. |
| 100 |
* |
| 101 |
* @return Response |
| 102 |
* @defender_route |
| 103 |
*/ |
| 104 |
public function track_feature_handler( Request $request ): Response { |
| 105 |
// Forced tracking. |
| 106 |
$data = $request->get_data(); |
| 107 |
if ( isset( $data['force'] ) ) { |
| 108 |
$this->tracker()->track( $data['event'], $data['data'] ); |
| 109 |
|
| 110 |
return new Response( true, array() ); |
| 111 |
} |
| 112 |
// Otherwise it's a normal process. |
| 113 |
if ( $this->is_tracking_active() ) { |
| 114 |
$this->track_feature( $data['event'], $data['data'] ); |
| 115 |
} |
| 116 |
|
| 117 |
return new Response( true, array() ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Sets the intention of the object to the specified location. |
| 122 |
* |
| 123 |
* @param string $location The location to set as the intention. |
| 124 |
*/ |
| 125 |
public function set_intention( string $location ) { |
| 126 |
$this->location = $location; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Retrieves the location where the event was triggered. |
| 131 |
* |
| 132 |
* @return string The location where the event was triggered. |
| 133 |
*/ |
| 134 |
public function get_triggered_location() { |
| 135 |
return $this->location; |
| 136 |
} |
| 137 |
} |
| 138 |
|