Completed
Push — issues/1796 ( b53179...52862a )
by Ravinder
41:58 queued 22:00
created

Give_Logging   C

Complexity

Total Complexity 78

Size/Duplication

Total Lines 707
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 707
rs 5
c 0
b 0
f 0
wmc 78
lcom 1
cbo 3

17 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 29 2
A register_post_type() 0 19 1
A register_taxonomy() 0 5 1
A log_types() 0 9 1
A valid_type() 0 3 1
A add() 0 10 1
A get_logs() 0 7 1
C insert_log() 0 44 7
C update_log() 0 59 10
B get_connected_logs() 0 25 3
C get_log_count() 0 46 7
C delete_logs() 0 50 10
A background_process_delete_cache() 0 4 1
A delete_cache() 0 14 2
C bc_200_validate_params() 0 66 16
D bc_200_add_new_properties() 0 43 10
B bc_20_set_payment_as_log_parent() 0 46 4

How to fix   Complexity   

Complex Class

Complex classes like Give_Logging often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Give_Logging, and based on these observations, apply Extract Interface, too.

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 14.

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.

Loading history...
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
	 */
32
	public $log_db;
33
34
	/**
35
	 * Log meta data operation handler object.
36
	 *
37
	 * @since  2.0
38
	 * @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
	 *
48
	 * @since  1.0
49
	 * @access public
50
	 */
51
	public function __construct() {
52
		/**
53
		 * Setup properties
54
		 */
55
56
		require_once GIVE_PLUGIN_DIR . 'includes/class-give-db-logs.php';
57
		require_once GIVE_PLUGIN_DIR . 'includes/class-give-db-logs-meta.php';
58
		$this->log_db     = new Give_DB_Logs();
59
		$this->logmeta_db = new Give_DB_Log_Meta();
60
61
		/**
62
		 * Setup hooks.
63
		 */
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_20_set_payment_as_log_parent' ), 10, 4 );
70
71
		// Backward compatibility.
72
		if ( ! give_has_upgrade_completed( 'give_v20_logs_upgrades' ) ) {
73
			// Create the log post type
74
			add_action( 'init', array( $this, 'register_post_type' ), 1 );
75
76
			// Create types taxonomy and default types
77
			add_action( 'init', array( $this, 'register_taxonomy' ), 1 );
78
		}
79
	}
80
81
82
	/**
83
	 * Log Post Type
84
	 *
85
	 * Registers the 'give_log' Post Type.
86
	 *
87
	 * @since  1.0
88
	 * @access public
89
	 *
90
	 * @return void
91
	 */
92
	public function register_post_type() {
93
		/* Logs post type */
94
		$log_args = array(
95
			'labels'              => array(
96
				'name' => esc_html__( 'Logs', 'give' ),
97
			),
98
			'public'              => false,
99
			'exclude_from_search' => true,
100
			'publicly_queryable'  => false,
101
			'show_ui'             => false,
102
			'query_var'           => false,
103
			'rewrite'             => false,
104
			'capability_type'     => 'post',
105
			'supports'            => array( 'title', 'editor' ),
106
			'can_export'          => true,
107
		);
108
109
		register_post_type( 'give_log', $log_args );
110
	}
111
112
	/**
113
	 * Log Type Taxonomy
114
	 *
115
	 * Registers the "Log Type" taxonomy.  Used to determine the type of log entry.
116
	 *
117
	 * @since  1.0
118
	 * @access public
119
	 *
120
	 * @return void
121
	 */
122
	public function register_taxonomy() {
123
		register_taxonomy( 'give_log_type', 'give_log', array(
124
			'public' => false,
125
		) );
126
	}
127
128
	/**
129
	 * Log Types
130
	 *
131
	 * Sets up the default log types and allows for new ones to be created.
132
	 *
133
	 * @since  1.0
134
	 * @access public
135
	 *
136
	 * @return array $terms
137
	 */
