-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-log-hooks.php
666 lines (491 loc) · 16.3 KB
/
class-log-hooks.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
<?php
// No thank you
if ( ! defined('ABSPATH') ) die;
class WPB_Log_Hooks {
/**
* Listen for various actions.
*
* @since 2.0
*
* @return void
*/
public function __construct() {
add_action('transition_post_status', array( __CLASS__, 'new_post_status' ), 10, 3);
add_action('publish_post', array( __CLASS__, 'post_data_updated' ), 10, 1);
add_action('publish_page', array( __CLASS__, 'post_data_updated' ), 10, 1);
add_action('trash_post', array( __CLASS__, 'post_data_updated' ), 10, 1);
add_action('untrash_post', array( __CLASS__, 'post_data_updated' ), 10, 1);
add_action('before_delete_post', array( __CLASS__, 'post_data_updated' ), 10, 1);
add_action('created_term', array( __CLASS__, 'term_data_altered' ), 10, 3);
add_action('edit_terms', array( __CLASS__, 'term_data_edited' ), 10, 2);
add_action('delete_term', array( __CLASS__, 'term_data_altered' ), 10, 3);
add_action('add_attachment', array( __CLASS__, 'media_altered' ), 10, 1);
add_action('edit_attachment', array( __CLASS__, 'media_altered' ), 10, 1);
add_action('delete_attachment', array( __CLASS__, 'media_altered' ), 10, 1);
add_action('comment_post', array( __CLASS__, 'comment_altered' ), 10, 1);
add_action('edit_comment', array( __CLASS__, 'comment_altered' ), 10, 1);
add_action('deleted_comment', array( __CLASS__, 'comment_altered' ), 10, 1);
add_action('trashed_comment', array( __CLASS__, 'comment_altered' ), 10, 1);
add_action('untrashed_comment', array( __CLASS__, 'comment_altered' ), 10, 1);
add_action('spammed_comment', array( __CLASS__, 'comment_altered' ), 10, 1);
add_action('unspammed_comment', array( __CLASS__, 'comment_altered' ), 10, 1);
add_action('transition_comment_status', array( __CLASS__, 'comment_updated' ), 10, 3);
add_action('user_register', array( __CLASS__, 'user_account_altered' ), 10, 1);
add_action('profile_update', array( __CLASS__, 'user_account_altered' ), 10, 2);
add_action('delete_user', array( __CLASS__, 'user_account_altered' ), 10, 1);
add_action('wp_login', array( __CLASS__, 'user_authentication' ), 10, 2);
add_action('wp_login_failed', array( __CLASS__, 'user_authentication' ), 10, 1);
add_action('wp_logout', array( __CLASS__, 'user_authentication' ), 10, 0);
add_action('switch_theme', array( __CLASS__, 'theme_switched' ), 10, 0);
add_action('activated_plugin', array( __CLASS__, 'plugin_altered' ), 10, 2);
add_action('deactivated_plugin', array( __CLASS__, 'plugin_altered' ), 10, 2);
add_action('updated_option', array( __CLASS__, 'option_updated' ), 10, 3);
}
/**
* Creates a new log entry.
*
* @since 2.0
*
* @param array $log_data The data to save.
*
* @return boolean
*/
public static function save_new_log( $log_data = array() ) {
global $wpdb;
// Setup the whitelist trigger
$is_whitelisted = false;
// Get the username of the current user
$get_user = get_userdata( get_current_user_id() );
// Did we get a valid user
if ( false !== $get_user ) {
// Get the username
$username = $get_user->user_login;
} else {
return false;
}
// Get the user whitelist
$whitelisted_users = get_option('dtjwpb_user_filter_list');
// Explode the list into an array we can loop through
$whitelist = preg_split( '/\r\n|[\r\n]/', $whitelisted_users );
// Loop through the white list
foreach ( $whitelist as $a_user ) {
// Check if the current user is whitelisted or not
if ( $a_user === $username ) {
$is_whitelisted = true;
}
}
// Stop if we're whitelisted
if ( true === $is_whitelisted ) {
return false;
}
// Save the log data
$wpdb->insert(
$wpdb->prefix . 'logs',
array(
'site_id' => intval(get_current_blog_id()),
'user_id' => intval(get_current_user_id()),
'host_ip' => esc_html($_SERVER['REMOTE_ADDR']),
'object_id' => intval($log_data['object_id']),
'slug' => $log_data['slug'],
'setting' => $log_data['setting'],
'timestamp' => date('Y-m-d H:i:s'),
'action' => $log_data['action'],
'notes' => $log_data['notes']
)
);
return true;
}
/**
* Log a post status change.
*
* @since 2.0
*
* @param string $new_status The new post status.
* @param string $old_status The old post status.
* @param object $post The current post object.
*
* @return boolean
*/
public static function new_post_status( $new_status = false, $old_status = false, $post ) {
// Get the post type
$post_type = get_post_type( $post->ID );
// Setup log data array
$log_data = array(
'object_id' => $post->ID,
'slug' => 'post',
'setting' => $post_type,
'action' => __('Update', 'wp-blame'),
'notes' => ''
);
// Get the new status
if ( 'auto-draft' == $old_status && 'draft' == $new_status ) {
$log_data['action'] = __('Create', 'wp-blame');
$log_data['notes'] = __('New post created', 'wp-blame');
} elseif ( ( 'draft' == $old_status && 'draft' == $new_status ) || ( 'publish' == $old_status && 'publish' == $new_status ) ) {
$log_data['notes'] = __('Post was updated', 'wp-blame');
} elseif ( 'pending' == $new_status ) {
$log_data['notes'] = __('Post is pending', 'wp-blame');
} elseif ( 'private' == $new_status ) {
$log_data['notes'] = __('Post is private', 'wp-blame');
} elseif ( 'future' == $new_status ) {
$log_data['notes'] = __('Post is scheduled', 'wp-blame');
} elseif ( 'trash' == $new_status ) {
$log_data['notes'] = __('Post is binned', 'wp-blame');
} else {
return false;
}
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when a post is updated.
*
* @since 2.0
*
* @param string $post_id The post id.
*
* @return boolean
*/
public static function post_data_updated( $post_id = false ) {
// Get the current filter
$filter = current_filter();
// Get the post type
$post_type = get_post_type( $post_id );
// Make sure this isn't a revision
if ( 'Revision' == $post_type || 'revision' == $post_type ) {
return false;
}
// Setup log data array
$log_data = array(
'object_id' => $post_id,
'slug' => 'post',
'setting' => $post_type
);
// Which hook is active
if ( 'publish_post' == $filter ) {
$log_data['action'] = __('Published', 'wp-blame');
$log_data['notes'] = __('Post was published', 'wp-blame');
} elseif ( 'publish_page' == $filter ) {
$log_data['action'] = __('Published', 'wp-blame');
$log_data['notes'] = __('Page was published', 'wp-blame');
} elseif ( 'trash_post' == $filter ) {
$log_data['action'] = __('Binned', 'wp-blame');
$log_data['notes'] = __('Post was binned', 'wp-blame');
} elseif ( 'untrash_post' == $filter ) {
$log_data['action'] = __('Restored', 'wp-blame');
$log_data['notes'] = __('Post was restored', 'wp-blame');
} elseif ( 'before_delete_post' == $filter ) {
$log_data['action'] = __('Deleted', 'wp-blame');
$log_data['notes'] = __('Post was deleted', 'wp-blame');
}
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when a term is added or deleted.
*
* @since 2.0
*
* @param string $term_id The term id.
* @param string $tt_id The taxonomy term id.
* @param string $taxonomy The taxonomy name.
*
* @return boolean
*/
public static function term_data_altered( $term_id = false, $tt_id = false, $taxonomy = false ) {
// Get the current filter
$filter = current_filter();
// Setup log data array
$log_data = array(
'object_id' => $term_id,
'slug' => 'term',
'setting' => $taxonomy
);
// Check which filter is used
if ( 'created_term' == $filter ) {
$log_data['action'] = __('Created', 'wp-blame');
$log_data['notes'] = __('Term was created', 'wp-blame');
} elseif ( 'delete_term' == $filter ) {
$log_data['action'] = __('Deleted', 'wp-blame');
$log_data['notes'] = __('Term was deleted', 'wp-blame');
}
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when a term is edited.
*
* @since 2.0
*
* @param string $term_id The term id.
* @param string $taxonomy The taxonomy name.
*
* @return boolean
*/
public static function term_data_edited( $term_id = false, $taxonomy = false ) {
// Get the current filter
$filter = current_filter();
// Setup log data array
$log_data = array(
'object_id' => $term_id,
'slug' => 'term',
'setting' => $taxonomy,
'action' => __('Updated', 'wp-blame'),
'notes' => __('Term was edited', 'wp-blame')
);
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when an attachment is added, edited or deleted.
*
* @since 2.0
*
* @param string $attachmentid The attachment id.
*
* @return boolean
*/
public static function media_altered( $attachmentid = false ) {
// Get the current filter
$filter = current_filter();
// Setup log data array
$log_data = array(
'object_id' => $attachmentid,
'slug' => 'media',
'setting' => __('Media', 'wp-blame')
);
// Which action was performed
if ( 'add_attachment' == $filter ) {
$log_data['action'] = __('Created', 'wp-blame');
$log_data['notes'] = __('Attachment uploaded', 'wp-blame');
} elseif ( 'edit_attachment' == $filter ) {
$log_data['action'] = __('Updated', 'wp-blame');
$log_data['notes'] = __('Attachment updated', 'wp-blame');
} elseif ( 'delete_attachment' == $filter ) {
$log_data['action'] = __('Deleted', 'wp-blame');
$log_data['notes'] = __('Attachment deleted', 'wp-blame');
}
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when a comment is altered in some way.
*
* @since 2.0
*
* @param string $comment_id The comment id.
*
* @return boolean
*/
public static function comment_altered( $comment_id = false ) {
// Get the current filter
$filter = current_filter();
// Setup log data array
$log_data = array(
'object_id' => $comment_id,
'slug' => 'comment',
'setting' => __('Comment', 'wp-blame')
);
// Which action was performed
if ( 'comment_post' == $filter ) {
$log_data['action'] = __('Created', 'wp-blame');
$log_data['notes'] = __('Comment posted', 'wp-blame');
} elseif ( 'edit_comment' == $filter ) {
$log_data['action'] = __('Updated', 'wp-blame');
$log_data['notes'] = __('Comment updated', 'wp-blame');
} elseif ( 'deleted_comment' == $filter ) {
$log_data['action'] = __('Deleted', 'wp-blame');
$log_data['notes'] = __('Comment deleted', 'wp-blame');
} elseif ( 'trashed_comment' == $filter ) {
$log_data['action'] = __('Binned', 'wp-blame');
$log_data['notes'] = __('Comment binned', 'wp-blame');
} elseif ( 'untrashed_comment' == $filter ) {
$log_data['action'] = __('Restored', 'wp-blame');
$log_data['notes'] = __('Comment restored', 'wp-blame');
} elseif ( 'spammed_comment' == $filter ) {
$log_data['action'] = __('Spammed', 'wp-blame');
$log_data['notes'] = __('Comment marked as spam', 'wp-blame');
} elseif ( 'unspammed_comment' == $filter ) {
$log_data['action'] = __('Not Spam', 'wp-blame');
$log_data['notes'] = __('Comment unmarked as spam', 'wp-blame');
}
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when a comment status is edited.
*
* @since 2.0
*
* @param string $new_status The new comment status.
* @param string $old_status The old comment status.
* @param string $comment The comment id.
*
* @return boolean
*/
public static function comment_updated( $new_status = false, $old_status = false, $comment = false ) {
// Check a status was changed
if ( $new_status == $old_statusd ) {
return false;
}
// Setup log data array
$log_data = array(
'object_id' => $comment,
'slug' => 'comment',
'setting' => __('Comment', 'wp-blame'),
'action' => __('Updated', 'wp-blame'),
'notes' => sprintf( _x('Comment status changed to %s', 'Comment status', 'wp-blame'), $new_status )
);
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when a user account is altered.
*
* @since 2.0
*
* @param string $user_id The user id.
*
* @return boolean
*/
public static function user_account_altered( $user_id = false ) {
// Get the current filter
$filter = current_filter();
// Setup log data array
$log_data = array(
'object_id' => $user_id,
'slug' => 'user',
'setting' => __('User', 'wp-blame')
);
// What happened to the user
if ( 'user_register' == $filter ) {
$log_data['action'] = __('Registered', 'wp-blame');
$log_data['notes'] = __('New user registered', 'wp-blame');
} elseif ( 'profile_update' == $filter ) {
$log_data['action'] = __('Updated', 'wp-blame');
$log_data['notes'] = __('User profile updated', 'wp-blame');
} elseif ( 'delete_user' == $filter ) {
$log_data['action'] = __('Deleted', 'wp-blame');
$log_data['notes'] = __('User account deleted', 'wp-blame');
}
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when a user tries to authenticate.
*
* @since 2.0
*
* @param string $username The username of the user.
*
* @return boolean
*/
public static function user_authentication( $username = false ) {
// Get the current filter
$filter = current_filter();
// Get the users id by login name.
$user = get_user_by( 'login', $username );
// Did we find the user
if ( false !== $user ) {
$user_id = $user->ID;
} else {
$user_id = 0;
}
// Setup log data array
$log_data = array(
'object_id' => $user_id,
'slug' => 'user',
'setting' => __('User', 'wp-blame')
);
// Check which filter we have
if ( 'wp_login' == $filter ) {
$log_data['action'] = __('Logged in', 'wp-blame');
$log_data['notes'] = __('User logged into account', 'wp-blame');
} elseif ( 'wp_login_failed' == $filter ) {
$log_data['action'] = __('Login failed', 'wp-blame');
$log_data['notes'] = __('User could not login', 'wp-blame');
} elseif ( 'wp_logout' == $filter ) {
$log_data['action'] = __('Logged out', 'wp-blame');
$log_data['notes'] = __('User logged out of account', 'wp-blame');
}
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when a new theme is activated.
*
* @since 2.0
*
* @return boolean
*/
public static function theme_switched() {
// Get the current theme
$theme = get_option('stylesheet');
// Setup log data array
$log_data = array(
'object_id' => 0,
'slug' => 'theme',
'setting' => __('Theme', 'wp-blame'),
'action' => __('Activated', 'wp-blame'),
'notes' => sprintf( __('%s theme activated', 'wp-blame'), $theme )
);
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when a plugin is activated or deactivated.
*
* @since 2.0
*
* @param string $plugin The plugin slug.
*
* @return boolean
*/
public static function plugin_altered( $plugin = false ) {
// Get the current filter
$filter = current_filter();
// Setup log data array
$log_data = array(
'object_id' => 0,
'slug' => 'plugin',
'setting' => __('Plugin', 'wp-blame')
);
// Which filter is being run
if ( 'activated_plugin' == $filter ) {
$log_data['action'] = __('Plugin activated', 'wp-blame');
$log_data['notes'] = sprintf( __('%s was activated', 'wp-blame'), $plugin );
} elseif ( 'deactivated_plugin' == $filter ) {
$log_data['action'] = __('Plugin deactivated', 'wp-blame');
$log_data['notes'] = sprintf( __('%s was deactivated', 'wp-blame'), $plugin );
}
// Record the log
return self::save_new_log( $log_data );
}
/**
* Called when an option is updated.
*
* @since 2.0
*
* @param string $option The option slug.
* @param string $new_value The new option value.
* @param string $old_value The old option value.
*
* @return boolean
*/
public static function option_updated( $option = false, $new_value = false, $old_value = false ) {
// Don't always log the update
if ( $new_value == $old_value || 'cron' == $option || false !== strpos( $option, '_transient' ) ) {
return false;
}
// Setup log data array
$log_data = array(
'object_id' => 0,
'slug' => 'option',
'setting' => __('Option', 'wp-blame'),
'action' => __('Option updated', 'wp-blame'),
'notes' => sprintf( _x('%s was updated', 'The updated option', 'wp-blame'), $option )
);
// Record the log
return self::save_new_log( $log_data );
}
}