Completed
Pull Request — master (#1201)
by Ravinder
23:20
created

GIVE_CLI_COMMAND::report()   F

Complexity

Conditions 12

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 16
nop 2
dl 0
loc 22
rs 3.5128
c 0
b 0
f 0

How to fix   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 25 and the first side effect is on line 11.

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' ) ) {
11
	exit;
12
}
13
14
// Add give command.
15
WP_CLI::add_command( 'give', 'GIVE_CLI_COMMAND' );
16
17
18
/**
19
 * Work with Give through WP-CLI
20
 *
21
 * Adds CLI support to Give through WP-CLI
22
 *
23
 * @since 1.7
24
 */
25
class GIVE_CLI_COMMAND {
26
27
	/**
28
	 * This param uses to count process/step inside loop.
29
	 *
30
	 * @var int $counter Counter.
31
	 */
32
	private static $counter;
33
34
	/**
35
	 * This helps to get information give plugin data.
36
	 *
37
	 * @var Give_API Object.
38
	 */
39
	private $api;
40
41
42
	/**
43
	 * GIVE_CLI_Command constructor.
44
	 */
45
	public function __construct() {
46
		$this->api = new Give_API();
47
	}
48
49
50
	/**
51
	 * Get Give details
52
	 *
53
	 * ## OPTIONS
54
	 *
55
	 * None. Returns basic info regarding your Give instance.
56
	 *
57
	 * ## EXAMPLES
58
	 *
59
	 * wp give details
60
	 *
61
	 * @since         1.7
62
	 * @access        public
63
	 *
64
	 * @param        string $args       Command Data.
65
	 * @param        array  $assoc_args List of command data.
66
	 *
67
	 * @return        void
68
	 *
69
	 * @subcommand    details
70
	 */
71
	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...
72
73
		/**
74
		 * Plugin Information
75
		 */
76
		WP_CLI::log( $this->color_message( __( 'Give Version: ', 'give' ) ) . GIVE_VERSION );
77
78
		/**
79
		 * General Information.
80
		 */
81
		WP_CLI::log( "\n####   " . $this->color_message( __( 'General information', 'give' ) ) . '   ####' );
82
83
		$success_page = give_get_option( 'success_page' );
84
		$failure_page = give_get_option( 'failure_page' );
85
		$history_page = give_get_option( 'history_page' );
86
87
		WP_CLI::log( $this->color_message( sprintf( __( 'Success Page: ', 'give' ) ) ) . ( $success_page ? "[{$success_page}] " . get_permalink( $success_page ) : __( 'Not Set', 'give' ) ) );
88
		WP_CLI::log( $this->color_message( __( 'Failed Donation Page: ', 'give' ) ) . ( $failure_page ? "[{$failure_page}] " . get_permalink( $failure_page ) : __( 'Not Set', 'give' ) ) );
89
		WP_CLI::log( $this->color_message( __( 'Donation History Page: ', 'give' ) ) . ( $history_page ? "[{$history_page}] " . get_permalink( $history_page ) : __( 'Not Set', 'give' ) ) );
90
		WP_CLI::log( $this->color_message( __( 'Country: ', 'give' ) ) . give_get_country() );
91
92
		/**
93
		 * Currency Information.
94
		 */
95
		$default_gateway = give_get_option( 'default_gateway' );
96
97
		WP_CLI::log( "\n####   " . $this->color_message( __( 'Currency Information', 'give' ) ) . '   ####' );
98
99
		WP_CLI::log( $this->color_message( __( 'Currency: ', 'give' ), give_get_currency() ) );
100
		WP_CLI::log( $this->color_message( __( 'Currency Position: ', 'give' ), give_get_currency_position() ) );
101
		WP_CLI::log( $this->color_message( __( 'Thousand Separator: ', 'give' ), give_get_price_thousand_separator() ) );
102
		WP_CLI::log( $this->color_message( __( 'Decimal Separator: ', 'give' ), give_get_price_decimal_separator() ) );
103
		WP_CLI::log( $this->color_message( __( 'Number of Decimals: ', 'give' ), give_get_price_decimals() ) );
104
		WP_CLI::log( $this->color_message( __( 'Test Mode: ', 'give' ), ( give_get_option( 'test_mode' ) ? __( 'Yes', 'give' ) : __( 'No', 'give' ) ) ) );
105
		WP_CLI::log( $this->color_message( __( 'Default Gateway: ', 'give' ), ( $default_gateway ? $default_gateway : __( 'Not Set', 'give' ) ) ) );
106
107
		// Payment gateways Information.
108
		$gateways = give_get_ordered_payment_gateways( give_get_payment_gateways() );
109
		WP_CLI::log( $this->color_message( __( 'Enabled Gateways: ', 'give' ) ) );
110
111
		if ( ! empty( $gateways ) ) {
112
			self::$counter = 1;
113
			foreach ( $gateways as $gateway ) {
114
				WP_CLI::log( '  ' . $this->color_message( self::$counter, $gateway['admin_label'] ) );
115
				self::$counter ++;
116
			}
117
		} else {
118
			WP_CLI::log( __( 'Not any payment gateways found', 'give' ) );
119
		}
120
	}
