Test Failed
Push — master ( 966cf3...fe1ced )
by Devin
13:57 queued 06:53
created

Give_Logging   D

Complexity

Total Complexity 84

Size/Duplication

Total Lines 722
Duplicated Lines 3.6 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 82.86%

Importance

Changes 0
Metric Value
dl 26
loc 722
ccs 116
cts 140
cp 0.8286
rs 4.4444
c 0
b 0
f 0
wmc 84
lcom 1
cbo 3

17 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 31 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() 5 44 7
C update_log() 7 59 10
B get_connected_logs() 0 25 3
C get_log_count() 0 39 7
D delete_logs() 0 44 10
A background_process_delete_cache() 0 4 1
A delete_cache() 0 14 2
D bc_200_validate_params() 14 92 22
D bc_200_add_new_properties() 0 43 10
B bc_200_set_payment_as_log_parent() 0 46 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
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
		);
146
147
		return apply_filters( 'give_log_types', $terms );
148
	}
149
150
	/**
151
	 * Check if a log type is valid
152
	 *
153
	 * Checks to see if the specified type is in the registered list of types.
154
	 *
155 1
	 * @since  1.0
156 1
	 * @access public
157 1
	 *
158 1
	 * @param  string $type Log type.
159
	 *
160 1
	 * @return bool         Whether log type is valid.
161
	 */
162
	public function valid_type( $type ) {
163
		return in_array( $type, $this->log_types() );
164
	}
165
166
	/**
167
	 * Create new log entry
168
	 *
169
	 * This is just a simple and fast way to log something. Use $this->insert_log()
170
	 * if you need to store custom meta data.
171
	 *
172
	 * @since  1.0
173
	 * @access public
174
	 *
175 43
	 * @param  string $title   Log entry title. Default is empty.
176
	 * @param  string $message Log entry message. Default is empty.
177 43
	 * @param  int    $parent  Log entry parent. Default is 0.
178 43
	 * @param  string $type    Log type. Default is empty string.
179 43
	 *
180 43
	 * @return int             Log ID.
181
	 */
182 43
	public function add( $title = '', $message = '', $parent = 0, $type = '' ) {
183
		$log_data = array(
184 43
			'post_title'   => $title,
185
			'post_content' => $message,
186 43
			'post_parent'  => $parent,
187
			'log_type'     => $type,
188
		);
189 43
190
		return $this->insert_log( $log_data );
191
	}
192 43
193 43
	/**
194 43
	 * Get Logs
195
	 *
196
	 * Retrieves log items for a particular object ID.
197 43
	 *
198 42
	 * @since  1.0
199 42
	 * @access public
200 42
	 *
201 42
	 * @param  int    $object_id Log object ID. Default is 0.
202
	 * @param  string $type      Log type. Default is empty string.
203 43
	 * @param  int    $paged     Page number Default is null.
204
	 *
205 43
	 * @return array             An array of the connected logs.
206
	 */
207
	public function get_logs( $object_id = 0, $type = '', $paged = null ) {
208
		return $this->get_connected_logs( array(
209
			'log_parent' => $object_id,
210
			'paged'      => $paged,
211
			'log_type'   => $type,
212
		) );
213
	}
214
215
	/**
216
	 * Stores a log entry
217
	 *
218
	 * @since  1.0
219
	 * @access public
220
	 *
221
	 * @param  array $log_data Log entry data.
222
	 * @param  array $log_meta Log entry meta.
223
	 *
224
	 * @return int             The ID of the newly created log item.
225
	 */
226
	public function insert_log( $log_data = array(), $log_meta = array() ) {
227
		$log_id = 0;
0 ignored issues
show
Unused Code introduced by
$log_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
228
229
		$defaults = array(
230
			'log_parent'  => 0,
231
			'log_content' => '',
232
			'log_type'    => false,
233
234
			// Backward compatibility.
235
			'post_type'   => 'give_log',
236
			'post_status' => 'publish',
237
		);
238
239
		$args = wp_parse_args( $log_data, $defaults );
240
		$this->bc_200_validate_params( $args, $log_meta );
241
242
		if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
243
			global $wpdb;
244
245
			// Backward Compatibility.
246
			if ( ! $wpdb->get_var( "SELECT ID from {$this->log_db->table_name} ORDER BY id DESC LIMIT 1" ) ) {
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
247
				$latest_log_id = $wpdb->get_var( "SELECT ID from $wpdb->posts ORDER BY id DESC LIMIT 1" );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
248
				$latest_log_id = empty( $latest_log_id ) ? 1 : ++ $latest_log_id;
249
250
				$args['ID'] = $latest_log_id;
251
				$this->log_db->insert( $args );
252
			}
253
		}
