Give_Tools_Import_Donors::get_data()   F
last analyzed

Complexity

Conditions 16
Paths 176

Size

Total Lines 133

Duplication

Lines 12
Ratio 9.02 %

Importance

Changes 0
Metric Value
cc 16
nc 176
nop 0
dl 12
loc 133
rs 3.9466
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Delete Donors.
4
 *
5
 * This class handles batch processing of deleting donor data.
6
 *
7
 * @subpackage  Admin/Tools/Give_Tools_Delete_Donors
8
 * @copyright   Copyright (c) 2016, WordImpress
9
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
10
 * @since       1.8.12
11
 */
12
13
// Exit if accessed directly.
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
19
/**
20
 * Give_Tools_Import_Donors Class
21
 *
22
 * @since 1.8.13
23
 */
24
class Give_Tools_Import_Donors extends Give_Batch_Export {
25
26
	/**
27
	 * Form Data passed in batch processing.
28
	 *
29
	 * @var $request
30
	 */
31
	var $request;
32
33
	/**
34
	 * Used to store form id's that are going to get recount.
35
	 *
36
	 * @var $form_key
37
	 *
38
	 * @since 1.8.13
39
	 */
40
	var $form_key = 'give_temp_delete_form_ids';
41
42
	/**
43
	 * Used to store donation id's that are going to get deleted.
44
	 *
45
	 * @var $donation_key
46
	 *
47
	 * @since 1.8.12
48
	 */
49
	var $donation_key = 'give_temp_delete_donation_ids';
50
51
	/**
52
	 * Used to store the step where the step will be. ( 'count', 'donations', 'donors' ).
53
	 *
54
	 * @var $step_key
55
	 *
56
	 * @since 1.8.12
57
	 */
58
	var $step_key = 'give_temp_delete_step';
59
60
	/**
61
	 * Used to store donors id's that are going to get deleted.
62
	 *
63
	 * @var $donor_key
64
	 *
65
	 * @since 1.8.12
66
	 */
67
	var $donor_key = 'give_temp_delete_donor_ids';
68
69
	/**
70
	 * Used to store to get the page count in the loop.
71
	 *
72
	 * @var $step_on_key
73
	 *
74
	 * @since 1.8.12
75
	 */
76
	var $step_on_key = 'give_temp_delete_step_on';
77
78
	/**
79
	 * Contain total number of step.
80
	 *
81
	 * @var $total_step
82
	 *
83
	 * @since 1.8.12
84
	 */
85
	var $total_step;
86
87
	/**
88
	 * Counting contain total number of step that completed.
89
	 *
90
	 * @var $step_completed
91
	 *
92
	 * @since 1.8.12
93
	 */
94
	var $step_completed;
95
96
	/**
97
	 * Our export type. Used for export-type specific filters/actions.
98
	 *
99
	 * @var $export_type
100
	 *
101
	 * @since 1.8.12
102
	 */
103
	public $export_type = '';
104
105
	/**
106
	 * Allows for a non-form batch processing to be run.
107
	 *
108
	 * @var $is_void
109
	 *
110
	 * @since 1.8.12
111
	 */
112
	public $is_void = true;
113
114
	/**
115
	 * Sets the number of items to pull on each step
116
	 *
117
	 * @var $per_step
118
	 *
119
	 * @since 1.8.12
120
	 */
121
	public $per_step = 10;
122
123
	/**
124
	 * Set's all the donors id's
125
	 *
126
	 * @var $donor_ids
127
	 *
128
	 * @since 1.8.12
129
	 */
130
	public $donor_ids = array();
131
132
	/**
133
	 * Give_Tools_Import_Donors constructor.
134
	 *
135
	 * @param int $_step Steps.
136
	 */
137
	public function __construct( $_step = 1 ) {
138
		parent::__construct( $_step );
139
140
		$this->is_writable = true;
141
	}
142
143
	/**
144
	 * Get the Export Data
145
	 *
146
	 * @access public
147
	 * @since 1.8.12
148
	 * @global object $wpdb Used to query the database using the WordPress Database API
149
	 *
150
	 * @return void
151
	 */
152 View Code Duplication
	public function pre_fetch() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
153
		$donation_ids = array();
154
		$donor_ids    = array();
155
156
		// Check if the ajax request if running for the first time.
157
		if ( 1 === (int) $this->step ) {
158
159
			// Delete all the form ids.
160
			$this->delete_option( $this->form_key );
161
162
			// Delete all the donation ids.
163
			$this->delete_option( $this->donation_key );
164
165
			// Delete all the donor ids.
166
			$this->delete_option( $this->donor_key );
167
168
			// Delete all the step and set to 'count' which if the first step in the process of deleting the donors.
169
			$this->update_option( $this->step_key, 'count' );
170
171
			// Delete tha page count of the step.
172
			$this->update_option( $this->step_on_key, '0' );
173
		} else {
174
175
			// Get the old donors list.
176
			$donor_ids = $this->get_option( $this->donor_key );
177
178
			// Get the old donation list.
179
			$donation_ids = $this->get_option( $this->donation_key );
180
		}
181
182
		// Get the step and check for it if it's on the first step( 'count' ) or not.
183
		$step = (int) $this->get_step();
184
		if ( 1 === $step ) {
185
			/**
186
			 * Will add or update the donation and donor data by running wp query.
187
			 */
188
			$this->count( $step, $donation_ids, $donor_ids );
189
		}
190
	}
