Completed
Pull Request — master (#986)
by Rami
20:36
created

GIVE_CLI_COMMAND::donors()   F

Complexity

Conditions 32

Size

Total Lines 140
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 32
eloc 71
nop 2
dl 0
loc 140
rs 2
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
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 23 and the first side effect is on line 10.

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
 * GIVE WP_CLI commands
4
 *
5
 * @package give
6
 * @since 1.7
7
 */
8
9
// Exit if accessed directly.
10
if ( ! defined( 'ABSPATH' ) ) { exit; }
11
12
// Add give command.
13
WP_CLI::add_command( 'give', 'GIVE_CLI_COMMAND' );
14
15
16
/**
17
 * Work with Give through WP-CLI
18
 *
19
 * Adds CLI support to Give through WP-CLI
20
 *
21
 * @since 1.7
22
 */
23
class GIVE_CLI_COMMAND {
24
25
	/**
26
	 * This param uses to count process/step inside loop.
27
	 *
28
	 * @var int $counter Counter.
29
	 */
30
	private static $counter;
31
32
	/**
33
	 * This helps to get information give plugin data.
34
	 *
35
	 * @var Give_API Object.
36
	 */
37
	private $api;
38
39
40
	/**
41
	 * GIVE_CLI_Command constructor.
42
	 */
43
	public function __construct() {
44
		$this->api = new Give_API();
45
	}
46
47
48
	/**
49
	 * Get Give details
50
	 *
51
	 * ## OPTIONS
52
	 *
53
	 * None. Returns basic info regarding your Give instance.
54
	 *
55
	 * ## EXAMPLES
56
	 *
57
	 * wp give details
58
	 *
59
	 * @since		1.7
60
	 * @access		public
61
	 *
62
	 * @param		string $args        Command Data.
63
	 * @param		array  $assoc_args  List of command data.
64
	 *
65
	 * @return		void
66
	 *
67
	 * @subcommand  details
68
	 */
69
	public function details( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
Unused Code introduced by
The parameter $assoc_args 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...
70
71
		/**
72
		 * Plugin Information
73
		 */
74
		WP_CLI::log( $this->color_message( __( 'Give Version: ', 'give' ) ) . GIVE_VERSION );
75
76
		/**
77
		 * General Information.
78
		 */
79
		WP_CLI::log( "\n####   " . $this->color_message( __( 'General information', 'give' ) ) . '   ####' );
80
81
		$success_page = give_get_option( 'success_page' );
82
		$failure_page = give_get_option( 'failure_page' );
83
		$history_page = give_get_option( 'history_page' );
84
85
		WP_CLI::log( $this->color_message( sprintf( __( 'Success Page: ', 'give' ) ) ) . ( $success_page ? "[{$success_page}] " . get_permalink( $success_page ) : __( 'Not Set', 'give' ) ) );
86
		WP_CLI::log( $this->color_message( __( 'Failed Donation Page: ', 'give' ) ) . ( $failure_page ? "[{$failure_page}] " . get_permalink( $failure_page ) : __( 'Not Set', 'give' ) ) );
87
		WP_CLI::log( $this->color_message( __( 'Donation History Page: ', 'give' ) ) . ( $history_page ? "[{$history_page}] " . get_permalink( $history_page ) : __( 'Not Set', 'give' ) ) );
88
		WP_CLI::log( $this->color_message( __( 'Country: ', 'give' ) ) . give_get_country() );
89
90
		/**
91
		 * Currency Information.
92
		 */
93
		$default_gateway = give_get_option( 'default_gateway' );
94
95
		WP_CLI::log( "\n####   " . $this->color_message( __( 'Currency Information', 'give' ) ) . '   ####' );
96
97
		WP_CLI::log( $this->color_message( __( 'Currency: ', 'give' ), give_get_currency() ) );
98
		WP_CLI::log( $this->color_message( __( 'Currency Position: ', 'give' ), give_get_currency_position() ) );
99
		WP_CLI::log( $this->color_message( __( 'Thousand Separator: ', 'give' ), give_get_price_thousand_separator() ) );
100
		WP_CLI::log( $this->color_message( __( 'Decimal Separator: ', 'give' ), give_get_price_decimal_separator() ) );
101
		WP_CLI::log( $this->color_message( __( 'Number of Decimals: ', 'give' ), give_get_price_decimals() ) );
102
		WP_CLI::log( $this->color_message( __( 'Test Mode: ', 'give' ), ( give_get_option( 'test_mode' ) ? __( 'Yes', 'give' ) : __( 'No', 'give' )  ) ) );
103
		WP_CLI::log( $this->color_message( __( 'Default Gateway: ', 'give' ), ( $default_gateway ? $default_gateway : __( 'Not Set', 'give' )  ) ) );
104
105
		// Payment gateways Information.
106
		$gateways = give_get_ordered_payment_gateways( give_get_payment_gateways() );
107
		WP_CLI::log( $this->color_message( __( 'Enabled Gateways: ', 'give' ) ) );
108
109
		if ( ! empty( $gateways ) ) {
110
			self::$counter = 1;
111
			foreach ( $gateways as $gateway ) {
112
				WP_CLI::log( '  ' . $this->color_message( self::$counter, $gateway['admin_label'] ) );
113
				self::$counter++;
114
			}
115
		} else {
116
			WP_CLI::log( __( 'Not any payment gateways found', 'give' ) );
117
		}
118
	}
119
120
121
	/**
122
	 * Get the forms currently posted on your Give site
123
	 *
124
	 * ## OPTIONS
125
	 *
126
	 * [--id=<form_id>]
127
	 * : A specific form ID to retrieve
128
	 *
129
	 * [--number=<form_count>]
130
	 * : Number of form to retrieve
131
	 *
132
	 * ## EXAMPLES
133
	 *
134
	 * wp give forms
135
	 * wp give forms --id=103
136
	 * wp give forms --number=103
137
	 *
138
	 * @since		1.7
139
	 * @access		public
140
	 *
141
	 * @param		string $args        Command Data.
142
	 * @param		array  $assoc_args  List of command data.
143
	 *
144
	 * @return		void
145
	 *
146
	 * @subcommand	forms
147
	 */
148
	public function forms( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
149
		global $wp_query;
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...
150
		$form_id = isset( $assoc_args ) && array_key_exists( 'id', $assoc_args )     ? absint( $assoc_args['id'] )     : false;
151
		$number  = isset( $assoc_args ) && array_key_exists( 'number', $assoc_args ) ? absint( $assoc_args['number'] ) : 10;
152
		$start   = time();
153
154
		// Cache previous number query var.
155
		$is_set_number = $cache_per_page = false;
156
		if ( isset( $wp_query->query_vars['number'] ) ) {
157
			$cache_per_page = $wp_query->query_vars['number'];
158
			$is_set_number = true;
159
		}
160
161
		// Change number query var.
162
		$wp_query->query_vars['number'] = $number;
163
164
		// Get forms.
165
		$forms = $form_id ? $this->api->get_forms( $form_id ) : $this->api->get_forms();
166
167
		// Reset number query var.
168
		if ( $is_set_number ) {
169
			$wp_query->query_vars['number'] = $cache_per_page;
170
		}
171
172
		// Bailout.
173
		if ( array_key_exists( 'error', $forms ) ) {
174
175
			WP_CLI::warning( $forms['error'] );
176
			return;
177
		} elseif ( empty( $forms['forms'] ) ) {
178
179
			WP_CLI::error( __( 'No form found.', 'give' ) );
180
			return;
181
		}
182
183
		// Param to check if form typeis already showed or not.
184
		$is_show_form_type = false;
185
186
		if ( 1 === count( $forms ) && $form_id ) {
187
			// Show single form.
188
			foreach ( $forms['forms'][0] as $key => $info ) {
189
				switch ( $key ) {
190
					case 'stats':
191
						$this->color_main_heading( ucfirst( $key ) );
192
193
						foreach ( $info as $heading => $data ) {
194
							$this->color_sub_heading( ucfirst( $heading ) );
195
							switch ( $heading ) {
196
								default:
0 ignored issues
show
Unused Code introduced by
default: foreach ($d...data)); } } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
197
									foreach ( $data as $subheading => $subdata ) {
198
199
										switch ( $subheading ) {
200
											case 'earnings':
201
												WP_CLI::log( $this->color_message( $subheading . ': ', give_currency_filter( $subdata ) ) );
202
												break;
203
											default:
204
												WP_CLI::log( $this->color_message( $subheading . ': ', $subdata ) );
205
										}
206
									}
207
							}
208
						}
209
						break;
210
211
					case 'pricing':
212
					case 'info':
213
					default:
214
						$this->color_main_heading( ucfirst( $key ) );
215
216
						// Show form type.
217
						if ( ! $is_show_form_type ) {
218
							$form = new Give_Donate_Form( $form_id );
219
							$is_show_form_type = true;
220
221
							WP_CLI::log( $this->color_message( __( 'form type', 'give' ), $form->get_type() ) );
222
						}
223
224
						foreach ( $info as $heading => $data ) {
225
226
							switch ( $heading ) {
227
								case 'id':
228
									WP_CLI::log( $this->color_message( $heading, $data ) );
229
									break;
230
231
								default:
232
									$data = empty( $data ) ? __( 'Not set', 'give' ) : $data;
233
									WP_CLI::log( $this->color_message( $heading, $data ) );
234
							}
235
						}
236
				}
237
			}
238
		} else {
239
			// Show multiple form.
240
			$table_data = array();
241
			$is_table_first_row_set = false;
242
			$table_column_count = 0;
243
244
			WP_CLI::line( $this->color_message( sprintf( __( '%1$d donation forms found', 'give' ), count( $forms['forms'] ) ), '', false ) );
245
246
			foreach ( $forms['forms'] as $index => $form_data ) {
247
248
				// Default table data.
249
				$table_first_row = array();
250
				$table_row = array();
251
252
				foreach ( $form_data['info'] as $key => $form ) {
253
254
					// Do not show thumbnail, content and link in table.
255
					if ( in_array( $key, array( 'content', 'thumbnail', 'link' ), true ) ) {
256
						continue;
257
					}
258
259
					if ( ! $is_table_first_row_set ) {
260
						$table_first_row[] = $key;
261
					}
262
263
					$table_row[] = $form;
264
265
					if ( 'status' === $key ) {
266
						// First array item will be an form id in our case.
267
						$form = new Give_Donate_Form( absint( $table_row[0] ) );
268
269
						$table_row[] = $form->get_type();
270
					}
271
				}
272
273
				// Set table first row.
274
				if ( ! $is_table_first_row_set ) {
275
276
					// Add extra column to table.
277
					$table_first_row[]      = 'type';
278
279
					$table_data[]           = $table_first_row;
280
					$is_table_first_row_set = true;
281
				}
282
283
				// set table data.
284
				$table_data[] = $table_row;
285
			}
286
287
			$this->display_table( $table_data );
288
		}
