PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.2
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.2
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / admin / classes / class-merchant-admin-statistics-tracking.php

class-merchant-admin-statistics-tracking.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.3.2, at admin/classes/class-merchant-admin-statistics-tracking.php

419 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Merchant Admin Statistics Tracking
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly
8 }
9
10 /**
11 * Class Merchant_Admin_Statistics_Tracking
12 *
13 * This class handles the tracking of statistics for the Merchant Admin.
14 * It includes methods for saving site keys, checking user consent, and sending module usage statistics.
15 *
16 * @package Merchant_Admin
17 */
18 class Merchant_Admin_Statistics_Tracking {
19
20 /**
21 * The URL for the website API.
22 *
23 * @var string
24 */
25 private const WEBSITE_API_LINK = 'https://athemes.com/wp-json/athemes-stats/v1';
26
27 /**
28 * The endpoint for registering the site.
29 *
30 * @var string
31 */
32 private const REGISTER_SITE_ENDPOINT = 'register-site';
33
34 /**
35 * The endpoint for sending bulk usage statistics.
36 *
37 * @var string
38 */
39 private const SEND_BULK_USAGE_ENDPOINT = 'track-event-bulk';
40
41 /**
42 * The endpoint for sending single usage statistics.
43 *
44 * @var string
45 */
46 private const SEND_SINGLE_USAGE_ENDPOINT = 'track-event';
47
48 /**
49 * The endpoint for sending heartbeat updates.
50 *
51 * @var string
52 */
53 private const SEND_HEART_BEAT_ENDPOINT = 'update-heartbeat';
54
55 /**
56 * @var Merchant_Admin_Statistics_Tracking $instance Singleton instance
57 */
58 private static $instance;
59
60 /**
61 * Get the singleton instance of the class
62 *
63 * @return Merchant_Admin_Statistics_Tracking
64 */
65 public static function instance() {
66 if ( ! isset( self::$instance ) ) {
67 self::$instance = new self();
68 }
69
70 return self::$instance;
71 }
72
73 /**
74 * Attach hooks to the WordPress API
75 *
76 * @return void
77 */
78 public function load_hooks() {
79 add_action( 'init', array( $this, 'register_website' ) );
80 add_action( 'merchant_request_site_key', array( $this, 'register_website_background_process' ) );
81 add_action( 'init', array( $this, 'send_bulk_usage_statistics' ) );
82 add_action( 'merchant_bulk_send_statistics', array( $this, 'send_bulk_usage_statistics_background_process' ) );
83 add_action( 'merchant_admin_module_activated', array( $this, 'log_module_activation' ) );
84 add_action( 'merchant_single_module_activation_tracking', array( $this, 'log_module_activation_background_process' ) );
85 add_action( 'merchant_admin_module_deactivated', array( $this, 'log_module_deactivation' ) );
86 add_action( 'merchant_single_module_deactivation_tracking', array( $this, 'log_module_deactivation_background_process' ) );
87 add_action( 'merchant_weekly_schedule', array( $this, 'update_last_seen' ) );
88 }
89
90 /**
91 * Log module activation
92 *
93 * @param string $module_id The module that was activated.
94 *
95 * @return void
96 */
97 public function log_module_activation( $module_id ) {
98 if ( ! function_exists( 'as_next_scheduled_action' ) ) {
99 return;
100 }
101 $sent = as_next_scheduled_action( 'merchant_single_module_activation_tracking', array( $module_id ), 'merchant' );
102 if ( $this->should_send_statistics() && ! $sent ) {
103 as_enqueue_async_action(
104 'merchant_single_module_activation_tracking',
105 array( $module_id ),
106 'merchant'
107 );
108 }
109 }
110
111 /**
112 * Log module activation background process
113 *
114 * @param string $module_id The module that was activated.
115 *
116 * @return void
117 */
118 public function log_module_activation_background_process( $module_id ) {
119 if ( $this->should_send_statistics() ) {
120 $site_key = $this->get_site_key();
121 $event_type = 'activate';
122 $body = array(
123 'site_key' => $site_key,
124 'event_type' => $event_type,
125 'module_slug' => $module_id,
126 'project_name' => 'merchant',
127 );
128
129 $response = wp_remote_post(
130 self::WEBSITE_API_LINK . '/' . self::SEND_SINGLE_USAGE_ENDPOINT,
131 array(
132 'body' => $body,
133 )
134 );
135
136 /**
137 * Hook to track the response of the single module activation
138 *
139 * @param string $module_id The module that was activated.
140 * @param array $response The response from the API.
141 *
142 * @since 2.1.0
143 */
144 do_action( 'merchant_single_module_activation_tracking_background_process', $module_id, $response );;
145 }
146 }
147
148 /**
149 * Log module deactivation
150 *
151 * @param string $module_id The module that was deactivated.
152 *
153 * @return void
154 */
155 public function log_module_deactivation( $module_id ) {
156 $sent = as_next_scheduled_action( 'merchant_single_module_deactivation_tracking', array( $module_id ), 'merchant' );
157 if ( $this->should_send_statistics() && ! $sent ) {
158 as_enqueue_async_action(
159 'merchant_single_module_deactivation_tracking',
160 array( $module_id ),
161 'merchant'
162 );
163 }
164 }
165
166 /**
167 * Log module deactivation background process
168 *
169 * @param string $module_id The module that was deactivated.
170 *
171 * @return void
172 */
173 public function log_module_deactivation_background_process( $module_id ) {
174 if ( $this->should_send_statistics() ) {
175 $site_key = $this->get_site_key();
176 $event_type = 'deactivate';
177 $body = array(
178 'site_key' => $site_key,
179 'event_type' => $event_type,
180 'module_slug' => $module_id,
181 'project_name' => 'merchant',
182 );
183
184 $response = wp_remote_post(
185 self::WEBSITE_API_LINK . '/' . self::SEND_SINGLE_USAGE_ENDPOINT,
186 array(
187 'body' => $body,
188 )
189 );
190
191 /**
192 * Hook to track the response of the single module deactivation
193 *
194 * @param string $module_id The module that was deactivated.
195 * @param array $response The response from the API.
196 *
197 * @since 2.1.0
198 */
199 do_action( 'merchant_single_module_deactivation_tracking_background_process', $module_id, $response );;
200 }
201 }
202
203 /**
204 * Send bulk usage statistics
205 *
206 * This method is called during the 'init' action.
207 * It checks if the statistics should be sent and schedules the request if necessary.
208 *
209 * @return void
210 */
211 public function send_bulk_usage_statistics() {
212 if ( ! function_exists( 'as_next_scheduled_action' ) ) {
213 return;
214 }
215 if ( $this->should_send_statistics() && ! empty( merchant_get_active_modules() ) && ! as_next_scheduled_action( 'merchant_bulk_send_statistics', array(), 'merchant' ) ) {
216 as_enqueue_async_action(
217 'merchant_bulk_send_statistics',
218 array(),
219 'merchant'
220 );
221 }
222 }
223
224 /**
225 * Send bulk usage statistics background process
226 *
227 * This method is called when the 'merchant_bulk_send_statistics' action is triggered.
228 * It sends the bulk usage statistics to the API.
229 *
230 * @return void
231 */
232 public function send_bulk_usage_statistics_background_process() {
233 if ( $this->should_send_statistics() && ! $this->is_bulk_statistics_sent() ) {
234 $active_modules = merchant_get_active_modules();
235 $site_key = $this->get_site_key();
236 $response = wp_remote_post(
237 self::WEBSITE_API_LINK . '/' . self::SEND_BULK_USAGE_ENDPOINT,
238 array(
239 'body' => array(
240 'site_key' => $site_key,
241 'module_slugs' => $active_modules,
242 'project_name' => 'merchant',
243 'event_type' => 'activate',
244 ),
245 )
246 );
247
248 if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) {
249 $this->mark_bulk_statistics_sent();
250 }
251
252 /**
253 * Hook to track the response of the bulk statistics
254 *
255 * @param array $response The response from the API.
256 *
257 * @since 2.1.0
258 */
259 do_action( 'merchant_bulk_send_statistics_background_process', $response );
260 }
261 }
262
263 /**
264 * Registers the website key if it should be requested.
265 *
266 * This method is called during the 'init' action.
267 * It checks if the site key should be requested and schedules the request if necessary.
268 *
269 * @return void
270 */
271 public function register_website() {
272 if ( ! function_exists( 'as_next_scheduled_action' ) ) {
273 return;
274 }
275 if ( $this->should_request_website_key() && ! as_next_scheduled_action( 'merchant_request_site_key', array(), 'merchant' ) ) {
276 as_enqueue_async_action(
277 'merchant_request_site_key',
278 array(),
279 'merchant'
280 );
281 }
282 }
283
284 /**
285 * Registers the website background process.
286 *
287 * This method is called when the 'merchant_request_site_key' action is triggered.
288 * It checks if the site key exists and requests a new one if it doesn't.
289 *
290 * @return void
291 */
292 public function register_website_background_process() {
293 if ( ! $this->get_site_key() ) {
294 $site_key = $this->request_website_key();
295 if ( $site_key ) {
296 $this->save_site_key( $site_key );
297 }
298 }
299 }
300
301 /**
302 * Check if we should send statistics.
303 *
304 * @return bool
305 */
306 public function should_send_statistics() {
307 return $this->is_user_consent() && $this->get_site_key();
308 }
309
310 /**
311 * Checks if the site key should be requested.
312 *
313 * @return bool True if the site key should be requested, false otherwise.
314 */
315 public function should_request_website_key() {
316 return $this->is_user_consent() && ! $this->get_site_key();
317 }
318
319 /**
320 * Requests a new site key from the API.
321 *
322 * @return string|false The site key if successful, false otherwise.
323 */
324 public function request_website_key() {
325 $blog_id = get_option( 'blog_id' );
326 $website_url = get_option( 'siteurl' );
327
328 $response = wp_remote_post(
329 self::WEBSITE_API_LINK . '/' . self::REGISTER_SITE_ENDPOINT,
330 array(
331 'body' => array(
332 'site_url' => $website_url,
333 'blog_id' => $blog_id,
334 ),
335 )
336 );
337
338
339 // check if response is 200
340 if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) {
341 $response_body = wp_remote_retrieve_body( $response );
342 $response_data = json_decode( $response_body, true );
343 // save the new site key
344 if ( ! empty( $response_data['data']['site_key'] ) ) {
345 return $response_data['data']['site_key'];
346 }
347 }
348
349 return false;
350 }
351
352 /**
353 * Update the last seen timestamp for the site
354 *
355 * This method is called during the 'merchant_weekly_schedule' action.
356 * It sends a request to the API to update the last seen timestamp for the site.
357 *
358 * @return void
359 */
360 public function update_last_seen() {
361 wp_remote_post(
362 self::WEBSITE_API_LINK . '/' . self::SEND_HEART_BEAT_ENDPOINT,
363 array(
364 'body' => array(
365 'site_key' => $this->get_site_key(),
366 'blog_id' => get_option( 'blog_id' ),
367 ),
368 )
369 );
370 }
371
372 /**
373 * Checks if the site key is already saved in the database.
374 *
375 * @return string site key if the site key exists, false otherwise.
376 */
377 private function get_site_key() {
378 return get_option( 'merchant_site_key', false );
379 }
380
381 /**
382 * Retrieves the site key from the database.
383 *
384 * @return string|false The site key if it exists, false otherwise.
385 */
386 private function save_site_key( $key ) {
387 return update_option( 'merchant_site_key', $key, false );
388 }
389
390 /**
391 * Checks if the bulk statistics have already been sent.
392 *
393 * @return bool True if the bulk statistics have been sent, false otherwise.
394 */
395 private function is_bulk_statistics_sent() {
396 return get_option( 'merchant_statistics_sent', false );
397 }
398
399 /**
400 * Checks if the user has given consent for tracking.
401 *
402 * @return bool True if the user has given consent, false otherwise.
403 */
404 private function is_user_consent() {
405 return Merchant_Admin_Options::get( 'global-settings', 'usage_statistics_tracking', false );
406 }
407
408 /**
409 * Marks the bulk statistics as sent by updating the corresponding option in the database.
410 *
411 * @return bool True if the option was successfully updated, false otherwise.
412 */
413 private function mark_bulk_statistics_sent() {
414 return update_option( 'merchant_statistics_sent', true, false );
415 }
416 }
417
418 $merchant_statistics_tracking = Merchant_Admin_Statistics_Tracking::instance();
419 $merchant_statistics_tracking->load_hooks();