191
192
	/**
193
	 * Will Update or Add the donation and donors ids in the with option table for there respected key.
194
	 *
195
	 * @param string $step         On which the current ajax is running.
196
	 * @param array  $donation_ids Contain the list of all the donation id's that has being add before this.
197
	 * @param array  $donor_ids    Contain the list of all the donors id's that has being add before this.
198
	 */
199
	private function count( $step, $donation_ids = array(), $donor_ids = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $step 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...
200
201
		// Get the Page count by default it's zero.
202
		$paged = (int) $this->get_step_page();
203
204
		// Increased the page count by one.
205
		++ $paged;
206
207
		/**
208
		 * Filter add to alter the argument before the wp quest run
209
		 */
210
		$args = apply_filters( 'give_tools_reset_stats_total_args', array(
211
			'post_type'      => 'give_payment',
212
			'post_status'    => 'any',
213
			'posts_per_page' => $this->per_step,
214
			'paged'          => $paged,
215
			'meta_key'       => '_give_payment_import',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
216
			'meta_value_num' => 1,
217
			'meta_compare'   => '=',
218
		) );
219
220
		// Reset the post data.
221
		wp_reset_postdata();
222
223
		// Getting the new donation.
224
		$donation_posts = new WP_Query( $args );
225
226
		// The Loop.
227
		if ( $donation_posts->have_posts() ) {
228
			while ( $donation_posts->have_posts() ) {
229
				$add_author = true;
230
				$donation_posts->the_post();
231
				global $post;
232
				// Add the donation id in side the array.
233
				$donation_ids[] = $post->ID;
234
235
				$donor_id = (int) give_get_meta( $post->ID, '_give_payment_customer_id', true );
236
				if ( ! empty( $donor_id ) ) {
237
					$donor = new Give_Donor( $donor_id );
238
					if ( ! empty( $donor->id ) ) {
239
						if ( empty( $donor->user_id ) && ! empty( $donor->payment_ids ) ) {
240
							$add_author = false;
241
							$count      = (int) count( $donor->payment_ids );
242
							if ( 1 === $count ) {
243
								give_delete_donor_and_related_donation( $donor );
244
							} else {
245
								$donor->remove_payment( $post->ID );
246
								$donor->decrease_donation_count();
247
							}
248
						}
249
					}
250
				}
251
252
				if ( ! empty( $add_author ) ) {
253
					// Add the donor id in side the array.
254
					$donor_ids[] = (int) $post->post_author;
255
				}
256
			}
257
		}
258
259
		// Get the total number of post found.
260
		$total_donation = (int) $donation_posts->found_posts;
261
262
		// Maximum number of page can be display.
263
		$max_num_pages = (int) $donation_posts->max_num_pages;
264
265
		// Check current page is less then max number of page or not.
266 View Code Duplication
		if ( $paged < $max_num_pages ) {
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...
267
268
			// Update the current page variable for the next step.
269
			$this->update_option( $this->step_on_key, $paged );
270
271
			// Calculating percentage.
272
			$page_remain          = $max_num_pages - $paged;
273
			$this->total_step     = (int) $max_num_pages + ( $total_donation / $this->per_step ) + ( ( $page_remain * 2 ) * count( $donor_ids ) );
274
			$this->step_completed = $paged;
275
		} else {
276
			$donation_ids_count = count( $donor_ids );
0 ignored issues
show
Unused Code introduced by
$donation_ids_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...
277
			$this->update_option( $this->step_key, 'donation' );
278
			$this->update_option( $this->step_on_key, '0' );
279
		}
280
281
		$donor_ids = array_unique( $donor_ids );
282
		$this->update_option( $this->donor_key, $donor_ids );
283
		$this->update_option( $this->donation_key, $donation_ids );
284
285
		wp_reset_postdata();
286
	}