121
122
123
	/**
124
	 * Get the forms currently posted on your Give site
125
	 *
126
	 * ## OPTIONS
127
	 *
128
	 * [--id=<form_id>]
129
	 * : A specific form ID to retrieve
130
	 *
131
	 * [--number=<form_count>]
132
	 * : Number of form to retrieve
133
	 *
134
	 * ## EXAMPLES
135
	 *
136
	 * wp give forms
137
	 * wp give forms --id=103
138
	 * wp give forms --number=103
139
	 *
140
	 * @since         1.7
141
	 * @access        public
142
	 *
143
	 * @param        string $args       Command Data.
144
	 * @param        array  $assoc_args List of command data.
145
	 *
146
	 * @return        void
147
	 *
148
	 * @subcommand    forms
149
	 */
150
	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...
151
		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...
152
		$form_id = isset( $assoc_args ) && array_key_exists( 'id', $assoc_args ) ? absint( $assoc_args['id'] ) : false;
153
		$number  = isset( $assoc_args ) && array_key_exists( 'number', $assoc_args ) ? absint( $assoc_args['number'] ) : 10;
154
		$start   = time();
155
156
		// Cache previous number query var.
157
		$is_set_number = $cache_per_page = false;
158
		if ( isset( $wp_query->query_vars['number'] ) ) {
159
			$cache_per_page = $wp_query->query_vars['number'];
160
			$is_set_number  = true;
161
		}
162
163
		// Change number query var.
164
		$wp_query->query_vars['number'] = $number;
165
166
		// Get forms.
167
		$forms = $form_id ? $this->api->get_forms( $form_id ) : $this->api->get_forms();
168
169
		// Reset number query var.
170
		if ( $is_set_number ) {
171
			$wp_query->query_vars['number'] = $cache_per_page;
172
		}
173
174
		// Bailout.
175
		if ( array_key_exists( 'error', $forms ) ) {
176
177
			WP_CLI::warning( $forms['error'] );
178
179
			return;
180
		} elseif ( empty( $forms['forms'] ) ) {
181
182
			WP_CLI::error( __( 'No forms found.', 'give' ) );
183
184
			return;
185
		}
186
187
		// Param to check if form typeis already showed or not.
188
		$is_show_form_type = false;