289
	}
290
291
292
	/**
293
	 * Get the donors currently on your Give site. Can also be used to create donors records
294
	 *
295
	 * ## OPTIONS
296
	 *
297
	 * [--id=<donor_id>]
298
	 * : A specific donor ID to retrieve
299
	 *
300
	 * [--email=<donor_email>]
301
	 * : The email address of the donor to retrieve
302
	 *
303
	 * [--number=<donor_count>]
304
	 * : The number of donor to retrieve
305
	 *
306
	 * [--create=<number>]
307
	 * : The number of arbitrary donors to create. Leave as 1 or blank to create a donor with a specific email
308
	 *
309
	 * ## EXAMPLES
310
	 *
311
	 * wp give donors --id=103
312
	 * wp give donors --id=103 --number=100
313
	 * wp give donors [email protected]
314
	 * wp give donors --create=1 [email protected]
315
	 * wp give donors --create=1 [email protected] --name="John Doe"
316
	 * wp give donors --create=1000
317
	 *
318
	 * @since		1.7
319
	 * @access		public
320
	 *
321
	 * @param		string $args        Command Data.
322
	 * @param		array  $assoc_args  List of command data.
323
	 *
324
	 * @return		void
325
	 *
326
	 * @subcommand	donors
327
	 */
328
	public function donors( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
329
	    global $wp_query;
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...
330
		$donor_id    = isset( $assoc_args ) && array_key_exists( 'id', $assoc_args )      ? absint( $assoc_args['id'] ) : false;
331
		$email       = isset( $assoc_args ) && array_key_exists( 'email', $assoc_args )   ? $assoc_args['email']        : false;
332
		$name        = isset( $assoc_args ) && array_key_exists( 'name', $assoc_args )    ? $assoc_args['name']         : '';
333
		$create      = isset( $assoc_args ) && array_key_exists( 'create', $assoc_args )  ? $assoc_args['create']       : false;
334
		$number      = isset( $assoc_args ) && array_key_exists( 'number', $assoc_args )  ? $assoc_args['number']       : 10;
335
		$start       = time();
336
337
		if ( $create ) {
338
			$number = 1;
339
340
			// Create one or more donors.
341
			if ( ! $email ) {
342
				// If no email is specified, look to see if we are generating arbitrary donor accounts.
343
				$number = is_numeric( $create ) ? absint( $create ) : 1;
344
			}
345
346
			for ( $i = 0; $i < $number; $i++ ) {
347
				if ( ! $email ) {
348
349
					// Generate fake email.
350
					$email = 'customer-' . uniqid() . '@test.com';
351
				}
352
353
				$args = array(
354
					'email'   => $email,
355
					'name'    => $name,
356
				);
357
358
				$customer_id = Give()->customers->add( $args );
359
360
				if ( $customer_id ) {
361
					WP_CLI::line( $this->color_message( sprintf( __( 'Donor %d created successfully', 'give' ), $customer_id ) ) );
362
				} else {
363
					WP_CLI::error( __( 'Failed to create donor', 'give' ) );
364
				}
365
366
				// Reset email to false so it is generated on the next loop (if creating donors).
367
				$email = false;
368
			}
369
370
			WP_CLI::line( $this->color_message( sprintf( __( '%1$d donors created in %2$d seconds', 'give' ), $number, time() - $start ) ) );
371
372
		} else {
373
			// Counter.
374
			self::$counter = 1;
375
376
			// Search for customers.
377
			$search    = $donor_id ? $donor_id : $email;
378
379
			/**
380
			 * Get donors.
381
			 */
382
			// Cache previous number query var.
383
			$is_set_number = $cache_per_page = false;
384
			if ( isset( $wp_query->query_vars['number'] ) ) {
385
				$cache_per_page = $wp_query->query_vars['number'];
386
				$is_set_number = true;
387
			}
388
389
			// Change number query var.
390
			$wp_query->query_vars['number'] = $number;
391
392
			// Get donors.
393
			$donors = $this->api->get_customers( $search );
394
395
			// Reset number query var.
396
			if ( $is_set_number ) {
397
				$wp_query->query_vars['number'] = $cache_per_page;
398
			}
399
400
			if ( isset( $donors['error'] ) ) {
401
				WP_CLI::error( $donors['error'] );
402
			}
403
404
			if ( empty( $donors ) ) {
405
				WP_CLI::error( __( 'No donors found', 'give' ) );
406
				return;
407
			}
408
409
			$table_data = array();
410
			$is_table_first_row_set = false;
411
412
			foreach ( $donors['donors'] as $donor_data ) {
413
				// Set default table row data.
414
				$table_first_row = array( __( 'S. No.', 'give' ) );
415
				$table_row = array( self::$counter );
416
417
				foreach ( $donor_data as $key => $donor ) {
418
					switch ( $key ) {
419
						case 'stats':
420
							foreach ( $donor as $heading => $data ) {
421
422
								// Get first row.
423
								if ( ! $is_table_first_row_set ) {
424
									$table_first_row[] = $heading;
425
								}
426
427
								switch ( $heading ) {
428
									case 'total_spent':
429
										$table_row[] = give_currency_filter( $data );
430
										break;
431
432
									default:
433
										$table_row[] = $data;
434
								}
435
							}
436
							break;
437
438
						case 'info':
439
						default:
440
							foreach ( $donor as $heading => $data ) {
441
442
								// Get first row.
443
								if ( ! $is_table_first_row_set ) {
444
									$table_first_row[] = $heading;
445
								}
446
447
								$table_row[] = $data;
448
							}
449
					}
450
				}
451
452
				// Add first row data to table data.
453
				if ( ! $is_table_first_row_set ) {
454
					$table_data[] = $table_first_row;
455
					$is_table_first_row_set = true;
456
				}
457
458
				// Add table row data.
459
				$table_data[] = $table_row;
460
461
				// Increase counter.
462
				self::$counter++;
463
			}
464
465
			$this->display_table( $table_data );
466
		}
467
	}