138
	public function log_types() {
139
		$terms = array(
140
			'sale',
141
			'gateway_error',
142
			'api_request',
143
		);
144
145
		return apply_filters( 'give_log_types', $terms );
146
	}
147
148
	/**
149
	 * Check if a log type is valid
150
	 *
151
	 * Checks to see if the specified type is in the registered list of types.
152
	 *
153
	 * @since  1.0
154
	 * @access public
155
	 *
156
	 * @param  string $type Log type.
157
	 *
158
	 * @return bool         Whether log type is valid.
159
	 */
160
	public function valid_type( $type ) {
161
		return in_array( $type, $this->log_types() );
162
	}
163
164
	/**
165
	 * Create new log entry
166
	 *
167
	 * This is just a simple and fast way to log something. Use $this->insert_log()
168
	 * if you need to store custom meta data.
169
	 *
170
	 * @since  1.0
171
	 * @access public
172
	 *
173
	 * @param  string $title   Log entry title. Default is empty.
174
	 * @param  string $message Log entry message. Default is empty.
175
	 * @param  int    $parent  Log entry parent. Default is 0.
176
	 * @param  string $type    Log type. Default is empty string.
177
	 *
178
	 * @return int             Log ID.
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
179
	 */
180
	public function add( $title = '', $message = '', $parent = 0, $type = '' ) {
181
		$log_data = array(
182
			'post_title'   => $title,
183
			'post_content' => $message,
184
			'post_parent'  => $parent,
185
			'log_type'     => $type,
186
		);
187
188
		return $this->insert_log( $log_data );
189
	}
190
191
	/**
192
	 * Get Logs
193
	 *
194
	 * Retrieves log items for a particular object ID.
195
	 *
196
	 * @since  1.0
197
	 * @access public
198
	 *
199
	 * @param  int    $object_id Log object ID. Default is 0.
200
	 * @param  string $type      Log type. Default is empty string.
201
	 * @param  int    $paged     Page number Default is null.
202
	 *
203
	 * @return array             An array of the connected logs.
0 ignored issues
show
Documentation introduced by
Should the return type not be array|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
204
	 */
205
	public function get_logs( $object_id = 0, $type = '', $paged = null ) {
206
		return $this->get_connected_logs( array(
207
			'log_parent' => $object_id,
208
			'paged'      => $paged,
209
			'log_type'   => $type,
210
		) );
211
	}
212
213
	/**
214
	 * Stores a log entry
215
	 *
216
	 * @since  1.0
217
	 * @access public
218
	 *
219
	 * @param  array $log_data Log entry data.
220
	 * @param  array $log_meta Log entry meta.
221
	 *
222
	 * @return int             The ID of the newly created log item.
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
223
	 */
224
	public function insert_log( $log_data = array(), $log_meta = array() ) {
225
		$log_id = 0;
226
227
		$defaults = array(
228
			'log_parent'  => 0,
229
			'log_content' => '',
230
			'log_type'    => false,
231
232
			// Backward compatibility.
233
			'post_type'   => 'give_log',
234
			'post_status' => 'publish',
235
		);
236
237
		$args = wp_parse_args( $log_data, $defaults );
238
		$this->bc_200_validate_params( $args, $log_meta );
239
240
		if ( ! give_has_upgrade_completed( 'give_v20_logs_upgrades' ) ) {
241
			global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
242
243
			// Backward Compatibility.
244
			if ( ! $wpdb->get_var( "SELECT ID from {$this->log_db->table_name} ORDER BY id DESC LIMIT 1" ) ) {
245
				$latest_log_id = $wpdb->get_var( "SELECT ID from $wpdb->posts ORDER BY id DESC LIMIT 1" );
246
				$latest_log_id = empty( $latest_log_id ) ? 1 : ++ $latest_log_id;
247
248
				$args['ID'] = $latest_log_id;
249
				$this->log_db->insert( $args );
250
			}
251
		}
252
253
		$log_id = $this->log_db->add( $args );
254
255
		// Set log meta, if any
256
		if ( $log_id && ! empty( $log_meta ) ) {
257
			foreach ( (array) $log_meta as $key => $meta ) {
258
				$this->logmeta_db->update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta );
259
			}
260
		}
