PluginProbe
Black Bar / trunk
Black Bar vtrunk
trunk 1.0.0 1.1.0 1.2.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.2.0 2.2.1 3.0.0 3.1.0 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0
blackbar / plugin / Modules / Console.php

Console.php in Black Bar trunk, at plugin/Modules/Console.php

123 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace GeminiLabs\BlackBar\Modules;
4
5 use GeminiLabs\BlackBar\Dump;
6
7 class Console extends Module
8 {
9 public const ERROR_CODES = [
10 E_ERROR => 'error', // 1
11 E_WARNING => 'warning', // 2
12 E_NOTICE => 'notice', // 8
13 E_DEPRECATED => 'deprecated', // 8192
14 ];
15
16 public const MAPPED_ERROR_CODES = [
17 'debug' => 0,
18 'info' => E_NOTICE,
19 'deprecated' => E_DEPRECATED, // 8192
20 'error' => E_ERROR, // 1
21 'notice' => E_NOTICE, // 8
22 'warning' => E_WARNING, // 2
23 'critical' => E_ERROR, // 1
24 'alert' => E_ERROR, // 1
25 'emergency' => E_ERROR, // 1
26 ];
27
28 public function classes(): string
29 {
30 $errno = array_unique(wp_list_pluck($this->entries, 'errno'));
31 if (in_array(E_ERROR, $errno)) {
32 return sprintf('%s glbb-error', $this->id());
33 }
34 if (in_array(E_WARNING, $errno)) {
35 return sprintf('%s glbb-warning', $this->id());
36 }
37 return $this->id();
38 }
39
40 public function entries(): array
41 {
42 $entries = [];
43 foreach ($this->entries as $entry) {
44 $entry['name'] = ucfirst($entry['errname']);
45 if ($entry['count'] > 1) {
46 $entry['name'] = sprintf('%s (%s)', $entry['name'], $entry['count']);
47 }
48 $entries[] = $entry;
49 }
50 return $entries;
51 }
52
53 public function icon(): string
54 {
55 return 'dashicons-warning';
56 }
57
58 public function info(): string
59 {
60 $counts = array_count_values(wp_list_pluck($this->entries, 'errno'));
61 $entryCount = count($this->entries);
62 if (!empty($counts[E_ERROR])) {
63 return sprintf('%d, %d!', $entryCount, $counts[E_ERROR]);
64 }
65 if ($entryCount > 0) {
66 return (string) $entryCount;
67 }
68 return '';
69 }
70
71 public function label(): string
72 {
73 return __('Console', 'blackbar');
74 }
75
76 public function store($message, string $errno = '', string $location = ''): void
77 {
78 if (is_numeric($errno)) { // entry likely stored by set_error_handler()
79 $errname = 'Unknown';
80 if (array_key_exists((int) $errno, static::ERROR_CODES)) {
81 $errname = static::ERROR_CODES[$errno];
82 }
83 } else { // entry likely stored by filter hook
84 $errname = 'Debug';
85 if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) {
86 $errname = $errno;
87 $errno = static::MAPPED_ERROR_CODES[$errno];
88 }
89 }
90 $errname = strtolower($errname);
91 $location = sanitize_text_field($location);
92 $message = $this->normalizeMessage($message, $location);
93 $hash = md5($errno.$errname.$message.$location);
94 if (array_key_exists($hash, $this->entries)) {
95 ++$this->entries[$hash]['count'];
96 } else {
97 $this->entries[$hash] = [
98 'count' => 0,
99 'errname' => $errname,
100 'errno' => (int) $errno,
101 'message' => $message,
102 ];
103 }
104 }
105
106 protected function normalizeMessage($message, string $location): string
107 {
108 if ($message instanceof \DateTime) {
109 $message = $message->format('Y-m-d H:i:s');
110 } elseif (is_object($message) || is_array($message)) {
111 $message = (new Dump())->dump($message);
112 } else {
113 $message = esc_html(trim((string) $message));
114 }
115 $location = trim($location);
116 if (!empty($location)) {
117 $location = str_replace([WP_CONTENT_DIR, ABSPATH], '', $location);
118 $location = sprintf('[%s]', $location);
119 }
120 return trim(sprintf('%s %s', $location, $message));
121 }
122 }
123