Jake Vanderwerf
2026-04-15 c4aa5cdb5e90ad4b420e22772797d16980232a2b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
namespace JVBase\managers;
 
use JVBase\JVB;
use WP_Error;
use Exception;
use WP_REST_Request;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
class ErrorHandler
{
    protected object $wpdb;
    protected string $tableName;
    protected int $notification_threshold = 5; // Critical errors within 1 hour
 
    protected array $error_levels = [
        'debug' => 0,
        'info' => 1,
        'warning' => 2,
        'error' => 3,
        'critical' => 4
    ];
 
    protected CustomTable $table;
 
    public function __construct()
    {
        $this->defineTables();
//        global $wpdb;
//        $this->wpdb = $wpdb;
//      $this->tableName = $wpdb->prefix . BASE . 'error_log';
 
        add_filter(BASE.'handle_bulk_operation', [$this, 'processOperation'], 10, 3);
 
        // Hook our send function to the cron event
        add_action('jvb_daily_error_summary', [$this, 'sendErrorSummary']);
 
        jvb_register_do_once('error_admin_action_registered', [$this, 'registerAdminAction']);
//        add_filter(BASE.'admin_action_filter', [$this, 'adminActionFilter'], 10, 3);
    }
 
    public function defineTables():void
    {
        $table = CustomTable::for('error_log');
        $table->setColumns([
            'id'            => 'bigint(20) unsigned NOT NULL AUTO_INCREMENT',
            'error_type'    => 'varchar(50) NOT NULL',
            'component'     => 'varchar(100) NOT NULL',
            'method'        => 'varchar(100) DEFAULT NULL',
            'page_url'      => 'varchar(255) DEFAULT NULL',
            'message'       => 'text NOT NULL',
            'context'       => 'JSON',
            'severity'      => 'ENUM(\'high\',\'normal\',\'low\') DEFAULT \'normal\'',
            'user_id'       => $table->getUserIDType().' DEFAULT NULL',
            'user_was_logged_in'    => 'tinyint(1) NOT NULL',
            'source'        => 'ENUM(\'frontend\', \'backend\') NOT NULL',
            'created_at'    => 'timestamp DEFAULT CURRENT_TIMESTAMP',
        ]);
 
        $table->setKeys([
            ['key' => 'PRIMARY', 'value' => 'id'],
            '`created_at` (`created_at`)',
            '`component_severity_date` (`component`, `severity`, `created_at`)',
            '`error_type_date` (`error_type`, `created_at`)',
            '`severity_date` (`severity`, `created_at`)'
        ]);
 
        $table->defineTable();
        $this->table = $table;
    }
    public function registerAdminAction():void
    {
        $admin = JVB()->admin();
 
        $admin->registerSubpage(
            'Error Log',
            'Errors',
            'manage_options',
            'jvb-error-log',
            [$this, 'renderAdminPage'],
        );
    }
 
    /**
     * Process error log operations queued through OperationQueue
     *
     * @param WP_Error|array $result The result we are filtering
     * @param object $operation The operation information
     * @param array $data The current chunk of data to process
     *
     * @return WP_Error|array Either the unfiltered WP_Error, or the result of the operation
     */
    public function processOperation(WP_Error|array $result, object $operation, array $data):WP_Error|array
    {
        if ($operation->type !== 'error_log') {
            return $result;
        }
 
        try {
            $table = $this->tableName;
            // Extract error data
            $component = sanitize_text_field($data['component'] ?? '');
            $message = sanitize_textarea_field($data['message'] ?? '');
            $context = $data['context'] ?? [];
            $severity = sanitize_text_field($data['severity'] ?? 'error');
 
            // Validate severity level
            if (!array_key_exists($severity, $this->error_levels)) {
                $severity = 'error'; // Default to error if invalid severity provided
            }
 
            // Determine error type from context or use component as fallback
            $error_type = sanitize_text_field($context['error_type'] ?? $component);
 
            // Store context as JSON
            $context_json = json_encode($context);
            if (json_last_error() !== JSON_ERROR_NONE) {
                // If JSON encoding fails, store only basic info
                $context_json = json_encode([
                    'error' => 'Failed to encode original context',
                    'json_error' => json_last_error_msg()
                ]);
            }
 
            // Insert into database
            $result = $this->wpdb->insert(
                $table,
                [
                    'error_type' => $error_type,
                    'component' => $component,
                    'message' => $message,
                    'context' => $context_json,
                    'severity' => $severity,
                    'user_id' => get_current_user_id(),
                    'created_at' => current_time('mysql')
                ],
                [
                    '%s', // error_type
                    '%s', // component
                    '%s', // message
                    '%s', // context (JSON)
                    '%s', // severity
                    '%d', // user_id
                    '%s'  // created_at
                ]
            );
 
            if ($result === false) {
                // If insert fails, log to PHP error log as fallback
                error_log("[ErrorHandler] Database insert failed: " . $this->wpdb->last_error);
                return [
                    'success'   => false,
                    'message'   => "[ErrorHandler] Database insert failed: " . $this->wpdb->last_error
                ];
            }
 
            // Potentially track error metrics or trigger additional actions for critical errors
            if ($severity === 'critical') {
                $this->checkErrorThreshold($error_type, $component);
            }
 
            return [
                'success' => true,
                'result'    => $result
            ];
 
        } catch (Exception $e) {
            // Fallback to PHP error log if our error handler itself fails
            error_log("[ErrorHandler Exception] " . $e->getMessage());
            return [
                'success'   => false,
                'message'   => $e->getMessage()
            ];
        }
    }
 