189
190
		if ( 1 === count( $forms ) && $form_id ) {
191
			// Show single form.
192
			foreach ( $forms['forms'][0] as $key => $info ) {
193
				switch ( $key ) {
194
					case 'stats':
195
						$this->color_main_heading( ucfirst( $key ) );
196
197
						foreach ( $info as $heading => $data ) {
198
							$this->color_sub_heading( ucfirst( $heading ) );
199
							switch ( $heading ) {
200
								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...
201
									foreach ( $data as $subheading => $subdata ) {
202
203
										switch ( $subheading ) {
204
											case 'earnings':
205
												WP_CLI::log( $this->color_message( $subheading . ': ', give_currency_filter( $subdata ) ) );
206
												break;
207
											default:
208
												WP_CLI::log( $this->color_message( $subheading . ': ', $subdata ) );
209
										}
210
									}
211
							}
212
						}
213
						break;
214
215
					case 'pricing':
216
					case 'info':
217
					default:
218
						$this->color_main_heading( ucfirst( $key ) );
219
220
						// Show form type.
221
						if ( ! $is_show_form_type ) {
222
							$form              = new Give_Donate_Form( $form_id );
223
							$is_show_form_type = true;
224
225
							WP_CLI::log( $this->color_message( __( 'form type', 'give' ), $form->get_type() ) );
226
						}
227
228
						foreach ( $info as $heading => $data ) {
229
230
							switch ( $heading ) {
231
								case 'id':
232
									WP_CLI::log( $this->color_message( $heading, $data ) );
233
									break;
234
235
								default:
236
									$data = empty( $data ) ? __( 'Not set', 'give' ) : $data;
237
									WP_CLI::log( $this->color_message( $heading, $data ) );
238
							}
239
						}
240
				}
241
			}
242
		} else {
243
			// Show multiple form.
244
			$table_data             = array();
245
			$is_table_first_row_set = false;
246
			$table_column_count     = 0;
247
248
			WP_CLI::line( $this->color_message( sprintf( __( '%d donation forms found', 'give' ), count( $forms['forms'] ) ), '', false ) );
249
250
			foreach ( $forms['forms'] as $index => $form_data ) {
251
252
				// Default table data.
253
				$table_first_row = array();
254
				$table_row       = array();
255
256
				foreach ( $form_data['info'] as $key => $form ) {
257
258
					// Do not show thumbnail, content and link in table.
259
					if ( in_array( $key, array( 'content', 'thumbnail', 'link' ), true ) ) {
260
						continue;
261
					}
262
263
					if ( ! $is_table_first_row_set ) {
264
						$table_first_row[] = $key;
265
					}
266
267
					$table_row[] = $form;
268
269
					if ( 'status' === $key ) {
270
						// First array item will be an form id in our case.
271
						$form = new Give_Donate_Form( absint( $table_row[0] ) );
272
273
						$table_row[] = $form->get_type();
274
					}
275
				}
276
277
				// Set table first row.
278
				if ( ! $is_table_first_row_set ) {
279
280
					// Add extra column to table.
281
					$table_first_row[] = 'type';
282
283
					$table_data[]           = $table_first_row;
284
					$is_table_first_row_set = true;
285
				}
286
287
				// set table data.
288
				$table_data[] = $table_row;
289
			}
290
291
			$this->display_table( $table_data );
292
		}
293
	}
294
295
296
	/**
297
	 * Get the donors currently on your Give site. Can also be used to create donors records
298
	 *
299
	 * ## OPTIONS
300
	 *
301
	 * [--id=<donor_id>]
302
	 * : A specific donor ID to retrieve
303
	 *
304
	 * [--email=<donor_email>]
305
	 * : The email address of the donor to retrieve
306
	 *
307
	 * [--number=<donor_count>]
308
	 * : The number of donor to retrieve
309
	 *
310
	 * [--create=<number>]
311
	 * : The number of arbitrary donors to create. Leave as 1 or blank to create a donor with a specific email
312
	 *
313
	 * [--form-id=<donation_form_id>]
314
	 * : Get list of donors of specific donation form
315
	 *
316
	 * ## EXAMPLES
317
	 *
318
	 * wp give donors --id=103
319
	 * wp give donors --id=103 --number=100
320
	 * wp give donors [email protected]
321
	 * wp give donors --create=1 [email protected]
322
	 * wp give donors --create=1 [email protected] --name="John Doe"
323
	 * wp give donors --create=1000
324
	 * wp give donors --number=1000
325
	 * wp give donors --form-id=1024
326
	 *
327
	 * @since         1.7
328
	 * @access        public
329
	 *
330
	 * @param        string $args       Command Data.
331
	 * @param        array  $assoc_args List of command data.
332
	 *
333
	 * @return        void
334
	 *
335
	 * @subcommand    donors
336
	 */
337
	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...
338
		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...
