Completed
Push — master ( 45b05a...b92b5a )
by Devin
53s
created

reports.php ➔ give_reports_page()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 35
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 32
nc 4
nop 0
dl 0
loc 35
rs 6.7272
c 1
b 0
f 0
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 31 and the first side effect is on line 20.

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
 * Admin Reports Page
4
 *
5
 * Language Changes from EDD:
6
 * 1. "Report Type" stays
7
 * 2. "Earnings" changes to "Income"
8
 * 3. "Donors" changes to "Donors"
9
 * 4. "Payment Method" stays.
10
 *
11
 * @package     Give
12
 * @subpackage  Admin/Reports
13
 * @copyright   Copyright (c) 2016, WordImpress
14
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
15
 * @since       1.0
16
 */
17
18
// Exit if accessed directly
19
if ( ! defined( 'ABSPATH' ) ) {
20
	exit;
21
}
22
23
/**
24
 * Reports Page
25
 *
26
 * Renders the reports page contents.
27
 *
28
 * @since 1.0
29
 * @return void
30
 */
31
function give_reports_page() {
32
	$current_page = admin_url( 'edit.php?post_type=give_forms&page=give-reports' );
33
	$active_tab   = isset( $_GET['tab'] ) ? $_GET['tab'] : 'reports';
34
	?>
35
	<div class="wrap">
36
		<h1 class="nav-tab-wrapper">
37
			<a href="<?php echo esc_url( add_query_arg( array(
38
				'tab'              => 'reports',
39
				'settings-updated' => false
40
			), $current_page ) ); ?>" class="nav-tab <?php echo $active_tab == 'reports' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Reports', 'give' ); ?></a>
41
			<?php if ( current_user_can( 'export_give_reports' ) ) { ?>
42
				<a href="<?php echo esc_url( add_query_arg( array(
43
					'tab'              => 'export',
44
					'settings-updated' => false
45
				), $current_page ) ); ?>" class="nav-tab <?php echo $active_tab == 'export' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Export', 'give' ); ?></a>
46
			<?php } ?>
47
			<a href="<?php echo esc_url( add_query_arg( array(
48
				'tab'              => 'logs',
49
				'settings-updated' => false
50
			), $current_page ) ); ?>" class="nav-tab <?php echo $active_tab == 'logs' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Logs', 'give' ); ?></a>
51
			<a href="<?php echo esc_url( add_query_arg( array(
52
				'tab'              => 'tools',
53
				'settings-updated' => false
54
			), $current_page ) ); ?>" class="nav-tab <?php echo $active_tab == 'tools' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Tools', 'give' ); ?></a>
55
			<?php do_action( 'give_reports_tabs' ); ?>
56
		</h1>
57
58
		<?php
59
		do_action( 'give_reports_page_top' );
60
		do_action( 'give_reports_tab_' . $active_tab );
61
		do_action( 'give_reports_page_bottom' );
62
		?>
63
	</div><!-- .wrap -->
64
	<?php
65
}
66
67
/**
68
 * Default Report Views
69
 *
70
 * @since 1.0
71
 * @return array $views Report Views
72
 */
73
function give_reports_default_views() {
74
	$views = array(
75
		'earnings' => __( 'Income', 'give' ),
76
		'forms'    => give_get_forms_label_plural(),
77
		'donors'   => __( 'Donors', 'give' ),
78
		'gateways' => __( 'Payment Methods', 'give' )
79
	);
80
81
	$views = apply_filters( 'give_report_views', $views );
82
83
	return $views;
84
}
85
86
/**
87
 * Default Report Views
88
 *
89
 * Checks the $_GET['view'] parameter to ensure it exists within the default allowed views.
90
 *
91
 * @param string $default Default view to use.
92
 *
93
 * @since 1.0
94
 * @return string $view Report View
95
 *
96
 */
