Completed
Push — master ( 10354b...44551c )
by Devin
29:37 queued 13:08
created

graphing.php ➔ give_get_report_dates()   F

Complexity

Conditions 31
Paths 2816

Size

Total Lines 183
Code Lines 132

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 31
eloc 132
nc 2816
nop 0
dl 0
loc 183
rs 2

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 14.

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
 * Graphing Functions
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Reports
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php 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' ) ) {
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' ) {
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' ) {
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
				endif;
169
170
				$i ++;
171
172
			endwhile;
173
174
			$y ++;
175
		endwhile;
176
177
	}
178
179
	$data = array(
180
		__( 'Income', 'give' )    => $earnings_data,
181
		__( 'Donations', 'give' ) => $sales_data
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
193
					$graph = new Give_Graph( $data );
194
					$graph->set( 'x_mode', 'time' );
195
					$graph->set( 'multiple_y_axes', true );
196
					$graph->display();
197
198
					if ( 'this_month' == $dates['range'] ) {
199
						$estimated = give_estimated_monthly_stats();
200
					}
201
					?>
202
				</div>
203
			</div>
204
			<?php give_reports_graph_controls(); ?>
205
			<table class="widefat reports-table alignleft" style="max-width:450px">
206
				<tbody>
207
				<tr>
208
					<td class="row-title">
209
						<label for="tablecell"><?php _e( 'Total income for period: ', 'give' ); ?></label></td>
210
					<td><?php echo give_currency_filter( give_format_amount( $earnings_totals ) ); ?></td>
211
				</tr>
212
				<tr class="alternate">
213
					<td class="row-title">
214
						<label for="tablecell"><?php _e( 'Total donations for period shown: ', 'give' ); ?></label>
215
					</td>
216
					<td><?php echo give_format_amount( $sales_totals, false ); ?></td>
217
				</tr>
218
				<?php if ( 'this_month' == $dates['range'] ) : ?>
219
					<tr>
220
						<td class="row-title">
221
							<label for="tablecell"><?php _e( 'Estimated monthly income: ', 'give' ); ?></label>
222
						</td>
223
						<td><?php echo give_currency_filter( give_format_amount( $estimated['earnings'] ) ); ?></td>
0 ignored issues
show
Bug introduced by
The variable $estimated does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
224
					</tr>
225
					<tr class="alternate">
226
						<td class="row-title">
227
							<label for="tablecell"><?php _e( 'Estimated monthly donations: ', 'give' ); ?></label>
228
						</td>
229
						<td><?php echo give_format_amount( $estimated['sales'], false ); ?></td>
230
					</tr>
231
				<?php endif; ?>
232
			</table>
233
234
			<?php do_action( 'give_reports_graph_additional_stats' ); ?>
235
236
		</div>
237
	</div>
238
	<?php
239
	// get output buffer contents and end our own buffer
240
	$output = ob_get_contents();
241
	ob_end_clean();
242
243
	echo $output;
244
}
245
246
/**
247
 * Show report graphs of a specific product
248
 *
249
 * @since 1.0
250
 * @return void
251
 */
