forked from stayallive/wp-sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-wp-sentry-js-tracker.php
153 lines (127 loc) · 3.33 KB
/
class-wp-sentry-js-tracker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* WordPress Sentry JavaScript Tracker.
*/
final class WP_Sentry_Js_Tracker {
use WP_Sentry_Resolve_User, WP_Sentry_Resolve_Environment;
/**
* Holds the class instance.
*
* @var WP_Sentry_Js_Tracker
*/
private static $instance;
/**
* Get the sentry tracker instance.
*
* @return WP_Sentry_Js_Tracker
*/
public static function get_instance(): self {
return self::$instance ?: self::$instance = new self;
}
/**
* Class constructor.
*/
protected function __construct() {
// Register on front-end using the highest priority.
add_action( 'wp_enqueue_scripts', [ $this, 'on_enqueue_scripts' ], 0, 1 );
// Register on admin using the highest priority.
add_action( 'admin_enqueue_scripts', [ $this, 'on_enqueue_scripts' ], 0, 1 );
// Register on login using the highest priority.
add_action( 'login_enqueue_scripts', [ $this, 'on_enqueue_scripts' ], 0, 1 );
}
/**
* Get sentry dsn.
*
* @return string
*/
public function get_dsn(): ?string {
$dsn = defined( 'WP_SENTRY_BROWSER_DSN' ) ? WP_SENTRY_BROWSER_DSN : null;
if ( $dsn === null ) {
$dsn = defined( 'WP_SENTRY_PUBLIC_DSN' ) ? WP_SENTRY_PUBLIC_DSN : null;
}
if ( has_filter( 'wp_sentry_public_dsn' ) ) {
$dsn = (string) apply_filters( 'wp_sentry_public_dsn', $dsn );
}
return $dsn;
}
/**
* Get sentry options.
*
* @return array
*/
public function get_options(): array {
$context = $this->get_default_context();
if ( has_filter( 'wp_sentry_public_context' ) ) {
$context = (array) apply_filters( 'wp_sentry_public_context', $context );
}
foreach ( $context as $key => $value ) {
if ( empty( $value ) ) {
unset( $context[ $key ] );
}
}
$options = $this->get_default_options();
$options['content'] = $context;
if ( has_filter( 'wp_sentry_public_options' ) ) {
$options = (array) apply_filters( 'wp_sentry_public_options', $options );
}
return $options;
}
/**
* Get sentry default options.
*
* @return array
*/
public function get_default_options(): array {
$options = [
'environment' => $this->get_environment(),
];
if ( defined( 'WP_SENTRY_VERSION' ) ) {
$options['release'] = WP_SENTRY_VERSION;
}
return $options;
}
/**
* Get sentry default context.
*
* @return array
*/
public function get_default_context(): array {
$context = [
'tags' => [
'wordpress' => get_bloginfo( 'version' ),
'language' => get_bloginfo( 'language' ),
],
'extra' => [],
];
if ( defined( 'WP_SENTRY_SEND_DEFAULT_PII' ) && WP_SENTRY_SEND_DEFAULT_PII ) {
$context['user'] = $this->get_current_user_info();
}
return $context;
}
/**
* Target of set_current_user action.
*
* @access private
*/
public function on_enqueue_scripts(): void {
$traces_sample_rate = defined( 'WP_SENTRY_BROWSER_TRACES_SAMPLE_RATE' )
? (float) WP_SENTRY_BROWSER_TRACES_SAMPLE_RATE
: 0.0;
wp_enqueue_script(
'wp-sentry-browser',
$traces_sample_rate > 0
? plugin_dir_url( WP_SENTRY_PLUGIN_FILE ) . 'public/wp-sentry-browser-tracing.min.js'
: plugin_dir_url( WP_SENTRY_PLUGIN_FILE ) . 'public/wp-sentry-browser.min.js',
[],
WP_Sentry_Version::SDK_VERSION
);
wp_localize_script(
'wp-sentry-browser',
'wp_sentry',
[
'dsn' => $this->get_dsn(),
'tracesSampleRate' => $traces_sample_rate,
] + $this->get_options()
);
}
}