468
469
470
	/**
471
	 * Get the recent donations for your Give site
472
	 *
473
	 * ## OPTIONS
474
	 *
475
	 * [--number=<donation_count>]
476
	 * : The number of donations to retrieve
477
	 *
478
	 *
479
	 * ## EXAMPLES
480
	 *
481
	 * wp give donations
482
	 * wp give donations --number=100
483
	 *
484
	 * @since		1.7
485
	 * @access		public
486
	 *
487
	 * @param		string $args        Command Data.
488
	 * @param		array  $assoc_args  List of command data.
489
	 *
490
	 * @return		void
491
	 *
492
	 * @subcommand	donations
493
	 */
494
	public function donations( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
495
	    global $wp_query;
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...
496
		$number = isset( $assoc_args ) && array_key_exists( 'number', $assoc_args ) ? $assoc_args['number'] : 10;
497
498
		// Cache previous number query var.
499
		$is_set_number = $cache_per_page = false;
500
		if ( isset( $wp_query->query_vars['number'] ) ) {
501
			$cache_per_page = $wp_query->query_vars['number'];
502
			$is_set_number = true;
503
		}
504
505
		// Change number query var.
506
		$wp_query->query_vars['number'] = $number;
507
508
		// Get donations.
509
		$donations = $this->api->get_recent_donations();
510
511
		// Reset number query var.
512
		if ( $is_set_number ) {
513
			$wp_query->query_vars['number'] = $cache_per_page;
514
		}
515
516
		if ( empty( $donations ) ) {
517
			WP_CLI::error( __( 'No sales found', 'give' ) );
518
			return;
519
		}
520
521
		self::$counter = 1;
522
523
		foreach ( $donations['donations'] as $key => $donation ) {
524
			$this->color_main_heading( sprintf( __( '%1$s. Payment #%2$s', 'give' ), self::$counter, $donation['ID'] ), 'Y' );
525
			self::$counter++;
526
527
			foreach ( $donation as $column => $data ) {
528
529
				if ( is_array( $data ) ) {
530
					$this->color_sub_heading( $column );
531
					foreach ( $data as $subcolumn => $subdata ) {
532
533
						// Decode html codes.
534
						switch ( $subcolumn ) {
535
							case 'name':
536
								$subdata = html_entity_decode( $subdata );
537
								break;
538
						}
539
540
						// @TODO Check if multi dimension array information is importent to show or not. For example inside donation array we have array for fees data inside payment meta.
541
						if ( is_array( $subdata ) ) {
542
							continue;
543
						}
544
545
						WP_CLI::log( $this->color_message( $subcolumn, $subdata ) );
546
					}
547
					continue;
548
				}
549
550
				WP_CLI::log( $this->color_message( $column, $data ) );
551
			}
552
		}
553
	}
