PluginProbe
Parse.ly / 3.16.1
Parse.ly v3.16.1
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 / class-telemetry-system.php

class-telemetry-system.php in Parse.ly 3.16.1, at src/Telemetry/class-telemetry-system.php

180 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Telemetry: Telemetry System abstract 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 * Base class for all telemetry system implementations.
20 *
21 * @since 3.12.0
22 */
23 abstract class Telemetry_System {
24 /**
25 * Holds the list of events to be tracked.
26 *
27 * @var array<array<string, string|int>>
28 */
29 protected $events;
30
31 /**
32 * Registers the telemetry system.
33 *
34 * @since 3.12.0
35 */
36 abstract public function run(): void;
37
38 /**
39 * Activates event tracking.
40 *
41 * @since 3.12.0
42 */
43 abstract protected function activate_tracking(): void;
44
45 /**
46 * Returns whether wp-admin telemetry is allowed to be enabled. This is off
47 * by default.
48 *
49 * @since 3.12.0
50 *
51 * @return bool Whether wp-admin telemetry is allowed to be enabled.
52 */
53 public static function is_wpadmin_telemetry_allowed(): bool {
54 return apply_filters( 'wp_parsely_enable_wpadmin_telemetry', false );
55 }
56
57 /**
58 * Registers the passed events so they can be recorded later.
59 *
60 * Note: All events must be registered before the run() function of this
61 * class gets called.
62 *
63 * @since 3.12.0
64 *
65 * @param array<string, string|int> ...$events The events to register.
66 */
67 public function register_events( array ...$events ): void {
68 foreach ( $events as $event ) {
69 $this->events[] = $event;
70 }
71 }
72
73 /**
74 * Records the passed event.
75 *
76 * @since 3.12.0
77 *
78 * @param string $event_name The event's name.
79 * @param array<string, mixed> $event_properties Any additional properties
80 * to include with the event.
81 * @return bool|WP_Error True if recording the event succeeded.
82 * False if telemetry is disabled.
83 * WP_Error if recording the event failed.
84 */
85 abstract public function record_event(
86 string $event_name,
87 array $event_properties = array()
88 );
89
90 /**
91 * Initializes JavaScript tracking.
92 *
93 * This method is responsible for setting up the JavaScript tracking for the application.
94 * It enqueues the necessary scripts and sets up the parameters for the tracking script.
95 *
96 * If the user has disabled wp-admin telemetry, the script will be enqueued, however the
97 * global object `wpParselyTra§cksTelemetry` will not be available.
98 *
99 * @since 3.12.0
100 */
101 public static function init_js_tracking(): void {
102 // Enqueue the JS file.
103 add_action(
104 'admin_enqueue_scripts',
105 function (): void {
106 $asset_php = get_asset_info( 'build/telemetry.asset.php' );
107 $built_assets_url = plugin_dir_url( PARSELY_FILE ) . 'build/';
108
109 // The Telemetry script is always enqueued in the admin.
110 // If the user has disabled wp-admin telemetry, the global object will not be available.
111 wp_enqueue_script(
112 'wp-parsely-tracks-telemetry',
113 $built_assets_url . 'telemetry.js',
114 $asset_php['dependencies'],
115 $asset_php['version'],
116 true
117 );
118
119 // If the user has disabled wp-admin telemetry, return early.
120 if ( ! Telemetry_System::is_wpadmin_telemetry_allowed() ) {
121 return;
122 }
123
124 // Set the script params.
125 $script_params = array(
126 'version' => PARSELY_VERSION,
127 'user' => array(),
128 );
129
130 // If it's a VIP environment, add the VIP environment to the script params.
131 if ( defined( 'VIP_GO_APP_ENVIRONMENT' ) ) {
132 $app_environment = constant( 'VIP_GO_APP_ENVIRONMENT' );
133 if ( is_string( $app_environment ) && '' !== $app_environment ) {
134 $script_params['vipgo_env'] = $app_environment;
135 }
136 }
137
138 // Define user-specific params.
139 $wp_user_id = get_current_user_id();
140 if ( 0 !== $wp_user_id ) {
141 // If it's VIP environment, add the VIP user ID to the script params.
142 if ( defined( 'VIP_GO_APP_ID' ) ) {
143 $app_id = constant( 'VIP_GO_APP_ID' );
144 if ( is_integer( $app_id ) && 0 < $app_id ) {
145 $script_params['user'] = array(
146 'type' => 'vip_go_app_wp_user',
147 'id' => $app_id . '_' . $wp_user_id,
148 );
149 }
150 }
151
152 // If not VIP, fallback to the generated parse.ly user ID.
153 if ( 0 === count( $script_params['user'] ) ) {
154 $wp_base_url = get_option( 'home' );
155 if ( ! is_string( $wp_base_url ) || '' === $wp_base_url ) {
156 $wp_base_url = get_option( 'siteurl' );
157 }
158
159 /**
160 * The base URL of the site.
161 *
162 * @var string $wp_base_url
163 */
164 $script_params['user'] = array(
165 'type' => 'wpparsely:user_id',
166 'id' => wp_hash( sprintf( '%s|%s', $wp_base_url, $wp_user_id ) ),
167 );
168 }
169 }
170
171 wp_add_inline_script(
172 'wp-parsely-tracks-telemetry',
173 'const wpParselyTracksTelemetry = ' . wp_json_encode( $script_params ) . ';',
174 'before'
175 );
176 }
177 );
178 }
179 }
180