-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
wp-sentry.php
163 lines (134 loc) · 5.25 KB
/
wp-sentry.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
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Plugin Name: WordPress Sentry
* Plugin URI: https://github.com/stayallive/wp-sentry
* Description: A (unofficial) WordPress plugin to report PHP and JavaScript errors to Sentry.
* Version: 8.2.0
* Requires at least: 4.4
* Requires PHP: 7.2.5
* Author: Alex Bouma
* Author URI: https://alex.bouma.dev
* License: MIT
* License URI: https://github.com/stayallive/wp-sentry/blob/v8.2.0/LICENSE.md
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
// If the plugin was already loaded as a mu-plugin or from somewhere else do not load again
if ( defined( 'WP_SENTRY_MU_LOADED' ) || defined( 'WP_SENTRY_LOADED' ) ) {
return;
}
define( 'WP_SENTRY_LOADED', true );
define( 'WP_SENTRY_WPINC', ABSPATH . ( defined( 'WPINC' ) ? WPINC : 'wp-includes' ) );
define( 'WP_SENTRY_WPADMIN', ABSPATH . 'wp-admin' );
// Load the WordPress plugin API early so hooks can be used even if Sentry is loaded before WordPress
if ( ! function_exists( 'add_action' ) ) {
require_once WP_SENTRY_WPINC . '/plugin.php';
}
// Make sure the PHP version is at least 7.2
if ( ! defined( 'PHP_VERSION_ID' ) || PHP_VERSION_ID < 70200 ) {
function wp_sentry_php_version_notice() { ?>
<div class="error below-h2">
<p>
<?php printf(
'The WordPress Sentry plugin requires at least PHP 7.2. You have %s. WordPress Sentry will not be active unless this is resolved!',
PHP_VERSION
); ?>
</p>
</div>
<?php }
add_action( 'admin_notices', 'wp_sentry_php_version_notice' );
return;
}
// Resolve the sentry plugin file
define( 'WP_SENTRY_PLUGIN_FILE', call_user_func( static function () {
global $wp_plugin_paths;
$plugin_file = __FILE__;
if ( ! empty( $wp_plugin_paths ) ) {
$wp_plugin_real_paths = array_flip( $wp_plugin_paths );
$plugin_path = wp_normalize_path( dirname( $plugin_file ) );
if ( isset( $wp_plugin_real_paths[ $plugin_path ] ) ) {
$plugin_file = str_replace( $plugin_path, $wp_plugin_real_paths[ $plugin_path ], $plugin_file );
}
}
return $plugin_file;
} ) );
// Load dependencies
if ( ! class_exists( WP_Sentry_Version::class ) ) {
$scopedAutoloaderExists = file_exists(
$scopedAutoloaderPath = __DIR__ . '/build/vendor/scoper-autoload.php'
);
// If ther is a scoped autoloader we use that version, otherwise we use the normal autoloader
require_once $scopedAutoloaderExists
? $scopedAutoloaderPath
: __DIR__ . '/vendor/autoload.php';
define( 'WP_SENTRY_SCOPED_AUTOLOADER', $scopedAutoloaderExists );
}
// Define the default version if none was set
if ( ! defined( 'WP_SENTRY_VERSION' ) && function_exists( 'add_action' ) ) {
// We need to wait until the theme is loaded and setup to get the version
add_action( 'after_setup_theme', function () {
// It makes no sense to set a version based on the theme version if the plugin is enabled for the network since every site can have a different theme
if ( function_exists( 'is_plugin_active_for_network' ) && is_plugin_active_for_network( __DIR__ ) ) {
return;
}
// The JS client has not been initialized yet so we can just set the `WP_SENTRY_VERSION` constant
define( 'WP_SENTRY_VERSION', wp_get_theme()->get( 'Version' ) ?: 'unknown' );
// The PHP client has probably already been initialized so we need to update the release on the options directly
add_filter( 'wp_sentry_options', static function ( Sentry\Options $options ) {
$options->setRelease( WP_SENTRY_VERSION );
return $options;
} );
}, /* priority: */ 1 );
}
// Load the PHP tracker if we have a PHP DSN
if ( defined( 'WP_SENTRY_PHP_DSN' ) || defined( 'WP_SENTRY_DSN' ) || defined( 'WP_SENTRY_SPOTLIGHT' ) ) {
$sentry_php_tracker_dsn = defined( 'WP_SENTRY_PHP_DSN' )
? WP_SENTRY_PHP_DSN
: null;
if ( $sentry_php_tracker_dsn === null ) {
$sentry_php_tracker_dsn = defined( 'WP_SENTRY_DSN' )
? WP_SENTRY_DSN
: null;
}
if ( ! empty( $sentry_php_tracker_dsn ) || WP_Sentry_Php_Tracker::get_spotlight_enabled() ) {
WP_Sentry_Php_Tracker::get_instance();
WP_Sentry_Php_Tracing::get_instance();
}
}
// Load the JavaScript tracker if we have a browser/public DSN
if ( defined( 'WP_SENTRY_BROWSER_DSN' ) || defined( 'WP_SENTRY_PUBLIC_DSN' ) ) {
$sentry_js_tracker_dsn = defined( 'WP_SENTRY_BROWSER_DSN' )
? WP_SENTRY_BROWSER_DSN
: WP_SENTRY_PUBLIC_DSN;
if ( ! empty( $sentry_js_tracker_dsn ) ) {
WP_Sentry_Js_Tracker::get_instance();
}
}
// Load the admin page
WP_Sentry_Admin_Page::get_instance();
/**
* Register a "safe" function to call Sentry functions safer in your own code,
* the callback only executed if a DSN was set and thus the client is able to sent events.
*
* Usage:
* if ( function_exists( 'wp_sentry_safe' ) ) {
* wp_sentry_safe( function ( \Sentry\State\HubInterface $client ) {
* $client->captureMessage( 'This is a test message!', \Sentry\Severity::debug() );
* } );
* }
*/
if ( ! function_exists( 'wp_sentry_safe' ) ) {
/**
* Call the callback with the Sentry client, or not at all if there is no client.
*
* @param callable $callback
*/
function wp_sentry_safe( callable $callback ) {
if ( class_exists( 'WP_Sentry_Php_Tracker' ) ) {
$tracker = WP_Sentry_Php_Tracker::get_instance();
if ( ! empty( $tracker->get_dsn() ) ) {
$callback( $tracker->get_client() );
}
}
}
}