554
555
	/**
556
	 * Get give plugin report.
557
	 *
558
	 * ## OPTIONS
559
	 *
560
	 * [--id=<donation_form_id>]
561
	 * : The ID of a specific donation_form to retrieve stats for, or all
562
	 *
563
	 * [--date=<range|this_month|last_month|today|yesterday|this_quarter|last_quarter|this_year|last_year>]
564
	 * : A specific date range to retrieve stats for
565
	 *
566
	 * [--start-date=<date>]
567
	 * : The start date of a date range to retrieve stats for
568
	 *
569
	 * [--end-date=<date>]
570
	 * : The end date of a date range to retrieve stats for
571
	 *
572
	 * ## EXAMPLES
573
	 *
574
	 * wp give report --date=this_month
575
	 * wp give report --start-date=01/02/2014 --end-date=02/23/2014
576
	 * wp give report --date=last_year
577
	 * wp give report --date=last_year --id=15
578
	 *
579
	 * @since		1.7
580
	 * @access		public
581
	 *
582
	 * @param		string $args        Command Data.
583
	 * @param		array  $assoc_args  List of command data.
584
	 *
585
	 * @subcommand	report
586
	 *
587
	 * @return		void
588
	 */
589
	public function report( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
590
		$stats      = new Give_Payment_Stats();
591
		$date       = isset( $assoc_args ) && array_key_exists( 'date', $assoc_args )      ? $assoc_args['date']      : false;
592
		$start_date = isset( $assoc_args ) && array_key_exists( 'start-date', $assoc_args ) ? $assoc_args['start-date'] : false;
593
		$end_date   = isset( $assoc_args ) && array_key_exists( 'end-date', $assoc_args )   ? $assoc_args['end-date']   : false;
594
		$form_id    = isset( $assoc_args ) && array_key_exists( 'id', $assoc_args )        ? $assoc_args['id']        : 0;
595
596
		if ( ! empty( $date ) ) {
597
			$start_date = $date;
598
			$end_date   = false;
599
		} elseif ( empty( $date ) && empty( $start_date ) ) {
600
			$start_date = 'this_month';
601
			$end_date   = false;
602
		}
603
604
		// Get stats.
605
		$earnings   = $stats->get_earnings( $form_id, $start_date, $end_date );
606
		$sales      = $stats->get_sales( $form_id, $start_date, $end_date );
607
608
		WP_CLI::line( $this->color_message( __( 'Earnings', 'give' ), give_currency_filter( $earnings ) ) );
609
		WP_CLI::line( $this->color_message( __( 'Sales', 'give' ), $sales ) );
610
	}
