Test Failed
Pull Request — master (#1942)
by Devin
05:03
created

graphing.php ➔ give_reports_graph_controls()   F

Complexity

Conditions 12
Paths 2048

Size

Total Lines 100
Code Lines 60

Duplication

Lines 12
Ratio 12 %

Importance

Changes 0
Metric Value
cc 12
eloc 60
nc 2048
nop 0
dl 12
loc 100
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
2
/**
3
 * Graphing Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Reports
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
 * Show report graphs
19
 *
20
 * @since 1.0
21
 * @return void
22
 */
23
function give_reports_graph() {
24
	// Retrieve the queried dates
25
	$dates = give_get_report_dates();
26
27
	// Determine graph options
28
	switch ( $dates['range'] ) :
29
		case 'today' :
30
		case 'yesterday' :
31
			$day_by_day = true;
32
			break;
33
		case 'last_year' :
34
		case 'this_year' :
35
		case 'last_quarter' :
36
		case 'this_quarter' :
37
			$day_by_day = false;
38
			break;
39
		case 'other' :
40
			if ( $dates['m_end'] - $dates['m_start'] >= 2 || $dates['year_end'] > $dates['year'] && ( $dates['m_start'] != '12' && $dates['m_end'] != '1' ) ) {
0 ignored issues
show
introduced by
Found "!= '". Use Yoda Condition checks, you must
Loading history...
41
				$day_by_day = false;
42
			} else {
43
				$day_by_day = true;
44
			}
45
			break;
46
		default:
47
			$day_by_day = true;
48
			break;
49
	endswitch;
50
51
	$earnings_totals = 0.00; // Total earnings for time period shown
52
	$sales_totals    = 0;            // Total sales for time period shown
53
54
	$earnings_data = array();
55
	$sales_data    = array();
56
57
	if ( $dates['range'] == 'today' || $dates['range'] == 'yesterday' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
58
		// Hour by hour
59
		$hour  = 1;
60
		$month = date( 'n', current_time( 'timestamp' ) );
61
		while ( $hour <= 23 ) :
62
63
			$sales    = give_get_sales_by_date( $dates['day'], $month, $dates['year'], $hour );
64
			$earnings = give_get_earnings_by_date( $dates['day'], $month, $dates['year'], $hour );
65
66
			$sales_totals += $sales;
67
			$earnings_totals += $earnings;
68
69
			$date            = mktime( $hour, 0, 0, $month, $dates['day'], $dates['year'] ) * 1000;
70
			$sales_data[]    = array( $date, $sales );
71
			$earnings_data[] = array( $date, $earnings );
72
73
			$hour ++;
74
		endwhile;
75
76
	} elseif ( $dates['range'] == 'this_week' || $dates['range'] == 'last_week' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
77
78
		// Day by day
79
		$day     = $dates['day'];
80
		$day_end = $dates['day_end'];
81
		$month   = $dates['m_start'];
82
		while ( $day <= $day_end ) :
83
			$sales = give_get_sales_by_date( $day, $month, $dates['year'] );
84
			$sales_totals += $sales;
85
86
			$earnings = give_get_earnings_by_date( $day, $month, $dates['year'] );
87
			$earnings_totals += $earnings;
88
89
			$date            = mktime( 0, 0, 0, $month, $day, $dates['year'] ) * 1000;
90
			$sales_data[]    = array( $date, $sales );
91
			$earnings_data[] = array( $date, $earnings );
92
			$day ++;
93
		endwhile;
94
95
	} else {
96
97
		$y = $dates['year'];
98
		while ( $y <= $dates['year_end'] ) :
99
100
			if ( $dates['year'] == $dates['year_end'] ) {
101
				$month_start = $dates['m_start'];
102
				$month_end   = $dates['m_end'];
103
			} elseif ( $y == $dates['year'] ) {
104
				$month_start = $dates['m_start'];
105
				$month_end   = 12;
106
			} elseif ( $y == $dates['year_end'] ) {
107
				$month_start = 1;
108
				$month_end   = $dates['m_end'];
109
			} else {
110
				$month_start = 1;
111
				$month_end   = 12;
112
			}
113
114
			$i = $month_start;
115
			while ( $i <= $month_end ) :
116
117
				if ( $day_by_day ) {
118
119
					if ( $i == $month_end ) {
120
121
						$num_of_days = $dates['day_end'];
122
123
					} else {
124
125
						$num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y );
126
127
					}
128
129
					$d = $dates['day'];
130
131
					while ( $d <= $num_of_days ) :
132
133
						$sales = give_get_sales_by_date( $d, $i, $y );
134
						$sales_totals += $sales;
135
136
						$earnings = give_get_earnings_by_date( $d, $i, $y );
137
						$earnings_totals += $earnings;
138
139
						$date            = mktime( 0, 0, 0, $i, $d, $y ) * 1000;
140
						$sales_data[]    = array( $date, $sales );
141
						$earnings_data[] = array( $date, $earnings );
142
						$d ++;
143
144
					endwhile;
145
146
				} else {
147
148
					$sales = give_get_sales_by_date( null, $i, $y );
149
					$sales_totals += $sales;
150
151
					$earnings = give_get_earnings_by_date( null, $i, $y );
152
					$earnings_totals += $earnings;
153
154
					if ( $i == $month_end ) {
155
156
						$num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y );
157
158
					} else {
159
160
						$num_of_days = 1;
161
162
					}
163
164
					$date            = mktime( 0, 0, 0, $i, $num_of_days, $y ) * 1000;
165
					$sales_data[]    = array( $date, $sales );
166
					$earnings_data[] = array( $date, $earnings );
167
168
				}
169
170
				$i ++;
171
172
			endwhile;
173
174
			$y ++;
175
		endwhile;
176
177
	}
