|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Class for logging events and errors |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Logging |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
8
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
|
9
|
|
|
* @since 1.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// Exit if accessed directly |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Give_Logging Class |
|
19
|
|
|
* |
|
20
|
|
|
* A general use class for logging events and errors. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 1.0 |
|
23
|
|
|
*/ |
|
24
|
|
|
class Give_Logging { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Set up the Give Logging Class |
|
28
|
|
|
* |
|
29
|
|
|
* @since 1.0 |
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function __construct() { |
|
32
|
|
|
// Create the log post type |
|
33
|
6 |
|
add_action( 'init', array( $this, 'register_post_type' ), 1 ); |
|
34
|
|
|
|
|
35
|
|
|
// Create types taxonomy and default types |
|
36
|
6 |
|
add_action( 'init', array( $this, 'register_taxonomy' ), 1 ); |
|
37
|
|
|
|
|
38
|
6 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Registers the give_log Post Type |
|
42
|
|
|
* |
|
43
|
|
|
* @access public |
|
44
|
|
|
* @since 1.0 |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
6 |
|
public function register_post_type() { |
|
48
|
|
|
/* Logs post type */ |
|
49
|
|
|
$log_args = array( |
|
50
|
6 |
|
'labels' => array( 'name' => __( 'Logs', 'give' ) ), |
|
51
|
6 |
|
'public' => false, |
|
52
|
6 |
|
'exclude_from_search' => true, |
|
53
|
6 |
|
'publicly_queryable' => false, |
|
54
|
6 |
|
'show_ui' => false, |
|
55
|
6 |
|
'query_var' => false, |
|
56
|
6 |
|
'rewrite' => false, |
|
57
|
6 |
|
'capability_type' => 'post', |
|
58
|
6 |
|
'supports' => array( 'title', 'editor' ), |
|
59
|
|
|
'can_export' => true |
|
60
|
6 |
|
); |
|
61
|
|
|
|
|
62
|
6 |
|
register_post_type( 'give_log', $log_args ); |
|
63
|
6 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Registers the Type Taxonomy |
|
67
|
|
|
* |
|
68
|
|
|
* The "Type" taxonomy is used to determine the type of log entry |
|
69
|
|
|
* |
|
70
|
|
|
* @access public |
|
71
|
|
|
* @since 1.0 |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
6 |
|
public function register_taxonomy() { |
|
75
|
6 |
|
register_taxonomy( 'give_log_type', 'give_log', array( 'public' => false ) ); |
|
76
|
6 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Log types |
|
80
|
|
|
* |
|
81
|
|
|
* Sets up the default log types and allows for new ones to be created |
|
82
|
|
|
* |
|
83
|
|
|
* @access public |
|
84
|
|
|
* @since 1.0 |
|
85
|
|
|
* @return array $terms |
|
86
|
|
|
*/ |
|
87
|
55 |
|
public function log_types() { |
|
88
|
|
|
$terms = array( |
|
89
|
55 |
|
'sale', |
|
90
|
55 |
|
'gateway_error', |
|
91
|
|
|
'api_request' |
|
92
|
55 |
|
); |
|
93
|
|
|
|
|
94
|
55 |
|
return apply_filters( 'give_log_types', $terms ); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Check if a log type is valid |
|
99
|
|
|
* |
|
100
|
|
|
* Checks to see if the specified type is in the registered list of types |
|
101
|
|
|
* |
|
102
|
|
|
* @access public |
|
103
|
|
|
* @since 1.0 |
|
104
|
|
|
* @uses Give_Logging::log_types() |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $type Log type |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool Whether log type is valid |
|
109
|
|
|
*/ |
|
110
|
54 |
|
function valid_type( $type ) { |
|
|
|
|
|
|
111
|
54 |
|
return in_array( $type, $this->log_types() ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Create new log entry |
|
116
|
|
|
* |
|
117
|
|
|
* This is just a simple and fast way to log something. Use $this->insert_log() |
|
118
|
|
|
* if you need to store custom meta data |
|
119
|
|
|
* |
|
120
|
|
|
* @access public |
|
121
|
|
|
* @since 1.0 |
|
122
|
|
|
* @uses Give_Logging::insert_log() |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $title Log entry title |
|
125
|
|
|
* @param string $message Log entry message |
|
126
|
|
|
* @param int $parent Log entry parent |
|
127
|
|
|
* @param string $type Log type (default: null) |
|
128
|
|
|
* |
|
129
|
|
|
* @return int Log ID |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function add( $title = '', $message = '', $parent = 0, $type = null ) { |
|
132
|
|
|
$log_data = array( |
|
133
|
1 |
|
'post_title' => $title, |
|
134
|
1 |
|
'post_content' => $message, |
|
135
|
1 |
|
'post_parent' => $parent, |
|
136
|
|
|
'log_type' => $type |
|
137
|
1 |
|
); |
|
138
|
|
|
|
|
139
|
1 |
|
return $this->insert_log( $log_data ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Easily retrieves log items for a particular object ID |
|
144
|
|
|
* |
|
145
|
|
|
* @access public |
|
146
|
|
|
* @since 1.0 |
|
147
|
|
|
* @uses Give_Logging::get_connected_logs() |
|
148
|
|
|
* |
|
149
|
|
|
* @param int $object_id (default: 0) |
|
150
|
|
|
* @param string $type Log type (default: null) |
|
151
|
|
|
* @param int $paged Page number (default: null) |
|
152
|
|
|
* |
|
153
|
|
|
* @return array Array of the connected logs |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function get_logs( $object_id = 0, $type = null, $paged = null ) { |
|
156
|
1 |
|
return $this->get_connected_logs( array( |
|
157
|
1 |
|
'post_parent' => $object_id, |
|
158
|
1 |
|
'paged' => $paged, |
|
159
|
|
|
'log_type' => $type |
|
160
|
1 |
|
) ); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Stores a log entry |
|
165
|
|
|
* |
|
166
|
|
|
* @access public |
|
167
|
|
|
* @since 1.0 |
|
168
|
|
|
* @uses Give_Logging::valid_type() |
|
169
|
|
|
* |
|
170
|
|
|
* @param array $log_data Log entry data |
|
171
|
|
|
* @param array $log_meta Log entry meta |
|
172
|
|
|
* |
|
173
|
|
|
* @return int The ID of the newly created log item |
|
174
|
|
|
*/ |
|
175
|
43 |
|
function insert_log( $log_data = array(), $log_meta = array() ) { |
|
|
|
|
|
|
176
|
|
|
$defaults = array( |
|
177
|
43 |
|
'post_type' => 'give_log', |
|
178
|
43 |
|
'post_status' => 'publish', |
|
179
|
43 |
|
'post_parent' => 0, |
|
180
|
43 |
|
'post_content' => '', |
|
181
|
|
|
'log_type' => false |
|
182
|
43 |
|
); |
|
183
|
|
|
|
|
184
|
43 |
|
$args = wp_parse_args( $log_data, $defaults ); |
|
185
|
|
|
|
|
186
|
43 |
|
do_action( 'give_pre_insert_log', $log_data, $log_meta ); |
|
187
|
|
|
|
|
188
|
|
|
// Store the log entry |
|
189
|
43 |
|
$log_id = wp_insert_post( $args ); |
|
190
|
|
|
|
|
191
|
|
|
// Set the log type, if any |
|
192
|
43 |
|
if ( $log_data['log_type'] && $this->valid_type( $log_data['log_type'] ) ) { |
|
193
|
43 |
|
wp_set_object_terms( $log_id, $log_data['log_type'], 'give_log_type', false ); |
|
194
|
43 |
|
} |
|
195
|
|
|
|
|
196
|
|
|
// Set log meta, if any |
|
197
|
43 |
|
if ( $log_id && ! empty( $log_meta ) ) { |
|
198
|
42 |
|
foreach ( (array) $log_meta as $key => $meta ) { |
|
199
|
42 |
|
update_post_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta ); |
|
200
|
42 |
|
} |
|
201
|
42 |
|
} |
|
202
|
|
|
|
|
203
|
43 |
|
do_action( 'give_post_insert_log', $log_id, $log_data, $log_meta ); |
|
204
|
|
|
|
|
205
|
43 |
|
return $log_id; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Update and existing log item |
|
210
|
|
|
* |
|
211
|
|
|
* @access public |
|
212
|
|
|
* @since 1.0 |
|
213
|
|
|
* |
|
214
|
|
|
* @param array $log_data Log entry data |
|
215
|
|
|
* @param array $log_meta Log entry meta |
|
216
|
|
|
* |
|
217
|
|
|
* @return bool True if successful, false otherwise |
|
218
|
|
|
*/ |
|
219
|
|
|
public function update_log( $log_data = array(), $log_meta = array() ) { |
|
220
|
|
|
|
|
221
|
|
|
do_action( 'give_pre_update_log', $log_data, $log_meta ); |
|
222
|
|
|
|
|
223
|
|
|
$defaults = array( |
|
224
|
|
|
'post_type' => 'give_log', |
|
225
|
|
|
'post_status' => 'publish', |
|
226
|
|
|
'post_parent' => 0 |
|
227
|
|
|
); |
|
228
|
|
|
|
|
229
|
|
|
$args = wp_parse_args( $log_data, $defaults ); |
|
230
|
|
|
|
|
231
|
|
|
// Store the log entry |
|
232
|
|
|
$log_id = wp_update_post( $args ); |
|
233
|
|
|
|
|
234
|
|
|
if ( $log_id && ! empty( $log_meta ) ) { |
|
235
|
|
|
foreach ( (array) $log_meta as $key => $meta ) { |
|
236
|
|
|
if ( ! empty( $meta ) ) { |
|
237
|
|
|
update_post_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta ); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
do_action( 'give_post_update_log', $log_id, $log_data, $log_meta ); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Retrieve all connected logs |
|
247
|
|
|
* |
|
248
|
|
|
* Used for retrieving logs related to particular items, such as a specific donation. |
|
249
|
|
|
* |
|
250
|
|
|
* @access private |
|
251
|
|
|
* @since 1.0 |
|
252
|
|
|
* |
|
253
|
|
|
* @param array $args Query arguments |
|
254
|
|
|
* |
|
255
|
|
|
* @return mixed array if logs were found, false otherwise |
|
256
|
|
|
*/ |
|
257
|
1 |
|
public function get_connected_logs( $args = array() ) { |
|
258
|
|
|
|
|
259
|
|
|
$defaults = array( |
|
260
|
1 |
|
'post_type' => 'give_log', |
|
261
|
1 |
|
'posts_per_page' => 20, |
|
262
|
1 |
|
'post_status' => 'publish', |
|
263
|
1 |
|
'paged' => get_query_var( 'paged' ), |
|
264
|
|
|
'log_type' => false |
|
265
|
1 |
|
); |
|
266
|
|
|
|
|
267
|
1 |
|
$query_args = wp_parse_args( $args, $defaults ); |
|
268
|
|
|
|
|
269
|
1 |
|
if ( $query_args['log_type'] && $this->valid_type( $query_args['log_type'] ) ) { |
|
270
|
1 |
|
$query_args['tax_query'] = array( |
|
271
|
|
|
array( |
|
272
|
1 |
|
'taxonomy' => 'give_log_type', |
|
273
|
1 |
|
'field' => 'slug', |
|
274
|
1 |
|
'terms' => $query_args['log_type'] |
|
275
|
1 |
|
) |
|
276
|
1 |
|
); |
|
277
|
1 |
|
} |
|
278
|
|
|
|
|
279
|
1 |
|
$logs = get_posts( $query_args ); |
|
280
|
|
|
|
|
281
|
1 |
|
if ( $logs ) { |
|
282
|
1 |
|
return $logs; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
// No logs found |
|
286
|
|
|
return false; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Retrieves number of log entries connected to particular object ID |
|
291
|
|
|
* |
|
292
|
|
|
* @access public |
|
293
|
|
|
* @since 1.0 |
|
294
|
|
|
* |
|
295
|
|
|
* @param int $object_id (default: 0) |
|
296
|
|
|
* @param string $type Log type (default: null) |
|
297
|
|
|
* @param array $meta_query Log meta query (default: null) |
|
298
|
|
|
* @param array $date_query Log data query (default: null) (since 1.9) |
|
299
|
|
|
* |
|
300
|
|
|
* @return int Log count |
|
301
|
|
|
*/ |
|
302
|
1 |
|
public function get_log_count( $object_id = 0, $type = null, $meta_query = null, $date_query = null ) { |
|
303
|
|
|
|
|
304
|
|
|
$query_args = array( |
|
305
|
1 |
|
'post_parent' => $object_id, |
|
306
|
1 |
|
'post_type' => 'give_log', |
|
307
|
1 |
|
'posts_per_page' => - 1, |
|
308
|
1 |
|
'post_status' => 'publish', |
|
309
|
1 |
|
'fields' => 'ids', |
|
310
|
1 |
|
); |
|
311
|
|
|
|
|
312
|
1 |
|
if ( ! empty( $type ) && $this->valid_type( $type ) ) { |
|
313
|
1 |
|
$query_args['tax_query'] = array( |
|
314
|
|
|
array( |
|
315
|
1 |
|
'taxonomy' => 'give_log_type', |
|
316
|
1 |
|
'field' => 'slug', |
|
317
|
|
|
'terms' => $type |
|
318
|
1 |
|
) |
|
319
|
1 |
|
); |
|
320
|
1 |
|
} |
|
321
|
|
|
|
|
322
|
1 |
|
if ( ! empty( $meta_query ) ) { |
|
323
|
|
|
$query_args['meta_query'] = $meta_query; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
1 |
|
if ( ! empty( $date_query ) ) { |
|
327
|
|
|
$query_args['date_query'] = $date_query; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
1 |
|
$logs = new WP_Query( $query_args ); |
|
331
|
|
|
|
|
332
|
1 |
|
return (int) $logs->post_count; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Delete a log |
|
337
|
|
|
* |
|
338
|
|
|
* @access public |
|
339
|
|
|
* @since 1.0 |
|
340
|
|
|
* @uses Give_Logging::valid_type |
|
341
|
|
|
* |
|
342
|
|
|
* @param int $object_id (default: 0) |
|
343
|
|
|
* @param string $type Log type (default: null) |
|
344
|
|
|
* @param array $meta_query Log meta query (default: null) |
|
345
|
|
|
* |
|
346
|
|
|
* @return void |
|
347
|
|
|
*/ |
|
348
|
32 |
|
public function delete_logs( $object_id = 0, $type = null, $meta_query = null ) { |
|
349
|
|
|
$query_args = array( |
|
350
|
32 |
|
'post_parent' => $object_id, |
|
351
|
32 |
|
'post_type' => 'give_log', |
|
352
|
32 |
|
'posts_per_page' => - 1, |
|
353
|
32 |
|
'post_status' => 'publish', |
|
354
|
|
|
'fields' => 'ids' |
|
355
|
32 |
|
); |
|
356
|
|
|
|
|
357
|
32 |
|
if ( ! empty( $type ) && $this->valid_type( $type ) ) { |
|
358
|
31 |
|
$query_args['tax_query'] = array( |
|
359
|
|
|
array( |
|
360
|
31 |
|
'taxonomy' => 'give_log_type', |
|
361
|
31 |
|
'field' => 'slug', |
|
362
|
31 |
|
'terms' => $type, |
|
363
|
|
|
) |
|
364
|
31 |
|
); |
|
365
|
31 |
|
} |
|
366
|
|
|
|
|
367
|
32 |
|
if ( ! empty( $meta_query ) ) { |
|
368
|
31 |
|
$query_args['meta_query'] = $meta_query; |
|
369
|
31 |
|
} |
|
370
|
|
|
|
|
371
|
32 |
|
$logs = get_posts( $query_args ); |
|
372
|
|
|
|
|
373
|
32 |
|
if ( $logs ) { |
|
374
|
7 |
|
foreach ( $logs as $log ) { |
|
375
|
7 |
|
wp_delete_post( $log, true ); |
|
376
|
7 |
|
} |
|
377
|
7 |
|
} |
|
378
|
32 |
|
} |
|
379
|
|
|
|
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
// Initiate the logging system |
|
383
|
|
|
$GLOBALS['give_logs'] = new Give_Logging(); |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Record a log entry |
|
387
|
|
|
* |
|
388
|
|
|
* This is just a simple wrapper function for the log class add() function |
|
389
|
|
|
* |
|
390
|
|
|
* @since 1.0 |
|
391
|
|
|
* |
|
392
|
|
|
* @param string $title |
|
393
|
|
|
* @param string $message |
|
394
|
|
|
* @param int $parent |
|
395
|
|
|
* @param null $type |
|
396
|
|
|
* |
|
397
|
|
|
* @global $give_logs GIVE Logs Object |
|
398
|
|
|
* |
|
399
|
|
|
* @uses Give_Logging::add() |
|
400
|
|
|
* |
|
401
|
|
|
* @return mixed ID of the new log entry |
|
402
|
|
|
*/ |
|
403
|
|
|
function give_record_log( $title = '', $message = '', $parent = 0, $type = null ) { |
|
404
|
|
|
global $give_logs; |
|
|
|
|
|
|
405
|
|
|
$log = $give_logs->add( $title, $message, $parent, $type ); |
|
406
|
|
|
|
|
407
|
|
|
return $log; |
|
408
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.