339
		$donor_id = isset( $assoc_args ) && array_key_exists( 'id', $assoc_args ) ? absint( $assoc_args['id'] ) : false;
340
		$email    = isset( $assoc_args ) && array_key_exists( 'email', $assoc_args ) ? $assoc_args['email'] : false;
341
		$name     = isset( $assoc_args ) && array_key_exists( 'name', $assoc_args ) ? $assoc_args['name'] : '';
342
		$create   = isset( $assoc_args ) && array_key_exists( 'create', $assoc_args ) ? $assoc_args['create'] : false;
343
		$number   = isset( $assoc_args ) && array_key_exists( 'number', $assoc_args ) ? $assoc_args['number'] : 10;
344
		$form_id  = isset( $assoc_args ) && array_key_exists( 'form-id', $assoc_args ) ? $assoc_args['form-id'] : 0;
345
		$start    = time();
346
347
		if ( $create ) {
348
			$number = 1;
349
350
			// Create one or more donors.
351
			if ( ! $email ) {
352
				// If no email is specified, look to see if we are generating arbitrary donor accounts.
353
				$number = is_numeric( $create ) ? absint( $create ) : 1;
354
			}
355
356
			for ( $i = 0; $i < $number; $i ++ ) {
357
				if ( ! $email ) {
358
359
					// Generate fake email.
360
					$email = 'customer-' . uniqid() . '@test.com';
361
				}
362
363
				$args = array(
364
					'email' => $email,
365
					'name'  => $name,
366
				);
367
368
				$customer_id = Give()->customers->add( $args );
369
370
				if ( $customer_id ) {
371
					WP_CLI::line( $this->color_message( sprintf( __( 'Donor %d created successfully', 'give' ), $customer_id ) ) );
372
				} else {
373
					WP_CLI::error( __( 'Failed to create donor', 'give' ) );
374
				}
375
376
				// Reset email to false so it is generated on the next loop (if creating donors).
377
				$email = false;
378
			}
379
380
			WP_CLI::line( $this->color_message( sprintf( __( '%1$d donors created in %2$d seconds', 'give' ), $number, time() - $start ) ) );
381
382
		} else {
383
			// Counter.
384
			self::$counter = 1;
385
386
			// Search for customers.
387
			$search = $donor_id ? $donor_id : $email;
388
389
			/**
390
			 * Get donors.
391
			 */
392
			// Cache previous number query var.
393
			$is_set_number = $cache_per_page = false;
394
			if ( isset( $wp_query->query_vars['number'] ) ) {
395
				$cache_per_page = $wp_query->query_vars['number'];
396
				$is_set_number  = true;
397
			}
398
399
			// Change number query var.
400
			$wp_query->query_vars['number'] = $number;
401
402
			// Get donors.
403
			if ( $form_id ) {
404
				// @TODO: Allow user to get a list of donors by donation status.
405
				$donors = $this->get_donors_by_form_id( $form_id );
406
			} else {
407
				$donors = $this->api->get_customers( $search );
408
			}
409
410
			// Reset number query var.
411
			if ( $is_set_number ) {
412
				$wp_query->query_vars['number'] = $cache_per_page;
413
			}
414
415
			if ( isset( $donors['error'] ) ) {
416
				WP_CLI::error( $donors['error'] );
417
			}
418
419
			if ( empty( $donors ) ) {
420
				WP_CLI::error( __( 'No donors found.', 'give' ) );
421
422
				return;
423
			}
424
425
			$table_data             = array();
426
			$is_table_first_row_set = false;
427
428
			foreach ( $donors['donors'] as $donor_data ) {
429
				// Set default table row data.
430
				$table_first_row = array( __( 'S. No.', 'give' ) );
431
				$table_row       = array( self::$counter );
432
433
				foreach ( $donor_data as $key => $donor ) {
434
					switch ( $key ) {
435
						case 'stats':
436
							foreach ( $donor as $heading => $data ) {
437
438
								// Get first row.
439
								if ( ! $is_table_first_row_set ) {
440
									$table_first_row[] = $heading;
441
								}
442
443
								switch ( $heading ) {
444
									case 'total_spent':
445
										$table_row[] = give_currency_filter( $data );
446
										break;
447
448
									default:
449
										$table_row[] = $data;
450
								}
451
							}
452
							break;
453
454
						case 'info':
455
						default:
456
							foreach ( $donor as $heading => $data ) {
457
458
								// Get first row.
459
								if ( ! $is_table_first_row_set ) {
460
									$table_first_row[] = $heading;
461
								}
462
463
								$table_row[] = $data;
464
							}
465
					}
466
				}
467
468
				// Add first row data to table data.
469
				if ( ! $is_table_first_row_set ) {
470
					$table_data[]           = $table_first_row;
471
					$is_table_first_row_set = true;
472
				}
473
474
				// Add table row data.
475
				$table_data[] = $table_row;
476
477
				// Increase counter.
478
				self::$counter ++;
479
			}
480
481
			$this->display_table( $table_data );
482
		}
