73 lines
2.1 KiB
PHP
73 lines
2.1 KiB
PHP
<?php
|
|
/*
|
|
Plugin Name: Simple Glitchtip
|
|
Description: A simple plugin to handle Sentry/Glitchtip DSNs and report errors.
|
|
Version: 1.0
|
|
Author: Colin
|
|
Plugin URI: https://git.nixc.us/colin/simple-glitchtip
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit; // Exit if accessed directly.
|
|
}
|
|
|
|
// Register settings
|
|
function sg_register_settings() {
|
|
register_setting('sg-settings-group', 'sg_dsn');
|
|
}
|
|
|
|
// Settings page
|
|
function sg_settings_page() {
|
|
?>
|
|
<div class="wrap">
|
|
<h1>Simple Glitchtip Settings</h1>
|
|
<form method="post" action="options.php">
|
|
<?php settings_fields('sg-settings-group'); ?>
|
|
<?php do_settings_sections('sg-settings-group'); ?>
|
|
<table class="form-table">
|
|
<tr valign="top">
|
|
<th scope="row">DSN</th>
|
|
<td><input type="text" name="sg_dsn" value="<?php echo esc_attr(get_option('sg_dsn')); ?>" /></td>
|
|
</tr>
|
|
</table>
|
|
<?php submit_button(); ?>
|
|
</form>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
// Add settings menu
|
|
function sg_add_settings_menu() {
|
|
add_options_page('Simple Glitchtip Settings', 'Glitchtip', 'manage_options', 'sg-settings', 'sg_settings_page');
|
|
}
|
|
add_action('admin_menu', 'sg_add_settings_menu');
|
|
add_action('admin_init', 'sg_register_settings');
|
|
|
|
// Initialize error handler
|
|
function sg_initialize_error_handler($dsn) {
|
|
if (!class_exists('Sentry\State\Hub')) {
|
|
require_once plugin_dir_path(__FILE__) . 'sentry.phar';
|
|
}
|
|
|
|
Sentry\init(['dsn' => $dsn]);
|
|
|
|
function handle_exception($exception) {
|
|
Sentry\captureException($exception);
|
|
}
|
|
set_exception_handler('handle_exception');
|
|
|
|
function handle_error($errno, $errstr, $errfile, $errline) {
|
|
Sentry\captureMessage("$errstr in $errfile on line $errline");
|
|
}
|
|
set_error_handler('handle_error');
|
|
}
|
|
|
|
// Initialize the plugin
|
|
function sg_initialize_plugin() {
|
|
$dsn = getenv('SENTRY_DSN') ?: getenv('GLITCHTIP_DSN') ?: get_option('sg_dsn');
|
|
if ($dsn) {
|
|
sg_initialize_error_handler($dsn);
|
|
}
|
|
}
|
|
add_action('plugins_loaded', 'sg_initialize_plugin');
|