178
179
	$data = array(
180
		esc_html__( 'Income', 'give' )    => $earnings_data,
181
		esc_html__( 'Donations', 'give' ) => $sales_data
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
182
	);
183
184
	// start our own output buffer
185
	ob_start();
186
	?>
187
188
	<div id="give-dashboard-widgets-wrap">
189
		<div class="metabox-holder" style="padding-top: 0;">
190
			<div class="postbox">
191
				<div class="inside">
192
					<?php give_reports_graph_controls(); ?>
193
					<?php
194
					$graph = new Give_Graph( $data, array( 'dataType' => array( 'amount', 'count' ) ) );
195
					$graph->set( 'x_mode', 'time' );
196
					$graph->set( 'multiple_y_axes', true );
197
					$graph->display();
198
199
					if ( 'this_month' == $dates['range'] ) {
200
						$estimated = give_estimated_monthly_stats();
201
					}
202
					?>
203
				</div>
204
			</div>
205
			<table class="widefat reports-table alignleft" style="max-width:450px">
206
				<tbody>
207
				<tr>
208
					<th scope="row"><strong><?php esc_html_e( 'Total income for period:', 'give' ); ?></strong></th>
209
					<td><?php echo give_currency_filter( give_format_amount( $earnings_totals, array( 'sanitize' => false ) ) ); ?></td>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_currency_filter'
Loading history...
210
				</tr>
211
				<tr class="alternate">
212
					<th scope="row"><strong><?php esc_html_e( 'Total donations for period:', 'give' ); ?><strong></th>
213
					<td><?php echo $sales_totals; ?></td>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$sales_totals'
Loading history...
214
				</tr>
215
				<?php if ( 'this_month' == $dates['range'] ) : ?>
216
					<tr>
217
						<th scope="row"><strong><?php esc_html_e( 'Estimated monthly income:', 'give' ); ?></strong></th>
218
						<td><?php echo give_currency_filter( give_format_amount( $estimated['earnings'], array( 'sanitize' => false ) ) ); ?></td>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_currency_filter'
Loading history...
219
					</tr>
220
					<tr class="alternate">
221
						<th scope="row"><strong><?php esc_html_e( 'Estimated monthly donations:', 'give' ); ?></strong></th>
222
						<td><?php echo floor( $estimated['sales'] ); ?></td>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'floor'
Loading history...
223
					</tr>
224
				<?php endif; ?>
225
			</table>
226
227
			<?php
228
			/**
229
			 * Fires on report graphs widget.
230
			 *
231
			 * Allows you to add additional stats to the widget.
232
			 *
233
			 * @since 1.0
234
			 */
235
			do_action( 'give_reports_graph_additional_stats' );