    /**
     * Check if error threshold has been reached for a specific error type/component
     * and take additional actions if needed
     *
     * @param string $error_type The type of error
     * @param string $component The component where the error occurred
     */
    protected function checkErrorThreshold(string $error_type, string $component)
    {
        // Get count of similar critical errors in the last hour
        $count = $this->wpdb->get_var($this->wpdb->prepare(
            "SELECT COUNT(*)
         FROM {$this->tableName}
         WHERE error_type = %s
         AND component = %s
         AND severity = 'critical'
         AND created_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)",
            $error_type,
            $component
        ));
 
        // If threshold reached, take additional actions (e.g., notify developers)
        if ((int)$count >= $this->notification_threshold) {
            // You could send an urgent notification, Slack message, etc.
            $admin_email = get_option('admin_email');
            $subject = "[URGENT] Error Threshold Exceeded for {$component}";
            $message = "The error '{$error_type}' in component '{$component}' has occurred {$count} times in the last hour.";
 
            wp_mail($admin_email, $subject, $message);
        }
    }
 
 
    /**
     * @param string $component Where the error is being logged from
     * @param string $message The error message, usually extracted from $e->getMessage())
     * @param array $context Additional context (i.e: data that was attempted to be acted on)
     * @param string $severity
     *
     * @return bool Whether it gets logged successfully
     */
    public function log(string $component, string $message, array $context = [], string $severity = 'error'): array
    {
        try {
            $table = $this->wpdb->prefix . BASE . 'error_log';
 
            // Validate severity
            if (!array_key_exists($severity, $this->error_levels)) {
                $severity = 'error';
            }
 
            // Extract info
            $error_type = sanitize_text_field($context['error_type'] ?? $component);
            $method = isset($context['method']) ? sanitize_text_field($context['method']) : null;
            $page_url = isset($context['url']) ? esc_url_raw($context['url']) : null;
            $user_id = get_current_user_id();
            $user_was_logged_in = $user_id > 0 || (!empty($context['isLoggedIn']));
 
            // Determine source from context
            $source = isset($context['source']) ? $context['source'] :
                (isset($context['url']) ? 'frontend' : 'backend');
 
            $result = $this->wpdb->insert(
                $table,
                [
                    'error_type' => $error_type,
                    'component' => $component,
                    'method' => $method,
                    'page_url' => $page_url,
                    'message' => sanitize_textarea_field($message),
                    'context' => json_encode($context),
                    'severity' => $severity,
                    'user_id' => $user_id ?: null,
                    'user_was_logged_in' => $user_was_logged_in ? 1 : 0,
                    'source' => $source,
                    'created_at' => current_time('mysql')
                ],
                ['%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s']
            );
 
            if ($result === false) {
                error_log("[ErrorHandler] Database insert failed: " . $this->wpdb->last_error);
                return ['success' => false, 'message' => $this->wpdb->last_error];
            }
 
            if ($severity === 'critical') {
                $this->checkErrorThreshold($error_type, $component);
            }
 
            return ['success' => true, 'id' => $this->wpdb->insert_id];
 
        } catch (Exception $e) {
            error_log("[ErrorHandler Exception] " . $e->getMessage());
            return ['success' => false, 'message' => $e->getMessage()];
        }
    }
 
    /**
     * @param string $component What class or function logs the error
     * @param string $message Details on the error
     * @param array $context additional context of the data that was being processed
     *
     * @return bool Whether the notification is sent successfully
     */
    protected function notifyAdmin(string $component, string $message, array $context):bool
    {
        $admin_email = get_option('admin_email');
        $subject = "[" . get_bloginfo('name') . " Critical Error] {$component}";
 
        $body = JVB()->email()->alert(
            'A critical error has occurred and requires immediate attention',
            'error'
        );
 
        $body .= JVB()->email()->h2('Error Details');
        $body .= JVB()->email()->card(
            '<p><strong>Component:</strong> ' . esc_html($component) . '</p>' .
            '<p><strong>Message:</strong></p>' .
            JVB()->email()->codeBlock($message),
            'Error Information'
        );
 
        if (!empty($context)) {
            $body .= JVB()->email()->h3('Additional Context');
            $body .= JVB()->email()->codeBlock(json_encode($context, JSON_PRETTY_PRINT));
        }
 
        return JVB()->email()->sendEmail($admin_email, $subject, $body, 'CRITICAL ERROR');
    }
 
    /**
     * Gather summary of the most important errors
     * @param ?string $start_date Defaults to today
     * @param ?string $end_date Defaults to today
     * @return array
     */
    public function gatherErrorSummary(?string $start_date = null, ?string $end_date = null): array
    {
        $table = $this->wpdb->prefix . BASE . 'error_log';
 
        if (!$start_date) {
            $start_date = gmdate('Y-m-d 00:00:00', strtotime('-1 day'));
        }
        if (!$end_date) {
            $end_date = gmdate('Y-m-d 23:59:59');
        }
 
        // Most frequent error patterns (deduplicated by component/method/message)
        $frequent = $this->wpdb->get_results($this->wpdb->prepare(
            "SELECT
            component,
            method,
            error_type,
            message,
            severity,
            source,
            COUNT(*) as count,
            SUM(CASE WHEN user_was_logged_in = 1 THEN 1 ELSE 0 END) as logged_in_count,
            SUM(CASE WHEN user_was_logged_in = 0 THEN 1 ELSE 0 END) as logged_out_count,
            MIN(created_at) as first_seen,
            MAX(created_at) as last_seen
         FROM {$table}
         WHERE created_at BETWEEN %s AND %s
         GROUP BY component, method, error_type, message, severity, source
         ORDER BY count DESC, severity DESC
         LIMIT 10",
            $start_date,
            $end_date
        ));
 
        // Critical errors
        $critical = $this->wpdb->get_results($this->wpdb->prepare(
            "SELECT
            component,
            method,
            error_type,
            message,
            source,
            COUNT(*) as count,
            SUM(CASE WHEN user_was_logged_in = 1 THEN 1 ELSE 0 END) as logged_in_count,
            SUM(CASE WHEN user_was_logged_in = 0 THEN 1 ELSE 0 END) as logged_out_count,
            MIN(created_at) as first_seen,
            MAX(created_at) as last_seen
         FROM {$table}
         WHERE created_at BETWEEN %s AND %s AND severity = 'critical'
         GROUP BY component, method, error_type, message, source
         ORDER BY count DESC
         LIMIT 5",
            $start_date,
            $end_date
        ));
 
        // Overall stats
        $stats = $this->wpdb->get_row($this->wpdb->prepare(
            "SELECT
            COUNT(*) as total_errors,
            COUNT(DISTINCT CONCAT(component, '-', COALESCE(method, ''), '-', error_type)) as unique_error_types,
            SUM(CASE WHEN user_was_logged_in = 1 THEN 1 ELSE 0 END) as logged_in_errors,
            SUM(CASE WHEN user_was_logged_in = 0 THEN 1 ELSE 0 END) as logged_out_errors,
            SUM(CASE WHEN source = 'frontend' THEN 1 ELSE 0 END) as frontend_errors,
            SUM(CASE WHEN source = 'backend' THEN 1 ELSE 0 END) as backend_errors,
            SUM(CASE WHEN severity = 'critical' THEN 1 ELSE 0 END) as critical_count,
            SUM(CASE WHEN severity = 'error' THEN 1 ELSE 0 END) as error_count,
            SUM(CASE WHEN severity = 'warning' THEN 1 ELSE 0 END) as warning_count
         FROM {$table}
         WHERE created_at BETWEEN %s AND %s",
            $start_date,
            $end_date
        ));
 
        return [
            'frequent' => $frequent,
            'critical' => $critical,
            'stats' => $stats,
            'date_range' => ['start' => $start_date, 'end' => $end_date]
        ];
    }
 
    /**
     * Send daily error summary email to administrator
     * @return bool Whether email is sent
     */
    public function sendErrorSummary():bool
    {
        $summary = $this->gatherErrorSummary();
 
        if (empty($summary['frequent']) && empty($summary['critical'])) {
            return false;
        }
 
        $admin_email = get_option('admin_email');
        $site_name = get_bloginfo('name');
        $yesterday = date('Y-m-d', strtotime('-1 day'));
        $subject = "[{$site_name}] Daily Error Summary - " . date('Y-m-d');
 
        // Header with alert
        $body = JVB()->email()->h1('Daily Error Summary');
        $body .= sprintf('<p>Error summary for <strong>%s</strong></p>', $yesterday);
 
        // Summary stats in a grid
        if (!empty($summary['stats'])) {
            $stats = [
                JVB()->email()->stat($summary['stats']->total_errors, 'Total Errors'),
                JVB()->email()->stat($summary['stats']->critical_count, 'Critical', 'Requires attention'),
                JVB()->email()->stat($summary['stats']->error_count, 'Errors'),
                JVB()->email()->stat($summary['stats']->warning_count, 'Warnings')
            ];
            $body .= JVB()->email()->grid($stats, 4);
        }
 
        // Alert if critical errors exist
        if (!empty($summary['critical'])) {
            $body .= JVB()->email()->alert(
                sprintf('Found %d critical errors that need immediate attention', count($summary['critical'])),
                'error'
            );
        }
 
        $body .= JVB()->email()->spacer(20);
 
        // Frequent errors section
        if (!empty($summary['frequent'])) {
            $body .= JVB()->email()->h2('Most Frequent Errors');
 
            foreach ($summary['frequent'] as $error) {
                $cardContent = JVB()->email()->badge($error->count . 'x', 'warning') . ' ';
                $cardContent .= '<strong>' . esc_html($error->error_type) . '</strong>';
                $cardContent .= '<p style="margin:10px 0 5px 0;font-size:13px;">' . esc_html(wp_trim_words($error->message, 15)) . '</p>';
                $cardContent .= '<p style="margin:0;font-size:12px;color:' . JVB()->email()->colours['dark-200'] . ';">
                Source: ' . esc_html($error->source) . ' |
                Logged in: ' . $error->logged_in_count . ' |
                Logged out: ' . $error->logged_out_count . '
            </p>';
 
                $body .= JVB()->email()->card($cardContent, $error->component);
            }
        }
 
        // Critical errors section
        if (!empty($summary['critical'])) {
            $body .= JVB()->email()->spacer(30);
            $body .= JVB()->email()->h2('Recent Critical Errors');
 
            foreach ($summary['critical'] as $error) {
                $cardContent = '<p><strong>Time:</strong> ' . esc_html($error->created_at) . '</p>';
                $cardContent .= '<p><strong>Message:</strong></p>';
                $cardContent .= JVB()->email()->codeBlock($error->message);
 
                // Include context if available
                if (!empty($error->context)) {
                    $context = json_decode($error->context, true);
                    if (is_array($context)) {
                        $cardContent .= '<p><strong>Context:</strong></p>';
                        $cardContent .= JVB()->email()->codeBlock(json_encode($context, JSON_PRETTY_PRINT));
                    }
                }
 
                $body .= JVB()->email()->card($cardContent, $error->component . ': ' . $error->error_type);
            }
        }
 
        // Dashboard link
        $admin_url = admin_url('admin.php?page=jvb-error-logs');
        $body .= JVB()->email()->spacer(30);
        $body .= JVB()->email()->button($admin_url, 'View Detailed Logs');
 
        return JVB()->email()->sendEmail($admin_email, $subject, $body, 'ERROR SUMMARY');
    }
 
    /**
     * Get HTML version of the error summary for nicer emails
     *
     * @param array $summary The error summary data
     * @return string HTML formatted error summary
     */
    protected function getHtmlErrorSummary(array $summary):string
    {
        $html = '<div style="font-family: sans-serif; max-width: 800px; margin: 0 auto;">';
        $html .= '<h1 style="color:#FF0080;border-bottom:2px solid #1B1B1B;padding-bottom:10px;">Error Summary</h1>';
 
        // Frequent errors table
        if (!empty($summary['frequent'])) {
            $html .= '<h2>Most Frequent Errors</h2>';
            $html .= '<table style="width: 100%; border-collapse: collapse; margin-bottom: 20px;">';
            $html .= '<tr style="background-color: #1B1B1B; color: #EFEFEF;">';
            $html .= '<th style="padding: 8px; text-align: left; border: 1px solid #ddd;">Component</th>';
            $html .= '<th style="padding: 8px; text-align: left; border: 1px solid #ddd;">Error Type</th>';
            $html .= '<th style="padding: 8px; text-align: left; border: 1px solid #ddd;">Message</th>';
            $html .= '<th style="padding: 8px; text-align: center; border: 1px solid #ddd;">Count</th>';
            $html .= '</tr>';
 
            foreach ($summary['frequent'] as $error) {
                $html .= '<tr>';
                $html .= '<td style="padding:8px;border:1px solid #ddd;">' . esc_html($error->component) . '</td>';
                $html .= '<td style="padding:8px;border:1px solid #ddd;">' . esc_html($error->error_type) . '</td>';
                $html .= '<td style="padding:8px;border:1px solid #ddd;">' . esc_html(wp_trim_words($error->message, 10, '...')) . '</td>';
                $html .= '<td style="padding:8px;border:1px solid #ddd;text-align:center;"><strong>' . esc_html($error->count) . '</strong></td>';
                $html .= '</tr>';
            }
 
            $html .= '</table>';
        }
 
        // Critical errors section
        if (!empty($summary['critical'])) {
            $html .= '<h2>Recent Critical Errors</h2>';
 
            foreach ($summary['critical'] as $error) {
                $html .= '<div style="margin-bottom: 20px; padding: 15px; border-left: 4px solid #FF0080; background-color: #fff9fb;">';
                $html .= '<h3 style="margin-top: 0; color: #1B1B1B;">' . esc_html($error->component) . ': ' . esc_html($error->error_type) . '</h3>';
                $html .= '<p><strong>Time:</strong> ' . esc_html($error->created_at) . '</p>';
                $html .= '<p><strong>Message:</strong> ' . esc_html($error->message) . '</p>';
 
                // Context display
                if (!empty($error->context)) {
                    $context = json_decode($error->context, true);
                    if (is_array($context)) {
                        $html .= '<div style="margin-top: 10px;">';
                        $html .= '<strong>Context:</strong>';
                        $html .= '<pre style="background-color: #f5f5f5; padding: 10px; border-radius: 4px; overflow: auto; max-height: 200px;">';
                        $html .= esc_html(json_encode($context, JSON_PRETTY_PRINT));
                        $html .= '</pre>';
                        $html .= '</div>';
                    }
                }
 
                $html .= '</div>';
            }
        }
 
        // Dashboard link
        $admin_url = admin_url('admin.php?page=jvb-error-logs');
        $html .= '<p style="margin-top: 30px; padding-top: 15px; border-top: 1px solid #ddd;">';
        $html .= '<a href="' . esc_url($admin_url) . '" style="display: inline-block; background-color: #FF0080; color: white; padding: 10px 15px; text-decoration: none; border-radius: 4px;">View All Errors in Dashboard</a>';
        $html .= '</p>';
 
        $html .= '</div>';
 
        return $html;
    }
 
    public function renderAdminPage()
    {
        ?>
        <h1>Error Logs</h1>
        <?php
    }
 
    public function getErrors(WP_REST_Request $request) {
        $params = $this->buildParams($request);
 
    }
 
 
 
    protected function buildParams(WP_REST_Request $request):array {
        $allowedSeverity = [
            'all',
            'notice',
            'warning',
            'error',
            'critical',
        ];
        $allowedComponents = [
            'all',
            //Get all components
        ];
        $severity = $request->get_param('severity')??'all';
        $component = $request->get_param('component')??'all';
 
        return [
            'page'  => (int)$request->get_param('page'),
            'severity'  => (in_array($severity, $allowedSeverity)) ? $severity : 'all',
            'component' => (in_array($component, $allowedComponents)) ? $component : 'all',
        ];
    }
}