483
	}
484
485
486
	/**
487
	 * Get the recent donations for your Give site
488
	 *
489
	 * ## OPTIONS
490
	 *
491
	 * [--number=<donation_count>]
492
	 * : The number of donations to retrieve
493
	 *
494
	 *
495
	 * ## EXAMPLES
496
	 *
497
	 * wp give donations
498
	 * wp give donations --number=100
499
	 *
500
	 * @since         1.7
501
	 * @access        public
502
	 *
503
	 * @param        string $args       Command Data.
504
	 * @param        array  $assoc_args List of command data.
505
	 *
506
	 * @return        void
507
	 *
508
	 * @subcommand    donations
509
	 */
510
	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...
511
		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...
512
		$number = isset( $assoc_args ) && array_key_exists( 'number', $assoc_args ) ? $assoc_args['number'] : 10;
513
514
		// Cache previous number query var.
515
		$is_set_number = $cache_per_page = false;
516
		if ( isset( $wp_query->query_vars['number'] ) ) {
517
			$cache_per_page = $wp_query->query_vars['number'];
518
			$is_set_number  = true;
519
		}
520
521
		// Change number query var.
522
		$wp_query->query_vars['number'] = $number;
523
524
		// Get donations.
525
		$donations = $this->api->get_recent_donations();
526
527
		// Reset number query var.
528
		if ( $is_set_number ) {
529
			$wp_query->query_vars['number'] = $cache_per_page;
530
		}
531
532
		if ( empty( $donations ) ) {
533
			WP_CLI::error( __( 'No donations found.', 'give' ) );
534
535
			return;
536
		}
537
538
		self::$counter = 1;
539
540
		foreach ( $donations['donations'] as $key => $donation ) {
541
			$this->color_main_heading( sprintf( __( '%1$s. Donation #%2$s', 'give' ), self::$counter, $donation['ID'] ), 'Y' );
542
			self::$counter ++;
543
544
			foreach ( $donation as $column => $data ) {
545
546
				if ( is_array( $data ) ) {
547
					$this->color_sub_heading( $column );
548
					foreach ( $data as $subcolumn => $subdata ) {
549
550
						// Decode html codes.
551
						switch ( $subcolumn ) {
552
							case 'name':
553
								$subdata = html_entity_decode( $subdata );
554
								break;
555
						}
556
557
						// @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.
558
						if ( is_array( $subdata ) ) {
559
							continue;
560
						}
561
562
						WP_CLI::log( $this->color_message( $subcolumn, $subdata ) );
563
					}
564
					continue;
565
				}
566
567
				WP_CLI::log( $this->color_message( $column, $data ) );
568
			}
569
		}
570
	}