287
288
	/**
289
	 * Return the calculated completion percentage.
290
	 *
291
	 * @since 1.8.12
292
	 *
293
	 * @return int
294
	 */
295
	public function get_percentage_complete() {
296
		return ceil( ( 100 * $this->step_completed ) / $this->total_step );
297
	}
298
299
	/**
300
	 * Process Steps
301
	 *
302
	 * @return bool
303
	 */
304
	public function process_step() {
305
306
		if ( ! $this->can_export() ) {
307
			wp_die(
308
				esc_html__( 'You do not have permission to delete Import transactions.', 'give' ),
309
				esc_html__( 'Error', 'give' ),
310
				array(
311
					'response' => 403,
312
				)
313
			);
314
		}
315
316
		$had_data = $this->get_data();
317
318
		if ( $had_data ) {
319
			$this->done = false;
320
321
			return true;
322
		} else {
323
			update_option( 'give_earnings_total', give_get_total_earnings( true ), false );
324
			Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
325
326
			$this->delete_option( $this->donation_key );
327
328
			$this->done    = true;
329
			$this->message = __( 'Imported donor and transactions successfully deleted.', 'give' );
330
331
			return false;
332
		}
333
	}
334
335
	/**
336
	 * Get the Export Data
337
	 *
338
	 * @access public
339
	 * @since 1.8.12
340
	 * @global object $wpdb Used to query the database using the WordPress Database API
341
	 *
342
	 * @return array|bool $data The data for the CSV file
343
	 */
344
	public function get_data() {
345
346
		// Get the donation id's.
347
		$donation_ids = $this->get_option( $this->donation_key );
348
349
		/**
350
		 * Return false id not Import donation is found.
351
		 */
352
		if ( empty( $donation_ids ) ) {
353
			$this->is_empty   = true;
354
			$this->total_step = 1;
355
356
			return false;
357
		}
358
359
		// Get the current step.
360
		$step = (int) $this->get_step();
361
362
		// Get the donor ids.
363
		$donor_ids = $this->get_option( $this->donor_key );
364
365
		// Delete all the imported donations.
366
		if ( 2 === $step ) {
367
			$pass_to_donor = false;
368
			$page          = (int) $this->get_step_page();
369
			$page ++;
370
			$count = count( $donation_ids );
371
372
			$this->total_step     = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids );
373
			$this->step_completed = $page;
374
375 View Code Duplication
			if ( $count > $this->per_step ) {
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...
376
377
				$this->update_option( $this->step_on_key, $page );
378
				$donation_ids = $this->get_delete_ids( $donation_ids, $page );
379
				$current_page = (int) ceil( $count / $this->per_step );
380
381
				if ( $page === $current_page ) {
382
					$pass_to_donor = true;
383
				}
384
			} else {
385
				$pass_to_donor = true;
386
			}
387
388
			if ( true === $pass_to_donor ) {
389
				$this->update_option( $this->step_key, 'donor' );
390
				$this->update_option( $this->step_on_key, '0' );
391
			}
392
393
			// Get the old form list.
394
			$form_ids = (array) $this->get_option( $this->form_key );
395
396
			foreach ( $donation_ids as $item ) {
397
				$form_ids[] = give_get_meta( $item, '_give_payment_form_id', true );
398
399
				// Delete the main payment.
400
				give_delete_donation( absint( $item ) );
401
			}
402
403
			// Update the new form list.
404
			$this->update_option( $this->form_key, $form_ids );
405
		} // End if().
406
407
		// Delete all the donors.
