microsoft-clarity
Last commit date
js
4 months ago
LICENSE.txt
5 years ago
clarity-collect-batch.php
3 months ago
clarity-collect-storage.php
3 months ago
clarity-hooks.php
4 months ago
clarity-page.php
4 months ago
clarity-server-analytics.php
4 months ago
clarity.php
3 months ago
index.php
5 years ago
readme.txt
3 months 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. |
| 34 | */ |
| 35 | function clarity_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 |