-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit-log.php
More file actions
156 lines (121 loc) · 3.37 KB
/
bit-log.php
File metadata and controls
156 lines (121 loc) · 3.37 KB
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
<?php
/**
* Bit Log
*
* @package BitLog
* @author Sovware
* @copyright 2022 Sovware
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: Bit Log
* Plugin URI: https://github.com/sovware/bit-log
* Description: A PHP debugging tool for WordPress
* Version: 1.0.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: wpWax
* Author URI: https://wpwax.com
* Text Domain: bit-log
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Update URI: https://github.com/sovware/bit-log
*/
final class BitLog {
protected static $instance = null;
protected $option_key = 'bit-logs';
protected function __construct() {
$this->init_setup();
}
protected function init_setup() {
add_action( 'rest_api_init', [ $this, 'register_rest_api' ] );
}
public function register_rest_api() {
register_rest_route(
'bit-log/v1', '/logs',
[
[
'methods' => 'GET',
'callback' => [ $this, 'get_rest_api_logs' ],
'permission_callback' => '__return_true',
]
]
);
}
public static function isTruthy( $value ) {
if ( true === $value ) {
return true;
}
if ( 'true' === $value ) {
return true;
}
if ( 1 === $value ) {
return true;
}
if ( '1' === $value ) {
return true;
}
return false;
}
public static function isFalsy( $value ) {
if ( false === $value ) {
return true;
}
if ( 'false' === $value ) {
return true;
}
if ( 0 === $value ) {
return true;
}
if ( '0' === $value ) {
return true;
}
return false;
}
public function get_rest_api_logs( $request ) {
$logs = get_option( $this->option_key, [] );
$group = ( isset( $request['group'] ) ) ? $request['group'] : '';
$reset = true;
if ( isset( $request['reset'] ) && self::isFalsy( $request['reset'] ) ) {
$reset = false;
}
if ( empty( $group ) ) {
if ( $reset ) {
$this->clear_logs();
}
return $logs;
}
if ( ! isset( $logs[ $group ] ) ) {
return [];
}
if ( $reset ) {
$this->clear_logs();
}
return $logs[ $group ];
}
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new BitLog();
}
return self::$instance;
}
public function push( $group, $data, $file = __FILE__, $line = __LINE__ ) {
$logs = get_option( $this->option_key, [] );
$logs[ $group ][] = [
'file' => $file,
'line' => $line,
'data_type' => gettype( $data ),
'data' => $data,
];
update_option( $this->option_key, $logs );
}
public function clear_logs() {
update_option( $this->option_key, [] );
}
}
if ( ! function_exists( 'BitLog' ) ) {
function BitLog() {
return BitLog::get_instance();
}
}
BitLog();