97
function give_get_reporting_view( $default = 'earnings' ) {
98
99
	if ( ! isset( $_GET['view'] ) || ! in_array( $_GET['view'], array_keys( give_reports_default_views() ) ) ) {
100
		$view = $default;
101
	} else {
102
		$view = $_GET['view'];
103
	}
104
105
	return apply_filters( 'give_get_reporting_view', $view );
106
}
107
108
/**
109
 * Renders the Reports page
110
 *
111
 * @since 1.0
112
 * @return void
113
 */
114
function give_reports_tab_reports() {
115
	$current_view = 'earnings';
116
	$views        = give_reports_default_views();
117
118
	if ( isset( $_GET['view'] ) && array_key_exists( $_GET['view'], $views ) ) {
119
		$current_view = $_GET['view'];
120
	}
121
122
	do_action( 'give_reports_view_' . $current_view );
123
}
124
125
add_action( 'give_reports_tab_reports', 'give_reports_tab_reports' );
126
127
/**
128
 * Renders the Reports Page Views Drop Downs
129
 *
130
 * @since 1.0
131
 * @return void
132
 */
133
function give_report_views() {
134
	$views        = give_reports_default_views();
135
	$current_view = isset( $_GET['view'] ) ? $_GET['view'] : 'earnings';
136
	do_action( 'give_report_view_actions_before' );
137
	?>
138
	<form id="give-reports-filter" method="get">
139
		<select id="give-reports-view" name="view">
140
			<option value="-1"><?php _e( 'Report Type', 'give' ); ?></option>
141
			<?php foreach ( $views as $view_id => $label ) : ?>
142
				<option value="<?php echo esc_attr( $view_id ); ?>" <?php selected( $view_id, $current_view ); ?>><?php echo $label; ?></option>
143
			<?php endforeach; ?>
144
		</select>
145
146
		<?php do_action( 'give_report_view_actions' ); ?>
147
148
		<input type="hidden" name="post_type" value="give_forms"/>
149
		<input type="hidden" name="page" value="give-reports"/>
150
		<?php submit_button( __( 'Show', 'give' ), 'secondary', 'submit', false ); ?>
151
	</form>
152
	<?php
153
	do_action( 'give_report_view_actions_after' );
154
}
155
156
/**
157
 * Renders the Reports Give Form Table
158
 *
159
 * @since 1.0
160
 * @uses  Give_Form_Reports_Table::prepare_items()
161
 * @uses  Give_Form_Reports_Table::display()
162
 * @return void
163
 */
164
function give_reports_forms_table() {
165
166
	if ( isset( $_GET['form-id'] ) ) {
167
		return;
168
	}
169
170
	include( dirname( __FILE__ ) . '/class-form-reports-table.php' );
171
172
	$give_table = new Give_Form_Reports_Table();
173
	$give_table->prepare_items();
174
	$give_table->display();
175
}
176
177
add_action( 'give_reports_view_forms', 'give_reports_forms_table' );
178
179
/**
180
 * Renders the detailed report for a specific give form
181
 *
182
 * @since 1.0
183
 * @return void
184
 */
185
function give_reports_form_details() {
186
	if ( ! isset( $_GET['form-id'] ) ) {
187
		return;
188
	}
189
	?>
190
	<div class="tablenav top reports-forms-details-wrap">
191
		<div class="actions bulkactions">
192
			<?php give_report_views(); ?>
193
			&nbsp;
194
			<button onclick="history.go(-1);" class="button-secondary"><?php _e( 'Go Back', 'give' ); ?></button>
195
		</div>
196
	</div>
197
	<?php
198
	give_reports_graph_of_form( absint( $_GET['form-id'] ) );
199
}
200
201
add_action( 'give_reports_view_forms', 'give_reports_form_details' );
202
203
/**
204
 * Renders the Reports Donors Table
205
 *
206
 * @since 1.0
207
 * @uses  Give_Donor_Reports_Table::prepare_items()
208
 * @uses  Give_Donor_Reports_Table::display()
209
 * @return void
210
 */