252
function give_reports_graph_of_form( $form_id = 0 ) {
253
	// Retrieve the queried dates
254
	$dates = give_get_report_dates();
255
256
	// Determine graph options
257
	switch ( $dates['range'] ) :
258
		case 'today' :
259
		case 'yesterday' :
260
			$day_by_day = true;
261
			break;
262
		case 'last_year' :
263
			$day_by_day = false;
264
			break;
265
		case 'this_year' :
266
			$day_by_day = false;
267
			break;
268
		case 'last_quarter' :
269
			$day_by_day = false;
270
			break;
271
		case 'this_quarter' :
272
			$day_by_day = false;
273
			break;
274
		case 'other' :
275
			if ( $dates['m_end'] - $dates['m_start'] >= 2 || $dates['year_end'] > $dates['year'] ) {
276
				$day_by_day = false;
277
			} else {
278
				$day_by_day = true;
279
			}
280
			break;
281
		default:
282
			$day_by_day = true;
283
			break;
284
	endswitch;
285
286
	$earnings_totals = (float) 0.00; // Total earnings for time period shown
287
	$sales_totals    = 0;            // Total sales for time period shown
288
289
	$earnings_data = array();
290
	$sales_data    = array();
291
	$stats         = new Give_Payment_Stats;
292
293
	if ( $dates['range'] == 'today' || $dates['range'] == 'yesterday' ) {
294
295
		// Hour by hour
296
		$month  = $dates['m_start'];
297
		$hour   = 1;
298
		$minute = 0;
299
		$second = 0;
300
		while ( $hour <= 23 ) :
301
302
			if ( $hour == 23 ) {
303
				$minute = $second = 59;
304
			}
305
306
			$date     = mktime( $hour, $minute, $second, $month, $dates['day'], $dates['year'] );
307
			$date_end = mktime( $hour + 1, $minute, $second, $month, $dates['day'], $dates['year'] );
308
309
			$sales = $stats->get_sales( $form_id, $date, $date_end );
310
			$sales_totals += $sales;
311
312
			$earnings = $stats->get_earnings( $form_id, $date, $date_end );
313
			$earnings_totals += $earnings;
314
315
			$sales_data[]    = array( $date * 1000, $sales );
316
			$earnings_data[] = array( $date * 1000, $earnings );
317
318
			$hour ++;
319
		endwhile;
320
321
	} elseif ( $dates['range'] == 'this_week' || $dates['range'] == 'last_week' ) {
322
323
		//Day by day
324
		$day     = $dates['day'];
325
		$day_end = $dates['day_end'];
326
		$month   = $dates['m_start'];
327
		while ( $day <= $day_end ) :
328
329
			$date     = mktime( 0, 0, 0, $month, $day, $dates['year'] );
330
			$date_end = mktime( 0, 0, 0, $month, $day + 1, $dates['year'] );
331
			$sales    = $stats->get_sales( $form_id, $date, $date_end );
332
			$sales_totals += $sales;
333
334
			$earnings = $stats->get_earnings( $form_id, $date, $date_end );
335
			$earnings_totals += $earnings;
336
337
			$sales_data[]    = array( $date * 1000, $sales );
338
			$earnings_data[] = array( $date * 1000, $earnings );
339
340
			$day ++;
341
		endwhile;
342
343
	} else {
344
345
		$y = $dates['year'];
346
347
		while ( $y <= $dates['year_end'] ) :
348
349
			$last_year = false;
350
351
			if ( $dates['year'] == $dates['year_end'] ) {
352
				$month_start = $dates['m_start'];
353
				$month_end   = $dates['m_end'];
354
				$last_year   = true;
355
			} elseif ( $y == $dates['year'] ) {
356
				$month_start = $dates['m_start'];
357
				$month_end   = 12;
358
			} else {
359
				$month_start = 1;
360
				$month_end   = 12;
361
			}
362
363
			$i = $month_start;
364
			while ( $i <= $month_end ) :
365
366
				if ( $day_by_day ) :
367
368
					if ( $i == $month_end && $last_year ) {
369
370
						$num_of_days = $dates['day_end'];
371
372
					} else {
373
374
						$num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y );
375
376
					}
377
378
					$d = $dates['day'];
379
					while ( $d <= $num_of_days ) :
380
381
						$date     = mktime( 0, 0, 0, $i, $d, $y );
382
						$end_date = mktime( 23, 59, 59, $i, $d, $y );
383
384
						$sales = $stats->get_sales( $form_id, $date, $end_date );
385
						$sales_totals += $sales;
386
387
						$earnings = $stats->get_earnings( $form_id, $date, $end_date );
388
						$earnings_totals += $earnings;
389
390
						$sales_data[]    = array( $date * 1000, $sales );
391
						$earnings_data[] = array( $date * 1000, $earnings );
392
						$d ++;
393
394
					endwhile;
395
396
				else :
397
398
					$num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y );
399
400
					$date     = mktime( 0, 0, 0, $i, 1, $y );
401
					$end_date = mktime( 23, 59, 59, $i, $num_of_days, $y );
402
403
					$sales = $stats->get_sales( $form_id, $date, $end_date );
404
					$sales_totals += $sales;
405
406
					$earnings = $stats->get_earnings( $form_id, $date, $end_date );
407
					$earnings_totals += $earnings;
408
409
					$sales_data[]    = array( $date * 1000, $sales );
410
					$earnings_data[] = array( $date * 1000, $earnings );
