-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-blame.php
174 lines (118 loc) · 4.58 KB
/
wp-blame.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
<?php
/*
Plugin Name: WP Blame
Plugin URI:
Description:
Author:
Version: 1.0
Author
*/
class WP_Blame {
static $table = 'wpblamelog';
static function init() {
add_action('updated_option', array(__CLASS__, 'update_option'), 10, 3);
add_action('added_option', array(__CLASS__, 'add_option'), 10, 2);
add_action('deleted_option', array(__CLASS__, 'delete_option'));
add_action('wp_login', array(__CLASS__, 'login'));
add_action('admin_head', array(__CLASS__, 'pageview'));
}
static function update_option($option, $oldvalue, $newvalue) {
// log it
if (!self::is_protected_option($option)) {
$diff = self::diff($oldvalue, $newvalue);
self::log($option, $diff, 'update');
}
}
static function add_option($option, $value) {
// log it
if (!self::is_protected_option($option))
self::log($option, $value, 'add');
}
static function delete_option($option) {
// log it
if (!self::is_protected_option($option))
self::log($option, '', 'delete');
}
static function is_protected_option($option) {
return preg_match('/^_.+/', $option);
}
static function activate() {
// creates table
global $wpdb;
$create = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}".self::$table." (
`ID` int(11) NOT NULL auto_increment,
`user_id` int(11),
`option_name` varchar(255),
`value` LONGTEXT,
`action` enum('add', 'update', 'delete', 'pageview', 'login', 'logout'),
`time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)) DEFAULT CHARSET=utf8";
$wpdb->query($create);
}
static function log($option_name, $value, $action) {
global $wpdb;
$user = wp_get_current_user();
if (is_user_logged_in() && is_object($user))
$user_id = $user->ID;
else
$user_id = 0;
$value = maybe_serialize($value);
return $wpdb->insert( $wpdb->prefix . self::$table, array('option_name' => $option_name, 'value' => $value, 'user_id' => $user_id, 'action' => $action) );
}
static function login($login) {
self::log('', '', 'login');
}
function pageview() {
global $pagenow;
# We dont want to log when saving anything
if (isset($_POST) && sizeof($_POST) > 0)
return;
if (!empty($pagenow))
self::log('', $pagenow, 'pageview');
}
static function recursive_newValuearray($obj){
// se nao é array nem objeto, retorna o valor
if(!is_array($obj) && !is_object($obj))
return $obj;
// se for objeto, faz o cast
elseif(is_object($obj))
$obj = (array) $obj;
foreach($obj as $key => $val)
if(is_object($val))
$obj[$key] = self::recursive_newValuearray ($val);
return $obj;
}
static function diff($oldValue, $newValue){
if($oldValue == $newValue)
return array();
if(is_object($oldValue))
$oldValue = (array) $oldValue;
if(is_object($newValue))
$newValue = (array) $newValue;
// se um dos dois objetos não for array, retorna o segundo objeto
if(!is_array($oldValue) || !is_array($newValue))
return $newValue;
$result = array();
foreach($oldValue as $key => $val){
// se a chave não existe no newValue
if(!isset($newValue[$key])){
$result[$key] = null;
}elseif(isset($newValue[$key]) && $newValue[$key] != $val){
$diff = self::diff($val, $newValue[$key]);
if($diff)
$result[$key] = $diff;
}
}
// coloca no resultado todos os valores do newValue que não existem no oldValue
foreach ($newValue as $key => $val)
if(!isset ($oldValue[$key]))
$result[$key] = self::recursive_newValuearray($newValue[$key]);
return $result;
}
}
WP_Blame::init();
register_activation_hook(__FILE__, 'wp_blame_activate');
function wp_blame_activate() {
WP_Blame::activate();
}
?>