Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 92 additions & 84 deletions includes/BetterStackService.php
Original file line number Diff line number Diff line change
@@ -1,88 +1,96 @@
<?php
<?php

namespace FatalErrorSentinel;


if ( defined( 'BETTERSTACK_LOGS_SOURCE_TOKEN' ) ) {
add_action( 'shutdown', function () {

$error = error_get_last();

if ( is_null( $error ) ) {
return;
}

if ( $error['type'] != E_ERROR ) {
return;
}

send_to_betterstack( $error );
}, 1 );

add_filter( 'wp_php_error_message', function ($message, $error) {

send_to_betterstack( $error );

return $message;
}, 11, 2 );

}


function send_to_betterstack( $error ) {
$message = explode( 'Stack trace:', $error['message'] );

$data = [
'message' => 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 () {
?>
<p>Configure the BetterStack Logs settings for Fatal Error Sentinel.</p>
<p>Add source here <a href="https://telemetry.betterstack.com/" target="_blank">https://telemetry.betterstack.com/</a></p>
<?php
},
'fatal-error-sentinel'
);

add_settings_field(
'betterstack_enabled',
'Enable BetterStack Logs Integration',
function () {
$checked = Plugin::getConfig('betterstack_enabled', false) ? 'checked' : '';
printf(
'<input type="checkbox" name="%s" value="1" %s>',
esc_attr(Plugin::getConfigFieldName('betterstack_enabled')),
$checked
);
echo '<p class="description">Check to enable BetterStack Logs integration for fatal errors.</p>';
},
'fatal-error-sentinel',
'fatal_error_sentinel_betterstack_settings'
);

add_settings_field(
'betterstack_token',
'BetterStack Logs Source Token',
function () {
printf(
'<input type="text" name="%s" value="%s" class="regular-text">',
esc_attr(Plugin::getConfigFieldName('betterstack_token')),
esc_attr(Plugin::getConfig('betterstack_token', ''))
);
echo '<p class="description">Enter the BetterStack Logs Source Token used to send logs. You can find this token in your BetterStack Logs account.</p>';
},
'fatal-error-sentinel',
'fatal_error_sentinel_betterstack_settings'
);

//add settings field - url
add_settings_field(
'betterstack_url',
'BetterStack Logs URL',
function () {
printf(
'<input type="text" name="%s" value="%s" class="regular-text">',
esc_attr(Plugin::getConfigFieldName('betterstack_url')),
esc_attr(Plugin::getConfig('betterstack_url', ''))
);
echo '<p class="description">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();
} );
57 changes: 57 additions & 0 deletions includes/EmailService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace FatalErrorSentinel;

EmailService::init();

class EmailService
{
public static function init()
{
add_action('admin_init', [self::class, 'add_settings']);
}

public static function add_settings()
{

add_settings_section(
'fatal_error_sentinel_email_settings',
'Email Notifications',
function () {
echo '<p>Configure the email settings for Fatal Error Sentinel.</p>';
},
'fatal-error-sentinel'
);

add_settings_field(
'email_enabled',
'Enable Email Notifications',
function () {
$checked = Plugin::getConfig('email_enabled', false) ? 'checked' : '';
printf(
'<input type="checkbox" name="%s" value="1" %s>',
esc_attr(Plugin::getConfigFieldName('email_enabled')),
$checked
);
echo '<p class="description">Check to enable email notifications for fatal errors.</p>';
},
'fatal-error-sentinel',
'fatal_error_sentinel_email_settings'
);

add_settings_field(
'notification_email',
'Notification Email Address',
function () {
printf(
'<input type="email" name="%s" value="%s" class="regular-text">',
esc_attr(Plugin::getConfigFieldName('notification_email')),
esc_attr(Plugin::getConfig('notification_email', get_option('admin_email')))
);
echo '<p class="description">Enter the email address where fatal error notifications will be sent. Defaults to the site admin email if left blank.</p>';
},
'fatal-error-sentinel',
'fatal_error_sentinel_email_settings'
);
}
}
4 changes: 2 additions & 2 deletions includes/TelegramService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function add_settings()

add_settings_section(
'fatal_error_sentinel_telegram_settings',
'Telegram',
'Telegram Notifications',
function () {
echo '<p>Configure the Telegram settings for Fatal Error Sentinel.</p>';
},
Expand Down Expand Up @@ -69,4 +69,4 @@ function () {
'fatal_error_sentinel_telegram_settings'
);
}
}
}
Loading