411
				endif;
412
413
				$i ++;
414
415
			endwhile;
416
417
			$y ++;
418
		endwhile;
419
420
	}
421
422
	$data = array(
423
		__( 'Income', 'give' )    => $earnings_data,
424
		__( 'Donations', 'give' ) => $sales_data
425
	);
426
427
	?>
428
	<h3><span><?php printf( __( 'Income Over Time for %s', 'give' ), get_the_title( $form_id ) ); ?></span></h3>
429
430
	<div class="metabox-holder" style="padding-top: 0;">
431
		<div class="postbox">
432
			<div class="inside">
433
				<?php
434
				$graph = new Give_Graph( $data );
435
				$graph->set( 'x_mode', 'time' );
436
				$graph->set( 'multiple_y_axes', true );
437
				$graph->display();
438
				?>
439
			</div>
440
		</div>
441
		<!--/.postbox -->
442
		<table class="widefat reports-table alignleft" style="max-width:450px">
443
			<tbody>
444
			<tr>
445
				<td class="row-title">
446
					<label for="tablecell"><?php _e( 'Total income for period: ', 'give' ); ?></label></td>
447
				<td><?php echo give_currency_filter( give_format_amount( $earnings_totals ) ); ?></td>
448
			</tr>
449
			<tr class="alternate">
450
				<td class="row-title">
451
					<label for="tablecell"><?php _e( 'Total donations for period: ', 'give' ); ?></label>
452
				</td>
453
				<td><?php echo $sales_totals; ?></td>
454
			</tr>
455
			<tr>
456
				<td class="row-title">
457
					<label for="tablecell"><?php _e( 'Average monthly income: %s', 'give' ); ?></label>
458
				</td>
459
				<td><?php echo give_currency_filter( give_format_amount( give_get_average_monthly_form_earnings( $form_id ) ) ); ?></td>
460
			</tr>
461
			<tr class="alternate">
462
				<td class="row-title">
463
					<label for="tablecell"><?php _e( 'Average monthly donations: %s', 'give' ); ?></label>
464
				</td>
465
				<td><?php echo number_format( give_get_average_monthly_form_sales( $form_id ), 0 ); ?></td>
466
			</tr>
467
			</tbody>
468
		</table>
469
		<?php give_reports_graph_controls(); ?>
470
	</div>
471
	<?php
472
	echo ob_get_clean();
473
}
474
475
/**
476
 * Show report graph date filters
477
 *
478
 * @since 1.0
479
 * @return void
480
 */
481
function give_reports_graph_controls() {
482
	$date_options = apply_filters( 'give_report_date_options', array(
483
		'today'        => __( 'Today', 'give' ),
484
		'yesterday'    => __( 'Yesterday', 'give' ),
485
		'this_week'    => __( 'This Week', 'give' ),
486
		'last_week'    => __( 'Last Week', 'give' ),
487
		'this_month'   => __( 'This Month', 'give' ),
488
		'last_month'   => __( 'Last Month', 'give' ),
489
		'this_quarter' => __( 'This Quarter', 'give' ),
490
		'last_quarter' => __( 'Last Quarter', 'give' ),
491
		'this_year'    => __( 'This Year', 'give' ),
492
		'last_year'    => __( 'Last Year', 'give' ),
493
		'other'        => __( 'Custom', 'give' )
494
	) );
495
496
	$dates   = give_get_report_dates();
497
	$display = $dates['range'] == 'other' ? '' : 'style="display:none;"';
498
	$view    = give_get_reporting_view();
499
500
	if ( empty( $dates['day_end'] ) ) {
501
		$dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, date( 'n' ), date( 'Y' ) );
502
	}
503
504
	//echo '<pre>'; print_r( $dates ); echo '</pre>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
505
	do_action( 'give_report_graph_controls_before' );
506
	?>
507
	<form id="give-graphs-filter" method="get" class="alignright">
508
		<div class="tablenav top alignright">
509
			<div class="actions">
510
511
				<input type="hidden" name="post_type" value="give_forms" />
512
				<input type="hidden" name="page" value="give-reports" />
513
				<input type="hidden" name="view" value="<?php echo esc_attr( $view ); ?>" />
514
515
				<?php if ( isset( $_GET['form-id'] ) ) : ?>
