PluginProbe
Parse.ly / 3.16.3
Parse.ly v3.16.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.16.3, at src/Telemetry/Tracks/class-tracks.php

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