diff --git a/bugsnag.php b/bugsnag.php index 2f71fe3..04fb6b4 100644 --- a/bugsnag.php +++ b/bugsnag.php @@ -9,6 +9,11 @@ License: GPLv2 or later */ +use Bugsnag\Client; +use Bugsnag\Handler; +use Bugsnag\ErrorTypes; +use Bugsnag\Report; + class Bugsnag_Wordpress { private static $COMPOSER_AUTOLOADER = 'vendor/autoload.php'; @@ -21,10 +26,11 @@ class Bugsnag_Wordpress 'url' => 'https://github.com/bugsnag/bugsnag-wordpress', ); - private $client; + private Client $client; private $apiKey; private $notifySeverities; private $filterFields; + private $trackSessions; private $pluginBase; public function __construct() @@ -32,10 +38,13 @@ public function __construct() // Activate bugsnag error monitoring as soon as possible $this->activateBugsnag(); + // Start session tracking if enabled as early as possible + $this->maybeTrackSession(); + $this->pluginBase = 'bugsnag/bugsnag.php'; // Run init actions (loading wp user) - add_action('init', array($this, 'initActions')); + add_action('init', array($this, 'registerUser')); // Load admin actions (admin links and pages) add_action('admin_menu', array($this, 'adminMenuActions')); @@ -61,11 +70,13 @@ private function activateBugsnag() $this->apiKey = get_option('bugsnag_api_key'); $this->notifySeverities = get_option('bugsnag_notify_severities'); $this->filterFields = get_option('bugsnag_filterfields'); + $this->trackSessions = get_option('bugsnag_track_sessions'); } 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->trackSessions = get_site_option('bugsnag_track_sessions'); } $this->constructBugsnag(); @@ -75,13 +86,13 @@ private function constructBugsnag() { // Activate the bugsnag client if (!empty($this->apiKey)) { - $this->client = new Bugsnag_Client($this->apiKey); + $this->client = Client::make($this->apiKey); $this->client->setReleaseStage($this->releaseStage()) ->setErrorReportingLevel($this->errorReportingLevel()) - ->setFilters($this->filterFields()); + ->setRedactedKeys($this->filterFields()); - $this->client->mergeDeviceData(['runtimeVersions' => ['wordpress' => get_bloginfo('version')]]); + $this->client->getConfig()->mergeDeviceData(['runtimeVersions' => ['wordpress' => get_bloginfo('version')]]); $this->client->setNotifier(self::$NOTIFIER); @@ -96,8 +107,7 @@ private function constructBugsnag() if ($set_error_and_exception_handlers === true) { // Hook up automatic error handling - set_error_handler(array($this->client, 'errorHandler')); - set_exception_handler(array($this->client, 'exceptionHandler')); + Handler::register($this->client); } } } @@ -105,7 +115,7 @@ private function constructBugsnag() private function requireBugsnagPhp() { // Bugsnag-php was already loaded by some 3rd-party code, don't need to load it again. - if (class_exists('Bugsnag_Client')) { + if (class_exists('Bugsnag\Client')) { return true; } @@ -142,7 +152,7 @@ private function errorReportingLevel() $severities = explode(',', $notifySeverities); foreach ($severities as $severity) { - $level |= Bugsnag_ErrorTypes::getLevelsForSeverity($severity); + $level |= ErrorTypes::getLevelsForSeverity($severity); } return $level; @@ -177,36 +187,38 @@ private function releaseStage() return $release_stage_filtered; } - // Action hooks - public function initActions() + private function maybeTrackSession() { - // This should be handled on stage of initializing, - // not even adding action if init failed. - // - // Leaving it here for now. - if (empty($this->client)) { + if(!checked($this->trackSessions, true, false)) { return; } - // Set the bugsnag user using the current WordPress user if available, - // set as anonymous otherwise. - $user = array(); - if (is_user_logged_in()) { - $wp_user = wp_get_current_user(); + $this->client->startSession(); + } - // Removed checks for !empty($wp_user->display_name), it should not be required. - $user['id'] = $wp_user->user_login; - $user['email'] = $wp_user->user_email; - $user['name'] = $wp_user->display_name; - } else { - $use_unsafe_spoofable_ip_address_getter = apply_filters('bugsnag_use_unsafe_spoofable_ip_address_getter', true); - $user['id'] = $use_unsafe_spoofable_ip_address_getter ? - $this->getClientIpAddressUnsafe() : - $this->getClientIpAddress(); - $user['name'] = 'anonymous'; - } + // Action hooks + public function registerUser() + { + $this->client->registerCallback(function(Report $report) { + // Set the bugsnag user using the current WordPress user if available, + // set as anonymous otherwise. + $user = []; + + if (is_user_logged_in()) { + $wp_user = wp_get_current_user(); + $user['id'] = $wp_user->user_login; + $user['email'] = $wp_user->user_email; + $user['name'] = $wp_user->display_name; + } else { + $use_unsafe_spoofable_ip_address_getter = apply_filters('bugsnag_use_unsafe_spoofable_ip_address_getter', true); + $user['id'] = $use_unsafe_spoofable_ip_address_getter ? + $this->getClientIpAddressUnsafe() : + $this->getClientIpAddress(); + $user['name'] = 'anonymous'; + } - $this->client->setUser($user); + $report->setUser($user); + }); } // Unsafe: client can spoof address. @@ -256,12 +268,14 @@ private function updateNetworkSettings($settings) 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_track_sessions', isset($_POST['bugsnag_track_sessions']) ? $_POST['bugsnag_track_sessions'] : ''); 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'); + $this->trackSessions = get_site_option('bugsnag_track_sessions'); echo '
Settings saved.
+ Enable automatic session tracking to monitor stability metrics in your BugSnag dashboard.
+
Read more about stability metrics.
+