211
function give_reports_donors_table() {
212
	include( dirname( __FILE__ ) . '/class-donor-reports-table.php' );
213
214
	$give_table = new Give_Donor_Reports_Table();
215
	$give_table->prepare_items();
216
	?>
217
	<div class="wrap give-reports-donors-wrap">
218
		<?php do_action( 'give_logs_donors_table_top' ); ?>
219
		<form id="give-donors-filter" method="get" action="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-reports&view=donors' ); ?>">
220
			<?php
221
			$give_table->search_box( __( 'Search', 'give' ), 'give-donors' );
0 ignored issues
show
Unused Code introduced by
The call to the method Give_Donor_Reports_Table::search_box() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
222
			$give_table->display();
223
			?>
224
			<input type="hidden" name="post_type" value="give_forms"/>
225
			<input type="hidden" name="page" value="give-reports"/>
226
			<input type="hidden" name="view" value="donors"/>
227
		</form>
228
		<?php do_action( 'give_logs_donors_table_bottom' ); ?>
229
	</div>
230
	<?php
231
}
232
233
add_action( 'give_reports_view_donors', 'give_reports_donors_table' );
234
235
236
/**
237
 * Renders the Gateways Table
238
 *
239
 * @since 1.3
240
 * @uses  Give_Gateway_Reports_Table::prepare_items()
241
 * @uses  Give_Gateway_Reports_Table::display()
242
 * @return void
243
 */
244
function give_reports_gateways_table() {
245
	include( dirname( __FILE__ ) . '/class-gateways-reports-table.php' );
246
247
	$give_table = new Give_Gateawy_Reports_Table();
248
	$give_table->prepare_items();
249
	$give_table->display();
250
}
251
252
add_action( 'give_reports_view_gateways', 'give_reports_gateways_table' );
253
254
255
/**
256
 * Renders the Reports Earnings Graphs
257
 *
258
 * @since 1.0
259
 * @return void
260
 */
261
function give_reports_earnings() {
262
	?>
263
	<div class="tablenav top reports-table-nav">
264
		<h3 class="alignleft reports-earnings-title"><span><?php _e( 'Income Over Time', 'give' ); ?></span></h3>
265
266
		<div class="alignright actions reports-views-wrap"><?php give_report_views(); ?></div>
267
	</div>
268
	<?php
269
	give_reports_graph();
270
}
271
272
add_action( 'give_reports_view_earnings', 'give_reports_earnings' );
273
274
275
/**
276
 * Renders the 'Export' tab on the Reports Page
277
 *
278
 * @since 1.0
279
 * @return void
280
 */