516
					<input type="hidden" name="form-id" value="<?php echo absint( $_GET['form-id'] ); ?>" />
517
				<?php endif; ?>
518
519
				<div id="give-graphs-date-options-wrap" class="alignright">
520
					<select id="give-graphs-date-options" name="range">
521
						<?php foreach ( $date_options as $key => $option ) : ?>
522
							<option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $dates['range'] ); ?>><?php echo esc_html( $option ); ?></option>
523
						<?php endforeach; ?>
524
					</select>
525
526
					<input type="submit" class="button-secondary" value="<?php _e( 'Filter', 'give' ); ?>" />
527
				</div>
528
529
				<div id="give-date-range-options" <?php echo $display; ?>>
530
					<span><?php _e( 'From', 'give' ); ?>&nbsp;</span>
531
					<select id="give-graphs-month-start" name="m_start">
532
						<?php for ( $i = 1; $i <= 12; $i ++ ) : ?>
533
							<option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['m_start'] ); ?>><?php echo give_month_num_to_name( $i ); ?></option>
534
						<?php endfor; ?>
535
					</select>
536
					<select id="give-graphs-day-start" name="day">
537
						<?php for ( $i = 1; $i <= 31; $i ++ ) : ?>
538
							<option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['day'] ); ?>><?php echo $i; ?></option>
539
						<?php endfor; ?>
540
					</select>
541
					<select id="give-graphs-year-start" name="year">
542
						<?php for ( $i = 2007; $i <= date( 'Y' ); $i ++ ) : ?>
543
							<option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['year'] ); ?>><?php echo $i; ?></option>
544
						<?php endfor; ?>
545
					</select>
546
					<span><?php _e( 'To', 'give' ); ?>&nbsp;</span>
547
					<select id="give-graphs-month-end" name="m_end">
548
						<?php for ( $i = 1; $i <= 12; $i ++ ) : ?>
549
							<option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['m_end'] ); ?>><?php echo give_month_num_to_name( $i ); ?></option>
550
						<?php endfor; ?>
551
					</select>
552
					<select id="give-graphs-day-end" name="day_end">
553
						<?php for ( $i = 1; $i <= 31; $i ++ ) : ?>
554
							<option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['day_end'] ); ?>><?php echo $i; ?></option>
555
						<?php endfor; ?>
556
					</select>
557
					<select id="give-graphs-year-end" name="year_end">
558
						<?php for ( $i = 2007; $i <= date( 'Y' ); $i ++ ) : ?>
559
							<option value="<?php echo absint( $i ); ?>" <?php selected( $i, $dates['year_end'] ); ?>><?php echo $i; ?></option>
560
						<?php endfor; ?>
561
					</select>
562
				</div>
563
564
				<input type="hidden" name="give_action" value="filter_reports" />
565
			</div>
566
		</div>
567
	</form>
568
	<?php
569
	do_action( 'give_report_graph_controls_after' );
570
}
571
572
/**
573
 * Sets up the dates used to filter graph data
574
 *
575
 * Date sent via $_GET is read first and then modified (if needed) to match the
576
 * selected date-range (if any)
577
 *
578
 * @since 1.0
579
 * @return array
580
 */
581
function give_get_report_dates() {
582
	$dates = array();
583
584
	$current_time = current_time( 'timestamp' );
585
586
	$dates['range']    = isset( $_GET['range'] ) ? $_GET['range'] : 'this_month';
587
	$dates['year']     = isset( $_GET['year'] ) ? $_GET['year'] : date( 'Y' );
588
	$dates['year_end'] = isset( $_GET['year_end'] ) ? $_GET['year_end'] : date( 'Y' );
589
	$dates['m_start']  = isset( $_GET['m_start'] ) ? $_GET['m_start'] : 1;
590
	$dates['m_end']    = isset( $_GET['m_end'] ) ? $_GET['m_end'] : 12;
591
	$dates['day']      = isset( $_GET['day'] ) ? $_GET['day'] : 1;
592
	$dates['day_end']  = isset( $_GET['day_end'] ) ? $_GET['day_end'] : cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
593
594
	// Modify dates based on predefined ranges
595
	switch ( $dates['range'] ) :
596
597
		case 'this_month' :
598
			$dates['m_start']  = date( 'n', $current_time );
599
			$dates['m_end']    = date( 'n', $current_time );
600
			$dates['day']      = 1;
601
			$dates['day_end']  = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
602
			$dates['year']     = date( 'Y' );
603
			$dates['year_end'] = date( 'Y' );
604
			break;
605
606
		case 'last_month' :
607
			if ( date( 'n' ) == 1 ) {
608
				$dates['m_start']  = 12;
609
				$dates['m_end']    = 12;
610
				$dates['year']     = date( 'Y', $current_time ) - 1;
611
				$dates['year_end'] = date( 'Y', $current_time ) - 1;
612
			} else {
613
				$dates['m_start']  = date( 'n' ) - 1;
614
				$dates['m_end']    = date( 'n' ) - 1;
615
				$dates['year_end'] = $dates['year'];
616
			}
617
			$dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] );