254
255
		$log_id = $this->log_db->add( $args );
256
257 1
		// Set log meta, if any
258 View Code Duplication
		if ( $log_id && ! empty( $log_meta ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
			foreach ( (array) $log_meta as $key => $meta ) {
260 1
				$this->logmeta_db->update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta );
0 ignored issues
show
Bug introduced by
It seems like $log_id defined by $this->log_db->add($args) on line 255 can also be of type boolean; however, Give_DB_Meta::update_meta() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
261 1
			}
262 1
		}
263 1
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
264
265 1
		// Delete cache.
266
		$this->delete_cache();
267 1
268
		return $log_id;
269 1
	}
270 1
271
	/**
272 1
	 * Update and existing log item
273 1
	 *
274 1
	 * @since  1.0
275 1
	 * @access public
276 1
	 *
277 1
	 * @param  array $log_data Log entry data.
278
	 * @param  array $log_meta Log entry meta.
279 1
	 *
280
	 * @return bool|null       True if successful, false otherwise.
281 1
	 */
282 1
	public function update_log( $log_data = array(), $log_meta = array() ) {
283
		$log_id = 0;
0 ignored issues
show
Unused Code introduced by
$log_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
284
285
		/**
286
		 * Fires before updating log entry.
287
		 *
288
		 * @since 1.0
289
		 *
290
		 * @param array $log_data Log entry data.
291
		 * @param array $log_meta Log entry meta.
292
		 */
293
		do_action( 'give_pre_update_log', $log_data, $log_meta );
294
295
		$defaults = array(
296
			'log_parent'  => 0,
297
298
			// Backward compatibility.
299
			'post_type'   => 'give_log',
300
			'post_status' => 'publish',
301
		);
302 1
303
		$args = wp_parse_args( $log_data, $defaults );
304
		$this->bc_200_validate_params( $args, $log_meta );
305 1
306 1
		// Store the log entry
307 1
		if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
308 1
			// Backward compatibility.
309 1
			$log_id = wp_update_post( $args );
310 1
311
			if ( $log_id && ! empty( $log_meta ) ) {
312 1
				foreach ( (array) $log_meta as $key => $meta ) {
313 1
					if ( ! empty( $meta ) ) {
314
						give_update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta );
315 1
					}
316 1
				}
317
			}
318 1
		} else {
319 1
			$log_id = $this->log_db->add( $args );
320 1
321 View Code Duplication
			if ( $log_id && ! empty( $log_meta ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
322 1
				foreach ( (array) $log_meta as $key => $meta ) {
323
					if ( ! empty( $meta ) ) {
324
						$this->logmeta_db->update_meta( $log_id, '_give_log_' . sanitize_key( $key ), $meta );
0 ignored issues
show
Bug introduced by
It seems like $log_id defined by $this->log_db->add($args) on line 319 can also be of type boolean; however, Give_DB_Meta::update_meta() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
325
					}
326 1
				}
327
			}
328
		}
329
330 1
		/**
331
		 * Fires after updating log entry.
332 1
		 *
333
		 * @since 1.0
334
		 *
335
		 * @param int   $log_id   Log entry id.
336
		 * @param array $log_data Log entry data.
337
		 * @param array $log_meta Log entry meta.
338
		 */
339
		do_action( 'give_post_update_log', $log_id, $log_data, $log_meta );
340
	}
341
342
	/**
343
	 * Retrieve all connected logs
344
	 *
345
	 * Used for retrieving logs related to particular items, such as a specific donation.
346
	 * For new table params check: Give_DB_Logs::get_column_defaults and Give_DB_Logs::get_sql#L262
347
	 *
348 32
	 * @since  1.0
349
	 * @since  2.0 Added new table logic.
350 32
	 * @access public
351 32
	 *
352 32
	 * @param  array $args Query arguments.
353 32
	 *
354
	 * @return array|false Array if logs were found, false otherwise.
355 32
	 */