408
		if ( 3 === $step ) {
409
410
			// Get the old form list.
411
			$form_ids = (array) $this->get_option( $this->form_key );
412
			if ( ! empty( $form_ids ) ) {
413
				$form_ids = array_unique( $form_ids );
414
				foreach ( $form_ids as $form_id ) {
415
					give_recount_form_income_donation( (int) $form_id );
416
				}
417
			}
418
			// update the new form list.
419
			$this->update_option( $this->form_key, array() );
420
421
			$page  = (int) $this->get_step_page();
422
			$count = count( $donor_ids );
423
424
			$this->total_step     = ( ( count( $donation_ids ) / $this->per_step ) * 2 ) + count( $donor_ids );
425
			$this->step_completed = $page + ( count( $donation_ids ) / $this->per_step );
426
427
			if ( ! empty( $donor_ids[ $page ] ) ) {
428
				$args = apply_filters( 'give_tools_reset_stats_total_args', array(
429
					'post_status'    => 'any',
430
					'posts_per_page' => 1,
431
					'author'         => $donor_ids[ $page ],
432
				) );
433
434
				$donations = array();
435
				$payments  = new Give_Payments_Query( $args );
436
				$payments  = $payments->get_payments();
437
				if ( empty( $payments ) ) {
438
					Give()->donors->delete_by_user_id( $donor_ids[ $page ] );
439
440
					/**
441
					 * If Checked then delete WP user.
442
					 *
443
					 * @since 1.8.14
444
					 */
445
					$delete_import_donors = isset( $_REQUEST['delete-import-donors'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['delete-import-donors'] ) ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
446
447
					if ( 'on' === (string) $delete_import_donors ) {
448
						wp_delete_user( $donor_ids[ $page ] );
449
					}
450
				} else {
451
					foreach ( $payments as $payment ) {
452
						$donations[] = $payment->ID;
453
					}
454
455
					$donor          = new Give_Donor( $donor_ids[ $page ], true );
456
					$data_to_update = array(
457
						'purchase_count' => count( $donations ),
458
						'payment_ids'    => implode( ',', $donations ),
459
					);
460
					$donor->update( $data_to_update );
461
				}
462
			} // End if().
463
464
			$page ++;
465
			$this->update_option( $this->step_on_key, $page );
466
			if ( $count === $page ) {
467
				$this->is_empty = false;
468
469
				return false;
470
			}
471
472
			return true;
473
		} // End if().
474
475
		return true;
476
	}
477
478
	/**
479
	 * This function will get list of donation ids ready for deletion.
480
	 *
481
	 * @param array  $donation_ids List of donation ids.
482
	 * @param string $page         Ajax on Page.
483
	 *
484
	 * @return mixed
485
	 */
486 View Code Duplication
	public function get_delete_ids( $donation_ids, $page ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
487
		$index            = $page --;
0 ignored issues
show
Unused Code introduced by
$index 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...
488
		$count            = count( $donation_ids );
0 ignored issues
show
Unused Code introduced by
$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...
489
		$temp             = 0;
490
		$current_page     = 0;
491
		$post_delete      = $this->per_step;
492
		$page_donation_id = array();
493
494
		foreach ( $donation_ids as $item ) {
495
			$temp ++;
496
			$page_donation_id[ $current_page ][] = $item;
497
			if ( $temp === $post_delete ) {
498
				$current_page ++;
499
				$temp = 0;
500
			}
501
		}
502
503
		return $page_donation_id[ $page ];
504
	}
505
506
	/**
507
	 * Given a key, get the information from the Database Directly
508
	 *
509
	 * @since 1.8.12
510
	 *
511
	 * @param string $key           Option Key.
512
	 * @param bool   $default_value True, if default value, else false.
513
	 *
514
	 * @return mixed Returns the data from the database
515
	 */
516
	public function get_option( $key, $default_value = false ) {
517
		return get_option( $key, $default_value );
518
	}
519
520
	/**
521
	 * Give a key, store the value
522
	 *
523
	 * @since 1.8.12
524
	 *
525
	 * @param string $key   Option Key.
526
	 * @param mixed  $value Option Value.
527
	 *
528
	 * @return void
529
	 */
530
	public function update_option( $key, $value ) {
531
		update_option( $key, $value, false );
532
	}
533
534
	/**
535
	 * Delete an option
536
	 *
537
	 * @since 1.8.12
538
	 *
539
	 * @param string $key Option Key.
540
	 *
541
	 * @return void
542
	 */
543
	public function delete_option( $key ) {
544
		delete_option( $key );
545
	}
546
547
	/**
548
	 * Get the current step in number.
549
	 *
550
	 * There are three step to delete the total donor first counting, second deleting donotion and third deleting donors.
551
	 *
552
	 * @return int|string
553
	 */
554 View Code Duplication
	private function get_step() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
555
		$step_key = (string) $this->get_option( $this->step_key, false );
556
		if ( 'count' === $step_key ) {
557
			return 1;
558
		} elseif ( 'donation' === $step_key ) {
559
			return 2;
560
		} elseif ( 'donor' === $step_key ) {
561
			return 3;
562
		} else {
563
			return $step_key;
564
		}
565
	}
566
567
	/**
568
	 * Get the current $page value in the ajax.
569
	 */
570
	private function get_step_page() {
571
		return $this->get_option( $this->step_on_key, false );
572
	}
573
}
574