618
			break;
619
620
		case 'today' :
621
			$dates['day']     = date( 'd', $current_time );
622
			$dates['m_start'] = date( 'n', $current_time );
623
			$dates['m_end']   = date( 'n', $current_time );
624
			$dates['year']    = date( 'Y', $current_time );
625
			break;
626
627
		case 'yesterday' :
628
629
			$year  = date( 'Y', $current_time );
630
			$month = date( 'n', $current_time );
631
			$day   = date( 'd', $current_time );
632
633
			if ( $month == 1 && $day == 1 ) {
634
635
				$year -= 1;
636
				$month = 12;
637
				$day   = cal_days_in_month( CAL_GREGORIAN, $month, $year );
638
639
			} elseif ( $month > 1 && $day == 1 ) {
640
641
				$month -= 1;
642
				$day = cal_days_in_month( CAL_GREGORIAN, $month, $year );
643
644
			} else {
645
646
				$day -= 1;
647
648
			}
649
650
			$dates['day']      = $day;
651
			$dates['m_start']  = $month;
652
			$dates['m_end']    = $month;
653
			$dates['year']     = $year;
654
			$dates['year_end'] = $year;
655
			break;
656
657
		case 'this_week' :
658
			$dates['day'] = date( 'd', $current_time - ( date( 'w', $current_time ) - 1 ) * 60 * 60 * 24 ) - 1;
659
			$dates['day'] += get_option( 'start_of_week' );
660
			$dates['day_end'] = $dates['day'] + 6;
661
			$dates['m_start'] = date( 'n', $current_time );
662
			$dates['m_end']   = date( 'n', $current_time );
663
			$dates['year']    = date( 'Y', $current_time );
664
			break;
665
666
		case 'last_week' :
667
			$dates['day'] = date( 'd', $current_time - ( date( 'w' ) - 1 ) * 60 * 60 * 24 ) - 8;
668
			$dates['day'] += get_option( 'start_of_week' );
669
			$dates['day_end'] = $dates['day'] + 6;
670
			$dates['year']    = date( 'Y' );
671
672
			if ( date( 'j', $current_time ) <= 7 ) {
673
				$dates['m_start'] = date( 'n', $current_time ) - 1;
674
				$dates['m_end']   = date( 'n', $current_time ) - 1;
675
				if ( $dates['m_start'] <= 1 ) {
676
					$dates['year']     = date( 'Y', $current_time ) - 1;
677
					$dates['year_end'] = date( 'Y', $current_time ) - 1;
678
				}
679
			} else {
680
				$dates['m_start'] = date( 'n', $current_time );
681
				$dates['m_end']   = date( 'n', $current_time );
682
			}
683
			break;
684
685
		case 'this_quarter' :
686
			$month_now = date( 'n', $current_time );
687
688
			if ( $month_now <= 3 ) {
689
690
				$dates['m_start'] = 1;
691
				$dates['m_end']   = 4;
692
				$dates['year']    = date( 'Y', $current_time );
693
694
			} else if ( $month_now <= 6 ) {
695
696
				$dates['m_start'] = 4;
697
				$dates['m_end']   = 7;
698
				$dates['year']    = date( 'Y', $current_time );
699
700
			} else if ( $month_now <= 9 ) {
701
702
				$dates['m_start'] = 7;
703
				$dates['m_end']   = 10;
704
				$dates['year']    = date( 'Y', $current_time );
705
706
			} else {
707
708
				$dates['m_start']  = 10;
709
				$dates['m_end']    = 1;
710
				$dates['year']     = date( 'Y', $current_time );
711
				$dates['year_end'] = date( 'Y', $current_time ) + 1;
712
713
			}
