forked from bugsnag/bugsnag-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bugsnag.php
244 lines (196 loc) · 8.21 KB
/
bugsnag.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/*
Plugin Name: Bugsnag Error Monitoring
Plugin URI: https://bugsnag.com
Description: Bugsnag monitors for errors and crashes on your wordpress site, sends them to your bugsnag.com dashboard, and notifies you by email of each error.
Version: 1.2.1
Author: Bugsnag Inc.
Author URI: https://bugsnag.com
License: GPLv2 or later
*/
class Bugsnag_Wordpress
{
private static $COMPOSER_AUTOLOADER = 'vendor/autoload.php';
private static $PACKAGED_AUTOLOADER = 'bugsnag-php/Autoload.php';
private static $DEFAULT_NOTIFY_SEVERITIES = 'fatal,error';
private static $NOTIFIER = array(
'name' => 'Bugsnag Wordpress (Official)',
'version' => '1.2.1',
'url' => 'https://bugsnag.com/notifiers/wordpress'
);
private $client;
private $apiKey;
private $notifySeverities;
private $filterFields;
private $pluginBase;
public function __construct()
{
// Activate bugsnag error monitoring as soon as possible
$this->activateBugsnag();
$this->pluginBase = 'bugsnag/bugsnag.php';
// Run init actions (loading wp user)
add_action('init', array($this, 'initActions'));
// Load admin actions (admin links and pages)
add_action('admin_menu', array($this, 'adminMenuActions'));
// Load network admin menu if using multisite
add_action('network_admin_menu', array($this, 'networkAdminMenuActions'));
add_action('wp_ajax_test_bugsnag', array($this, 'testBugsnag'));
}
private function activateBugsnag()
{
// Require bugsnag-php
if(file_exists($this->relativePath(self::$COMPOSER_AUTOLOADER))) {
require_once $this->relativePath(self::$COMPOSER_AUTOLOADER);
} elseif (file_exists($this->relativePath(self::$PACKAGED_AUTOLOADER))) {
require_once $this->relativePath(self::$PACKAGED_AUTOLOADER);
} else {
error_log("Bugsnag Error: Couldn't activate Bugsnag Error Monitoring due to missing Bugsnag library!");
return;
}
// Load bugsnag settings
if ( ! get_site_option('bugsnag_network')) {
// Regular
$this->apiKey = get_option( 'bugsnag_api_key' );
$this->notifySeverities = get_option( 'bugsnag_notify_severities' );
$this->filterFields = get_option( 'bugsnag_filterfields' );
} else {
// Multisite
$this->apiKey = get_site_option( 'bugsnag_api_key' );
$this->notifySeverities = get_site_option( 'bugsnag_notify_severities' );
$this->filterFields = get_site_option( 'bugsnag_filterfields' );
}
$this->constructBugsnag();
}
private function constructBugsnag() {
// Activate the bugsnag client
if(!empty($this->apiKey)) {
$this->client = new Bugsnag_Client($this->apiKey);
$this->client->setReleaseStage($this->releaseStage())
->setErrorReportingLevel($this->errorReportingLevel())
->setFilters($this->filterFields());
$this->client->setNotifier(self::$NOTIFIER);
// Hook up automatic error handling
set_error_handler(array($this->client, "errorHandler"));
set_exception_handler(array($this->client, "exceptionHandler"));
}
}
private function relativePath($path)
{
return dirname(__FILE__) . '/' . $path;
}
private function errorReportingLevel()
{
$notifySeverities = empty($this->notifySeverities) ? self::$DEFAULT_NOTIFY_SEVERITIES : $this->notifySeverities;
$level = 0;
$severities = explode(",", $notifySeverities);
foreach($severities as $severity) {
$level |= Bugsnag_ErrorTypes::getLevelsForSeverity($severity);
}
return $level;
}
private function filterFields()
{
return array_map('trim', explode("\n", $this->filterFields));
}
private function releaseStage()
{
return defined('WP_ENV') ? WP_ENV : "production";
}
// Action hooks
public function initActions()
{
// Set the bugsnag user using the current WordPress user if available
$wpUser = wp_get_current_user();
if(!empty($this->client) && !empty($wpUser)) {
$user = array();
if(!empty($wpUser->user_login)) {
$user['id'] = $wpUser->user_login;
}
if(!empty($wpUser->user_email)) {
$user['email'] = $wpUser->user_email;
}
if(!empty($wpUser->user_display_name)) {
$user['name'] = $wpUser->user_display_name;
}
$this->client->setUser($user);
}
}
public function adminMenuActions()
{
if ( ! function_exists( 'is_plugin_active_for_network' ) || ! is_plugin_active_for_network($this->pluginBase)) {
// Add the "settings" link to the Bugsnag row of plugins.php
add_filter('plugin_action_links', array($this, 'pluginActionLinksFilter'), 10, 2);
// Create the settings page
add_options_page('Bugsnag Settings', 'Bugsnag', 'manage_options', 'bugsnag', array($this, 'renderSettings'));
}
}
public function networkAdminMenuActions()
{
if (function_exists('is_plugin_active_for_network') && is_plugin_active_for_network($this->pluginBase)) {
// Create the network settings page
add_submenu_page('settings.php', 'Bugsnag Settings', 'Bugsnag', 'manage_network_options', 'bugsnag', array($this, 'renderSettings'));
}
}
private function updateNetworkSettings( $settings )
{
// Update options
update_site_option('bugsnag_api_key', isset($_POST['bugsnag_api_key']) ? $_POST['bugsnag_api_key'] : '');
update_site_option('bugsnag_notify_severities', isset($_POST['bugsnag_notify_severities']) ? $_POST['bugsnag_notify_severities'] : '');
update_site_option('bugsnag_filterfields', isset($_POST['bugsnag_filterfields']) ? $_POST['bugsnag_filterfields'] : '');
update_site_option('bugsnag_network', true);
// Update variables
$this->apiKey = get_site_option( 'bugsnag_api_key' );
$this->notifySeverities = get_site_option( 'bugsnag_notify_severities' );
$this->filterFields = get_site_option( 'bugsnag_filterfields' );
echo '<div class="updated"><p>Settings saved.</p></div>';
}
// Filter hooks
public function pluginActionLinksFilter($links, $file)
{
// Add the "settings" link to the Bugsnag plugin row
if(basename($file) == basename(__FILE__)) {
$settings_link = '<a href="options-general.php?page=bugsnag">Settings</a>';
array_push($links, $settings_link);
}
return $links;
}
public function testBugsnag()
{
$this->apiKey = $_POST["bugsnag_api_key"];
$this->notifySeverities = $_POST['bugsnag_notify_severities'];
$this->filterFields = $_POST['bugsnag_filterfields'];
$this->constructBugsnag();
$this->client->notifyError('BugsnagTest', 'Testing bugsnag',
array('notifier' => self::$NOTIFIER));
die();
}
// Renderers
public function renderSettings()
{
if ( ! empty($_POST[ 'action' ]) && $_POST[ 'action' ] == 'update') {
$this->updateNetworkSettings( $_POST );
}
include $this->relativePath('views/settings.php');
}
private function renderOption($name, $value, $current)
{
$selected = ($value == $current) ? " selected=\"selected\"" : "";
echo "<option value=\"$value\"$selected>$name</option>";
}
/**
* Fluent interface to $this->client, simply call the methods on this object and this will proxy them through.
*
* @param $method
* @param $arguments
*
* @return mixed
*/
public function __call($method, $arguments)
{
if (method_exists($this->client, $method)) {
return call_user_func_array(array($this->client, $method), $arguments);
}
throw new BadMethodCallException(sprintf('Method %s does not exist on %s or Bugsnag_Client', $method, __CLASS__ ));
}
}
$bugsnagWordpress = new Bugsnag_Wordpress();