611
612
613
	/**
614
	 * Delete cache (transient).
615
	 *
616
	 * ## OPTIONS
617
	 *
618
	 * [--action=<cache_action>]
619
	 * : Value of this parameter can be delete (in case you want to delete all stat cache).
620
	 *
621
	 * ## EXAMPLES
622
	 *
623
	 *	# See form report
624
	 *	wp give cache --action=delete
625
	 *
626
	 * @since		1.7
627
	 * @access		public
628
	 *
629
	 * @param		string $args        Command Data.
630
	 * @param		array  $assoc_args  List of command data.
631
	 *
632
	 * @return		void
633
	 *
634
	 * @subcommand cache
635
	 */
636
	public function cache( $args, $assoc_args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
637
		$action = isset( $assoc_args ) && array_key_exists( 'action', $assoc_args ) ? $assoc_args['action'] : false;
638
639
		// Bailout.
640
		if ( ! $action || ! in_array( $action, array( 'delete' ), true ) ) {
641
			WP_CLI::warning( __( 'Type wp give cache --action=delete to delete all stat transients', 'give' ) );
642
			return;
643
		}
644
645
		switch ( $action ) {
646
			case 'delete' :
647
				// Reset counter.
648
				self::$counter = 1;
649
650
				if ( $this->delete_stats_transients() ) {
651
					// Report .eading.
652
					WP_CLI::success( 'All form stat transient cache deleted.' );
653
				} else {
654
					// Report .eading.
655
					WP_CLI::warning( 'We did not find any transient to delete :)' );
656
				}
657
				break;
658
		}
659
660
	}
