-
Notifications
You must be signed in to change notification settings - Fork 0
/
metronet-log.php
377 lines (336 loc) · 11.4 KB
/
metronet-log.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
<?php
/*
Plugin Name: Metronet Log
Plugin URI: http://wordpress.org/extend/plugins/metronet-log/
Description: Provides a WordPress API for date-based data storage based on a user ID.
Author: Metronet
Version: 1.0.0
Requires at least: 3.5
Author URI: http://www.metronet.no
Contributors: ronalfy, metronet
*/
class Metronet_Log {
//private
private $plugin_url = '';
private $plugin_dir = '';
private $plugin_path = '';
private $tablename = '';
/**
* __construct()
*
* Class constructor
*
*/
public function __construct(){
global $wpdb;
$this->plugin_path = plugin_basename( __FILE__ );
$this->plugin_url = rtrim( plugin_dir_url(__FILE__), '/' );
$this->plugin_dir = rtrim( plugin_dir_path(__FILE__), '/' );
$this->tablename = Metronet_Log::get_table_name();
} //end constructor
/**
* add_log_value()
*
* Inserts a log item into the database
*
* @param int $user_id The user id
* @param string $type The type of data to add
* @param mixed (string|object|array) - Value associated with the type
* @param string $datetime - Datetime in format 0000-00-00 00:00:00
* @return bool false on failure, true if success.
**/
public function add_log_value( $user_id, $type, $value = '', $datetime = false ) {
global $wpdb;
//Validate user
$user_id = absint( $user_id );
if ( $user_id == 0 ) return false;
//Validate date
if ( $datetime !== false ) {
//Make sure date is in right format
$date_timestamp = strtotime( $datetime );
if ( !$date_timestamp ) return false;
$datetime = date( 'Y-m-d H:i:s', $date_timestamp );
} else {
$datetime = current_time('mysql');
}
//Validate type
$type = stripslashes( $type );
$value = stripslashes_deep( $value );
$_value = $value;
$value = maybe_serialize( $value );
//Perform the DB insert
do_action( "mt_add_log_data", $user_id, $type, $_value, $datetime );
$result = $wpdb->insert( $this->tablename, array(
'user_id' => $user_id,
'date' => $datetime,
'type' => $type,
'value' => $value
), array(
'%d',
'%s',
'%s',
'%s'
) );
//Check result
if ( ! $result )
return false;
$log_id = (int) $wpdb->insert_id;
//Return
do_action( "mt_added_log_data", $log_id, $user_id, $type, $_value, $datetime );
return true;
} //end add_log_value
/**
* create_table()
*
* Creates the table for the plugin logs
*
**/
static function create_table() {
global $wpdb;
//Get collation - From /wp-admin/includes/schema.php
$charset_collate = '';
if ( ! empty($wpdb->charset) )
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty($wpdb->collate) )
$charset_collate .= " COLLATE $wpdb->collate";
//Create table
$tablename = Metronet_Log::get_table_name();
$sql = "CREATE TABLE {$tablename} (
log_id BIGINT(20) NOT NULL AUTO_INCREMENT,
user_id BIGINT (20) NOT NULL,
date DATETIME NOT NULL,
type VARCHAR(255) NOT NULL,
value LONGTEXT NULL,
PRIMARY KEY (log_id),
KEY user_id (user_id),
KEY type (type),
KEY date(date) ) {$charset_collate};";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql);
} //end create_table
/**
* get_log_count()
*
* Retrieves a log count for a specific date range by type
*
* @param string $type The type of data
* @param string $from_date date in format 0000-00-00 00:00:00
* @param string $to_date date in format 0000-00-00 00:00:00
* @return int $count
**/
public function get_log_count( $type, $from_date, $to_date ) {
global $wpdb;
$type = stripslashes( $type );
if ( !$this->validate_date_time( $from_date ) || !$this->validate_date_time( $to_date ) ) return 0;
$query = "SELECT COUNT(*) FROM {$this->tablename} WHERE type = %s AND date BETWEEN %s AND %s";
$query = $wpdb->prepare( $query, $type, $from_date, $to_date );
$result = $wpdb->get_var( $query );
return $result;
} //end get_user_log_count
/**
* get_log_count_by_value()
*
* Retrieves a log count for a specific date range by type and value
*
* @param string $type The type of data
* @param string $value The value of the data
* @param string $from_date date in format 0000-00-00 00:00:00
* @param string $to_date date in format 0000-00-00 00:00:00
* @return int $count
**/
public function get_log_count_by_value( $type, $value, $from_date, $to_date ) {
global $wpdb;
$type = stripslashes( $type );
$value = stripslashes_deep( $value );
$value = maybe_serialize( $value );
if ( !$this->validate_date_time( $from_date ) || !$this->validate_date_time( $to_date ) ) return 0;
$query = "SELECT COUNT(*) FROM {$this->tablename} WHERE type = %s AND value = %s AND date BETWEEN %s AND %s";
$query = $wpdb->prepare( $query, $type, $value, $from_date, $to_date );
$result = $wpdb->get_var( $query );
return $result;
} //end get_log_count_by_value
/**
* get_log_values()
*
* Retrieves log values for a specific date range by type
*
* @param string $type The type of data
* @param string $from_date date in format 0000-00-00 00:00:00
* @param string $to_date date in format 0000-00-00 00:00:00
* @return array of objects
**/
public function get_log_values( $type, $from_date, $to_date ) {
global $wpdb;
$type = stripslashes( $type );
if ( !$this->validate_date_time( $from_date ) || !$this->validate_date_time( $to_date ) ) return array();
$query = "SELECT * FROM {$this->tablename} WHERE type = %s AND date BETWEEN %s AND %s";
$query = $wpdb->prepare( $query, $type, $from_date, $to_date );
$result = $wpdb->get_results( $query, OBJECT );
return $result;
} //end get_user_log_values
/**
* get_table_name()
*
* Returns the tablename for the logs
*
* @return string $tablename - The tablename for the logs
**/
static function get_table_name() {
global $wpdb;
$tablename = $wpdb->base_prefix . 'mt_log';
return $tablename;
} //end get_table_name
/**
* get_user_log_count()
*
* Retrieves user log count for a specific date range
*
* @param int $user_id The user id
* @param string $type The type of data
* @param string $from_date date in format 0000-00-00 00:00:00
* @param string $to_date date in format 0000-00-00 00:00:00
* @return int $count
**/
public function get_user_log_count( $user_id, $type, $from_date, $to_date ) {
global $wpdb;
$user_id = absint( $user_id );
$type = stripslashes( $type );
if ( !$this->validate_date_time( $from_date ) || !$this->validate_date_time( $to_date ) ) return 0;
$query = "SELECT COUNT(*) FROM {$this->tablename} WHERE user_id = %d AND type = %s AND date BETWEEN %s AND %s";
$query = $wpdb->prepare( $query, $user_id, $type, $from_date, $to_date );
$result = $wpdb->get_var( $query );
return $result;
} //end get_user_log_count
/**
* get_user_log_values()
*
* Retrieves user log values for a specific date range
*
* @param int $user_id The user id
* @param string $type The type of data
* @param string $from_date date in format 0000-00-00 00:00:00
* @param string $to_date date in format 0000-00-00 00:00:00
* @return array of objects
**/
public function get_user_log_values( $user_id, $type, $from_date, $to_date ) {
global $wpdb;
$user_id = absint( $user_id );
$type = stripslashes( $type );
if ( !$this->validate_date_time( $from_date ) || !$this->validate_date_time( $to_date ) ) return array();
$query = "SELECT * FROM {$this->tablename} WHERE user_id = %d AND type = %s AND date BETWEEN %s AND %s";
$query = $wpdb->prepare( $query, $user_id, $type, $from_date, $to_date );
$result = $wpdb->get_results( $query, OBJECT );
return $result;
} //end get_user_log_values
/**
* remove_log_value()
*
* Removes a log value based on the log ID
*
* @param int $log_id The log id
* @return bool false on failure, true if success.
**/
public function remove_log_value( $log_id ) {
global $wpdb;
$log_id = absint( $log_id );
$result = $wpdb->query( $wpdb->prepare( "DELETE FROM {$this->tablename} WHERE log_id = %d", $log_id ) );
if ( $result ) return true;
return false;
} //end remove_log_value
/**
* remove_log_values()
*
* Removes log values based on user_id, type, and a date range
*
* @param int $user_id The user id
* @param string $type The type of data
* @param string $from_date date in format 0000-00-00 00:00:00
* @param string $to_date date in format 0000-00-00 00:00:00
* @return bool false on failure, true if success.
**/
public function remove_log_values( $user_id, $type, $from_date, $to_date ) {
global $wpdb;
$user_id = absint( $user_id );
$type = stripslashes( $type );
if ( !$this->validate_date_time( $from_date ) || !$this->validate_date_time( $to_date ) ) return false;
$query = "DELETE FROM {$this->tablename} WHERE user_id = %d AND type = %s AND date BETWEEN %s AND %s";
$query = $wpdb->prepare( $query, $user_id, $type, $from_date, $to_date );
$result = $wpdb->query( $query );
if ( $result ) return true;
return false;
} //end remove_log_value
/**
* update_log_value()
*
* Updates a log item into the database based on the user, type, and date
* This function should rarely be used unless you know the exact datetime
* Assumes user_id, type, and datetime are unique
*
* @param int $user_id The user id
* @param string $type The type of data to add
* @param mixed (string|object|array) - Value associated with the type
* @param string $datetime - Datetime in format 0000-00-00 00:00:00
* @return bool false on failure, true if success.
**/
public function update_log_value( $user_id, $type, $value = '', $datetime = '0000-00-00 00:00:00' ) {
global $wpdb;
//Validate user
$user_id = absint( $user_id );
if ( $user_id == 0 ) return false;
//Validate date
$date_timestamp = strtotime( $datetime );
if ( !$date_timestamp ) return false;
$datetime = date( 'Y-m-d H:i:s', $date_timestamp );
//Validate type
$type = stripslashes( $type );
$value = stripslashes_deep( $value );
$_value = $value;
$value = maybe_serialize( $value );
//Determine if we need to do an insert or update - Try to find previous logs
$query = "SELECT COUNT(*) FROM {$this->tablename} WHERE user_id = %d AND type = %s AND date = %s";
$query = $wpdb->prepare( $query, $user_id, $type, $datetime );
$result = $wpdb->get_var( $query );
if ( $result == 0 ) {
return $this->add_log_value( $user_id, $type, $_value, $datetime );
}
//Perform the DB update
do_action( "mt_update_log_data", $user_id, $type, $_value, $datetime );
$result = $wpdb->update( $this->tablename,
array(
'value' => $value
)
, array(
'user_id' => $user_id,
'type' => $type,
'date' => $datetime
), array(
'%s'
), array(
'%d',
'%s',
'%s'
) );
//Check result
if ( ! $result )
return false;
//Return
do_action( "mt_updated_log_data", $user_id, $type, $_value, $datetime );
return true;
} //end update_log_value
private function validate_date_time( $datetime ) {
if ( !is_string( $datetime ) ) return false;
//from http://stackoverflow.com/questions/37732/what-is-the-regex-pattern-for-datetime-2008-09-01-123545
if( preg_match( '/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/', $datetime ) ) {
return true;
} else {
return false;
}
} //end validate_date_time
}
register_activation_hook( __FILE__, 'Metronet_Log::create_table' );
//I'd say, this is a uniquely clever way of making sure a plugin is loaded. Hook into the action before running dependencies
function metronet_log_loaded() {
do_action( 'metronet_log_loaded' );
}
add_action( 'plugins_loaded', 'metronet_log_loaded' );
?>