714
			break;
715
716
		case 'last_quarter' :
717
			$month_now = date( 'n' );
718
719
			if ( $month_now <= 3 ) {
720
721
				$dates['m_start']  = 10;
722
				$dates['m_end']    = 12;
723
				$dates['year']     = date( 'Y', $current_time ) - 1; // Previous year
724
				$dates['year_end'] = date( 'Y', $current_time ) - 1; // Previous year
725
726
			} else if ( $month_now <= 6 ) {
727
728
				$dates['m_start'] = 1;
729
				$dates['m_end']   = 3;
730
				$dates['year']    = date( 'Y', $current_time );
731
732
			} else if ( $month_now <= 9 ) {
733
734
				$dates['m_start'] = 4;
735
				$dates['m_end']   = 6;
736
				$dates['year']    = date( 'Y', $current_time );
737
738
			} else {
739
740
				$dates['m_start'] = 7;
741
				$dates['m_end']   = 9;
742
				$dates['year']    = date( 'Y', $current_time );
743
744
			}
745
			break;
746
747
		case 'this_year' :
748
			$dates['m_start'] = 1;
749
			$dates['m_end']   = 12;
750
			$dates['year']    = date( 'Y', $current_time );
751
			break;
752
753
		case 'last_year' :
754
			$dates['m_start']  = 1;
755
			$dates['m_end']    = 12;
756
			$dates['year']     = date( 'Y', $current_time ) - 1;
757
			$dates['year_end'] = date( 'Y', $current_time ) - 1;
758
			break;
759
760
	endswitch;
761
762
	return apply_filters( 'give_report_dates', $dates );
763
}
764
765
/**
766
 * Grabs all of the selected date info and then redirects appropriately
767
 *
768
 * @since 1.0
769
 *
770
 * @param $data
771
 */
772
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...
773
	$dates = give_get_report_dates();
774
775
	$view = give_get_reporting_view();
776
	$id   = isset( $_GET['form-id'] ) ? $_GET['form-id'] : null;
777
778
	wp_redirect( add_query_arg( $dates, admin_url( 'edit.php?post_type=give_forms&page=give-reports&view=' . esc_attr( $view ) . '&form-id=' . absint( $id ) ) ) );
779
	give_die();
780
}
781
782
add_action( 'give_filter_reports', 'give_parse_report_dates' );
783
784
785
/**
786
 * Give Reports Refresh Button
787
 * @since      1.3
788
 * @description: Outputs a "Refresh Reports" button for graphs
789
 */
790
function give_reports_refresh_button() {
791
792
	$url = wp_nonce_url( add_query_arg( array(
793
		'give_action'  => 'refresh_reports_transients',
794
		'give-message' => 'refreshed-reports'
795
	) ), 'give-refresh-reports' );
796
797
	echo '<a href="' . $url . '" data-tooltip="' . __( 'Clicking this will clear the reports cache.', 'give' ) . '" data-tooltip-my-position="right center"  data-tooltip-target-position="left center" class="button alignright give-refresh-reports-button give-tooltip">' . __( 'Refresh Reports', 'give' ) . '</a>';
798
799
}
800
801
add_action( 'give_reports_graph_additional_stats', 'give_reports_refresh_button' );
802
803
/**
804
 * Trigger the refresh of reports transients
805
 *
806
 * @since 1.3
807
 *
808
 * @param array $data Parameters sent from Settings page
809
 *
810
 * @return void
811
 */
812
function give_run_refresh_reports_transients( $data ) {
813
814
	if ( ! wp_verify_nonce( $data['_wpnonce'], 'give-refresh-reports' ) ) {
815
		return;
816
	}
817
818
	//Delete transients
819
	delete_transient( 'give_estimated_monthly_stats' );
820
	delete_transient( 'give_earnings_total' );
821
	delete_transient( md5( 'give_earnings_this_monththis_month' ) );
822
	delete_transient( md5( 'give_earnings_todaytoday' ) );
823
824
}
825
826
add_action( 'give_refresh_reports_transients', 'give_run_refresh_reports_transients' );