356
	public function get_connected_logs( $args = array() ) {
357 32
		$logs = array();
0 ignored issues
show
Unused Code introduced by
$logs is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
358 31
359
		$defaults   = array(
360 31
			'number'      => 20,
361 31
			'paged'       => get_query_var( 'paged' ),
362 31
			'log_type'    => false,
363
364 31
			// Backward compatibility.
365 31
			'post_type'   => 'give_log',
366
			'post_status' => 'publish',
367 32
		);
368 31
		$query_args = wp_parse_args( $args, $defaults );
369 31
		$this->bc_200_validate_params( $query_args );
370
371 32
		if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
372
			// Backward compatibility.
373 32
			$logs = get_posts( $query_args );
374 7
			$this->bc_200_add_new_properties( $logs );
375 7
		} else {
376 7
			$logs = $this->log_db->get_logs( $query_args );
377 7
		}
378 32
379
		return ( ! empty( $logs ) ? $logs : false );
380
	}
381
382
	/**
383
	 * Retrieve Log Count
384
	 *
385
	 * Retrieves number of log entries connected to particular object ID.
386
	 *
387
	 * @since  1.0
388
	 * @access public
389
	 *
390
	 * @param  int    $object_id  Log object ID. Default is 0.
391
	 * @param  string $type       Log type. Default is empty string.
392
	 * @param  array  $meta_query Log meta query. Default is null.
393
	 * @param  array  $date_query Log data query. Default is null.
394
	 *
395
	 * @return int                Log count.
396
	 */
397
	public function get_log_count( $object_id = 0, $type = '', $meta_query = null, $date_query = null ) {
398
		$logs_count = 0;
0 ignored issues
show
Unused Code introduced by
$logs_count is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
399
400
		$query_args = array(
401
			'number'      => - 1,
402
403
			// Backward comatibility.
404
			'post_type'   => 'give_log',
405
			'post_status' => 'publish',
406
		);
407
408
		if ( $object_id ) {
409
			$query_args['log_parent'] = $object_id;
410
		}
411
412
		if ( ! empty( $type ) && $this->valid_type( $type ) ) {
413
			$query_args['log_type'] = $type;
414
		}
415
416
		if ( ! empty( $meta_query ) ) {
417
			$query_args['meta_query'] = $meta_query;
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
418
		}
419
420
		if ( ! empty( $date_query ) ) {
421
			$query_args['date_query'] = $date_query;
422
		}
423
424
		$this->bc_200_validate_params( $query_args );
425
426
		if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
427
			// Backward compatibility.
428
			$logs       = new WP_Query( $query_args );
429
			$logs_count = (int) $logs->post_count;
430
		} else {
431
			$logs_count = $this->log_db->count( $query_args );
432
		}
433
434
		return $logs_count;
435
	}
436
437
	/**
438
	 * Delete Logs
439
	 *
440
	 * Remove log entries connected to particular object ID.
441
	 *
442
	 * @since  1.0
443
	 * @access public
444
	 *
445
	 * @param  int    $object_id  Log object ID. Default is 0.
446
	 * @param  string $type       Log type. Default is empty string.
447
	 * @param  array  $meta_query Log meta query. Default is null.
448
	 *
449
	 * @return void
450
	 */
451
	public function delete_logs( $object_id = 0, $type = '', $meta_query = null ) {
452
		$query_args = array(
453
			'log_parent'  => $object_id,
454
			'number'      => - 1,
455
			'fields'      => 'ID',
456
457
			// Backward compatibility.
458
			'post_type'   => 'give_log',
459
			'post_status' => 'publish',
460
		);
461
462
		if ( ! empty( $type ) && $this->valid_type( $type ) ) {
463
			$query_args['log_type'] = $type;
464
		}
465
466
		if ( ! empty( $meta_query ) ) {
467
			$query_args['meta_query'] = $meta_query;
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
468
		}
469
470
		$this->bc_200_validate_params( $query_args );
471
472
		if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
473
			// Backward compatibility.
474
			$logs = get_posts( $query_args );
475
476
			if ( $logs ) {
477
				foreach ( $logs as $log ) {
478
					wp_delete_post( $log, true );
479
				}
480
			}
481
		} else {
482
			$logs = $this->log_db->get_logs( $query_args );
483
484
			if ( $logs ) {
485
				foreach ( $logs as $log ) {
486
					if ( $this->log_db->delete( $log->ID ) ) {
487
						$this->logmeta_db->delete_row( $log->ID );
488
					}
489
				}
490
			}
491
		}