281
function give_reports_tab_export() {
282
	?>
283
	<div id="give-dashboard-widgets-wrap">
284
		<div id="post-body">
285
			<div id="post-body-content">
286
287
				<?php do_action( 'give_reports_tab_export_content_top' ); ?>
288
289
				<table class="widefat export-options-table give-table">
290
					<thead>
291
					<tr>
292
						<th class="row-title"><?php _e( 'Export Type', 'give' ); ?></th>
293
						<th><?php _e( 'Export Options', 'give' ); ?></th>
294
					</tr>
295
					</thead>
296
					<tbody>
297
					<?php do_action( 'give_reports_tab_export_table_top' ); ?>
298
					<tr class="give-export-pdf-sales-earnings">
299
						<td class="row-title">
300
							<h3><span><?php _e( 'Export PDF of Donations and Income', 'give' ); ?></span></h3>
301
302
							<p><?php _e( 'Download a PDF of Donations and Income reports for all forms for the current year.', 'give' ); ?></p>
303
						</td>
304
						<td>
305
							<a class="button" href="<?php echo wp_nonce_url( add_query_arg( array( 'give-action' => 'generate_pdf' ) ), 'give_generate_pdf' ); ?>"><?php _e( 'Generate PDF', 'give' ); ?></a>
306
						</td>
307
					</tr>
308
					<tr class="alternate give-export-sales-earnings">
309
						<td class="row-title">
310
							<h3><span><?php _e( 'Export Income and Donation Stats', 'give' ); ?></span></h3>
311
312
							<p><?php _e( 'Download a CSV of income and donations over time.', 'give' ); ?></p>
313
						</td>
314
						<td>
315
							<form method="post">
316
								<?php echo Give()->html->year_dropdown( 'start_year' ); ?>
317
								<?php echo Give()->html->month_dropdown( 'start_month' ); ?>
318
								<?php echo _x( 'to', 'Date one to date two', 'give' ); ?>
319
								<?php echo Give()->html->year_dropdown( 'end_year' ); ?>
320
								<?php echo Give()->html->month_dropdown( 'end_month' ); ?>
321
								<input type="hidden" name="give-action" value="earnings_export"/>
322
								<input type="submit" value="<?php _e( 'Generate CSV', 'give' ); ?>" class="button-secondary"/>
323
							</form>
324
						</td>
325
					</tr>
326
					<tr class="give-export-payment-history">
327
						<td class="row-title">
328
							<h3><span><?php _e( 'Export Donation History', 'give' ); ?></span></h3>
329
330
							<p><?php _e( 'Download a CSV of all donations recorded.', 'give' ); ?></p>
331
						</td>
332
						<td>
333
							<form id="give-export-payments" class="give-export-form" method="post">
334
								<?php
335
								$args = array(
336
									'id'          => 'give-payment-export-start',
337
									'name'        => 'start',
338
									'placeholder' => __( 'Start date', 'give' )
339
								);
340
								echo Give()->html->date_field( $args ); ?>
341
								<?php
342
								$args = array(
343
									'id'          => 'give-payment-export-end',
344
									'name'        => 'end',
345
									'placeholder' => __( 'End date', 'give' )
346
								);
347
								echo Give()->html->date_field( $args ); ?>
348
								<select name="status">
349
									<option value="any"><?php _e( 'All Statuses', 'give' ); ?></option>
350
									<?php
351
									$statuses = give_get_payment_statuses();
352
									foreach ( $statuses as $status => $label ) {
353
										echo '<option value="' . $status . '">' . $label . '</option>';
354
									}
355
									?>
356
								</select>
357
								<?php wp_nonce_field( 'give_ajax_export', 'give_ajax_export' ); ?>
358
								<input type="hidden" name="give-export-class" value="Give_Batch_Payments_Export"/>
359
								<span>
360
									<input type="submit" value="<?php _e( 'Generate CSV', 'give' ); ?>" class="button-secondary"/>
361
									<span class="spinner"></span>
362
								</span>
363
							</form>
364
365
						</td>
366
					</tr>
367
					<tr class="alternate give-export-donors">
368
						<td class="row-title">
369
							<h3><span><?php _e( 'Export Donors in CSV', 'give' ); ?></span></h3>
370
371
							<p><?php _e( 'Download an export of donors for all donation forms or only those who have given to a particular form.', 'give' ); ?></p>
372
						</td>
373
						<td>
374
							<form method="post" id="give_donor_export" class="give-export-form">
375
376
									<?php
377
									$args = array(
378
										'name'   => 'forms',
379
										'id'     => 'give_customer_export_form',
380
										'chosen' => true
381
									);
382
									echo Give()->html->forms_dropdown( $args ); ?>
383
								
384
								<input type="submit" value="<?php _e( 'Generate CSV', 'give' ); ?>" class="button-secondary"/>
385
386
								<div id="export-donor-options-wrap" class="give-clearfix">
387
									<p><?php _e( 'Export Columns', 'give' ); ?>:</p>
388
									<ul id="give-export-option-ul">
389
										<li>
390
											<label for="give-export-fullname"><input type="checkbox" checked name="give_export_option[full_name]" id="give-export-fullname"><?php _e( 'Name', 'give' ); ?>
391
											</label>
392
										</li>
393
										<li>
394
											<label for="give-export-email"><input type="checkbox" checked name="give_export_option[email]" id="give-export-email"><?php _e( 'Email', 'give' ); ?>
395
											</label>
396
										</li>
397
										<li>
398
											<label for="give-export-address"><input type="checkbox" checked name="give_export_option[address]" id="give-export-address"><?php _e( 'Address', 'give' ); ?>
399
											</label>
400
										</li>
401
										<li>
402
											<label for="give-export-userid"><input type="checkbox" checked name="give_export_option[userid]" id="give-export-userid"><?php _e( 'User ID', 'give' ); ?>
403
											</label>
404
										</li>
405
										<li>
406
											<label for="give-export-first-donation-date"><input type="checkbox" checked name="give_export_option[date_first_donated]" id="give-export-first-donation-date"><?php _e( 'First Donation Date', 'give' ); ?>
407
											</label>
408
										</li>
409
										<li>
410
											<label for="give-export-donation-number"><input type="checkbox" checked name="give_export_option[donations]" id="give-export-donation-number"><?php _e( 'Number of Donations', 'give' ); ?>
411
											</label>
412
										</li>
413
										<li>
414
											<label for="give-export-donation-sum"><input type="checkbox" checked name="give_export_option[donation_sum]" id="give-export-donation-sum"><?php _e( 'Total Donated', 'give' ); ?>
415
											</label>
416
										</li>
417
									</ul>
418
								</div>
419
								<?php wp_nonce_field( 'give_ajax_export', 'give_ajax_export' ); ?>
420
								<input type="hidden" name="give-export-class" value="Give_Batch_Customers_Export"/>
421
								<input type="hidden" name="give-action" value="email_export"/>
422
							</form>
423
						</td>
424
					</tr>
425
					<?php do_action( 'give_reports_tab_export_table_bottom' ); ?>
426
					</tbody>
427
				</table>
428
429
				<?php do_action( 'give_reports_tab_export_content_bottom' ); ?>
430
431
			</div>
432
			<!-- .post-body-content -->
433
		</div>
434
		<!-- .post-body -->
435
	</div><!-- #give-dashboard-widgets-wrap -->
436
	<?php
437
}
438
439
add_action( 'give_reports_tab_export', 'give_reports_tab_export' );
440
441
/**
442
 * Renders the Reports page
443
 *
444
 * @since 1.0
445
 * @return void
446
 */
