|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class for logging events and errors |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Classes/Give_Logging |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
8
|
|
|
* @license https://opensource.org/licenses/gpl-license 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
|
|
|
* Logs data operation handler object. |
|
27
|
|
|
* |
|
28
|
|
|
* @since 2.0 |
|
29
|
|
|
* @access private |
|
30
|
|
|
* @var Give_DB_Logs |
|
31
|
6 |
|
*/ |
|
32
|
|
|
public $log_db; |
|
33
|
6 |
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Log meta data operation handler object. |
|
36
|
6 |
|
* |
|
37
|
|
|
* @since 2.0 |
|
38
|
6 |
|
* @access private |
|
39
|
|
|
* @var Give_DB_Log_Meta |
|
40
|
|
|
*/ |
|
41
|
|
|
public $logmeta_db; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Class Constructor |
|
45
|
|
|
* |
|
46
|
|
|
* Set up the Give Logging Class. |
|
47
|
6 |
|
* |
|
48
|
|
|
* @since 1.0 |
|
49
|
|
|
* @access public |
|
50
|
6 |
|
*/ |
|
51
|
6 |
|
public function __construct() { |
|
52
|
6 |
|
/** |
|
53
|
6 |
|
* Setup properties |
|
54
|
6 |
|
*/ |
|
55
|
6 |
|
|
|
56
|
6 |
|
require_once GIVE_PLUGIN_DIR . 'includes/class-give-db-logs.php'; |
|
57
|
6 |
|
require_once GIVE_PLUGIN_DIR . 'includes/class-give-db-logs-meta.php'; |
|
58
|
6 |
|
$this->log_db = new Give_DB_Logs(); |
|
59
|
|
|
$this->logmeta_db = new Give_DB_Log_Meta(); |
|
60
|
6 |
|
|
|
61
|
|
|
/** |
|
62
|
6 |
|
* Setup hooks. |
|
63
|
6 |
|
*/ |
|
64
|
|
|
|
|
65
|
|
|
add_action( 'save_post_give_payment', array( $this, 'background_process_delete_cache' ) ); |
|
66
|
|
|
add_action( 'save_post_give_forms', array( $this, 'background_process_delete_cache' ) ); |
|
67
|
|
|
add_action( 'save_post_give_log', array( $this, 'background_process_delete_cache' ) ); |
|
68
|
|
|
add_action( 'give_delete_log_cache', array( $this, 'delete_cache' ) ); |
|
69
|
|
|
add_action( 'update_log_metadata', array( $this, 'bc_200_set_payment_as_log_parent' ), 10, 4 ); |
|
70
|
|
|
|
|
71
|
|
|
// Backward compatibility. |
|
72
|
|
|
if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
|
73
|
|
|
// Create the log post type |
|
74
|
6 |
|
add_action( 'init', array( $this, 'register_post_type' ), -2 ); |
|
75
|
6 |
|
} |
|
76
|
6 |
|
|
|
77
|
|
|
// Create types taxonomy and default types |
|
78
|
|
|
// @todo: remove this taxonomy, some addon use this taxonomy with there custom log post type for example: recurring |
|
79
|
|
|
// Do not use this taxonomy with your log type because we will remove it in future releases. |
|
80
|
|
|
add_action( 'init', array( $this, 'register_taxonomy' ), -2 ); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Log Post Type |
|
86
|
|
|
* |
|
87
|
55 |
|
* Registers the 'give_log' Post Type. |
|
88
|
|
|
* |
|
89
|
55 |
|
* @since 1.0 |
|
90
|
55 |
|
* @access public |
|
91
|
|
|
* |
|
92
|
55 |
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
55 |
|
public function register_post_type() { |
|
95
|
|
|
/* Logs post type */ |
|
96
|
|
|
$log_args = array( |
|
97
|
|
|
'labels' => array( |
|
98
|
|
|
'name' => esc_html__( 'Logs', 'give' ), |
|
99
|
|
|
), |
|
100
|
|
|
'public' => false, |
|
101
|
|
|
'exclude_from_search' => true, |
|
102
|
|
|
'publicly_queryable' => false, |
|
103
|
|
|
'show_ui' => false, |
|
104
|
|
|
'query_var' => false, |
|
105
|
|
|
'rewrite' => false, |
|
106
|
|
|
'capability_type' => 'post', |
|
107
|
|
|
'supports' => array( 'title', 'editor' ), |
|
108
|
|
|
'can_export' => true, |
|
109
|
|
|
); |
|
110
|
54 |
|
|
|
111
|
54 |
|
register_post_type( 'give_log', $log_args ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Log Type Taxonomy |
|
116
|
|
|
* |
|
117
|
|
|
* Registers the "Log Type" taxonomy. Used to determine the type of log entry. |
|
118
|
|
|
* |
|
119
|
|
|
* @since 1.0 |
|
120
|
|
|
* @access public |
|
121
|
|
|
* |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
|
|
public function register_taxonomy() { |
|
125
|
|
|
register_taxonomy( 'give_log_type', 'give_log', array( |
|
126
|
|
|
'public' => false, |
|
127
|
|
|
) ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
1 |
|
* Log Types |
|
132
|
|
|
* |
|
133
|
1 |
|
* Sets up the default log types and allows for new ones to be created. |
|
134
|
1 |
|
* |
|
135
|
1 |
|
* @since 1.0 |
|
136
|
|
|
* @access public |
|
137
|
1 |
|
* |
|
138
|
|
|
* @return array $terms |
|
139
|
1 |
|
*/ |
|
140
|
|
|
public function log_types() { |
|
141
|
|
|
$terms = array( |
|
142
|
|
|
'sale', |
|
143
|
|
|
'gateway_error', |
|
144
|
|
|
'api_request', |
|
145
|
|
|
'update', |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
return apply_filters( 'give_log_types', $terms ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Check if a log type is valid |
|
153
|
|
|
* |
|
154
|
|
|
* Checks to see if the specified type is in the registered list of types. |
|
155
|
1 |
|
* |
|
156
|
1 |
|
* @since 1.0 |
|
157
|
1 |
|
* @access public |
|
158
|
1 |
|
* |
|
159
|
|
|
* @param string $type Log type. |
|
160
|
1 |
|
* |
|
161
|
|
|
* @return bool Whether log type is valid. |
|
162
|
|
|
*/ |
|
163
|
|
|
public function valid_type( $type ) { |
|
164
|
|
|
return in_array( $type, $this->log_types() ); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Create new log entry |
|
169
|
|
|
* |
|
170
|
|
|
* This is just a simple and fast way to log something. Use $this->insert_log() |
|
171
|
|
|
* if you need to store custom meta data. |
|
172
|
|
|
* |
|
173
|
|
|
* @since 1.0 |
|
174
|
|
|
* @access public |
|
175
|
43 |
|
* |
|
176
|
|
|
* @param string $title Log entry title. Default is empty. |
|
177
|
43 |
|
* @param string $message Log entry message. Default is empty. |
|
178
|
43 |
|
* @param int $parent Log entry parent. Default is 0. |
|
179
|
43 |
|
* @param string $type Log type. Default is empty string. |
|
180
|
43 |
|
* |
|
181
|
|
|
* @return int Log ID. |
|
182
|
43 |
|
*/ |
|
183
|
|
|
public function add( $title = '', $message = '', $parent = 0, $type = '' ) { |
|
184
|
43 |
|
$log_data = array( |
|
185
|
|
|
'post_title' => $title, |
|
186
|
43 |
|
'post_content' => $message, |
|
187
|
|
|
'post_parent' => $parent, |
|
188
|
|
|
'log_type' => $type, |
|
189
|
43 |
|
); |
|
190
|
|
|
|
|
191
|
|
|
return $this->insert_log( $log_data ); |
|
192
|
43 |
|
} |
|
193
|
43 |
|
|
|
194
|
43 |
|
/** |
|
195
|
|
|
* Get Logs |
|
196
|
|
|
* |
|
197
|
43 |
|
* Retrieves log items for a particular object ID. |
|
198
|
42 |
|
* |
|
199
|
42 |
|
* @since 1.0 |
|
200
|
42 |
|
* @access public |
|
201
|
42 |
|
* |
|
202
|
|
|
* @param int $object_id Log object ID. Default is 0. |
|
203
|
43 |
|
* @param string $type Log type. Default is empty string. |
|
204
|
|
|
* @param int $paged Page number Default is null. |
|
205
|
43 |
|
* |
|
206
|
|
|
* @return array An array of the connected logs. |
|
207
|
|
|
*/ |
|
208
|
|
|
public function get_logs( $object_id = 0, $type = '', $paged = null ) { |
|
209
|
|
|
return $this->get_connected_logs( array( |
|
210
|
|
|
'log_parent' => $object_id, |
|
211
|
|
|
'paged' => $paged, |
|
212
|
|
|
'log_type' => $type, |
|
213
|
|
|
) ); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Stores a log entry |
|
218
|
|
|
* |
|
219
|
|
|
* @since 1.0 |
|
220
|
|
|
* @access public |
|
221
|
|
|
* |
|
222
|
|
|
* @param array $log_data Log entry data. |
|
223
|
|
|
* @param array $log_meta Log entry meta. |
|
224
|
|
|
* |
|
225
|
|
|
* @return int The ID of the newly created log item. |
|
226
|
|
|
*/ |
|
227
|
|
|
public function insert_log( $log_data = array(), $log_meta = array() ) { |
|
228
|
|
|
$log_id = 0; |
|
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
$defaults = array( |
|
231
|
|
|
'log_parent' => 0, |
|
232
|
|
|
'log_content' => '', |
|
233
|
|
|
'log_type' => false, |
|
234
|
|
|
|
|
235
|
|
|
// Backward compatibility. |
|
236
|
|
|
'post_type' => 'give_log', |
|
237
|
|
|
'post_status' => 'publish', |
|
238
|
|
|
); |
|
239
|
|
|
|
|
240
|
|
|
$args = wp_parse_args( $log_data, $defaults ); |
|
241
|
|
|
$this->bc_200_validate_params( $args, $log_meta ); |
|
242
|
|
|
|
|
243
|
|
|
if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
|
244
|
|
|
global $wpdb; |
|
245
|
|
|
|
|
246
|
|
|
// Backward Compatibility. |
|
247
|
|
|
if ( ! $wpdb->get_var( "SELECT ID from {$this->log_db->table_name} ORDER BY id DESC LIMIT 1" ) ) { |
|
|
|
|
|
|
248
|
|
|
$latest_log_id = $wpdb->get_var( "SELECT ID from $wpdb->posts ORDER BY id DESC LIMIT 1" ); |
|
|
|
|
|
|
249
|
|
|
$latest_log_id = empty( $latest_log_id ) ? 1 : ++ $latest_log_id; |
|
250
|
|
|
|
|
251
|
|
|
$args['ID'] = $latest_log_id; |
|
252
|
|
|
$this->log_db->insert( $args ); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
$log_id = $this->log_db->add( $args ); |
|
257
|
1 |
|
|
|
258
|
|
|
// Set log meta, if any |
|
259
|
|
View Code Duplication |
if ( $log_id && ! empty( $log_meta ) ) { |
|
|
|
|
|
|
260
|
1 |
|
foreach ( (array) $log_meta as $key => $meta ) { |
|
261
|
1 |
|
$this->logmeta_db->update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta ); |
|
|
|
|
|
|
262
|
1 |
|
} |
|
263
|
1 |
|
} |
|
264
|
|
|
|
|
|
|
|
|
|
265
|
1 |
|
|
|
266
|
|
|
// Delete cache. |
|
267
|
1 |
|
$this->delete_cache(); |
|
268
|
|
|
|
|
269
|
1 |
|
return $log_id; |
|
270
|
1 |
|
} |
|
271
|
|
|
|
|
272
|
1 |
|
/** |
|
273
|
1 |
|
* Update and existing log item |
|
274
|
1 |
|
* |
|
275
|
1 |
|
* @since 1.0 |
|
276
|
1 |
|
* @access public |
|
277
|
1 |
|
* |
|
278
|
|
|
* @param array $log_data Log entry data. |
|
279
|
1 |
|
* @param array $log_meta Log entry meta. |
|
280
|
|
|
* |
|
281
|
1 |
|
* @return bool|null True if successful, false otherwise. |
|
282
|
1 |
|
*/ |
|
283
|
|
|
public function update_log( $log_data = array(), $log_meta = array() ) { |
|
284
|
|
|
$log_id = 0; |
|
|
|
|
|
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Fires before updating log entry. |
|
288
|
|
|
* |
|
289
|
|
|
* @since 1.0 |
|
290
|
|
|
* |
|
291
|
|
|
* @param array $log_data Log entry data. |
|
292
|
|
|
* @param array $log_meta Log entry meta. |
|
293
|
|
|
*/ |
|
294
|
|
|
do_action( 'give_pre_update_log', $log_data, $log_meta ); |
|
295
|
|
|
|
|
296
|
|
|
$defaults = array( |
|
297
|
|
|
'log_parent' => 0, |
|
298
|
|
|
|
|
299
|
|
|
// Backward compatibility. |
|
300
|
|
|
'post_type' => 'give_log', |
|
301
|
|
|
'post_status' => 'publish', |
|
302
|
1 |
|
); |
|
303
|
|
|
|
|
304
|
|
|
$args = wp_parse_args( $log_data, $defaults ); |
|
305
|
1 |
|
$this->bc_200_validate_params( $args, $log_meta ); |
|
306
|
1 |
|
|
|
307
|
1 |
|
// Store the log entry |
|
308
|
1 |
|
if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
|
309
|
1 |
|
// Backward compatibility. |
|
310
|
1 |
|
$log_id = wp_update_post( $args ); |
|
311
|
|
|
|
|
312
|
1 |
|
if ( $log_id && ! empty( $log_meta ) ) { |
|
313
|
1 |
|
foreach ( (array) $log_meta as $key => $meta ) { |
|
314
|
|
|
if ( ! empty( $meta ) ) { |
|
315
|
1 |
|
give_update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta ); |
|
316
|
1 |
|
} |
|
317
|
|
|
} |
|
318
|
1 |
|
} |
|
319
|
1 |
|
} else { |
|
320
|
1 |
|
$log_id = $this->log_db->add( $args ); |
|
321
|
|
|
|
|
322
|
1 |
View Code Duplication |
if ( $log_id && ! empty( $log_meta ) ) { |
|
|
|
|
|
|
323
|
|
|
foreach ( (array) $log_meta as $key => $meta ) { |
|
324
|
|
|
if ( ! empty( $meta ) ) { |
|
325
|
|
|
$this->logmeta_db->update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta ); |
|
|
|
|
|
|
326
|
1 |
|
} |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
1 |
|
|
|
331
|
|
|
/** |
|
332
|
1 |
|
* Fires after updating log entry. |
|
333
|
|
|
* |
|
334
|
|
|
* @since 1.0 |
|
335
|
|
|
* |
|
336
|
|
|
* @param int $log_id Log entry id. |
|
337
|
|
|
* @param array $log_data Log entry data. |
|
338
|
|
|
* @param array $log_meta Log entry meta. |
|
339
|
|
|
*/ |
|
340
|
|
|
do_action( 'give_post_update_log', $log_id, $log_data, $log_meta ); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Retrieve all connected logs |
|
345
|
|
|
* |
|
346
|
|
|
* Used for retrieving logs related to particular items, such as a specific donation. |
|
347
|
|
|
* For new table params check: Give_DB_Logs::get_column_defaults and Give_DB_Logs::get_sql#L262 |
|
348
|
32 |
|
* |
|
349
|
|
|
* @since 1.0 |
|
350
|
32 |
|
* @since 2.0 Added new table logic. |
|
351
|
32 |
|
* @access public |
|
352
|
32 |
|
* |
|
353
|
32 |
|
* @param array $args Query arguments. |
|
354
|
|
|
* |
|
355
|
32 |
|
* @return array|false Array if logs were found, false otherwise. |
|
356
|
|
|
*/ |
|
357
|
32 |
|
public function get_connected_logs( $args = array() ) { |
|
358
|
31 |
|
$logs = array(); |
|
|
|
|
|
|
359
|
|
|
|
|
360
|
31 |
|
$defaults = array( |
|
361
|
31 |
|
'number' => 20, |
|
362
|
31 |
|
'paged' => get_query_var( 'paged' ), |
|
363
|
|
|
'log_type' => false, |
|
364
|
31 |
|
|
|
365
|
31 |
|
// Backward compatibility. |
|
366
|
|
|
'post_type' => 'give_log', |
|
367
|
32 |
|
'post_status' => 'publish', |
|
368
|
31 |
|
); |
|
369
|
31 |
|
$query_args = wp_parse_args( $args, $defaults ); |
|
370
|
|
|
$this->bc_200_validate_params( $query_args ); |
|
371
|
32 |
|
|
|
372
|
|
|
if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
|
373
|
32 |
|
// Backward compatibility. |
|
374
|
7 |
|
$logs = get_posts( $query_args ); |
|
375
|
7 |
|
$this->bc_200_add_new_properties( $logs ); |
|
376
|
7 |
|
} else { |
|
377
|
7 |
|
$logs = $this->log_db->get_logs( $query_args ); |
|
378
|
32 |
|
} |
|
379
|
|
|
|
|
380
|
|
|
return ( ! empty( $logs ) ? $logs : false ); |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* Retrieve Log Count |
|
385
|
|
|
* |
|
386
|
|
|
* Retrieves number of log entries connected to particular object ID. |
|
387
|
|
|
* |
|
388
|
|
|
* @since 1.0 |
|
389
|
|
|
* @access public |
|
390
|
|
|
* |
|
391
|
|
|
* @param int $object_id Log object ID. Default is 0. |
|
392
|
|
|
* @param string $type Log type. Default is empty string. |
|
393
|
|
|
* @param array $meta_query Log meta query. Default is null. |
|
394
|
|
|
* @param array $date_query Log data query. Default is null. |
|
395
|
|
|
* |
|
396
|
|
|
* @return int Log count. |
|
397
|
|
|
*/ |
|
398
|
|
|
public function get_log_count( $object_id = 0, $type = '', $meta_query = null, $date_query = null ) { |
|
399
|
|
|
$logs_count = 0; |
|
|
|
|
|
|
400
|
|
|
|
|
401
|
|
|
$query_args = array( |
|
402
|
|
|
'number' => - 1, |
|
403
|
|
|
|
|
404
|
|
|
// Backward comatibility. |
|
405
|
|
|
'post_type' => 'give_log', |
|
406
|
|
|
'post_status' => 'publish', |
|
407
|
|
|
); |
|
408
|
|
|
|
|
409
|
|
|
if ( $object_id ) { |
|
410
|
|
|
$query_args['log_parent'] = $object_id; |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
if ( ! empty( $type ) && $this->valid_type( $type ) ) { |
|
414
|
|
|
$query_args['log_type'] = $type; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
if ( ! empty( $meta_query ) ) { |
|
418
|
|
|
$query_args['meta_query'] = $meta_query; |
|
|
|
|
|
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
if ( ! empty( $date_query ) ) { |
|
422
|
|
|
$query_args['date_query'] = $date_query; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
$this->bc_200_validate_params( $query_args ); |
|
426
|
|
|
|
|
427
|
|
|
if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
|
428
|
|
|
// Backward compatibility. |
|
429
|
|
|
$logs = new WP_Query( $query_args ); |
|
430
|
|
|
$logs_count = (int) $logs->post_count; |
|
431
|
|
|
} else { |
|
432
|
|
|
$logs_count = $this->log_db->count( $query_args ); |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
return $logs_count; |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
/** |
|
439
|
|
|
* Delete Logs |
|
440
|
|
|
* |
|
441
|
|
|
* Remove log entries connected to particular object ID. |
|
442
|
|
|
* |
|
443
|
|
|
* @since 1.0 |
|
444
|
|
|
* @access public |
|
445
|
|
|
* |
|
446
|
|
|
* @param int $object_id Log object ID. Default is 0. |
|
447
|
|
|
* @param string $type Log type. Default is empty string. |
|
448
|
|
|
* @param array $meta_query Log meta query. Default is null. |
|
449
|
|
|
* |
|
450
|
|
|
* @return void |
|
451
|
|
|
*/ |
|
452
|
|
|
public function delete_logs( $object_id = 0, $type = '', $meta_query = null ) { |
|
453
|
|
|
$query_args = array( |
|
454
|
|
|
'log_parent' => $object_id, |
|
455
|
|
|
'number' => - 1, |
|
456
|
|
|
'fields' => 'ID', |
|
457
|
|
|
|
|
458
|
|
|
// Backward compatibility. |
|
459
|
|
|
'post_type' => 'give_log', |
|
460
|
|
|
'post_status' => 'publish', |
|
461
|
|
|
); |
|
462
|
|
|
|
|
463
|
|
|
if ( ! empty( $type ) && $this->valid_type( $type ) ) { |
|
464
|
|
|
$query_args['log_type'] = $type; |
|
465
|
|
|
} |
|
466
|
|
|
|
|
467
|
|
|
if ( ! empty( $meta_query ) ) { |
|
468
|
|
|
$query_args['meta_query'] = $meta_query; |
|
|
|
|
|
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
$this->bc_200_validate_params( $query_args ); |
|
472
|
|
|
|
|
473
|
|
|
if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
|
474
|
|
|
// Backward compatibility. |
|
475
|
|
|
$logs = get_posts( $query_args ); |
|
476
|
|
|
|
|
477
|
|
|
if ( $logs ) { |
|
478
|
|
|
foreach ( $logs as $log ) { |
|
479
|
|
|
wp_delete_post( $log, true ); |
|
480
|
|
|
} |
|
481
|
|
|
} |
|
482
|
|
|
} else { |
|
483
|
|
|
$logs = $this->log_db->get_logs( $query_args ); |
|
484
|
|
|
|
|
485
|
|
|
if ( $logs ) { |
|
486
|
|
|
foreach ( $logs as $log ) { |
|
487
|
|
|
if ( $this->log_db->delete( $log->ID ) ) { |
|
488
|
|
|
$this->logmeta_db->delete_row( $log->ID ); |
|
489
|
|
|
} |
|
490
|
|
|
} |
|
491
|
|
|
} |
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
|
|
$this->delete_cache(); |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
/** |
|
498
|
|
|
* Setup cron to delete log cache in background. |
|
499
|
|
|
* |
|
500
|
|
|
* @since 1.7 |
|
501
|
|
|
* @access public |
|
502
|
|
|
* |
|
503
|
|
|
* @param int $post_id |
|
504
|
|
|
*/ |
|
505
|
|
|
public function background_process_delete_cache( $post_id ) { |
|
|
|
|
|
|
506
|
|
|
// Delete log cache immediately |
|
507
|
|
|
wp_schedule_single_event( time() - 5, 'give_delete_log_cache' ); |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
/** |
|
511
|
|
|
* Delete all logging cache when form, log or payment updates |
|
512
|
|
|
* |
|
513
|
|
|
* @since 1.7 |
|
514
|
|
|
* @access public |
|
515
|
|
|
* |
|
516
|
|
|
* @return bool |
|
517
|
|
|
*/ |
|
518
|
|
|
public function delete_cache() { |
|
519
|
|
|
// Add log related keys to delete. |
|
520
|
|
|
$cache_give_logs = Give_Cache::get_options_like( 'give_logs' ); |
|
521
|
|
|
$cache_give_log_count = Give_Cache::get_options_like( 'log_count' ); |
|
522
|
|
|
|
|
523
|
|
|
$cache_option_names = array_merge( $cache_give_logs, $cache_give_log_count ); |
|
524
|
|
|
|
|
525
|
|
|
// Bailout. |
|
526
|
|
|
if ( empty( $cache_option_names ) ) { |
|
527
|
|
|
return false; |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
Give_Cache::delete( $cache_option_names ); |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
|
|
/** |
|
534
|
|
|
* Validate query params. |
|
535
|
|
|
* |
|
536
|
|
|
* @since 2.0 |
|
537
|
|
|
* @access private |
|
538
|
|
|
* |
|
539
|
|
|
* @param array $log_query |
|
540
|
|
|
* @param array $log_meta |
|
541
|
|
|
*/ |
|
542
|
|
|
private function bc_200_validate_params( &$log_query, &$log_meta = array() ) { |
|
|
|
|
|
|
543
|
|
|
$query_params = array( |
|
544
|
|
|
'log_title' => 'post_title', |
|
545
|
|
|
'log_parent' => 'post_parent', |
|
546
|
|
|
'log_content' => 'post_content', |
|
547
|
|
|
'log_type' => 'tax_query', |
|
548
|
|
|
'log_date' => 'post_date', |
|
549
|
|
|
'log_date_gmt' => 'post_date_gmt', |
|
550
|
|
|
'number' => 'posts_per_page', |
|
551
|
|
|
'meta_query' => 'meta_query', |
|
|
|
|
|
|
552
|
|
|
); |
|
553
|
|
|
|
|
554
|
|
|
if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
|
555
|
|
|
// Set old params. |
|
556
|
|
|
foreach ( $query_params as $new_query_param => $old_query_param ) { |
|
557
|
|
|
|
|
558
|
|
View Code Duplication |
if ( isset( $log_query[ $old_query_param ] ) && empty( $log_query[ $new_query_param ] ) ) { |
|
|
|
|
|
|
559
|
|
|
$log_query[ $new_query_param ] = $log_query[ $old_query_param ]; |
|
560
|
|
|
continue; |
|
561
|
|
|
} elseif ( ! isset( $log_query[ $new_query_param ] ) ) { |
|
562
|
|
|
continue; |
|
563
|
|
|
} elseif( empty( $log_query[ $new_query_param ] ) ) { |
|
|
|
|
|
|
564
|
|
|
continue; |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
switch ( $new_query_param ) { |
|
568
|
|
|
case 'log_type': |
|
569
|
|
|
$log_query['tax_query'] = array( |
|
|
|
|
|
|
570
|
|
|
array( |
|
571
|
|
|
'taxonomy' => 'give_log_type', |
|
572
|
|
|
'field' => 'slug', |
|
573
|
|
|
'terms' => $log_query[ $new_query_param ], |
|
574
|
|
|
), |
|
575
|
|
|
); |
|
576
|
|
|
break; |
|
577
|
|
|
|
|
578
|
|
|
case 'meta_query': |
|
579
|
|
|
if( ! empty( $log_query['meta_query'] ) && empty( $log_query['post_parent'] ) ) { |
|
|
|
|
|
|
580
|
|
|
foreach ( $log_query['meta_query'] as $index => $meta_query ){ |
|
581
|
|
|
if( ! is_array( $meta_query ) || empty( $meta_query['key'] ) ) { |
|
|
|
|
|
|
582
|
|
|
continue; |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
switch ( $meta_query['key'] ) { |
|
586
|
|
|
case '_give_log_form_id': |
|
587
|
|
|
$log_query['post_parent'] = $meta_query['value']; |
|
588
|
|
|
unset( $log_query['meta_query'][$index] ); |
|
|
|
|
|
|
589
|
|
|
break; |
|
590
|
|
|
} |
|
591
|
|
|
} |
|
592
|
|
|
} |
|
593
|
|
|
break; |
|
594
|
|
|
|
|
595
|
|
|
default: |
|
596
|
|
|
switch( $new_query_param ){ |
|
|
|
|
|
|
597
|
|
|
case 'log_parent': |
|
598
|
|
|
$log_query['meta_query'][] = array( |
|
599
|
|
|
'key' => '_give_log_payment_id', |
|
600
|
|
|
'value' => $log_query[ $new_query_param ] |
|
601
|
|
|
); |
|
602
|
|
|
|
|
603
|
|
|
break; |
|
604
|
|
|
|
|
605
|
|
|
default: |
|
606
|
|
|
$log_query[ $old_query_param ] = $log_query[ $new_query_param ]; |
|
607
|
|
|
} |
|
608
|
|
|
} |
|
609
|
|
|
} |
|
610
|
|
|
} else { |
|
611
|
|
|
// Set only old params. |
|
612
|
|
|
$query_params = array_flip( $query_params ); |
|
613
|
|
|
foreach ( $query_params as $old_query_param => $new_query_param ) { |
|
614
|
|
View Code Duplication |
if ( isset( $log_query[ $new_query_param ] ) && empty( $log_query[ $old_query_param ] ) ) { |
|
|
|
|
|
|
615
|
|
|
$log_query[ $old_query_param ] = $log_query[ $new_query_param ]; |
|
616
|
|
|
continue; |
|
617
|
|
|
} elseif ( ! isset( $log_query[ $old_query_param ] ) ) { |
|
618
|
|
|
continue; |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
switch ( $old_query_param ) { |
|
622
|
|
|
case 'tax_query': |
|
623
|
|
|
if ( isset( $log_query[ $old_query_param ][0]['terms'] ) ) { |
|
624
|
|
|
$log_query[ $new_query_param ] = $log_query[ $old_query_param ][0]['terms']; |
|
625
|
|
|
} |
|
626
|
|
|
break; |
|
627
|
|
|
|
|
628
|
|
|
default: |
|
629
|
|
|
$log_query[ $new_query_param ] = $log_query[ $old_query_param ]; |
|
630
|
|
|
} |
|
631
|
|
|
} |
|
632
|
|
|
} |
|
633
|
|
|
} |
|
634
|
|
|
|
|
635
|
|
|
/** |
|
636
|
|
|
* Set new log properties. |
|
637
|
|
|
* |
|
638
|
|
|
* @since 2.0 |
|
639
|
|
|
* @access private |
|
640
|
|
|
* |
|
641
|
|
|
* @param array $logs |
|
642
|
|
|
*/ |
|
643
|
|
|
private function bc_200_add_new_properties( &$logs ) { |
|
644
|
|
|
if ( empty( $logs ) ) { |
|
645
|
|
|
return; |
|
646
|
|
|
} |
|
647
|
|
|
|
|
648
|
|
|
$query_params = array( |
|
649
|
|
|
'log_title' => 'post_title', |
|
650
|
|
|
'log_parent' => 'post_parent', |
|
651
|
|
|
'log_content' => 'post_content', |
|
652
|
|
|
'log_date' => 'post_date', |
|
653
|
|
|
'log_date_gmt' => 'post_date_gmt', |
|
654
|
|
|
'log_type' => 'give_log_type', |
|
655
|
|
|
); |
|
656
|
|
|
|
|
657
|
|
|
if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) { |
|
658
|
|
|
foreach ( $logs as $index => $log ) { |
|
659
|
|
|
foreach ( $query_params as $new_query_param => $old_query_param ) { |
|
660
|
|
|
if ( ! property_exists( $log, $old_query_param ) ) { |
|
661
|
|
|
/** |
|
662
|
|
|
* Set unmatched properties. |
|
663
|
|
|
*/ |
|
664
|
|
|
|
|
665
|
|
|
// 1. log_type |
|
666
|
|
|
$term = get_the_terms( $log->ID, 'give_log_type' ); |
|
667
|
|
|
$term = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array(); |
|
668
|
|
|
|
|
669
|
|
|
$logs[ $index ]->{$new_query_param} = ! empty( $term ) ? $term->slug : ''; |
|
670
|
|
|
|
|
671
|
|
|
continue; |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
switch ( $old_query_param ) { |
|
675
|
|
|
case 'post_parent': |
|
676
|
|
|
$logs[ $index ]->{$new_query_param} = give_get_meta( $log->ID, '_give_log_payment_id', true ); |
|
677
|
|
|
break; |
|
678
|
|
|
|
|
679
|
|
|
default: |
|
680
|
|
|
$logs[ $index ]->{$new_query_param} = $log->{$old_query_param}; |
|
681
|
|
|
} |
|
682
|
|
|
} |
|
683
|
|
|
} |
|
684
|
|
|
} |
|
685
|
|
|
} |
|
686
|
|
|
|
|
687
|
|
|
/** |
|
688
|
|
|
* Change log parent to payment if set to form. |
|
689
|
|
|
* |
|
690
|
|
|
* @since 2.0 |
|
691
|
|
|
* @access public |
|
692
|
|
|
* |
|
693
|
|
|
* @param mixed $check |
|
694
|
|
|
* @param int $log_id |
|
695
|
|
|
* @param array $meta_key |
|
696
|
|
|
* @param array $meta_value |
|
697
|
|
|
* |
|
698
|
|
|
* @return mixed |
|
699
|
|
|
*/ |
|
700
|
|
|
public function bc_200_set_payment_as_log_parent( $check, $log_id, $meta_key, $meta_value ) { |
|
701
|
|
|
global $wpdb; |
|
702
|
|
|
$update_status = false; |
|
703
|
|
|
$post_type = get_post_type( $log_id ); |
|
704
|
|
|
|
|
705
|
|
|
// Bailout. |
|
706
|
|
|
if ( |
|
707
|
|
|
'give_payment' === $post_type || |
|
708
|
|
|
'_give_log_payment_id' !== $meta_key |
|
709
|
|
|
) { |
|
710
|
|
|
return $check; |
|
711
|
|
|
} |
|
712
|
|
|
|
|
713
|
|
|
$form_id = $wpdb->get_var( |
|
|
|
|
|
|
714
|
|
|
$wpdb->prepare( |
|
715
|
|
|
" |
|
716
|
|
|
SELECT log_parent FROM {$this->log_db->table_name} |
|
717
|
|
|
WHERE ID=%d |
|
718
|
|
|
", |
|
719
|
|
|
$log_id |
|
720
|
|
|
) |
|
721
|
|
|
); |
|
722
|
|
|
|
|
723
|
|
|
if ( $form_id ) { |
|
724
|
|
|
$this->logmeta_db->delete_meta( $log_id, '_give_log_payment_id' ); |
|
725
|
|
|
$this->logmeta_db->update_meta( $log_id, '_give_log_form_id', $form_id ); |
|
726
|
|
|
|
|
727
|
|
|
$update_status = $wpdb->update( |
|
|
|
|
|
|
728
|
|
|
$this->log_db->table_name, |
|
729
|
|
|
array( |
|
730
|
|
|
'log_parent' => $meta_value, |
|
731
|
|
|
), |
|
732
|
|
|
array( |
|
733
|
|
|
'ID' => $log_id, |
|
734
|
|
|
), |
|
735
|
|
|
array( |
|
736
|
|
|
'%s', |
|
737
|
|
|
), |
|
738
|
|
|
array( |
|
739
|
|
|
'%d', |
|
740
|
|
|
) |
|
741
|
|
|
); |
|
742
|
|
|
} |
|
743
|
|
|
|
|
744
|
|
|
return $update_status; |
|
745
|
|
|
} |
|
746
|
|
|
} |
|
747
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.