261
262
263
		// Delete cache.
264
		$this->delete_cache();
265
266
		return $log_id;
267
	}
268
269
	/**
270
	 * Update and existing log item
271
	 *
272
	 * @since  1.0
273
	 * @access public
274
	 *
275
	 * @param  array $log_data Log entry data.
276
	 * @param  array $log_meta Log entry meta.
277
	 *
278
	 * @return bool|null       True if successful, false otherwise.
279
	 */
280
	public function update_log( $log_data = array(), $log_meta = array() ) {
281
		$log_id = 0;
282
283
		/**
284
		 * Fires before updating log entry.
285
		 *
286
		 * @since 1.0
287
		 *
288
		 * @param array $log_data Log entry data.
289
		 * @param array $log_meta Log entry meta.
290
		 */
291
		do_action( 'give_pre_update_log', $log_data, $log_meta );
292
293
		$defaults = array(
294
			'log_parent'  => 0,
295
296
			// Backward compatibility.
297
			'post_type'   => 'give_log',
298
			'post_status' => 'publish',
299
		);
300
301
		$args = wp_parse_args( $log_data, $defaults );
302
		$this->bc_200_validate_params( $args, $log_meta );
303
304
		// Store the log entry
305
		if ( ! give_has_upgrade_completed( 'give_v20_logs_upgrades' ) ) {
306
			// Backward compatibility.
307
			$log_id = wp_update_post( $args );
308
309
			if ( $log_id && ! empty( $log_meta ) ) {
310
				foreach ( (array) $log_meta as $key => $meta ) {
311
					if ( ! empty( $meta ) ) {
312
						give_update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta );
313
					}
314
				}
315
			}
316
		} else {
317
			$log_id = $this->log_db->add( $args );
318
319
			if ( $log_id && ! empty( $log_meta ) ) {
320
				foreach ( (array) $log_meta as $key => $meta ) {
321
					if ( ! empty( $meta ) ) {
322
						$this->logmeta_db->update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta );
323
					}
324
				}
325
			}
326
		}
327
328
		/**
329
		 * Fires after updating log entry.
330
		 *
331
		 * @since 1.0
332
		 *
333
		 * @param int   $log_id   Log entry id.
334
		 * @param array $log_data Log entry data.
335
		 * @param array $log_meta Log entry meta.
336
		 */
337
		do_action( 'give_post_update_log', $log_id, $log_data, $log_meta );
338
	}
339
340
	/**
341
	 * Retrieve all connected logs
342
	 *
343
	 * Used for retrieving logs related to particular items, such as a specific donation.
344
	 * For new table params check: Give_DB_Logs::get_column_defaults and Give_DB_Logs::get_sql#L262
345
	 *
346
	 * @since  1.0
347
	 * @since  2.0 Added new table logic.
348
	 * @access public
349
	 *
350
	 * @param  array $args Query arguments.
351
	 *
352
	 * @return array|false Array if logs were found, false otherwise.
353
	 */
354
	public function get_connected_logs( $args = array() ) {
355
		$logs = array();
356
357
		$defaults   = array(
358
			'number'      => 20,
359
			'paged'       => get_query_var( 'paged' ),
360
			'log_type'    => false,
361
362
			// Backward compatibility.
363
			'post_type'   => 'give_log',
364
			'post_status' => 'publish',
365
		);
366
		$query_args = wp_parse_args( $args, $defaults );
367
		$this->bc_200_validate_params( $query_args );
368
369
		if ( ! give_has_upgrade_completed( 'give_v20_logs_upgrades' ) ) {
370
			// Backward compatibility.
371
			$logs = get_posts( $query_args );
372
			$this->bc_200_add_new_properties( $logs );
373
		} else {
374
			$logs = $this->log_db->get_logs( $query_args );
375
		}
376
377
		return ( ! empty( $logs ) ? $logs : false );
378
	}