236
			?>
237
238
		</div>
239
	</div>
240
	<?php
241
	// get output buffer contents and end our own buffer
242
	$output = ob_get_contents();
243
	ob_end_clean();
244
245
	echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
246
}
247
248
/**
249
 * Show report graphs of a specific product
250
 *
251
 * @since 1.0
252
 * @return void
253
 */
254
function give_reports_graph_of_form( $form_id = 0 ) {
255
	// Retrieve the queried dates
256
	$dates = give_get_report_dates();
257
258
	// Determine graph options
259
	switch ( $dates['range'] ) :
260
		case 'today' :
261
		case 'yesterday' :
262
			$day_by_day = true;
263
			break;
264
		case 'last_year' :
265
			$day_by_day = false;
266
			break;
267
		case 'this_year' :
268
			$day_by_day = false;
269
			break;
270
		case 'last_quarter' :
271
			$day_by_day = false;
272
			break;
273
		case 'this_quarter' :
274
			$day_by_day = false;
275
			break;
276
		case 'other' :
277
			if ( $dates['m_end'] - $dates['m_start'] >= 2 || $dates['year_end'] > $dates['year'] ) {
278
				$day_by_day = false;
279
			} else {
280
				$day_by_day = true;
281
			}
282
			break;
283
		default:
284
			$day_by_day = true;
285
			break;
286
	endswitch;
287
288
	$earnings_totals = (float) 0.00; // Total earnings for time period shown
289
	$sales_totals    = 0;            // Total sales for time period shown
290
291
	$earnings_data = array();
292
	$sales_data    = array();
293
	$stats         = new Give_Payment_Stats;
294
295
	if ( $dates['range'] == 'today' || $dates['range'] == 'yesterday' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
296
297
		// Hour by hour
298
		$month  = $dates['m_start'];
299
		$hour   = 1;
300
		$minute = 0;
301
		$second = 0;
302
		while ( $hour <= 23 ) :
303
304
			if ( $hour == 23 ) {
305
				$minute = $second = 59;
306
			}
307
308
			$date     = mktime( $hour, $minute, $second, $month, $dates['day'], $dates['year'] );
309
			$date_end = mktime( $hour + 1, $minute, $second, $month, $dates['day'], $dates['year'] );
310
311
			$sales = $stats->get_sales( $form_id, $date, $date_end );
0 ignored issues
show
Documentation introduced by
$date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$date_end is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
312
			$sales_totals += $sales;
313
314
			$earnings = $stats->get_earnings( $form_id, $date, $date_end );
0 ignored issues
show
Documentation introduced by
$date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$date_end is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
315
			$earnings_totals += $earnings;
316
317
			$sales_data[]    = array( $date * 1000, $sales );
318
			$earnings_data[] = array( $date * 1000, $earnings );
319
320
			$hour ++;
321
		endwhile;
322
323
	} elseif ( $dates['range'] == 'this_week' || $dates['range'] == 'last_week' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
324
325
		//Day by day
326
		$day     = $dates['day'];
327
		$day_end = $dates['day_end'];
328
		$month   = $dates['m_start'];
329
		while ( $day <= $day_end ) :
330
331
			$date     = mktime( 0, 0, 0, $month, $day, $dates['year'] );
332
			$date_end = mktime( 0, 0, 0, $month, $day + 1, $dates['year'] );
333
			$sales    = $stats->get_sales( $form_id, $date, $date_end );
0 ignored issues
show
Documentation introduced by
$date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$date_end is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
334
			$sales_totals += $sales;
335
336
			$earnings = $stats->get_earnings( $form_id, $date, $date_end );
0 ignored issues
show
Documentation introduced by
$date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$date_end is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
337
			$earnings_totals += $earnings;
338
339
			$sales_data[]    = array( $date * 1000, $sales );
340
			$earnings_data[] = array( $date * 1000, $earnings );
341
342
			$day ++;
343
		endwhile;
344
345
	} else {
346
347
		$y = $dates['year'];
348
349
		while ( $y <= $dates['year_end'] ) :
350
351
			$last_year = false;
352
353
			if ( $dates['year'] == $dates['year_end'] ) {
354
				$month_start = $dates['m_start'];
355
				$month_end   = $dates['m_end'];
356
				$last_year   = true;
357
			} elseif ( $y == $dates['year'] ) {
358
				$month_start = $dates['m_start'];
359
				$month_end   = 12;
360
			} else {
361
				$month_start = 1;
362
				$month_end   = 12;
363
			}
364
365
			$i = $month_start;
366
			while ( $i <= $month_end ) :
367
368
				if ( $day_by_day ) {
369
370
					if ( $i == $month_end && $last_year ) {
371
372
						$num_of_days = $dates['day_end'];
373
374
					} else {
375
376
						$num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y );
377
378
					}
379
380
					$d = $dates['day'];
381
					while ( $d <= $num_of_days ) :
382
383
						$date     = mktime( 0, 0, 0, $i, $d, $y );
384
						$end_date = mktime( 23, 59, 59, $i, $d, $y );
385
386
						$sales = $stats->get_sales( $form_id, $date, $end_date );
0 ignored issues
show
Documentation introduced by
$date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$end_date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
387
						$sales_totals += $sales;
388
389
						$earnings = $stats->get_earnings( $form_id, $date, $end_date );
0 ignored issues
show
Documentation introduced by
$date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$end_date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
390
						$earnings_totals += $earnings;
391
392
						$sales_data[]    = array( $date * 1000, $sales );
393
						$earnings_data[] = array( $date * 1000, $earnings );
394
						$d ++;
395
396
					endwhile;
397
398
				} else {
399
400
					$num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y );
401
402
					$date     = mktime( 0, 0, 0, $i, 1, $y );
403
					$end_date = mktime( 23, 59, 59, $i, $num_of_days, $y );
404
405
					$sales = $stats->get_sales( $form_id, $date, $end_date );
0 ignored issues
show
Documentation introduced by
$date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$end_date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
406
					$sales_totals += $sales;
407
408
					$earnings = $stats->get_earnings( $form_id, $date, $end_date );
0 ignored issues
show
Documentation introduced by
$date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$end_date is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
409
					$earnings_totals += $earnings;
410
411
					$sales_data[]    = array( $date * 1000, $sales );
412
					$earnings_data[] = array( $date * 1000, $earnings );
413
414
				}