492
493
		$this->delete_cache();
494
	}
495
496
	/**
497
	 * Setup cron to delete log cache in background.
498
	 *
499
	 * @since  1.7
500
	 * @access public
501
	 *
502
	 * @param int $post_id
503
	 */
504
	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...
505
		// Delete log cache immediately
506
		wp_schedule_single_event( time() - 5, 'give_delete_log_cache' );
507
	}
508
509
	/**
510
	 * Delete all logging cache when form, log or payment updates
511
	 *
512
	 * @since  1.7
513
	 * @access public
514
	 *
515
	 * @return bool
516
	 */
517
	public function delete_cache() {
518
		// Add log related keys to delete.
519
		$cache_give_logs      = Give_Cache::get_options_like( 'give_logs' );
520
		$cache_give_log_count = Give_Cache::get_options_like( 'log_count' );
521
522
		$cache_option_names = array_merge( $cache_give_logs, $cache_give_log_count );
523
524
		// Bailout.
525
		if ( empty( $cache_option_names ) ) {
526
			return false;
527
		}
528
529
		Give_Cache::delete( $cache_option_names );
530
	}
531
532
	/**
533
	 * Validate query params.
534
	 *
535
	 * @since  2.0
536
	 * @access private
537
	 *
538
	 * @param array $log_query
539
	 * @param array $log_meta
540
	 */
541
	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...
542
		$query_params = array(
543
			'log_title'    => 'post_title',
544
			'log_parent'   => 'post_parent',
545
			'log_content'  => 'post_content',
546
			'log_type'     => 'tax_query',
547
			'log_date'     => 'post_date',
548
			'log_date_gmt' => 'post_date_gmt',
549
			'number'       => 'posts_per_page',
550
			'meta_query'   => 'meta_query',
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
551
		);
552
553
		if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
554
			// Set old params.