447
function give_reports_tab_logs() {
448
449
	require( GIVE_PLUGIN_DIR . 'includes/admin/reporting/logs.php' );
450
451
	$current_view = 'sales';
452
	$log_views    = give_log_default_views();
453
454
	if ( isset( $_GET['view'] ) && array_key_exists( $_GET['view'], $log_views ) ) {
455
		$current_view = $_GET['view'];
456
	}
457
458
	do_action( 'give_logs_view_' . $current_view );
459
}
460
461
add_action( 'give_reports_tab_logs', 'give_reports_tab_logs' );
462
463
/**
464
 * Retrieves estimated monthly earnings and sales
465
 *
466
 * @since 1.0
467
 * @return array
468
 */
469
function give_estimated_monthly_stats() {
470
471
	$estimated = get_transient( 'give_estimated_monthly_stats' );
472
473
	if ( false === $estimated ) {
474
475
		$estimated = array(
476
			'earnings' => 0,
477
			'sales'    => 0
478
		);
479
480
		$stats = new Give_Payment_Stats;
481
482
		$to_date_earnings = $stats->get_earnings( 0, 'this_month' );
483
		$to_date_sales    = $stats->get_sales( 0, 'this_month' );
484
485
		$current_day   = date( 'd', current_time( 'timestamp' ) );
486
		$current_month = date( 'n', current_time( 'timestamp' ) );
487
		$current_year  = date( 'Y', current_time( 'timestamp' ) );
488
		$days_in_month = cal_days_in_month( CAL_GREGORIAN, $current_month, $current_year );
489
490
		$estimated['earnings'] = ( $to_date_earnings / $current_day ) * $days_in_month;
491
		$estimated['sales']    = ( $to_date_sales / $current_day ) * $days_in_month;
492
493
		// Cache for one day
494
		set_transient( 'give_estimated_monthly_stats', $estimated, 86400 );
495
	}
496
497
	return maybe_unserialize( $estimated );
498
}
499