379
380
	/**
381
	 * Retrieve Log Count
382
	 *
383
	 * Retrieves number of log entries connected to particular object ID.
384
	 *
385
	 * @since  1.0
386
	 * @access public
387
	 *
388
	 * @param  int    $object_id  Log object ID. Default is 0.
389
	 * @param  string $type       Log type. Default is empty string.
390
	 * @param  array  $meta_query Log meta query. Default is null.
391
	 * @param  array  $date_query Log data query. Default is null.
392
	 *
393
	 * @return int                Log count.
394
	 */
395
	public function get_log_count( $object_id = 0, $type = '', $meta_query = null, $date_query = null ) {
396
		$logs_count = 0;
397
398
		$query_args = array(
399
			'number'      => - 1,
400
			'fields'      => 'ID',
401
402
			// Backward comatibility.
403
			'post_type'   => 'give_log',
404
			'post_status' => 'publish',
405
		);
406
407
		if ( $object_id ) {
408
			$query_args['log_parent'] = $object_id;
409
		}
410
411
		if ( ! empty( $type ) && $this->valid_type( $type ) ) {
412
			$query_args['tax_query'] = array(
413
				array(
414
					'taxonomy' => 'give_log_type',
415
					'field'    => 'slug',
416
					'terms'    => $type,
417
				),
418
			);
419
		}
420
421
		if ( ! empty( $meta_query ) ) {
422
			$query_args['meta_query'] = $meta_query;
423
		}
424
425
		if ( ! empty( $date_query ) ) {
426
			$query_args['date_query'] = $date_query;
427
		}
428
429
		$this->bc_200_validate_params( $query_args );
430
431
		if ( ! give_has_upgrade_completed( 'give_v20_logs_upgrades' ) ) {
432
			// Backward compatibility.
433
			$logs       = new WP_Query( $query_args );
434
			$logs_count = (int) $logs->post_count;
435
		} else {
436
			$logs_count = $this->log_db->count( $query_args );
437
		}
438
439
		return $logs_count;
440
	}
441
442
	/**
443
	 * Delete Logs
444
	 *
445
	 * Remove log entries connected to particular object ID.
446
	 *
447
	 * @since  1.0
448
	 * @access public
449
	 *
450
	 * @param  int    $object_id  Log object ID. Default is 0.
451
	 * @param  string $type       Log type. Default is empty string.
452
	 * @param  array  $meta_query Log meta query. Default is null.
453
	 *
454
	 * @return void
455
	 */
456
	public function delete_logs( $object_id = 0, $type = '', $meta_query = null ) {
457
		$query_args = array(
458
			'log_parent'  => $object_id,
459
			'number'      => - 1,
460
			'fields'      => 'ID',
461
462
			// Backward compatibility.
463
			'post_type'   => 'give_log',
464
			'post_status' => 'publish',
465
		);
466
467
		if ( ! empty( $type ) && $this->valid_type( $type ) ) {
468
			$query_args['tax_query'] = array(
469
				array(
470
					'taxonomy' => 'give_log_type',
471
					'field'    => 'slug',
472
					'terms'    => $type,
473
				),
474
			);
475
		}
476
477
		if ( ! empty( $meta_query ) ) {
478
			$query_args['meta_query'] = $meta_query;
479
		}
480
481
		$this->bc_200_validate_params( $query_args );
482
483
		if ( ! give_has_upgrade_completed( 'give_v20_logs_upgrades' ) ) {
484
			// Backward compatibility.
485
			$logs = get_posts( $query_args );
486
487
			if ( $logs ) {
488
				foreach ( $logs as $log ) {
489
					wp_delete_post( $log, true );
490
				}
491
			}
492
		} else {
493
			$logs = $this->log_db->get_logs( $query_args );
494
495
			if ( $logs ) {
496
				foreach ( $logs as $log ) {
497
					if ( $this->log_db->delete( $log->ID ) ) {
498
						$this->logmeta_db->delete_row( $log->ID );
499
					}
500
				}
501
			}
502
		}
503
504
		$this->delete_cache();
505
	}
