PluginProbe
Parse.ly / 3.19.3
Parse.ly v3.19.3
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / Telemetry / Tracks / class-tracks.php

class-tracks.php in Parse.ly 3.19.3, at src/Telemetry/Tracks/class-tracks.php

89 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Telemetry: Tracks class
4 *
5 * @package Parsely\Telemetry
6 * @since 3.12.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Telemetry;
12
13 use WP_Error;
14
15 /**
16 * This class comprises the mechanics of sending events to the Automattic Tracks
17 * system.
18 *
19 * @since 3.12.0
20 */
21 class Tracks extends Telemetry_System {
22 /**
23 * Registers the events into WordPress hooks to activate tracking.
24 *
25 * @since 3.12.0
26 */
27 public function run(): void {
28 $this->activate_tracking();
29 }
30
31 /**
32 * Records an event to Tracks by using the Tracks pixel.
33 *
34 * Depending on the current context, the pixel will be recorded
35 * synchronously (as a GET request) or as asynchronously (as an injected
36 * pixel into the page's footer).
37 *
38 * If the event doesn't pass validation, it gets silently discarded.
39 *
40 * @since 3.12.0
41 *
42 * @param string $event_name The event name. Must be snake_case.
43 * @param array<string, mixed>|array<empty> $event_properties Any additional properties to include with the event.
44 * Key names must be lowercase and snake_case.
45 * @return bool|WP_Error True if recording the event succeeded.
46 * False if telemetry is disabled.
47 * WP_Error if recording the event failed.
48 */
49 public function record_event(
50 string $event_name,
51 array $event_properties = array()
52 ) {
53 $event = new Tracks_Event( $event_name, $event_properties );
54 $pixel = Tracks_Pixel::instance();
55
56 // Process AJAX/REST request events immediately.
57 if ( wp_doing_ajax() || defined( 'REST_REQUEST' ) ) {
58 $pixel->record_event_synchronously( $event );
59 }
60
61 return $pixel->record_event_asynchronously( $event );
62 }
63
64 /**
65 * Registers the events into their respective WordPress hooks, so they
66 * can be recorded when the hook fires.
67 *
68 * @since 3.12.0
69 */
70 protected function activate_tracking(): void {
71 foreach ( $this->events as $event ) {
72 if ( is_string( $event['action_hook'] ) && is_callable( $event['callable'] ) ) {
73 $accepted_args = $event['accepted_args'] ?? 1;
74 $func = function () use ( $accepted_args, $event ) {
75 if ( $accepted_args > 1 ) {
76 $args = func_get_args();
77 $args[] = $this;
78 } else {
79 $args = array( $this );
80 }
81 return call_user_func_array( $event['callable'], $args );
82 };
83
84 add_filter( $event['action_hook'], $func, 10, (int) $accepted_args );
85 }
86 }
87 }
88 }
89