PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.0
Defender Security – Malware Scanner, Login Security & Firewall v5.7.0
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 5.7.0, at src/class-event.php

142 lines 3.0 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 * Save tracking state.
102 *
103 * @param Request $request Request object.
104 *
105 * @return Response
106 * @defender_route
107 */
108 public function track_feature_handler( Request $request ): Response {
109 // Forced tracking.
110 $data = $request->get_data();
111 if ( isset( $data['force'] ) ) {
112 $this->tracker()->track( $data['event'], $data['data'] );
113
114 return new Response( true, array() );
115 }
116 // Otherwise it's a normal process.
117 if ( $this->is_tracking_active() ) {
118 $this->track_feature( $data['event'], $data['data'] );
119 }
120
121 return new Response( true, array() );
122 }
123
124 /**
125 * Sets the intention of the object to the specified location.
126 *
127 * @param string $location The location to set as the intention.
128 */
129 public function set_intention( string $location ) {
130 $this->location = $location;
131 }
132
133 /**
134 * Retrieves the location where the event was triggered.
135 *
136 * @return string The location where the event was triggered.
137 */
138 public function get_triggered_location() {
139 return $this->location;
140 }
141 }
142