PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.1.0
Defender Security – Malware Scanner, Login Security & Firewall v6.1.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 / controller / class-data-tracking.php

class-data-tracking.php in Defender Security – Malware Scanner, Login Security & Firewall 6.1.0, at src/controller/class-data-tracking.php

249 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles data tracking functionalities.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use Exception;
11 use WP_Defender\Event;
12 use Calotes\Component\Response;
13 use WP_Defender\Model\Setting\Main_Setting;
14 use WP_Defender\Component\Config\Config_Hub_Helper;
15
16 /**
17 * Handles data tracking functionalities.
18 *
19 * @since 4.2.0
20 */
21 class Data_Tracking extends Event {
22
23 public const TRACKING_SLUG = 'wd_show_usage_data';
24
25 /**
26 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
27 */
28 public function __construct() {
29 $this->register_routes();
30 }
31
32 /**
33 * Get the Tracking modal that is displayed on all plugin pages.
34 *
35 * @return array
36 */
37 public function get_tracking_modal(): array {
38 $title = esc_html__( 'Help Us Enhance Your Site\'s Security', 'defender-security' );
39
40 $current_user = wp_get_current_user();
41
42 $desc = sprintf(
43 /* translators: %s: user display name */
44 esc_html__(
45 'Hey there! %s, Defender is dedicated to protecting your WordPress website from hackers and malware. However, our mission is more effective with your collaboration. By opting in to share anonymous usage data, you help us refine and enhance our plugin for everyone\'s benefit.',
46 'defender-security'
47 ),
48 '<strong>' . esc_html( $current_user->display_name ) . '</strong>'
49 );
50 $desc .= '<br/><br/>';
51 $desc .= sprintf(
52 /* translators: %s: Link. */
53 esc_html__(
54 'Your privacy is important to us. We guarantee that your data stays anonymous and your identity stays secure. Learn more about our usage tracking %s.',
55 'defender-security'
56 ),
57 '<a href="' . Main_Setting::PRIVACY_LINK . '" target="_blank">' . esc_html__( 'here', 'defender-security' ) . '<a>'
58 );
59 $result = $this->dump_routes_and_nonces();
60
61 return array(
62 'title' => $title,
63 'desc' => $desc,
64 'banner_1x' => defender_asset_url( '/assets/img/modal/tracking-modal.png' ),
65 'banner_2x' => defender_asset_url( '/assets/img/modal/[email protected]' ),
66 'banner_alt' => esc_html__( 'Help us improve Defender', 'defender-security' ),
67 'optin_button_title' => esc_html__( 'OPT IN', 'defender-security' ),
68 'skip_button_title' => esc_html__( 'Skip for now', 'defender-security' ),
69 'state_usage_tracking' => wd_di()->get( Main_Setting::class )->usage_tracking,
70 'routes' => $result['routes'],
71 'nonces' => $result['nonces'],
72 );
73 }
74
75 /**
76 * Handles the closing of the tracking modal.
77 *
78 * @return Response Response object indicating success.
79 * @defender_route
80 */
81 public function close_track_modal(): Response {
82 $model_settings = wd_di()->get( Main_Setting::class );
83
84 if ( true === $model_settings->usage_tracking ) {
85 $model_settings->toggle_tracking( false );
86 Config_Hub_Helper::set_clear_active_flag();
87 $this->track_opt_toggle( false, 'Tracking modal' );
88 }
89
90 // Track.
91 $this->track_feature( 'def_tracking_modal', array( 'Modal Action' => 'closed' ) );
92 self::dismiss_modal_key();
93
94 return new Response( true, array() );
95 }
96
97 /**
98 * Save Enabled tracking state.
99 *
100 * @return Response
101 * @defender_route
102 */
103 public function save_track_modal(): Response {
104 $model_settings = wd_di()->get( Main_Setting::class );
105 // Update the value if it's changed.
106 if ( true !== $model_settings->usage_tracking ) {
107 $model_settings->toggle_tracking( true );
108 // Changes for Hub.
109 Config_Hub_Helper::set_clear_active_flag();
110 // Track#1.
111 $this->track_opt_toggle( true, 'Tracking modal' );
112 // Track#2.
113 $this->track_feature( 'def_tracking_modal', array( 'Modal Action' => 'cta_clicked' ) );
114 }
115 // Hide the modal.
116 self::dismiss_modal_key();
117
118 return new Response( true, array() );
119 }
120
121 /**
122 * Marks the tracking notice as dismissed without resetting initialization state.
123 *
124 * @return void
125 */
126 public static function dismiss_modal_key(): void {
127 update_site_option( self::TRACKING_SLUG, false );
128 }
129
130 /**
131 * Deletes the site option that controls the visibility of the tracking modal.
132 */
133 public static function delete_modal_key(): void {
134 delete_site_option( self::TRACKING_SLUG );
135 }
136
137 /**
138 * Conditions of the Tracking modal:
139 * 1)show on all Defender pages.
140 * 2)show to users upgrading from older versions.
141 * 3)should have higher priority than a Welcome modal on the Defender > Dashboard page.
142 * 4)if user closes or clicks on the Save button on one plugin page, we don't itl on another plugin page.
143 * 5)no display after the updated Onboarding with Opt-in.
144 * 6)no display when Whitelabel > Documentation, Tutorials and What’s New Modal is set to “Hide”
145 *
146 * @return bool
147 */
148 public function show_tracking_modal() {
149 $info = defender_white_label_status();
150
151 $white_label_is_hide = isset( $info['hide_doc_link'] ) && $info['hide_doc_link'];
152
153 return (bool) get_site_option( self::TRACKING_SLUG ) && ! $white_label_is_hide;
154 }
155
156 /**
157 * Delete all the data & the cache.
158 */
159 public function remove_data() {
160 self::delete_modal_key();
161 }
162
163 /**
164 * Exports strings.
165 *
166 * @return array An array of strings.
167 */
168 public function export_strings(): array {
169 return array();
170 }
171
172 /**
173 * Converts the object data to an array.
174 *
175 * @return array An array representation of the object.
176 */
177 public function to_array(): array {
178 return array();
179 }
180
181 /**
182 * Imports data into the model.
183 *
184 * @param array $data Data to be imported into the model.
185 *
186 * @throws Exception If table is not defined.
187 */
188 public function import_data( array $data ) {
189 }
190
191 /**
192 * Removes settings for all submodules.
193 */
194 public function remove_settings(): void {
195 self::delete_modal_key();
196 }
197
198 /**
199 * Provides data for the frontend.
200 *
201 * @return array An array of data for the frontend.
202 */
203 public function data_frontend(): array {
204 return array();
205 }
206
207 /**
208 * Returns whether the dashboard share usage notice should be displayed.
209 *
210 * @return bool
211 */
212 public function should_show_dashboard_notice(): bool {
213 if ( ! get_site_option( self::TRACKING_SLUG ) ) {
214 return false;
215 }
216
217 return ! wd_di()->get( Main_Setting::class )->usage_tracking;
218 }
219
220 /**
221 * Returns dashboard notice data and routes for the share usage bubble.
222 *
223 * @return array
224 */
225 public function get_dashboard_notice_data(): array {
226 $result = $this->dump_routes_and_nonces();
227
228 return array(
229 'shareUsageNotice' => array(
230 'isVisible' => $this->should_show_dashboard_notice(),
231 ),
232 'routes' => $result['routes'],
233 'nonces' => $result['nonces'],
234 );
235 }
236
237 /**
238 * Marks the share usage dashboard notice as already shown.
239 *
240 * @return Response
241 * @defender_route
242 */
243 public function mark_track_notice_displayed(): Response {
244 self::dismiss_modal_key();
245
246 return new Response( true, array() );
247 }
248 }
249