506
507
	/**
508
	 * Setup cron to delete log cache in background.
509
	 *
510
	 * @since  1.7
511
	 * @access public
512
	 *
513
	 * @param int $post_id
514
	 */
515
	public function background_process_delete_cache( $post_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
516
		// Delete log cache immediately
517
		wp_schedule_single_event( time() - 5, 'give_delete_log_cache' );
518
	}
519
520
	/**
521
	 * Delete all logging cache when form, log or payment updates
522
	 *
523
	 * @since  1.7
524
	 * @access public
525
	 *
526
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
527
	 */
528
	public function delete_cache() {
529
		// Add log related keys to delete.
530
		$cache_give_logs      = Give_Cache::get_options_like( 'give_logs' );
531
		$cache_give_log_count = Give_Cache::get_options_like( 'log_count' );
532
533
		$cache_option_names = array_merge( $cache_give_logs, $cache_give_log_count );
534
535
		// Bailout.
536
		if ( empty( $cache_option_names ) ) {
537
			return false;
538
		}
539
540
		Give_Cache::delete( $cache_option_names );
541
	}
542
543
	/**
544
	 * Validate query params.
545
	 *
546
	 * @since  2.0
547
	 * @access private
548
	 *
549
	 * @param array $log_query
550
	 * @param array $log_meta
551
	 */
552
	private function bc_200_validate_params( &$log_query, &$log_meta = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $log_meta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
553
		$query_params = array(
554
			'log_title'    => 'post_title',
555
			'log_parent'   => 'post_parent',
556
			'log_content'  => 'post_content',
557
			'log_type'     => 'tax_query',
558
			'log_date'     => 'post_date',
559
			'log_date_gmt' => 'post_date_gmt',
560
		);
561
562
		if ( ! give_has_upgrade_completed( 'give_v20_logs_upgrades' ) ) {
563
			// Set old params.
564
			foreach ( $query_params as $new_query_param => $old_query_param ) {
565
566
				if ( isset( $log_query[ $old_query_param ] ) && empty( $log_query[ $new_query_param ] ) ) {
567
					$log_query[ $new_query_param ] = $log_query[ $old_query_param ];
568
					continue;
569
				} elseif ( ! isset( $log_query[ $new_query_param ] ) ) {
570
					continue;
571
				}
572
573
				switch ( $new_query_param ) {
574
					case 'log_type':
575
						if ( ! empty( $log_query[ $new_query_param ] ) && $this->valid_type( $log_query[ $new_query_param ] ) ) {
576
							$log_query['tax_query'] = array(
577
								array(
578
									'taxonomy' => 'give_log_type',
579
									'field'    => 'slug',
580
									'terms'    => $log_query[ $new_query_param ],
581
								),
582
							);
583
						}
584
						break;
585
586
					default:
587
						$log_query[ $old_query_param ] = $log_query[ $new_query_param ];
588
				}
589
			}
590
		} else {
591
			// Set only old params.
592
			$query_params = array_flip( $query_params );
593
			foreach ( $query_params as $old_query_param => $new_query_param ) {
594
				if ( isset( $log_query[ $new_query_param ] ) && empty( $log_query[ $old_query_param ] ) ) {
595
					$log_query[ $old_query_param ] = $log_query[ $new_query_param ];
596
					continue;
597
				} elseif ( ! isset( $log_query[ $old_query_param ] ) ) {
598
					continue;
599
				}
600
601
				switch ( $old_query_param ) {
602
					case 'tax_query':
603
						if ( isset( $log_query[ $old_query_param ][0]['terms'] ) ) {
604
							$log_query[ $new_query_param ] = $log_query[ $old_query_param ][0]['terms'];
605
						}
606
						break;
607
608
					default:
609
						$log_query[ $new_query_param ] = $log_query[ $old_query_param ];
610
				}
611
			}
612
613
			if ( isset( $log_query['posts_per_page'] ) ) {
614
				$log_query['number'] = $log_query['posts_per_page'];
615
			}
616
		}
617
	}
