PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.0.1
Defender Security – Malware Scanner, Login Security & Firewall v6.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / class-event.php

class-event.php in Defender Security – Malware Scanner, Login Security & Firewall 6.0.1, at src/class-event.php

154 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 use WP_Defender\Traits\Array_Utils;
16
17 /**
18 * Abstract class for Mixpanel Events.
19 */
20 abstract class Event extends Controller {
21
22 use Array_Utils;
23
24 /**
25 * Location of the event
26 *
27 * @var string
28 */
29 protected $location = '';
30
31 /**
32 * Get mixpanel instance.
33 */
34 private function tracker() {
35 return wd_di()->get( Product_Analytics::class )->get_mixpanel();
36 }
37
38 /**
39 * Check if usage tracking is active.
40 *
41 * @return bool
42 */
43 protected function is_tracking_active() {
44 return wd_di()->get( Main_Setting::class )->usage_tracking;
45 }
46
47 /**
48 * Check if the current moment is right for tracking.
49 *
50 * @return bool
51 */
52 protected function maybe_track(): bool {
53 return ! defender_is_wp_cli() && $this->is_tracking_active();
54 }
55
56 /**
57 * Has the data changed?
58 *
59 * @param array $old_data Old data to compare.
60 * @param array $new_data New data to compare.
61 *
62 * @return bool
63 */
64 protected function is_feature_state_changed( $old_data, $new_data ) {
65 // Handle arrays with nested arrays or objects by using deep comparison.
66 return $this->arrays_differ_deeply( $old_data, $new_data );
67 }
68
69 /**
70 * Track data tracking opt in and opt out.
71 *
72 * @param bool $active Toggle value.
73 * @param string $from Triggered method.
74 *
75 * @return void
76 */
77 protected function track_opt_toggle( $active, $from ) {
78 $this->tracker()->track(
79 $active ? 'Opt In' : 'Opt Out',
80 array(
81 'Method' => $from,
82 )
83 );
84 }
85
86 /**
87 * Tracks a feature event if tracking is active.
88 *
89 * @param mixed $event The event to track.
90 * @param array $data The data associated with the event.
91 *
92 * @return void
93 */
94 public function track_feature( $event, $data ) {
95 if ( $this->is_tracking_active() ) {
96 $this->tracker()->track( $event, $data );
97 }
98 }
99
100 /**
101 * Forced tracking.
102 *
103 * @param string $event The event to track.
104 * @param array $data The data associated with the event.
105 *
106 * @return void
107 */
108 public function forced_track( $event, $data ) {
109 $this->tracker()->track( $event, $data );
110 }
111
112 /**
113 * Save tracking state.
114 *
115 * @param Request $request Request object.
116 *
117 * @return Response
118 * @defender_route
119 */
120 public function track_feature_handler( Request $request ): Response {
121 // Forced tracking.
122 $data = $request->get_data();
123 if ( isset( $data['force'] ) ) {
124 $this->forced_track( $data['event'], $data['data'] );
125
126 return new Response( true, array() );
127 }
128 // Otherwise it's a normal process.
129 if ( $this->is_tracking_active() ) {
130 $this->track_feature( $data['event'], $data['data'] );
131 }
132
133 return new Response( true, array() );
134 }
135
136 /**
137 * Sets the intention of the object to the specified location.
138 *
139 * @param string $location The location to set as the intention.
140 */
141 public function set_intention( string $location ) {
142 $this->location = $location;
143 }
144
145 /**
146 * Retrieves the location where the event was triggered.
147 *
148 * @return string The location where the event was triggered.
149 */
150 public function get_triggered_location() {
151 return $this->location;
152 }
153 }
154