forked from aiiddqd/logger-u7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-wp-data-logger.php
332 lines (286 loc) · 9.92 KB
/
class-wp-data-logger.php
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
<?php
class WP_Data_Logger{
protected static $instance;
protected static $table_name;
public static function get_instance(){
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
function __construct(){
global $wpdb;
add_action( 'admin_menu', function(){
add_management_page(
$page_title = 'WP Logger',
$menu_title = 'WP Logger',
$capability = 'manage_options',
$menu_slug = 'logger',
$func = array( $this, 'display_page' )
);
});
self::$table_name = $wpdb->get_blog_prefix() . 'data_logger';
add_action( 'upgrader_process_complete', array( $this, 'upgrader' ), 10, 2 );
add_filter( 'plugin_action_links_' . WPDL_PLUGIN_NAME, array( $this, 'add_settings_link' ) );
add_action( 'wp_ajax_LoggerClearLog', array( $this, 'clear_log' ) );
add_action( 'logger', array( $this, 'add' ), 0, 2 );
//self::check_installation();
}
public function activation(){
self::check_installation();
}
public function upgrader( $upgrader_object = null, $options = [] ){
if ( ! empty( $upgrader_object ) &&
(
@$options['action'] != 'update'
|| @$options['type'] != 'plugin'
|| empty( $options['plugins'] )
|| ! in_array( WPDL_PLUGIN_NAME, @$options['plugins'] )
)
) return;
self::check_installation();
//self::remove_old_data();
}
public function check_installation( $upgrader_object = null, $options = [] ){
self::$instance->db_delta();
}
function db_delta(){
global $wpdb;
$table = self::$table_name;
$query =
"CREATE TABLE {$table} (
ID int(11) unsigned NOT NULL auto_increment,
status varchar(255) NOT NULL default '',
date datetime NULL DEFAULT CURRENT_TIMESTAMP,
content longtext NULL default '',
PRIMARY KEY (ID)
)
DEFAULT CHARACTER SET {$wpdb->charset} COLLATE {$wpdb->collate};";
dbDelta( $query );
return true;
}
public function remove_old_data(){
delete_option( 'logger_u7' );
}
function display_page(){
global $wpdb;
$default_limit = 200;
$limit = ( ! defined( 'WPDL_DISPLAY_LIMIT' ) || ! is_numeric( WPDL_DISPLAY_LIMIT ) || WPDL_DISPLAY_LIMIT < 1 ) ? $default_limit : WPDL_DISPLAY_LIMIT;
echo '<h1>Log</h1>';
echo '<p>For adding data to log use the hook: <code>do_action( \'logger\', $data[, $status = \'info\' ] );</code></p>';
echo '<a class="button log_status_selector status_all" data-status="all">ALL</a>'.
'<a class="button log_status_selector status_info" data-status="info">INFO</a>'.
'<a class="button log_status_selector status_warning" data-status="warning">WARNING</a>'.
'<a class="button log_status_selector status_error" data-status="error">ERROR</a>'.
apply_filters( 'wp_logger_button_panel', '' );
echo '<a class="button clear_log button-link-delete" href="" data-status="info">Clear INFO</a>' .
'<a class="button clear_log button-link-delete" href="" data-status="warning">Clear WARNING</a>' .
'<a class="button clear_log button-link-delete" href="" data-status="error">Clear ERROR</a>' .
'<a class="button clear_log button-link-delete" href="" data-status="all">Clear Log</a>' ;
$suppress = $wpdb->suppress_errors( true );
$data = $wpdb->get_results( 'SELECT * FROM '. self::$table_name . ' ORDER BY ID DESC LIMIT ' . $limit, ARRAY_A );
$wpdb->suppress_errors( $suppress );
if ( empty( $data ) || ! is_array( $data ) ):
echo '<p>There is no data in the log</p>';
return;
endif;
$styles = apply_filters( 'wp_logger_inline_css', self::print_css() );
echo apply_filters( 'wp_logger_inline_js', self::print_js() );
?>
<style>
<?php echo $styles; ?>
</style>
<table border="1" width="100%" class="logger_table">
<thead>
<tr>
<th>№</th>
<th>Status</th>
<th class="th_data">Data <span class="hide_data"><span class="hide_all">[Hide all]</span> <span class="show_all">[Show all]</span></span></th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach( $data as $k => $item ):
$i++;
$status = 'all ' . @$item['status'];
ob_start();
var_dump( maybe_unserialize( @$item['content'] ) );
$content = esc_html( ob_get_clean() );
?>
<tr class="data <?php echo $status;?>" id="loggerDataRow_<?php echo $i; ?>">
<td width="100px" class="num_column dblClickToScroll" >
<span><span class="prev">[prev]</span><b>[<?php echo $k + 1; ?>]</b><span class="next">[next]</span></span>
</td>
<td class="status_column" align="center"><span><?php echo @$item['status'];?></span></td>
<td class="data_column">
<span class="timestamp"><?php echo "[{$item['date']}]"; ?></span> <span class="hide_data">[<span class="is_visible">Hide this</span><span class="is_hidden">Show this</span>]</span>
<pre class="data_content"><?php echo apply_filters( 'wp_data_logger_print_data', $content ); ?></pre>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
}
function add( $data = '', $status = 'info' ){
global $wpdb;
$suppress = $wpdb->suppress_errors( true );
$i = 0;
do{
$result = $wpdb->insert(
self::$table_name,
array( 'status' => $status, 'content' => maybe_serialize( $data ) )
);
} while( empty( $result ) && $this->db_delta() && $i++ < 1 );
$wpdb->suppress_errors( $suppress );
if ( empty( $result) ) error_log( '[WP DATA LOGGER] Error while DB table creating' );
}
/**
* Add fast link in plugins list
*/
function add_settings_link( $links ){
$settings_link = '<a href="tools.php?page=logger">View WP Logger</a>';
array_unshift( $links, $settings_link );
return $links;
}
function clear_log(){
global $wpdb;
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( json_encode( new \WP_Error( 'no_permission', 'You have no permission to do this', array( 'status' => 403 ) ) ) );
}
if ( $_POST['status'] == 'all' ) :
$wpdb->query( 'TRUNCATE ' . self::$table_name );
elseif ( ! empty( $_POST['status'] ) ) :
$wpdb->delete( self::$table_name, array( 'status' => $_POST['status'] ) );
endif;
wp_die( json_encode( array( 'done' => true ) ) );
}
function update_message( $data, $response ) {
if( isset( $data['upgrade_notice'] ) ) :
printf(
'<div class="update-message">%s</div>',
wpautop( $data['upgrade_notice'] )
);
endif;
if ( ! defined( 'WPDL_VERSION' ) || version_compare( WPDL_VERSION, '2.0', '<' ) ) :
printf(
'<div class="update-message">%s</div>',
wpautop( 'During update will be lost all logged data. Please, make sure this won\'t hurt you.' )
);
endif;
}
function print_css(){
$css = "
.dblClickToScroll{
vertical-align: top;
text-align: center;
}
.dblClickToScroll .prev,
.dblClickToScroll .next{
cursor : pointer;
font-size: 10px;
}
.logger_table{
margin-top: 6px;
}
.logger_table .status_column{
font-weight: 900;
vertical-align: top;
}
.logger_table .warning .status_column span{
color: #803600;
}
.logger_table .error .status_column span{
color : #F00;
}
.logger_table .num_column span{}
.logger_table .data_column span.timestamp{
font-size: 10px;
}
.logger_table .data_column .hide_data,
.logger_table .th_data .hide_data{
cursor: pointer;
font-family: monospace;
font-size: 10px;
}
.logger_table .data_column .hide_data .is_hidden{
display : none;
}
.logger_table .data_column .hide_data.hide .is_hidden{
display : initial;
}
.logger_table .data_column .hide_data.hide .is_visible{
display : none;
}
.logger_table .data_column .hide_data.hide + pre{
display : none;
}
.logger_table .data_column pre{
margin: 2px 0;
}
a.button.log_status_selector,
a.button.clear_log {
margin: 0 0 0 4px;
}
a.button.clear_log{
float : right;
}
a.delimiter {
width: 1px;
margin: 0 20px;
}";
return $css;
}
function print_js(){
$js = "
<script>
jQuery( '.log_status_selector' ).click( function( eventObject ){
eventObject.preventDefault();
var status = jQuery( this ).data( 'status' );
if ( status == 'all' ){
jQuery( '.logger_table tbody tr' ).show()
} else {
jQuery( '.logger_table tbody tr' ).hide()
jQuery( '.logger_table tbody tr.' + status ).show()
}
})
jQuery( '.clear_log' ).click( function( eventObject ){
eventObject.preventDefault();
var btn = jQuery( this );
if ( ! confirm( 'Do you wish really do this?' ) ) return;
jQuery.post(
'". get_admin_url() ."/admin-ajax.php',
{ action : 'LoggerClearLog', status : btn.data( 'status' ) },
function( response ){
response = JSON.parse( response );
if ( response.done == true ){
jQuery( '.logger_table tr.data.' + btn.data( 'status' ) ).remove();
}
}
);
} )
jQuery( '#wpbody-content' ).on( 'click', '.dblClickToScroll .prev', function(){
var prevRow = jQuery( this ).closest( 'tr.data' ).prev( 'tr.data' );
if ( prevRow.length )
jQuery('body,html').animate({scrollTop: prevRow.offset().top - 60}, 600);
} );
jQuery( '#wpbody-content' ).on( 'click', '.dblClickToScroll .next', function(){
var nextRow = jQuery( this ).closest( 'tr.data' ).next( 'tr.data' );
if ( nextRow.length )
jQuery('body,html').animate({scrollTop: nextRow.offset().top - 60}, 600);
} );
jQuery( '#wpbody-content' ).on( 'click', '.data_column .hide_data', function(){
jQuery( this ).toggleClass( 'hide' );
});
jQuery( '#wpbody-content' ).on( 'click', '.th_data .hide_all', function(){
jQuery( '.data_column .hide_data' ).addClass( 'hide' );
});
jQuery( '#wpbody-content' ).on( 'click', '.th_data .show_all', function(){
jQuery( '.data_column .hide_data' ).removeClass( 'hide' );
});
</script>
";
return $js;
}
}