From 2a6c7a892fe184ac58bab4bd8c9a2b9357673fcb Mon Sep 17 00:00:00 2001 From: Anatolii Iumashev Date: Mon, 29 Sep 2025 08:40:08 +0300 Subject: [PATCH 1/7] Enhance settings page with additional links and update section title for clarity --- plugin.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/plugin.php b/plugin.php index 497c3fe..0fa2757 100644 --- a/plugin.php +++ b/plugin.php @@ -47,9 +47,17 @@ public static function add_settings() register_setting('fatal_error_sentinel_options', 'fatal_error_sentinel_config'); add_settings_section( 'fatal_error_sentinel_settings_general', - 'General', + 'General Settings', function () { - echo '

Configure the general settings for Fatal Error Sentinel.

'; + ?> + +

Configure the general settings for Fatal Error Sentinel.

+ +
-

Fatal Error Sentinel Settings

+

Fatal Error Sentinel

Date: Fri, 3 Oct 2025 17:39:57 +0300 Subject: [PATCH 2/7] Add EmailService for email notifications configuration --- includes/EmailService.php | 57 ++++++++ includes/TelegramService.php | 4 +- plugin.php | 270 +++++++++++++++++++++++------------ 3 files changed, 241 insertions(+), 90 deletions(-) create mode 100644 includes/EmailService.php 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 0fa2757..0ccc94c 100644 --- a/plugin.php +++ b/plugin.php @@ -15,97 +15,191 @@ 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 Settings', - function () { - ?> - -

Configure the general settings for Fatal Error Sentinel.

- - -
-

Fatal Error Sentinel

- - - -
- 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']; + } + error_log('test: ' . print_r($data, true)); + do_action('fatal_error_sentinel_send_error', $data); + } + + public static function isEnabled() + { + if (self::getConfig('telegram_enabled', false)) { + return true; + } + + if (self::getConfig('email_enabled', false)) { + return true; + } + + return false; + } + + + + 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 Settings', + function () { + ?> + +

Configure the general settings for Fatal Error Sentinel.

+ + +
+

Fatal Error Sentinel

+
+ +
+
+ Date: Sun, 5 Oct 2025 17:13:18 +0300 Subject: [PATCH 3/7] Refactor BetterStack integration: streamline error handling and enhance settings configuration --- includes/BetterStackService.php | 176 +++++++++++++++++--------------- plugin.php | 109 +++++++++++++------- 2 files changed, 165 insertions(+), 120 deletions(-) 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/plugin.php b/plugin.php index 0ccc94c..d737113 100644 --- a/plugin.php +++ b/plugin.php @@ -25,54 +25,51 @@ 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'; - require_once __DIR__.'/includes/EmailService.php'; - - self::catchErrors(); - - add_filter('testeroid_tests', function($tests) { - $tests['fatal-error-sentinel'] = function(){ - - return true; - }; - return $tests; + add_action('admin_init', function () { + if (! isset($_GET['dddd'])) + return; + va1r_dump(1); + exit; }); - } - - public static function catchErrors() - { - if (! self::isEnabled()) { - return; - } - - add_action('shutdown', function () { - $error = error_get_last(); + /** + * simple test for check BetterStack + * + * 1. just run {{siteUrl}}/?test_BetterStackLogsIntegration + * 2. check logs https://telemetry.betterstack.com/ + */ + add_action('init', function () { - if (is_null($error)) { + if (! isset($_GET['test_FatalErrorSentinel'])) { return; } - if ($error['type'] != E_ERROR) { - return; - } + // $r = BetterStackService::sendLog([ + // 'message' => 'Test Fatal Error Sentinel 2', + // 'nested' => [ + // 'test_field' => 'test_value', + // ], + // ]); + // var_dump($r); + // asdfsdf(); + // exit; + }); - self::send_error($error); - }, 1); - add_filter('wp_php_error_message', function ($message, $error) { + 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']); - self::send_error($error); + require_once __DIR__.'/includes/TelegramService.php'; + require_once __DIR__.'/includes/EmailService.php'; + require_once __DIR__.'/includes/BetterStackService.php'; + + self::catchErrors(); - return $message; - }, 11, 2); } + public static function send_error($error) { $message = explode('Stack trace:', $error['message']); @@ -103,10 +100,46 @@ public static function send_error($error) if (isset($error['type'])) { $data['nested']['type'] = $error['type']; } - error_log('test: ' . print_r($data, true)); + // error_log('test: '.print_r($data, true)); + // var_dump($data); exit; + // require_once __DIR__.'/includes/BetterStackService.php'; + + + BetterStackService::sendLog($data); + do_action('fatal_error_sentinel_send_error', $data); } + + public static function catchErrors() + { + if (! self::isEnabled()) { + return; + } + + add_action('shutdown', function () { + + $error = error_get_last(); + + if (is_null($error)) { + return; + } + + if ($error['type'] != E_ERROR) { + return; + } + + self::send_error($error); + }, 1); + + add_filter('wp_php_error_message', function ($message, $error) { + + self::send_error($error); + + return $message; + }, 11, 2); + } + public static function isEnabled() { if (self::getConfig('telegram_enabled', false)) { @@ -117,6 +150,10 @@ public static function isEnabled() return true; } + if (self::getConfig('betterstack_enabled', false)) { + return true; + } + return false; } From 0e37a132e0dac60d4a384421850b2c834e6ccf83 Mon Sep 17 00:00:00 2001 From: Anatolii Iumashev Date: Sun, 5 Oct 2025 17:17:18 +0300 Subject: [PATCH 4/7] Refactor Plugin initialization: remove unused admin_init action and clean up error logging --- plugin.php | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/plugin.php b/plugin.php index d737113..dc1bde6 100644 --- a/plugin.php +++ b/plugin.php @@ -25,14 +25,6 @@ class Plugin public static function init() { - add_action('admin_init', function () { - if (! isset($_GET['dddd'])) - return; - va1r_dump(1); - exit; - }); - - /** * simple test for check BetterStack * @@ -44,16 +36,8 @@ public static function init() if (! isset($_GET['test_FatalErrorSentinel'])) { return; } + asdfsdf2(); - // $r = BetterStackService::sendLog([ - // 'message' => 'Test Fatal Error Sentinel 2', - // 'nested' => [ - // 'test_field' => 'test_value', - // ], - // ]); - // var_dump($r); - // asdfsdf(); - // exit; }); @@ -100,12 +84,10 @@ public static function send_error($error) if (isset($error['type'])) { $data['nested']['type'] = $error['type']; } - // error_log('test: '.print_r($data, true)); - // var_dump($data); exit; - // require_once __DIR__.'/includes/BetterStackService.php'; - - BetterStackService::sendLog($data); + if (self::getConfig('betterstack_enabled', false)) { + BetterStackService::sendLog($data); + } do_action('fatal_error_sentinel_send_error', $data); } From 4e92605f8d573c936f28108d40fdeb8530694eb8 Mon Sep 17 00:00:00 2001 From: Anatolii Iumashev Date: Wed, 15 Oct 2025 09:19:04 +0300 Subject: [PATCH 5/7] Update version to 0.3.251015 and add debug backtrace to error handling --- plugin.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugin.php b/plugin.php index dc1bde6..d9de232 100644 --- a/plugin.php +++ b/plugin.php @@ -9,7 +9,7 @@ * Author URI: https://github.com/aiiddqd * Domain Path: /languages * Text Domain: fatal-error-sentinel - * Version: 0.3.250928 + * Version: 0.3.251015 */ namespace FatalErrorSentinel; @@ -65,6 +65,8 @@ public static function send_error($error) if (isset($message[1])) { $data['nested']['stack_trace'] = explode("\n", trim($message[1])); + } else { + $data['nested']['debug_backtrace'] = debug_backtrace(); } if ($user_id = get_current_user_id()) { From 5fcda62f66acdad1c4a5e5278e50bf6bd6e64522 Mon Sep 17 00:00:00 2001 From: Anatolii Iumashev Date: Wed, 15 Oct 2025 09:39:16 +0300 Subject: [PATCH 6/7] Fix error handling: replace debug_backtrace with error data in send_error method --- plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.php b/plugin.php index d9de232..9e00872 100644 --- a/plugin.php +++ b/plugin.php @@ -66,7 +66,7 @@ public static function send_error($error) if (isset($message[1])) { $data['nested']['stack_trace'] = explode("\n", trim($message[1])); } else { - $data['nested']['debug_backtrace'] = debug_backtrace(); + $data['nested']['debug_backtrace'] = $error; } if ($user_id = get_current_user_id()) { From 575bcc9e38cec0ae500c434a0e48a2e4c185abb4 Mon Sep 17 00:00:00 2001 From: Anatolii Iumashev Date: Thu, 16 Oct 2025 07:59:57 +0300 Subject: [PATCH 7/7] Bump version to 0.3.251016 and add test link to settings page --- plugin.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin.php b/plugin.php index 9e00872..cba2d59 100644 --- a/plugin.php +++ b/plugin.php @@ -9,7 +9,7 @@ * Author URI: https://github.com/aiiddqd * Domain Path: /languages * Text Domain: fatal-error-sentinel - * Version: 0.3.251015 + * Version: 0.3.251016 */ namespace FatalErrorSentinel; @@ -168,6 +168,7 @@ function () {

  • Author: https://github.com/aiiddqd
  • Configure the general settings for Fatal Error Sentinel.

    +

    Test link: Check