415
416
				$i ++;
417
418
			endwhile;
419
420
			$y ++;
421
		endwhile;
422
423
	}
424
425
	$data = array(
426
		esc_html__( 'Income', 'give' )    => $earnings_data,
427
		esc_html__( 'Donations', 'give' ) => $sales_data
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
428
	);
429
430
	?>
431
	<h3><span><?php
432
		printf(
433
			/* translators: %s: form title */
434
			esc_html__( 'Income Report for %s', 'give' ),
435
			get_the_title( $form_id )
436
		);
437
	?></span></h3>
438
	<div id="give-dashboard-widgets-wrap">
439
		<div class="metabox-holder" style="padding-top: 0;">
440
			<div class="postbox">
441
				<div class="inside">
442
					<?php give_reports_graph_controls(); ?>
443
					<?php
444
					$graph = new Give_Graph( $data, array( 'dataType' => array( 'amount', 'count' ) ) );
445
					$graph->set( 'x_mode', 'time' );
446
					$graph->set( 'multiple_y_axes', true );
447
					$graph->display();
448
					?>
449
				</div>
450
			</div>
451
			<!--/.postbox -->
452
			<table class="widefat reports-table alignleft" style="max-width:450px">
453
				<tbody>
454
				<tr>
455
					<th scope="row"><strong><?php esc_html_e( 'Total income for period:', 'give' ); ?></strong></th>
456
					<td><?php echo give_currency_filter( give_format_amount( $earnings_totals, array( 'sanitize' => false ) ) ); ?></td>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_currency_filter'
Loading history...
457
				</tr>
458
				<tr class="alternate">
459
					<th scope="row"><strong><?php esc_html_e( 'Total donations for period:', 'give' ); ?></strong></th>
460
					<td><?php echo $sales_totals; ?></td>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$sales_totals'
Loading history...
461
				</tr>
462
				<tr>
463
					<th scope="row"><strong><?php esc_html_e( 'Average monthly income:', 'give' ); ?></strong></th>