571
572
	/**
573
	 * Get give plugin report.
574
	 *
575
	 * ## OPTIONS
576
	 *
577
	 * [--id=<donation_form_id>]
578
	 * : The ID of a specific donation_form to retrieve stats for, or all
579
	 *
580
	 * [--date=<range|this_month|last_month|today|yesterday|this_quarter|last_quarter|this_year|last_year>]
581
	 * : A specific date range to retrieve stats for
582
	 *
583
	 * [--start-date=<date>]
584
	 * : The start date of a date range to retrieve stats for
585
	 *
586
	 * [--end-date=<date>]
587
	 * : The end date of a date range to retrieve stats for
588
	 *
589
	 * ## EXAMPLES
590
	 *
591
	 * wp give report --date=this_month
592
	 * wp give report --start-date=01/02/2014 --end-date=02/23/2014
593
	 * wp give report --date=last_year
594
	 * wp give report --date=last_year --id=15
595
	 *
596
	 * @since         1.7
597
	 * @access        public
598
	 *
599
	 * @param        string $args       Command Data.
600
	 * @param        array  $assoc_args List of command data.
601
	 *
602
	 * @subcommand    report
603
	 *
604
	 * @return        void
605
	 */
606
	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...
607
		$stats      = new Give_Payment_Stats();
608
		$date       = isset( $assoc_args ) && array_key_exists( 'date', $assoc_args ) ? $assoc_args['date'] : false;
609
		$start_date = isset( $assoc_args ) && array_key_exists( 'start-date', $assoc_args ) ? $assoc_args['start-date'] : false;
610
		$end_date   = isset( $assoc_args ) && array_key_exists( 'end-date', $assoc_args ) ? $assoc_args['end-date'] : false;
611
		$form_id    = isset( $assoc_args ) && array_key_exists( 'id', $assoc_args ) ? $assoc_args['id'] : 0;
612
613
		if ( ! empty( $date ) ) {
614
			$start_date = $date;
615
			$end_date   = false;
616
		} elseif ( empty( $date ) && empty( $start_date ) ) {
617
			$start_date = 'this_month';
618
			$end_date   = false;
619
		}
620
621
		// Get stats.
622
		$earnings = $stats->get_earnings( $form_id, $start_date, $end_date );
623
		$sales    = $stats->get_sales( $form_id, $start_date, $end_date );
624
625
		WP_CLI::line( $this->color_message( __( 'Earnings', 'give' ), give_currency_filter( $earnings ) ) );
626
		WP_CLI::line( $this->color_message( __( 'Sales', 'give' ), $sales ) );
627
	}
628
629
630
	/**
631
	 * Delete cache (transient).
632
	 *
633
	 * ## OPTIONS
634
	 *
635
	 * [--action=<cache_action>]
636
	 * : Value of this parameter can be delete (in case you want to delete all stat cache).
637
	 *
638
	 * ## EXAMPLES
639
	 *
640
	 *    # See form report
641
	 *    wp give cache --action=delete
642
	 *
643
	 * @since         1.7
644
	 * @access        public
645
	 *
646
	 * @param        string $args       Command Data.
647
	 * @param        array  $assoc_args List of command data.
648
	 *
649
	 * @return        void
650
	 *
651
	 * @subcommand    cache
652
	 */
653
	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...
654
		$action = isset( $assoc_args ) && array_key_exists( 'action', $assoc_args ) ? $assoc_args['action'] : false;
655
656
		// Bailout.
657
		if ( ! $action || ! in_array( $action, array( 'delete' ), true ) ) {
658
			WP_CLI::warning( __( 'Type wp give cache --action=delete to delete all stat transients', 'give' ) );
659
660
			return;
661
		}
662
663
		switch ( $action ) {
664
			case 'delete' :
665
				// Reset counter.
666
				self::$counter = 1;
667
668
				if ( $this->delete_stats_transients() ) {
669
					// Report .eading.
670
					WP_CLI::success( 'All form stat transient cache deleted.' );
671
				} else {
672
					// Report .eading.
673
					WP_CLI::warning( 'We did not find any transient to delete :)' );
674
				}
675
				break;
676
		}
677
678
	}
679
680
	/**
681
	 * Delete all form stat transient
682
	 *
683
	 * @since     1.7
684
	 * @access    private
685
	 *
686
	 * @return    bool
687
	 */