661
662
	/**
663
	 * Delete all form stat transient
664
	 *
665
	 * @since	1.7
666
	 * @access	private
667
	 *
668
	 * @return	bool
669
	 */
670
	private function delete_stats_transients() {
671
		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...
672
673
		$stat_option_names = $wpdb->get_results(
674
			$wpdb->prepare(
675
				"SELECT option_name FROM {$wpdb->options} where option_name LIKE '%%%s%%'",
676
				'_transient_give_stats_'
677
			),
678
			ARRAY_A
679
		);
680
681
		if ( ! empty( $stat_option_names ) ) {
682
683
			// Convert transient option name to transient name.
684
			$stat_option_names = array_map(
685
				function( $option ) {
686
					return str_replace( '_transient_', '', $option['option_name'] );
687
				},
688
				$stat_option_names
689
			);
690
691
			foreach ( $stat_option_names as $option_name ) {
692
				if ( delete_transient( $option_name ) ) {
693
694
					WP_CLI::log( $this->color_message( self::$counter, $option_name ) );
695
					self::$counter++;
696
				} else {
697
					WP_CLI::log( $this->color_message( __( 'Error while deleting this transient', 'give' ), $option_name ) );
698
				}
699
			}
700
701
			return true;
702
		}
703
704
		return false;
705
	}
