1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class for logging events and errors |
5
|
|
|
* |
6
|
|
|
* @package WP Logging Class |
7
|
|
|
* @copyright Copyright (c) 2012, Pippin Williamson |
8
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class WP_Logging { |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class constructor. |
16
|
|
|
* |
17
|
|
|
* @since 1.0 |
18
|
|
|
* |
19
|
|
|
* @access public |
20
|
|
|
* @return void |
|
|
|
|
21
|
|
|
*/ |
22
|
|
|
function __construct() { |
|
|
|
|
23
|
|
|
|
24
|
|
|
// create the log post type |
25
|
|
|
add_action( 'init', array( $this, 'register_post_type' ) ); |
26
|
|
|
|
27
|
|
|
// create types taxonomy and default types |
28
|
|
|
add_action( 'init', array( $this, 'register_taxonomy' ) ); |
29
|
|
|
|
30
|
|
|
// make a cron job for this hook to start pruning |
31
|
|
|
add_action( 'wp_logging_prune_routine', array( $this, 'prune_logs' ) ); |
32
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Allows you to tie in a cron job and prune old logs. |
37
|
|
|
* |
38
|
|
|
* @since 1.1 |
39
|
|
|
* @access public |
40
|
|
|
* |
41
|
|
|
* @uses $this->get_logs_to_prune() Returns array of posts via get_posts of logs to prune |
42
|
|
|
* @uses $this->prune_old_logs() Deletes the logs that we don't want anymore |
43
|
|
|
*/ |
44
|
|
|
public function prune_logs(){ |
45
|
|
|
|
46
|
|
|
$should_we_prune = apply_filters( 'wp_logging_should_we_prune', false ); |
47
|
|
|
|
48
|
|
|
if ( $should_we_prune === false ){ |
|
|
|
|
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$logs_to_prune = $this->get_logs_to_prune(); |
53
|
|
|
|
54
|
|
|
if ( isset( $logs_to_prune ) && ! empty( $logs_to_prune ) ){ |
55
|
|
|
$this->prune_old_logs( $logs_to_prune ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
} // prune_logs |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Deletes the old logs that we don't want |
62
|
|
|
* |
63
|
|
|
* @since 1.1 |
64
|
|
|
* @access private |
65
|
|
|
* |
66
|
|
|
* @param array/obj $logs required The array of logs we want to prune |
|
|
|
|
67
|
|
|
* |
68
|
|
|
* @uses wp_delete_post() Deletes the post from WordPress |
69
|
|
|
* |
70
|
|
|
* @filter wp_logging_force_delete_log Allows user to override the force delete setting which bypasses the trash |
71
|
|
|
*/ |
72
|
|
|
private function prune_old_logs( $logs ){ |
73
|
|
|
|
74
|
|
|
$force = apply_filters( 'wp_logging_force_delete_log', true ); |
75
|
|
|
|
76
|
|
|
foreach( $logs as $l ){ |
|
|
|
|
77
|
|
|
$id = is_int( $l ) ? $l : $l->ID; |
78
|
|
|
wp_delete_post( $id, $force ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} // prune_old_logs |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns an array of posts that are prune candidates. |
85
|
|
|
* |
86
|
|
|
* @since 1.1 |
87
|
|
|
* @access private |
88
|
|
|
* |
89
|
|
|
* @return array $old_logs The array of posts that were returned from get_posts |
90
|
|
|
* |
91
|
|
|
* @uses apply_filters() Allows users to change given args |
92
|
|
|
* @uses get_posts() Returns an array of posts from given args |
93
|
|
|
* |
94
|
|
|
* @filter wp_logging_prune_when Users can change how long ago we are looking for logs to prune |
95
|
|
|
* @filter wp_logging_prune_query_args Gives users access to change any query args for pruning |
96
|
|
|
*/ |
97
|
|
|
private function get_logs_to_prune(){ |
98
|
|
|
|
99
|
|
|
$how_old = apply_filters( 'wp_logging_prune_when', '2 weeks ago' ); |
100
|
|
|
|
101
|
|
|
$args = array( |
102
|
|
|
'post_type' => 'wp_log', |
103
|
|
|
'posts_per_page' => '100', |
|
|
|
|
104
|
|
|
'date_query' => array( |
105
|
|
|
array( |
106
|
|
|
'column' => 'post_date_gmt', |
107
|
|
|
'before' => (string) $how_old, |
108
|
|
|
) |
|
|
|
|
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$old_logs = get_posts( apply_filters( 'wp_logging_prune_query_args', $args ) ); |
113
|
|
|
|
114
|
|
|
return $old_logs; |
115
|
|
|
|
116
|
|
|
} // get_logs_to_prune |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Log types |
120
|
|
|
* |
121
|
|
|
* Sets up the default log types and allows for new ones to be created |
122
|
|
|
* |
123
|
|
|
* @access private |
124
|
|
|
* @since 1.0 |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
|
129
|
|
|
private static function log_types() { |
130
|
|
|
$terms = array( |
131
|
|
|
'error', 'event' |
|
|
|
|
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
return apply_filters( 'wp_log_types', $terms ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Registers the wp_log Post Type |
140
|
|
|
* |
141
|
|
|
* @access public |
142
|
|
|
* @since 1.0 |
143
|
|
|
* |
144
|
|
|
* @uses register_post_type() |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
|
149
|
|
|
public function register_post_type() { |
150
|
|
|
|
151
|
|
|
/* logs post type */ |
152
|
|
|
|
153
|
|
|
$log_args = array( |
154
|
|
|
'labels' => array( 'name' => __( 'Logs', 'wp-logging' ) ), |
155
|
|
|
'public' => defined( 'WP_DEBUG' ) && WP_DEBUG, |
156
|
|
|
'query_var' => false, |
157
|
|
|
'rewrite' => false, |
158
|
|
|
'capability_type' => 'post', |
159
|
|
|
'supports' => array( 'title', 'editor' ), |
160
|
|
|
'can_export' => false |
|
|
|
|
161
|
|
|
); |
162
|
|
|
register_post_type( 'wp_log', apply_filters( 'wp_logging_post_type_args', $log_args ) ); |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Registers the Type Taxonomy |
169
|
|
|
* |
170
|
|
|
* The Type taxonomy is used to determine the type of log entry |
171
|
|
|
* |
172
|
|
|
* @access public |
173
|
|
|
* @since 1.0 |
174
|
|
|
* |
175
|
|
|
* @uses register_taxonomy() |
176
|
|
|
* @uses term_exists() |
177
|
|
|
* @uses wp_insert_term() |
178
|
|
|
* |
179
|
|
|
* @return void |
180
|
|
|
*/ |
181
|
|
|
|
182
|
|
|
public function register_taxonomy() { |
183
|
|
|
|
184
|
|
|
register_taxonomy( 'wp_log_type', 'wp_log', array( 'public' => defined( 'WP_DEBUG' ) && WP_DEBUG ) ); |
185
|
|
|
|
186
|
|
|
$types = self::log_types(); |
187
|
|
|
|
188
|
|
|
foreach ( $types as $type ) { |
189
|
|
|
if( ! term_exists( $type, 'wp_log_type' ) ) { |
|
|
|
|
190
|
|
|
wp_insert_term( $type, 'wp_log_type' ); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Check if a log type is valid |
198
|
|
|
* |
199
|
|
|
* Checks to see if the specified type is in the registered list of types |
200
|
|
|
* |
201
|
|
|
* @access private |
202
|
|
|
* @since 1.0 |
203
|
|
|
* |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
|
208
|
|
|
private static function valid_type( $type ) { |
209
|
|
|
return in_array( $type, self::log_types() ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Create new log entry |
215
|
|
|
* |
216
|
|
|
* This is just a simple and fast way to log something. Use self::insert_log() |
217
|
|
|
* if you need to store custom meta data |
218
|
|
|
* |
219
|
|
|
* @access private |
220
|
|
|
* @since 1.0 |
221
|
|
|
* |
222
|
|
|
* @uses self::insert_log() |
223
|
|
|
* |
224
|
|
|
* @return int The ID of the new log entry |
225
|
|
|
*/ |
226
|
|
|
|
227
|
|
|
public static function add( $title = '', $message = '', $parent = 0, $type = null ) { |
228
|
|
|
|
229
|
|
|
$log_data = array( |
230
|
|
|
'post_title' => $title, |
231
|
|
|
'post_content' => $message, |
232
|
|
|
'post_parent' => $parent, |
233
|
|
|
'log_type' => $type |
|
|
|
|
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
return self::insert_log( $log_data ); |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Stores a log entry |
243
|
|
|
* |
244
|
|
|
* @access private |
245
|
|
|
* @since 1.0 |
246
|
|
|
* |
247
|
|
|
* @uses wp_parse_args() |
248
|
|
|
* @uses wp_insert_post() |
249
|
|
|
* @uses update_post_meta() |
250
|
|
|
* @uses wp_set_object_terms() |
251
|
|
|
* @uses sanitize_key() |
252
|
|
|
* |
253
|
|
|
* @return int The ID of the newly created log item |
254
|
|
|
*/ |
255
|
|
|
|
256
|
|
|
public static function insert_log( $log_data = array(), $log_meta = array() ) { |
257
|
|
|
|
258
|
|
|
$defaults = array( |
259
|
|
|
'post_type' => 'wp_log', |
260
|
|
|
'post_status' => 'publish', |
261
|
|
|
'post_parent' => 0, |
262
|
|
|
'post_content' => '', |
263
|
|
|
'log_type' => false |
|
|
|
|
264
|
|
|
); |
265
|
|
|
|
266
|
|
|
$args = wp_parse_args( $log_data, $defaults ); |
267
|
|
|
|
268
|
|
|
do_action( 'wp_pre_insert_log' ); |
269
|
|
|
|
270
|
|
|
// store the log entry |
271
|
|
|
$log_id = wp_insert_post( $args ); |
272
|
|
|
|
273
|
|
|
// set the log type, if any |
274
|
|
|
if( $log_data['log_type'] && self::valid_type( $log_data['log_type'] ) ) { |
|
|
|
|
275
|
|
|
wp_set_object_terms( $log_id, $log_data['log_type'], 'wp_log_type', false ); |
276
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
279
|
|
|
// set log meta, if any |
280
|
|
View Code Duplication |
if( $log_id && ! empty( $log_meta ) ) { |
|
|
|
|
281
|
|
|
foreach( (array) $log_meta as $key => $meta ) { |
|
|
|
|
282
|
|
|
update_post_meta( $log_id, '_wp_log_' . sanitize_key( $key ), $meta ); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
do_action( 'wp_post_insert_log', $log_id ); |
287
|
|
|
|
288
|
|
|
return $log_id; |
289
|
|
|
|
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Update and existing log item |
295
|
|
|
* |
296
|
|
|
* @access private |
297
|
|
|
* @since 1.0 |
298
|
|
|
* |
299
|
|
|
* @uses wp_parse_args() |
300
|
|
|
* @uses wp_update_post() |
301
|
|
|
* @uses update_post_meta() |
302
|
|
|
* |
303
|
|
|
* @return bool True if successful, false otherwise |
304
|
|
|
*/ |
305
|
|
|
public static function update_log( $log_data = array(), $log_meta = array() ) { |
306
|
|
|
|
307
|
|
|
do_action( 'wp_pre_update_log', $log_id ); |
308
|
|
|
|
309
|
|
|
$defaults = array( |
310
|
|
|
'post_type' => 'wp_log', |
311
|
|
|
'post_status' => 'publish', |
312
|
|
|
'post_parent' => 0 |
|
|
|
|
313
|
|
|
); |
314
|
|
|
|
315
|
|
|
$args = wp_parse_args( $log_data, $defaults ); |
316
|
|
|
|
317
|
|
|
// store the log entry |
318
|
|
|
$log_id = wp_update_post( $args ); |
319
|
|
|
|
320
|
|
View Code Duplication |
if( $log_id && ! empty( $log_meta ) ) { |
|
|
|
|
321
|
|
|
foreach( (array) $log_meta as $key => $meta ) { |
|
|
|
|
322
|
|
|
if( ! empty( $meta ) ) |
|
|
|
|
323
|
|
|
update_post_meta( $log_id, '_wp_log_' . sanitize_key( $key ), $meta ); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
do_action( 'wp_post_update_log', $log_id ); |
328
|
|
|
|
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Easily retrieves log items for a particular object ID |
334
|
|
|
* |
335
|
|
|
* @access private |
336
|
|
|
* @since 1.0 |
337
|
|
|
* |
338
|
|
|
* @uses self::get_connected_logs() |
339
|
|
|
* |
340
|
|
|
* @return array |
341
|
|
|
*/ |
342
|
|
|
|
343
|
|
|
public static function get_logs( $object_id = 0, $type = null, $paged = null ) { |
344
|
|
|
return self::get_connected_logs( array( 'post_parent' => $object_id, 'paged' => $paged, 'log_type' => $type ) ); |
345
|
|
|
|
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Retrieve all connected logs |
351
|
|
|
* |
352
|
|
|
* Used for retrieving logs related to particular items, such as a specific purchase. |
353
|
|
|
* |
354
|
|
|
* @access private |
355
|
|
|
* @since 1.0 |
356
|
|
|
* |
357
|
|
|
* @uses wp_parse_args() |
358
|
|
|
* @uses get_posts() |
359
|
|
|
* @uses get_query_var() |
360
|
|
|
* @uses self::valid_type() |
361
|
|
|
* |
362
|
|
|
* @return array / false |
363
|
|
|
*/ |
364
|
|
|
|
365
|
|
View Code Duplication |
public static function get_connected_logs( $args = array() ) { |
|
|
|
|
366
|
|
|
|
367
|
|
|
$defaults = array( |
368
|
|
|
'post_parent' => 0, |
369
|
|
|
'post_type' => 'wp_log', |
370
|
|
|
'posts_per_page' => 10, |
371
|
|
|
'post_status' => 'publish', |
372
|
|
|
'paged' => get_query_var( 'paged' ), |
373
|
|
|
'log_type' => false |
|
|
|
|
374
|
|
|
); |
375
|
|
|
|
376
|
|
|
$query_args = wp_parse_args( $args, $defaults ); |
377
|
|
|
|
378
|
|
|
if( $query_args['log_type'] && self::valid_type( $query_args['log_type'] ) ) { |
|
|
|
|
379
|
|
|
|
380
|
|
|
$query_args['tax_query'] = array( |
|
|
|
|
381
|
|
|
array( |
382
|
|
|
'taxonomy' => 'wp_log_type', |
383
|
|
|
'field' => 'slug', |
384
|
|
|
'terms' => $query_args['log_type'] |
|
|
|
|
385
|
|
|
) |
|
|
|
|
386
|
|
|
); |
387
|
|
|
|
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
$logs = get_posts( $query_args ); |
391
|
|
|
|
392
|
|
|
if( $logs ) |
|
|
|
|
393
|
|
|
return $logs; |
394
|
|
|
|
395
|
|
|
// no logs found |
396
|
|
|
return false; |
397
|
|
|
|
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Retrieves number of log entries connected to particular object ID |
403
|
|
|
* |
404
|
|
|
* @access private |
405
|
|
|
* @since 1.0 |
406
|
|
|
* |
407
|
|
|
* @uses WP_Query() |
408
|
|
|
* @uses self::valid_type() |
409
|
|
|
* |
410
|
|
|
* @return int |
411
|
|
|
*/ |
412
|
|
|
|
413
|
|
|
public static function get_log_count( $object_id = 0, $type = null, $meta_query = null ) { |
414
|
|
|
|
415
|
|
|
$query_args = array( |
416
|
|
|
'post_parent' => $object_id, |
417
|
|
|
'post_type' => 'wp_log', |
418
|
|
|
'posts_per_page' => -1, |
|
|
|
|
419
|
|
|
'post_status' => 'publish' |
|
|
|
|
420
|
|
|
); |
421
|
|
|
|
422
|
|
View Code Duplication |
if( ! empty( $type ) && self::valid_type( $type ) ) { |
|
|
|
|
423
|
|
|
|
424
|
|
|
$query_args['tax_query'] = array( |
|
|
|
|
425
|
|
|
array( |
426
|
|
|
'taxonomy' => 'wp_log_type', |
427
|
|
|
'field' => 'slug', |
428
|
|
|
'terms' => $type |
|
|
|
|
429
|
|
|
) |
|
|
|
|
430
|
|
|
); |
431
|
|
|
|
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
if( ! empty( $meta_query ) ) { |
|
|
|
|
435
|
|
|
$query_args['meta_query'] = $meta_query; |
|
|
|
|
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$logs = new WP_Query( $query_args ); |
439
|
|
|
|
440
|
|
|
return (int) $logs->post_count; |
441
|
|
|
|
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
} |
445
|
|
|
$GLOBALS['wp_logs'] = new WP_Logging(); |
446
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.