464
					<td><?php echo give_currency_filter( give_format_amount( give_get_average_monthly_form_earnings( $form_id ), array( 'sanitize' => false ) ) ); ?></td>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'give_currency_filter'
Loading history...
465
				</tr>
466
				<tr class="alternate">
467
					<th scope="row"><strong><?php esc_html_e( 'Average monthly donations:', 'give' ); ?></strong></th>
468
					<td><?php echo number_format( give_get_average_monthly_form_sales( $form_id ), 0 ); ?></td>
469
				</tr>
470
				</tbody>
471
			</table>
472
		</div>
473
	</div>
474
	<?php
475
	echo ob_get_clean();
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'ob_get_clean'
Loading history...
476
}
477
478
/**
479
 * Show report graph date filters
480
 *
481
 * @since 1.0.0
482
 * @since 1.8.0 The hidden `view` field is replaced with `tab` field.
483
 *
484
 * @return void
485
 */
486
function give_reports_graph_controls() {
487
	$date_options = apply_filters( 'give_report_date_options', array(
488
		'today'        => esc_html__( 'Today', 'give' ),
489
		'yesterday'    => esc_html__( 'Yesterday', 'give' ),
490
		'this_week'    => esc_html__( 'This Week', 'give' ),
491
		'last_week'    => esc_html__( 'Last Week', 'give' ),
492
		'this_month'   => esc_html__( 'This Month', 'give' ),
493
		'last_month'   => esc_html__( 'Last Month', 'give' ),
494
		'this_quarter' => esc_html__( 'This Quarter', 'give' ),
495
		'last_quarter' => esc_html__( 'Last Quarter', 'give' ),
496
		'this_year'    => esc_html__( 'This Year', 'give' ),
497
		'last_year'    => esc_html__( 'Last Year', 'give' ),
498
		'other'        => esc_html__( 'Custom', 'give' )
499
	) );
500
501
	$dates   = give_get_report_dates();
502
	$display = $dates['range'] == 'other' ? '' : 'display: none;';
503
	$tab     = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'earnings';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
504
505
	if ( empty( $dates['day_end'] ) ) {
506
		$dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, date( 'n' ), date( 'Y' ) );
507
	}
508
509
	/**
510
	 * Fires before displaying report graph date filters.
511
	 *
512
	 * @since 1.0
513
	 */
514
	do_action( 'give_report_graph_controls_before' );
515
	?>
516
	<form id="give-graphs-filter" method="get">
517
		<div class="tablenav top">
518
			<div class="actions">
519
520
				<input type="hidden" name="post_type" value="give_forms" />
521
				<input type="hidden" name="page" value="give-reports" />
522
				<input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
523
524
				<?php if ( isset( $_GET['form-id'] ) ) : ?>
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
525
					<input type="hidden" name="form-id" value="<?php echo absint( $_GET['form-id'] ); ?>" />
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
526
				<?php endif; ?>
527
528
				<div id="give-graphs-date-options-wrap">
529
					<select id="give-graphs-date-options" name="range">
530
						<?php foreach ( $date_options as $key => $option ) : ?>
531
							<option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $dates['range'] ); ?>><?php echo esc_html( $option ); ?></option>
532
						<?php endforeach; ?>
533
					</select>
534
535
					<div id="give-date-range-options" style="<?php echo esc_attr( $display ); ?>">
536
						<span class="screen-reader-text"><?php esc_html_e( 'From', 'give' ); ?>&nbsp;</span>
537
						<select id="give-graphs-month-start" name="m_start" aria-label="Start Month">
538 View Code Duplication
							<?php for ( $i = 1; $i <= 12; $i ++ ) : ?>
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...
539
								<option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['m_start'] ) ); ?>><?php echo esc_html( give_month_num_to_name( $i ) ); ?></option>
540
							<?php endfor; ?>
541
						</select>
542
						<select id="give-graphs-day-start" name="day" aria-label="Start Day">
543
							<?php for ( $i = 1; $i <= 31; $i ++ ) : ?>
544
								<option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['day'] ) ); ?>><?php echo esc_html( $i ); ?></option>
545
							<?php endfor; ?>
546
						</select>
547
						<select id="give-graphs-year-start" name="year" aria-label="Start Year">