618
619
	/**
620
	 * Set new log properties.
621
	 *
622
	 * @since  2.0
623
	 * @access private
624
	 *
625
	 * @param  array $logs
626
	 */
627
	private function bc_200_add_new_properties( &$logs ) {
628
		if ( empty( $logs ) ) {
629
			return;
630
		}
631
632
		$query_params = array(
633
			'log_title'    => 'post_title',
634
			'log_parent'   => 'post_parent',
635
			'log_content'  => 'post_content',
636
			'log_date'     => 'post_date',
637
			'log_date_gmt' => 'post_date_gmt',
638
			'log_type'     => 'give_log_type',
639
		);
640
641
		if ( ! give_has_upgrade_completed( 'give_v20_logs_upgrades' ) ) {
642
			foreach ( $logs as $index => $log ) {
643
				foreach ( $query_params as $new_query_param => $old_query_param ) {
644
					if ( ! property_exists( $log, $old_query_param ) ) {
645
						/**
646
						 *  Set unmatched properties.
647
						 */
648
649
						// 1. log_type
650
						$term = get_the_terms( $log->ID, 'give_log_type' );
651
						$term = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
652
653
						$logs[ $index ]->{$new_query_param} = ! empty( $term ) ? $term->slug : '';
654
655
						continue;
656
					}
657
658
					switch ( $old_query_param ) {
659
						case 'post_parent':
660
							$logs[ $index ]->{$new_query_param} = give_get_meta( $log->ID, '_give_log_payment_id', true );
661
							break;
662
663
						default:
664
							$logs[ $index ]->{$new_query_param} = $log->{$old_query_param};
665
					}
666
				}
667
			}
668
		}
669
	}
670
671
	/**
672
	 * Change log parent to payment if set to form.
673
	 *
674
	 * @since  2.0
675
	 * @access public
676
	 *
677
	 * @param mixed $check
678
	 * @param int   $log_id
679
	 * @param array $meta_key
680
	 * @param array $meta_value
681
	 *
682
	 * @return mixed
683
	 */
684
	public function bc_20_set_payment_as_log_parent( $check, $log_id, $meta_key, $meta_value ) {
685
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
686
		$update_status = false;
687
		$post_type     = get_post_type( $log_id );
688
689
		// Bailout.
690
		if (
691
			'give_payment' === $post_type ||
692
			'_give_log_payment_id' !== $meta_key
693
		) {
694
			return $check;
695
		}
696
697
		$form_id = $wpdb->get_var(
698
			$wpdb->prepare(
699
				"
700
				SELECT log_parent FROM {$this->log_db->table_name}
701
				WHERE ID=%d
702
				",
703
				$log_id
704
			)
705
		);
706
707
		if ( $form_id ) {
708
			$this->logmeta_db->delete_meta( $log_id, '_give_log_payment_id' );
709
			$this->logmeta_db->update_meta( $log_id, '_give_log_form_id', $form_id );
710
711
			$update_status = $wpdb->update(
712
				$this->log_db->table_name,
713
				array(
714
					'log_parent' => $meta_value,
715
				),
716
				array(
717
					'ID' => $log_id,
718
				),
719
				array(
720
					'%s',
721
				),
722
				array(
723
					'%d',
724
				)
725
			);
726
		}
727
728
		return $update_status;
729
	}
730
}
731