555
			foreach ( $query_params as $new_query_param => $old_query_param ) {
556
557 View Code Duplication
				if ( isset( $log_query[ $old_query_param ] ) && empty( $log_query[ $new_query_param ] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
558
					$log_query[ $new_query_param ] = $log_query[ $old_query_param ];
559
					continue;
560
				} elseif ( ! isset( $log_query[ $new_query_param ] ) ) {
561
					continue;
562
				} elseif( empty( $log_query[ $new_query_param ] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
563
					continue;
564
				}
565
566
				switch ( $new_query_param ) {
567
					case 'log_type':
568
						$log_query['tax_query'] = array(
0 ignored issues
show
introduced by
Detected usage of tax_query, possible slow query.
Loading history...
569
							array(
570
								'taxonomy' => 'give_log_type',
571
								'field'    => 'slug',
572
								'terms'    => $log_query[ $new_query_param ],
573
							),
574
						);
575
						break;
576
577
					case 'meta_query':
578
						if( ! empty( $log_query['meta_query'] ) && empty( $log_query['post_parent'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
579
							foreach ( $log_query['meta_query'] as $index => $meta_query ){
580
								if( ! is_array( $meta_query ) || empty( $meta_query['key'] ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
581
									continue;
582
								}
583
584
								switch ( $meta_query['key'] ) {
585
									case '_give_log_form_id':
586
										$log_query['post_parent'] = $meta_query['value'];
587
										unset( $log_query['meta_query'][$index] );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
588
										break;
589
								}
590
							}
591
						}
592
						break;
593
594
					default:
595
						switch( $new_query_param ){
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
596
							case 'log_parent':
597
								$log_query['meta_query'][] = array(
598
									'key' => '_give_log_payment_id',
599
									'value' => $log_query[ $new_query_param ]
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
600
								);
601
602
								break;
603
604
							default:
605
								$log_query[ $old_query_param ] = $log_query[ $new_query_param ];
606
						}
607
				}
608
			}
609
		} else {
610
			// Set only old params.
611
			$query_params = array_flip( $query_params );
612
			foreach ( $query_params as $old_query_param => $new_query_param ) {
613 View Code Duplication
				if ( isset( $log_query[ $new_query_param ] ) && empty( $log_query[ $old_query_param ] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
614
					$log_query[ $old_query_param ] = $log_query[ $new_query_param ];
615
					continue;
616
				} elseif ( ! isset( $log_query[ $old_query_param ] ) ) {
617
					continue;
618
				}
619
620
				switch ( $old_query_param ) {
621
					case 'tax_query':
622
						if ( isset( $log_query[ $old_query_param ][0]['terms'] ) ) {
623
							$log_query[ $new_query_param ] = $log_query[ $old_query_param ][0]['terms'];
624
						}
625
						break;
626
627
					default:
628
						$log_query[ $new_query_param ] = $log_query[ $old_query_param ];
629
				}
630
			}
631
		}
632
	}
633
634
	/**
635
	 * Set new log properties.
636
	 *
637
	 * @since  2.0
638
	 * @access private
639
	 *
640
	 * @param  array $logs
641
	 */
642
	private function bc_200_add_new_properties( &$logs ) {
643
		if ( empty( $logs ) ) {
644
			return;
645
		}
646
647
		$query_params = array(
648
			'log_title'    => 'post_title',
649
			'log_parent'   => 'post_parent',
650
			'log_content'  => 'post_content',
651
			'log_date'     => 'post_date',
652
			'log_date_gmt' => 'post_date_gmt',
653
			'log_type'     => 'give_log_type',
654
		);
655
656
		if ( ! give_has_upgrade_completed( 'v20_logs_upgrades' ) ) {
657
			foreach ( $logs as $index => $log ) {
658
				foreach ( $query_params as $new_query_param => $old_query_param ) {
659
					if ( ! property_exists( $log, $old_query_param ) ) {
660
						/**
661
						 *  Set unmatched properties.
662
						 */
663
664
						// 1. log_type
665
						$term = get_the_terms( $log->ID, 'give_log_type' );
666
						$term = ! is_wp_error( $term ) && ! empty( $term ) ? $term[0] : array();
667
668
						$logs[ $index ]->{$new_query_param} = ! empty( $term ) ? $term->slug : '';
669
670
						continue;
671
					}
672
673
					switch ( $old_query_param ) {
674
						case 'post_parent':
675
							$logs[ $index ]->{$new_query_param} = give_get_meta( $log->ID, '_give_log_payment_id', true );
676
							break;
677
678
						default:
679
							$logs[ $index ]->{$new_query_param} = $log->{$old_query_param};
680
					}
681
				}
682
			}
683
		}
684
	}
685
686
	/**
687
	 * Change log parent to payment if set to form.
688
	 *
689
	 * @since  2.0
690
	 * @access public
691
	 *
692
	 * @param mixed $check
693
	 * @param int   $log_id
694
	 * @param array $meta_key
695
	 * @param array $meta_value
696
	 *
697
	 * @return mixed
698
	 */
699
	public function bc_200_set_payment_as_log_parent( $check, $log_id, $meta_key, $meta_value ) {
700
		global $wpdb;
701
		$update_status = false;
702
		$post_type     = get_post_type( $log_id );
703
704
		// Bailout.
705
		if (
706
			'give_payment' === $post_type ||
707
			'_give_log_payment_id' !== $meta_key
708
		) {
709
			return $check;
710
		}
711
712
		$form_id = $wpdb->get_var(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
713
			$wpdb->prepare(
714
				"
715
				SELECT log_parent FROM {$this->log_db->table_name}
716
				WHERE ID=%d
717
				",
718
				$log_id
719
			)
720
		);
721
722
		if ( $form_id ) {
723
			$this->logmeta_db->delete_meta( $log_id, '_give_log_payment_id' );
724
			$this->logmeta_db->update_meta( $log_id, '_give_log_form_id', $form_id );
725
726
			$update_status = $wpdb->update(
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
727
				$this->log_db->table_name,
728
				array(
729
					'log_parent' => $meta_value,
730
				),
731
				array(
732
					'ID' => $log_id,
733
				),
734
				array(
735
					'%s',
736
				),
737
				array(
738
					'%d',
739
				)
740
			);
741
		}
742
743
		return $update_status;
744
	}
745
}
746