548 View Code Duplication
							<?php for ( $i = 2007; $i <= date( 'Y' ); $i ++ ) : ?>
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...
549
								<option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['year'] ) ); ?>><?php echo esc_html( $i ); ?></option>
550
							<?php endfor; ?>
551
						</select>
552
						<span class="screen-reader-text"><?php esc_html_e( 'To', 'give' ); ?>&nbsp;</span>
553
						<span>&ndash;</span>
554
						<select id="give-graphs-month-end" name="m_end" aria-label="End Month">
555 View Code Duplication
							<?php for ( $i = 1; $i <= 12; $i ++ ) : ?>
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...
556
								<option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['m_end'] ) ); ?>><?php echo esc_html( give_month_num_to_name( $i ) ); ?></option>
557
							<?php endfor; ?>
558
						</select>
559
						<select id="give-graphs-day-end" name="day_end" aria-label="End Day">
560
							<?php for ( $i = 1; $i <= 31; $i ++ ) : ?>
561
								<option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['day_end'] ) ); ?>><?php echo esc_html( $i ); ?></option>
562
							<?php endfor; ?>
563
						</select>
564
						<select id="give-graphs-year-end" name="year_end" aria-label="End Year">
565 View Code Duplication
							<?php for ( $i = 2007; $i <= date( 'Y' ); $i ++ ) : ?>
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...
566
								<option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['year_end'] ) ); ?>><?php echo esc_html( $i ); ?></option>
567
							<?php endfor; ?>
568
						</select>
569
					</div>
570
571
					<input type="submit" class="button-secondary" value="<?php esc_attr_e( 'Filter', 'give' ); ?>" />
572
				</div>
573
574
				<input type="hidden" name="give_action" value="filter_reports" />
575
			</div>
576
		</div>
577
	</form>
578
	<?php
579
	/**
580
	 * Fires after displaying report graph date filters.
581
	 *
582
	 * @since 1.0
583
	 */
584
	do_action( 'give_report_graph_controls_after' );
585
}
586
587
/**
588
 * Sets up the dates used to filter graph data
589
 *
590
 * Date sent via $_GET is read first and then modified (if needed) to match the
591
 * selected date-range (if any)
592
 *
593
 * @since 1.0
594
 * @return array
595
 */
