diff --git a/includes/BetterStackService.php b/includes/BetterStackService.php index 1e99ffe..6a32448 100644 --- a/includes/BetterStackService.php +++ b/includes/BetterStackService.php @@ -1,88 +1,96 @@ - trim( $message[0] ), - 'nested' => [], - ]; - - if ( isset( $message[1] ) ) { - $data['nested']['stack_trace'] = explode( "\n", trim( $message[1] ) ); - } - - if ( $user_id = get_current_user_id() ) { - $data['nested']['user_id'] = $user_id; - } - - if ( isset( $_SERVER['REQUEST_URI'] ) ) { - $data['nested']['request'] = $_SERVER['REQUEST_URI']; - } - - if ( isset( $_SERVER['HTTP_REFERER'] ) ) { - $data['nested']['referer'] = $_SERVER['HTTP_REFERER']; - } else { - $data['nested']['referer'] = 'unknown'; - } - - if ( isset( $error['type'] ) ) { - $data['nested']['type'] = $error['type']; - } - - $json = json_encode( $data ); - - $result = wp_remote_post( 'https://in.logs.betterstack.com', [ - 'headers' => [ - 'Authorization' => 'Bearer ' . BETTERSTACK_LOGS_SOURCE_TOKEN, - 'Content-Type' => 'application/json' - ], - 'body' => $json, - ] ); - - return $result; +BetterStackService::init(); + +class BetterStackService +{ + public static function init() + { + add_action('admin_init', [self::class, 'add_settings']); + } + + public static function sendLog($data) + { + $json = json_encode($data); + $host = Plugin::getConfig('betterstack_url', ''); + //check $host has https or domain + if (empty($host) || (! str_starts_with($host, 'https://') && ! str_starts_with($host, 'http://'))) { + $host = 'https://' . $host; + } + + $result = wp_remote_post($host, [ + 'headers' => [ + 'Authorization' => 'Bearer '.Plugin::getConfig('betterstack_token'), + 'Content-Type' => 'application/json' + ], + 'body' => $json, + ]); + + return $result; + } + + public static function add_settings() + { + + add_settings_section( + 'fatal_error_sentinel_betterstack_settings', + 'BetterStack Logs Integration', + function () { + ?> +
Configure the BetterStack Logs settings for Fatal Error Sentinel.
+Add source here https://telemetry.betterstack.com/
+ ', + esc_attr(Plugin::getConfigFieldName('betterstack_enabled')), + $checked + ); + echo 'Check to enable BetterStack Logs integration for fatal errors.
'; + }, + 'fatal-error-sentinel', + 'fatal_error_sentinel_betterstack_settings' + ); + + add_settings_field( + 'betterstack_token', + 'BetterStack Logs Source Token', + function () { + printf( + '', + esc_attr(Plugin::getConfigFieldName('betterstack_token')), + esc_attr(Plugin::getConfig('betterstack_token', '')) + ); + echo 'Enter the BetterStack Logs Source Token used to send logs. You can find this token in your BetterStack Logs account.
'; + }, + 'fatal-error-sentinel', + 'fatal_error_sentinel_betterstack_settings' + ); + + //add settings field - url + add_settings_field( + 'betterstack_url', + 'BetterStack Logs URL', + function () { + printf( + '', + esc_attr(Plugin::getConfigFieldName('betterstack_url')), + esc_attr(Plugin::getConfig('betterstack_url', '')) + ); + echo 'Enter the BetterStack Logs URL'; + }, + 'fatal-error-sentinel', + 'fatal_error_sentinel_betterstack_settings' + ); + } } - -/** - * simple test for check BetterStack - * - * 1. just run {{siteUrl}}/?test_BetterStackLogsIntegration - * 2. check log https://logs.betterstack.com/ - */ -add_action( 'init', function () { - - if ( ! isset( $_GET['test_BetterStackLogsIntegration'] ) ) { - return; - } - - test_wrong_function(); -} ); diff --git a/includes/EmailService.php b/includes/EmailService.php new file mode 100644 index 0000000..f0701d0 --- /dev/null +++ b/includes/EmailService.php @@ -0,0 +1,57 @@ +Configure the email settings for Fatal Error Sentinel.
'; + }, + 'fatal-error-sentinel' + ); + + add_settings_field( + 'email_enabled', + 'Enable Email Notifications', + function () { + $checked = Plugin::getConfig('email_enabled', false) ? 'checked' : ''; + printf( + '', + esc_attr(Plugin::getConfigFieldName('email_enabled')), + $checked + ); + echo 'Check to enable email notifications for fatal errors.
'; + }, + 'fatal-error-sentinel', + 'fatal_error_sentinel_email_settings' + ); + + add_settings_field( + 'notification_email', + 'Notification Email Address', + function () { + printf( + '', + esc_attr(Plugin::getConfigFieldName('notification_email')), + esc_attr(Plugin::getConfig('notification_email', get_option('admin_email'))) + ); + echo 'Enter the email address where fatal error notifications will be sent. Defaults to the site admin email if left blank.
'; + }, + 'fatal-error-sentinel', + 'fatal_error_sentinel_email_settings' + ); + } +} diff --git a/includes/TelegramService.php b/includes/TelegramService.php index 80f727a..e7102f6 100644 --- a/includes/TelegramService.php +++ b/includes/TelegramService.php @@ -16,7 +16,7 @@ public static function add_settings() add_settings_section( 'fatal_error_sentinel_telegram_settings', - 'Telegram', + 'Telegram Notifications', function () { echo 'Configure the Telegram settings for Fatal Error Sentinel.
'; }, @@ -69,4 +69,4 @@ function () { 'fatal_error_sentinel_telegram_settings' ); } -} \ No newline at end of file +} diff --git a/plugin.php b/plugin.php index 497c3fe..cba2d59 100644 --- a/plugin.php +++ b/plugin.php @@ -9,95 +9,219 @@ * Author URI: https://github.com/aiiddqd * Domain Path: /languages * Text Domain: fatal-error-sentinel - * Version: 0.3.250928 + * Version: 0.3.251016 */ namespace FatalErrorSentinel; if (! defined('ABSPATH')) { - exit; // Exit if accessed directly. + exit; // Exit if accessed directly. } Plugin::init(); class Plugin { - public static function init() - { - - add_action('admin_menu', [self::class, 'settings_page']); - add_action('admin_init', [self::class, 'add_settings']); - add_filter('plugin_action_links_'.plugin_basename(__FILE__), [self::class, 'addSettingsLink'] ); - - require_once __DIR__.'/includes/TelegramService.php'; - - - } - - - public static function addSettingsLink($links) - { - $settings_link = 'Settings'; - array_unshift($links, $settings_link); - return $links; - } - - public static function add_settings() - { - register_setting('fatal_error_sentinel_options', 'fatal_error_sentinel_config'); - add_settings_section( - 'fatal_error_sentinel_settings_general', - 'General', - function () { - echo 'Configure the general settings for Fatal Error Sentinel.
'; - }, - 'fatal-error-sentinel' - ); - - } - - - - //add settings page - public static function settings_page() - { - add_options_page( - 'Fatal Error Sentinel Settings', - 'Fatal Error Sentinel', - 'manage_options', - 'fatal-error-sentinel', - function () { ?> -Configure the general settings for Fatal Error Sentinel.
+Test link: Check
+ + +