688
	private function delete_stats_transients() {
689
		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...
690
691
		$stat_option_names = $wpdb->get_results(
692
			$wpdb->prepare(
693
				"SELECT option_name FROM {$wpdb->options} where option_name LIKE '%%%s%%'",
694
				'_transient_give_stats_'
695
			),
696
			ARRAY_A
697
		);
698
699
		if ( ! empty( $stat_option_names ) ) {
700
701
			// Convert transient option name to transient name.
702
			$stat_option_names = array_map(
703
				function ( $option ) {
704
					return str_replace( '_transient_', '', $option['option_name'] );
705
				},
706
				$stat_option_names
707
			);
708
709
			foreach ( $stat_option_names as $option_name ) {
710
				if ( delete_transient( $option_name ) ) {
711
712
					WP_CLI::log( $this->color_message( self::$counter, $option_name ) );
713
					self::$counter ++;
714
				} else {
715
					WP_CLI::log( $this->color_message( __( 'Error while deleting this transient', 'give' ), $option_name ) );
716
				}
717
			}
718
719
			return true;
720
		}
721
722
		return false;
723
	}
724
725
726
	/**
727
	 * Return colored message
728
	 *
729
	 * @param    string $heading Message heading.
730
	 * @param    string $message Message content.
731
	 * @param    bool   $colon   Check if add colon between heading and message.
732
	 * @param    string $color   Heading color.
733
	 *
734
	 * @return    mixed
735
	 */
736
	private function color_message( $heading, $message = '', $colon = true, $color = 'g' ) {
737
		// Add colon.
738
		if ( $colon ) {
739
			$heading = $heading . ': ';
740
		}
741
742
		return WP_CLI::colorize( "%{$color}" . $heading . '%n' ) . $message;
743
	}
744
745
746
	/**
747
	 * Output section heading.
748
	 *
749
	 * @since     1.7
750
	 * @access    private
751
	 *
752
	 * @param    string $heading Heading.
753
	 * @param    string $color   Color.
754
	 *
755
	 * @return    void
756
	 */
757
	private function color_main_heading( $heading, $color = 'g' ) {
758
		WP_CLI::log( "\n######   " . $this->color_message( $heading, '', false, $color ) . '   ######' );
759
	}
760
761
	/**
762
	 * Output section sub heading.
763
	 *
764
	 * @since     1.7
765
	 * @access    private
766
	 *
767
	 * @param    string $subheading Sub heading.
768
	 *
769
	 * @return    void
770
	 */
771
	private function color_sub_heading( $subheading ) {
772
		WP_CLI::log( "\n--->" . $subheading . '', '', false );
773
	}
774
775
776
	/**
777
	 * Display data in table format.
778
	 *
779
	 * @since     1.7
780
	 * @access    private
781
	 *
782
	 * @param    array $data Array of table data.
783
	 *
784
	 * @return    void
785
	 */
786
	private function display_table( $data ) {
787
		$table = new \cli\Table();
788
789
		// Set table header.
790
		$table->setHeaders( $data[0] );
791
792
		// Remove table header.
793
		unset( $data[0] );
794
795
		// Set table data.
796
		$table->setRows( $data );
797
798
		// Display table.
799
		$table->display();
800
	}
801
802
803
	/**
804
	 * Get donors by form id
805
	 *
806
	 * @since 1.8
807
	 * @param int $form_id From id.
808
	 *
809
	 * @return array
810
	 */
811
812
	private function get_donors_by_form_id( $form_id ) {
813
		$donors = array();
814
815
		$donations = new Give_Payments_Query(
816
			array(
817
				'give_forms' => array( $form_id ),
818
				'number'     => -1,
819
				'status'     => array( 'publish' ),
820
			)
821
		);
822
823
		$donations   = $donations->get_payments();
824
		$skip_donors = array();
825
826
		/* @var Give_Payment|object $donation Payment object. */
827
		foreach ( $donations as $donation ) {
828
829
			if ( in_array( $donation->customer_id, $skip_donors ) ) {
830
				continue;
831
			}
832
833
			if ( ! empty( $donors ) ) {
834
				$donors['donors'][] = current( $this->api->get_customers( (int) $donation->customer_id ) )[0];
835
			} else {
836
				$donors = array_merge( $donors, $this->api->get_customers( (int) $donation->customer_id ) );
837
			}
838
839
			$skip_donors[] = $donation->customer_id;
840
		}
841
842
		return $donors;
843
	}
844
}
845