-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging.php
More file actions
239 lines (193 loc) · 5.43 KB
/
Copy pathlogging.php
File metadata and controls
239 lines (193 loc) · 5.43 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
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
<?php
/**
* Manage the error record
* @since 1.0.0
*/
class Prompt_Logging {
/**
* @since 2.0.0
* @type string
*/
protected static $log_option_name = 'prompt_log';
/**
* @since 2.0.0
* @type string
*/
protected static $last_submit_option_name = 'prompt_error_submit_time';
/**
* Shortcut to add a WP_Error to the log.
*
* @since 1.3.0
*
* @param WP_Error $error
* @return WP_Error
*/
public static function add_wp_error( WP_Error $error ) {
return self::add_error(
$error->get_error_code(),
$error->get_error_message(),
$error->get_error_data()
);
}
/**
* Record an error.
*
* @since 1.0.0
*
* @param string $code
* @param string $message
* @param mixed $data
* @return WP_Error
*/
public static function add_error( $code = '', $message = '', $data = array() ) {
try {
$backtrace = unserialize( serialize( debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 10) ) );
} catch (Exception $exception) {
$backtrace = 'Unavailable';
}
if ( is_array( $data ) ) {
$data['backtrace'] = $backtrace;
} else if ( $data instanceof WP_Error ) {
$data->add_data( $backtrace, 'backtrace' );
}
$wp_error = new WP_Error( $code, $message, $data );
$log = self::get_log();
if ( !$log ) {
$log = array();
}
// If we go over 25 messages, only keep the most recent 20
if ( count( $log ) > 25 )
$log = array_slice( $log, 0, 20 );
$time = time();
array_unshift( $log, compact( 'time', 'code', 'message', 'data' ) );
update_option( self::$log_option_name, $log, $autoload = 'no' );
// Puke a little in dev environments
trigger_error( $message, E_USER_NOTICE );
if (
Prompt_Core::$options->get('enable_collection') &&
! Prompt_Core::$options->get('suppress_error_submissions')
) {
self::submit($message);
Prompt_Core::$options->set('suppress_error_submissions', true);
}
return $wp_error;
}
/**
* Get saved error log entries.
*
* @since 1.0.0
*
* @param int $since Include only entries more recent than this timestamp.
* @param string $data_format Specify ARRAY_A to convert data to array format.
* @return array
*/
public static function get_log( $since = 0, $data_format = OBJECT ) {
$log = get_option( self::$log_option_name );
if ( !is_array( $log ) ) {
$log = json_decode( $log );
}
if ( !$log ) {
$log = array();
add_option( self::$log_option_name, $log, '', $autoload = false );
}
$filtered_log = array();
foreach ( $log as $entry ) {
$entry = (array) $entry;
if ( $data_format == ARRAY_A )
$entry['data'] = self::object_to_array( $entry['data'] );
if ( $entry['time'] > $since )
$filtered_log[] = $entry;
}
return $filtered_log;
}
/**
* Forget recorded errors.
*
* @since 1.0.0
*/
public static function delete_log() {
delete_option( self::$log_option_name );
}
/**
*
* @since 2.0.0
*
* @return int Unix time
*/
public static function get_last_submission_time() {
return absint( get_option( self::$last_submit_option_name ) );
}
/**
* Report new errors.
*
* @since 2.0.0
*
* @param string $message Optional email message.
*
* @return bool
*/
public static function submit($message = 'See attached log') {
$user = wp_get_current_user();
$last_submit_time = self::get_last_submission_time();
update_option( self::$last_submit_option_name, time(), $autoload = false );
$log = array( 'error_log' => self::get_log( $last_submit_time, ARRAY_A ) );
$environment = new Prompt_Environment();
$log = array_merge( $log, $environment->to_array() );
$log_file = wp_tempnam();
file_put_contents($log_file, json_encode($log));
$from_address = $user->exists() ? $user->user_email : get_option( 'admin_email' );
$from_name = $user->exists() ? $user->display_name : '';
$headers = array(
'From: ' . Prompt_Email_Batch::name_address($from_address, $from_name),
);
$subject = sprintf(
'Error submission from %s',
html_entity_decode( get_option( 'blogname' ) )
);
$sent = wp_mail(Prompt_Core::SUPPORT_EMAIL, $subject, $message, $headers, $log_file );
unlink($log_file);
return $sent;
}
/**
* @since 1.0.0
* @param mixed $obj
* @return array
*/
protected static function object_to_array( $obj ) {
if ( ! is_object( $obj ) and ! is_array( $obj ) ) {
return $obj;
}
if ( ! is_object( $obj ) ) {
return array_map( array( __CLASS__, 'object_to_array' ), $obj );
}
$object_vars = get_object_vars( $obj );
if ( $object_vars ) {
return array_map( array( __CLASS__, 'object_to_array' ), $object_vars );
}
if ( method_exists( $obj, 'to_array' ) ) {
return $obj->to_array();
}
$new = array();
$meta = new ReflectionClass( $obj );
$methods = $meta->getMethods( ReflectionMethod::IS_PUBLIC );
foreach ( $methods as $method ) {
$property_name = substr( $method->name, 4 );
if ( self::is_getter( $property_name, $meta, $method ) ) {
$new[$property_name] = $method->invoke( $obj );
}
}
return $new;
}
/**
* @since 2.0.13
* @param string $property_name
* @param ReflectionClass $class
* @param ReflectionMethod $method
* @return bool
*/
protected static function is_getter( $property_name, ReflectionClass $class, ReflectionMethod $method ) {
return $class->hasProperty( $property_name ) and
'get_' != substr( $method->name, 0, 4 ) and
$method->getNumberOfParameters() == 0;
}
}