596
function give_get_report_dates() {
597
	$dates = array();
598
599
	$current_time = current_time( 'timestamp' );
600
601
	$dates['range']    = isset( $_GET['range'] ) ? $_GET['range'] : 'this_month';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
602
	$dates['year']     = isset( $_GET['year'] ) ? $_GET['year'] : date( 'Y' );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
603
	$dates['year_end'] = isset( $_GET['year_end'] ) ? $_GET['year_end'] : date( 'Y' );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
604
	$dates['m_start']  = isset( $_GET['m_start'] ) ? $_GET['m_start'] : 1;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
605
	$dates['m_end']    = isset( $_GET['m_end'] ) ? $_GET['m_end'] : 12;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
606
	$dates['day']      = isset( $_GET['day'] ) ? $_GET['day'] : 1;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
607
	$dates['day_end']  = isset( $_GET['day_end'] ) ? $_GET['day_end'] : cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
608
609
	// Modify dates based on predefined ranges
610
	switch ( $dates['range'] ) :
611
612
		case 'this_month' :
613
			$dates['m_start']  = date( 'n', $current_time );
614
			$dates['m_end']    = date( 'n', $current_time );
615
			$dates['day']      = 1;
616
			$dates['day_end']  = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
617
			$dates['year']     = date( 'Y' );
618
			$dates['year_end'] = date( 'Y' );
619
			break;
620
621
		case 'last_month' :
622
			if ( date( 'n' ) == 1 ) {
0 ignored issues
show
introduced by
Found "== 1". Use Yoda Condition checks, you must
Loading history...
623
				$dates['m_start']  = 12;
624
				$dates['m_end']    = 12;
625
				$dates['year']     = date( 'Y', $current_time ) - 1;
626
				$dates['year_end'] = date( 'Y', $current_time ) - 1;
627 View Code Duplication
			} else {
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...
628
				$dates['m_start']  = date( 'n' ) - 1;
629
				$dates['m_end']    = date( 'n' ) - 1;
630
				$dates['year_end'] = $dates['year'];
631
			}
632
			$dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
633
			break;
634
635 View Code Duplication
		case 'today' :
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...
636
			$dates['day']     = date( 'd', $current_time );
637
			$dates['m_start'] = date( 'n', $current_time );
638
			$dates['m_end']   = date( 'n', $current_time );
639
			$dates['year']    = date( 'Y', $current_time );
640
			break;
641
642 View Code Duplication
		case 'yesterday' :
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...
643
644
			$year  = date( 'Y', $current_time );
645
			$month = date( 'n', $current_time );
646
			$day   = date( 'd', $current_time );
647
648
			if ( $month == 1 && $day == 1 ) {
0 ignored issues
show
introduced by
Found "== 1". Use Yoda Condition checks, you must
Loading history...
649
650
				$year -= 1;
651
				$month = 12;
652
				$day   = cal_days_in_month( CAL_GREGORIAN, $month, $year );
653
654
			} elseif ( $month > 1 && $day == 1 ) {
0 ignored issues
show
introduced by
Found "== 1". Use Yoda Condition checks, you must
Loading history...
655
656
				$month -= 1;
657
				$day = cal_days_in_month( CAL_GREGORIAN, $month, $year );
658
659
			} else {
660
661
				$day -= 1;
662
663
			}
664
665
			$dates['day']      = $day;
666
			$dates['m_start']  = $month;
667
			$dates['m_end']    = $month;
668
			$dates['year']     = $year;
669
			$dates['year_end'] = $year;
670
			break;
671
672
		case 'this_week' :
673
			$dates['day'] = date( 'd', $current_time - ( date( 'w', $current_time ) - 1 ) * 60 * 60 * 24 ) - 1;
674
			$dates['day'] += get_option( 'start_of_week' );
675
			$dates['day_end'] = $dates['day'] + 6;
676
			$dates['m_start'] = date( 'n', $current_time );
677
			$dates['m_end']   = date( 'n', $current_time );
678
			$dates['year']    = date( 'Y', $current_time );
679
			break;
680
681
		case 'last_week' :
682
			$dates['day'] = date( 'd', $current_time - ( date( 'w' ) - 1 ) * 60 * 60 * 24 ) - 8;
683
			$dates['day'] += get_option( 'start_of_week' );
684
			$dates['day_end'] = $dates['day'] + 6;
685
			$dates['year']    = date( 'Y' );
686
687
			if ( date( 'j', $current_time ) <= 7 ) {
688
				$dates['m_start'] = date( 'n', $current_time ) - 1;
689
				$dates['m_end']   = date( 'n', $current_time ) - 1;
690 View Code Duplication
				if ( $dates['m_start'] <= 1 ) {
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...
691
					$dates['year']     = date( 'Y', $current_time ) - 1;
692
					$dates['year_end'] = date( 'Y', $current_time ) - 1;
693
				}
694
			} else {
695
				$dates['m_start'] = date( 'n', $current_time );
696
				$dates['m_end']   = date( 'n', $current_time );
697
			}
698
			break;
699
700 View Code Duplication
		case 'this_quarter' :
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...
701
			$month_now = date( 'n', $current_time );
702
703
			if ( $month_now <= 3 ) {
704
705
				$dates['m_start'] = 1;
706
				$dates['m_end']   = 4;
707
				$dates['year']    = date( 'Y', $current_time );
708
709
			} else if ( $month_now <= 6 ) {
710
711
				$dates['m_start'] = 4;
712
				$dates['m_end']   = 7;
713
				$dates['year']    = date( 'Y', $current_time );
714
715
			} else if ( $month_now <= 9 ) {
716
717
				$dates['m_start'] = 7;
718
				$dates['m_end']   = 10;
719
				$dates['year']    = date( 'Y', $current_time );
720
721
			} else {
722
723
				$dates['m_start']  = 10;
724
				$dates['m_end']    = 1;
725
				$dates['year']     = date( 'Y', $current_time );
726
				$dates['year_end'] = date( 'Y', $current_time ) + 1;
727
728
			}
729
			break;
730
731 View Code Duplication
		case 'last_quarter' :
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...
732
			$month_now = date( 'n' );
733
734
			if ( $month_now <= 3 ) {
735
736
				$dates['m_start']  = 10;
737
				$dates['m_end']    = 12;
738
				$dates['year']     = date( 'Y', $current_time ) - 1; // Previous year
739
				$dates['year_end'] = date( 'Y', $current_time ) - 1; // Previous year
740
741
			} else if ( $month_now <= 6 ) {
742
743
				$dates['m_start'] = 1;
744
				$dates['m_end']   = 3;
745
				$dates['year']    = date( 'Y', $current_time );
746
747
			} else if ( $month_now <= 9 ) {
748
749
				$dates['m_start'] = 4;
750
				$dates['m_end']   = 6;
751
				$dates['year']    = date( 'Y', $current_time );
752
753
			} else {
754
755
				$dates['m_start'] = 7;
756
				$dates['m_end']   = 9;
757
				$dates['year']    = date( 'Y', $current_time );
758
759
			}
760
			break;
761
762
		case 'this_year' :
763
			$dates['m_start'] = 1;
764
			$dates['m_end']   = 12;
765
			$dates['year']    = date( 'Y', $current_time );
766
			break;
767
768
		case 'last_year' :
769
			$dates['m_start']  = 1;
770
			$dates['m_end']    = 12;
771
			$dates['year']     = date( 'Y', $current_time ) - 1;
772
			$dates['year_end'] = date( 'Y', $current_time ) - 1;
773
			break;
774
775
	endswitch;
776
777
	return apply_filters( 'give_report_dates', $dates );
778
}
779
780
/**
781
 * Grabs all of the selected date info and then redirects appropriately
782
 *
783
 * @since 1.0.0
784
 * @since 1.8.0 The `tab` query arg is added to the redirect.
785
 *
786
 * @param $data
787
 */
