PluginProbe ʕ •ᴥ•ʔ
Microsoft Clarity / 0.10.29
Microsoft Clarity v0.10.29
0.10.29 0.10.28 0.10.27 0.10.26 0.10.25 0.10.24 0.8.0 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 trunk 0.10.0 0.10.1 0.10.10 0.10.11 0.10.12 0.10.13 0.10.14 0.10.15 0.10.16 0.10.17 0.10.18 0.10.19 0.10.2 0.10.20 0.10.21 0.10.22 0.10.23 0.10.3 0.10.4 0.10.5 0.10.6 0.10.7 0.10.8 0.10.9 0.2 0.4 0.5 0.6 0.6.1 0.7 0.7.1 0.7.2 0.7.3 0.7.4 0.7.5
microsoft-clarity / clarity-collect-batch.php
microsoft-clarity Last commit date
includes 12 hours ago js 1 week ago LICENSE.txt 5 years ago clarity-collect-batch.php 5 months ago clarity-collect-storage.php 5 months ago clarity-hooks.php 6 months ago clarity-page.php 1 week ago clarity-server-analytics.php 2 months ago clarity.php 12 hours ago index.php 5 years ago readme.txt 12 hours ago
clarity-collect-batch.php
99 lines
1 <?php
2
3 // Batch helpers for collect events.
4
5 const CLARITY_COLLECT_BATCH_SIZE = 50;
6 const CLARITY_COLLECT_CRON_INTERVAL = 300;
7 const CLARITY_COLLECT_CRON_SCHEDULE = 'clarity_request_recurrence';
8 const CLARITY_COLLECT_CRON_HOOK = 'clarity_collect_batch_cron';
9
10 /**
11 * Registers a 5-minute cron schedule.
12 *
13 * @param array $schedules Existing schedules.
14 * @return array Updated schedules.
15 */
16 function clarity_register_collect_schedule($schedules)
17 {
18 if (!isset($schedules[CLARITY_COLLECT_CRON_SCHEDULE])) {
19 $schedules[CLARITY_COLLECT_CRON_SCHEDULE] = array(
20 'interval' => CLARITY_COLLECT_CRON_INTERVAL,
21 'display' => 'Every ' . CLARITY_COLLECT_CRON_INTERVAL . ' seconds'
22 );
23 }
24
25 return $schedules;
26 }
27
28 // Registering the Clarity collect schedule to the list of cron schedules
29 // This is done through a filter registering to $schedules.
30 add_filter('cron_schedules', 'clarity_register_collect_schedule');
31
32 /**
33 * Schedules the recurring batch worker if not already scheduled.
34 */
35 function clarity_maybe_schedule_collect_recurring()
36 {
37 if (wp_next_scheduled(CLARITY_COLLECT_CRON_HOOK)) {
38 return;
39 }
40
41 wp_schedule_event(time() + 5, CLARITY_COLLECT_CRON_SCHEDULE, CLARITY_COLLECT_CRON_HOOK);
42 }
43
44 /**
45 * Flushes pending events and clears the recurring batch worker.
46 */
47 function clarity_flush_and_clear_collect_recurring()
48 {
49 clarity_send_collect_event_batch_worker();
50 wp_clear_scheduled_hook(CLARITY_COLLECT_CRON_HOOK);
51 }
52
53 /**
54 * Runs the batch sender on cron.
55 */
56 function clarity_send_collect_event_batch_worker()
57 {
58 global $wpdb;
59
60 if (!$wpdb->ready || !clarity_collect_events_table_exists()) {
61 return;
62 }
63
64 $rows = clarity_fetch_and_delete_pending_events_transactionally();
65 if (empty($rows)) {
66 return;
67 }
68
69 $events = clarity_build_events_from_rows($rows);
70 if (empty($events)) {
71 return;
72 }
73
74 foreach (array_chunk($events, CLARITY_COLLECT_BATCH_SIZE) as $batch) {
75 clarity_send_collect_event_batch($batch);
76 }
77 }
78
79 add_action(CLARITY_COLLECT_CRON_HOOK, 'clarity_send_collect_event_batch_worker');
80
81 /**
82 * Builds event payloads from pending rows.
83 *
84 * @param array $rows Pending rows.
85 * @return array Payloads to send.
86 */
87 function clarity_build_events_from_rows($rows)
88 {
89 $events = array();
90 foreach ($rows as $row) {
91 $payload = json_decode($row['payload'], true);
92 if (is_array($payload)) {
93 $events[] = $payload;
94 }
95 }
96
97 return $events;
98 }
99