706
707
708
	/**
709
	 * Return colored message
710
	 *
711
	 * @param	string $heading Message heading.
712
	 * @param	string $message Message content.
713
	 * @param	bool   $colon   Check if add colon between heading and message.
714
	 * @param	string $color   Heading color.
715
	 *
716
	 * @return	mixed
717
	 */
718
	private function color_message( $heading, $message = '', $colon = true, $color = 'g' ) {
719
	    // Add colon.
720
	    if ( $colon ) {
721
	        $heading = $heading . ': ';
722
		}
723
		return WP_CLI::colorize( "%{$color}" . $heading . '%n' ) . $message;
724
	}
725
726
727
	/**
728
	 * Output section heading.
729
	 *
730
	 * @since	1.7
731
	 * @access	private
732
	 *
733
	 * @param	string $heading Heading.
734
	 * @param	string $color   Color.
735
	 *
736
	 * @return	void
737
	 */
738
	private function color_main_heading( $heading, $color = 'g' ) {
739
		WP_CLI::log( "\n######   " . $this->color_message( $heading, '', false, $color ) . '   ######' );
740
	}
741
742
	/**
743
	 * Output section sub heading.
744
	 *
745
	 * @since	1.7
746
	 * @access	private
747
	 *
748
	 * @param	string $subheading Sub heading.
749
	 *
750
	 * @return	void
751
	 */
752
	private function color_sub_heading( $subheading ) {
753
		WP_CLI::log( "\n--->" . $subheading . '', '', false );
754
	}
755
756
757
	/**
758
	 * Display data in table format.
759
	 *
760
	 * @since	1.7
761
	 * @access	private
762
	 *
763
	 * @param	array $data Array of table data.
764
	 *
765
	 * @return	void
766
	 */
767
	private function display_table( $data ) {
768
		$table = new \cli\Table();
769
770
		// Set table header.
771
		$table->setHeaders( $data[0] );
772
773
		// Remove table header.
774
		unset( $data[0] );
775
776
		// Set table data.
777
		$table->setRows( $data );
778
779
		// Display table.
780
		$table->display();
781
	}
782
}
783