788
function give_parse_report_dates( $data ) {
0 ignored issues
show
Unused Code introduced by
The parameter $data 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...
789
	$dates = give_get_report_dates();
790
791
	$view = give_get_reporting_view();
792
	$tab  = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'earnings';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
793
	$id   = isset( $_GET['form-id'] ) ? $_GET['form-id'] : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
794
795
	wp_redirect( add_query_arg( $dates, admin_url( 'edit.php?post_type=give_forms&page=give-reports&tab=' . esc_attr( $tab ) . '&view=' . esc_attr( $view ) . '&form-id=' . absint( $id ) ) ) );
796
	give_die();
797
}
798
799
add_action( 'give_filter_reports', 'give_parse_report_dates' );
800
801
802
/**
803
 * Give Reports Refresh Button
804
 *
805
 * Outputs a "Refresh Reports" button for graphs
806
 *
807
 * @since      1.3
808
 */
809
function give_reports_refresh_button() {
810
811
	$url = wp_nonce_url( add_query_arg( array(
812
		'give_action'  => 'refresh_reports_transients',
813
		'give-message' => 'refreshed-reports'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
814
	) ), 'give-refresh-reports' );
815
816
	echo '<a href="'
817
	     . esc_url_raw( $url )
818
	     . '" data-tooltip="'. esc_attr__( 'Clicking this will clear the reports cache.', 'give' )
819
	     . '" data-tooltip-my-position="right center"  data-tooltip-target-position="left center" class="button alignright give-admin-button give-tooltip">'
820
	     . '<span class="give-admin-button-icon give-admin-button-icon-update"></span>'
821
	     . esc_html__( 'Refresh Report Data', 'give' )
822
	     . '</a>';
823
824
}
825
826
add_action( 'give_reports_graph_additional_stats', 'give_reports_refresh_button' );
827
828
/**
829
 * Trigger the refresh of reports transients
830
 *
831
 * @since 1.3
832
 *
833
 * @param array $data Parameters sent from Settings page
834
 *
835
 * @return void
836
 */
837
function give_run_refresh_reports_transients( $data ) {
838
839
	if ( ! wp_verify_nonce( $data['_wpnonce'], 'give-refresh-reports' ) ) {
840
		return;
841
	}
842
843
	// Monthly stats.
844
	Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) );
845
846
	// Total earning.
847
	delete_option( 'give_earnings_total' );
848
849
	// @todo: Refresh only range related stat cache
850
	give_delete_donation_stats();
851
}
852
853
add_action( 'give_refresh_reports_transients', 'give_run_refresh_reports_transients' );