Test Failed
Push — release/1.8.12 ( b58a2f...d255b1 )
by Ravinder
375:09 queued 372:17
created

includes/admin/reports/reports.php (16 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
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     https://opensource.org/licenses/gpl-license 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'] ) ? sanitize_text_field( $_GET['tab'] ) : 'earnings';
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
34
	$views        = give_reports_default_views();
35
	?>
36
	<div class="wrap give-settings-page">
37
38
		<h1 class="screen-reader-text"><?php echo get_admin_page_title(); ?></h1>
0 ignored issues
show
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'get_admin_page_title'
Loading history...
39
40
		<h2 class="nav-tab-wrapper">
41
			<?php foreach ( $views as $tab => $label ) { ?>
42
				<a href="<?php echo esc_url( add_query_arg( array(
43
					'tab'              => $tab,
44
					'settings-updated' => false,
45
				), $current_page ) ); ?>" class="nav-tab <?php echo $tab === $active_tab ? esc_attr( 'nav-tab-active' ) : ''; ?>"><?php echo esc_html( $label ); ?></a>
0 ignored issues
show
Expected next thing to be a escaping function, not '$tab'
Loading history...
46
			<?php } ?>
47
			<?php if ( current_user_can( 'export_give_reports' ) ) { ?>
48
				<a href="<?php echo esc_url( add_query_arg( array(
49
					'tab'              => 'export',
50
					'settings-updated' => false,
51
				), $current_page ) ); ?>" class="nav-tab <?php echo 'export' === $active_tab ? esc_attr( 'nav-tab-active' ) : ''; ?>"><?php esc_html_e( 'Export', 'give' ); ?></a>
52
			<?php }
53
			/**
54
			 * Fires in the report tabs.
55
			 *
56
			 * Allows you to add new report tabs.
57
			 *
58
			 * @since 1.0
59
			 */
60
			do_action( 'give_reports_tabs' );
61
			?>
62
		</h2>
63
64
		<?php
65
		/**
66
		 * Fires before the report page.
67
		 *
68
		 * @since 1.0
69
		 */
70
		do_action( 'give_reports_page_top' );
71
72
		// Set $active_tab prior to hook firing.
73
		if ( in_array( $active_tab, array_keys( $views ) ) ) {
74
			$active_tab = 'reports';
75
		}
76
77
		/**
78
		 * Fires the report page active tab.
79
		 *
80
		 * @since 1.0
81
		 */
82
		do_action( "give_reports_tab_{$active_tab}" );
83
84
		/**
85
		 * Fires after the report page.
86
		 *
87
		 * @since 1.0
88
		 */
89
		do_action( 'give_reports_page_bottom' );
90
		?>
91
	</div><!-- .wrap -->
92
	<?php
93
}
94
95
/**
96
 * Default Report Views
97
 *
98
 * @since 1.0
99
 * @return array $views Report Views
100
 */
101
function give_reports_default_views() {
102
	$views = array(
103
		'earnings' => esc_html__( 'Income', 'give' ),
104
		'forms'    => esc_html__( 'Forms', 'give' ),
105
		'donors'   => esc_html__( 'Donors', 'give' ),
106
		'gateways' => esc_html__( 'Donation Methods', 'give' ),
107
	);
108
109
	$views = apply_filters( 'give_report_views', $views );
110
111
	return $views;
112
}
113
114
/**
115
 * Default Report Views
116
 *
117
 * Checks the $_GET['view'] parameter to ensure it exists within the default allowed views.
118
 *
119
 * @param string $default Default view to use.
120
 *
121
 * @since 1.0
122
 * @return string $view Report View
123
 */
124
function give_get_reporting_view( $default = 'earnings' ) {
125
126
	if ( ! isset( $_GET['view'] ) || ! in_array( $_GET['view'], array_keys( give_reports_default_views() ) ) ) {
127
		$view = $default;
128
	} else {
129
		$view = $_GET['view'];
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
Detected usage of a non-sanitized input variable: $_GET
Loading history...
130
	}
131
132
	return apply_filters( 'give_get_reporting_view', $view );
133
}
134
135
/**
136
 * Renders the Reports page
137
 *
138
 * @since 1.0
139
 * @return void
140
 */
141
function give_reports_tab_reports() {
142
	$current_view = 'earnings';
143
	$views        = give_reports_default_views();
144
145
	if ( isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $views ) ) {
146
		$current_view = $_GET['tab'];
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
Detected usage of a non-sanitized input variable: $_GET
Loading history...
147
	}
148
149
	/**
150
	 * Fires the report page view.
151
	 *
152
	 * @since 1.0
153
	 */
154
	do_action( "give_reports_view_{$current_view}" );
155
}
156
157
add_action( 'give_reports_tab_reports', 'give_reports_tab_reports' );
158
159
/**
160
 * Renders the Reports Page Views Drop Downs
161
 *
162
 * @since 1.0
163
 * @return void
164
 */
165
function give_report_views() {
166
	$views        = give_reports_default_views();
167
	$current_view = isset( $_GET['view'] ) ? $_GET['view'] : 'earnings';
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
Detected usage of a non-sanitized input variable: $_GET
Loading history...
168
	/**
169
	 * Fires before the report page actions form.
170
	 *
171
	 * @since 1.0
172
	 */
173
	do_action( 'give_report_view_actions_before' );
174
	?>
175
	<form id="give-reports-filter" method="get">
176
		<select id="give-reports-view" name="view">
177
			<option value="-1"><?php esc_html_e( 'Report Type', 'give' ); ?></option>
178
			<?php foreach ( $views as $view_id => $label ) : ?>
179
				<option value="<?php echo esc_attr( $view_id ); ?>" <?php selected( $view_id, $current_view ); ?>><?php echo $label; ?></option>
0 ignored issues
show
Expected next thing to be a escaping function, not '$label'
Loading history...
180
			<?php endforeach; ?>
181
		</select>
182
183
		<?php
184
		/**
185
		 * Fires in the report page actions area.
186
		 *
187
		 * Allows you to add new elements/actions after the "Report Type" drop down.
188
		 *
189
		 * @since 1.0
190
		 */
191
		do_action( 'give_report_view_actions' );
192
		?>
193
194
		<input type="hidden" name="post_type" value="give_forms"/>
195
		<input type="hidden" name="page" value="give-reports"/>
196
		<?php submit_button( esc_html__( 'Show', 'give' ), 'secondary', 'submit', false ); ?>
197
	</form>
198
	<?php
199
	/**
200
	 * Fires after the report page actions form.
201
	 *
202
	 * @since 1.0
203
	 */
204
	do_action( 'give_report_view_actions_after' );
205
}
206
207
/**
208
 * Renders the Reports Give Form Table
209
 *
210
 * @since 1.0
211
 * @uses  Give_Form_Reports_Table::prepare_items()
212
 * @uses  Give_Form_Reports_Table::display()
213
 * @return void
214
 */
215
function give_reports_forms_table() {
216
217
	if ( isset( $_GET['form-id'] ) ) {
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
218
		return;
219
	}
220
221
	include( dirname( __FILE__ ) . '/class-form-reports-table.php' );
222
223
	$give_table = new Give_Form_Reports_Table();
224
	$give_table->prepare_items();
225
	$give_table->display();
226
	?>
227
	<input type="hidden" name="post_type" value="give_forms"/>
228
	<input type="hidden" name="page" value="give-reports"/>
229
	<input type="hidden" name="tab" value="forms"/>
230
	<?php
231
}
232
233
add_action( 'give_reports_view_forms', 'give_reports_forms_table' );
234
235
/**
236
 * Renders the detailed report for a specific give form.
237
 *
238
 * @since 1.0
239
 * @return void
240
 */
241
function give_reports_form_details() {
242
	if ( ! isset( $_GET['form-id'] ) ) {
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
243
		return;
244
	}
245
	?>
246
	<div class="tablenav top reports-forms-details-wrap">
247
		<div class="actions bulkactions">
248
			<button onclick="history.go(-1);" class="button-secondary"><?php esc_html_e( 'Go Back', 'give' ); ?></button>
249
		</div>
250
	</div>
251
	<?php
252
	give_reports_graph_of_form( absint( $_GET['form-id'] ) );
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
253
}
254
255
add_action( 'give_reports_view_forms', 'give_reports_form_details' );
256
257
/**
258
 * Renders the Reports Donors Table
259
 *
260
 * @since 1.0
261
 * @uses  Give_Donor_Reports_Table::prepare_items()
262
 * @uses  Give_Donor_Reports_Table::display()
263
 * @return void
264
 */
265 View Code Duplication
function give_reports_donors_table() {
0 ignored issues
show
This function seems to be duplicated in 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...
266
	include( dirname( __FILE__ ) . '/class-donor-reports-table.php' );
267
268
	$give_table = new Give_Donor_Reports_Table();
269
	$give_table->prepare_items();
270
	?>
271
	<div class="wrap give-reports-donors-wrap">
272
		<?php
273
		/**
274
		 * Fires before the donors log actions form.
275
		 *
276
		 * @since 1.0
277
		 */
278
		do_action( 'give_logs_donors_table_top' );
279
280
		$give_table->search_box( esc_html__( 'Search', 'give' ), 'give-donors' );
281
		$give_table->display();
282
		?>
283
		<input type="hidden" name="post_type" value="give_forms"/>
284
		<input type="hidden" name="page" value="give-reports"/>
285
		<input type="hidden" name="tab" value="donors"/>
286
287
		<?php
288
		/**
289
		 * Fires after the donors log actions form.
290
		 *
291
		 * @since 1.0
292
		 */
293
		do_action( 'give_logs_donors_table_bottom' );
294
		?>
295
	</div>
296
	<?php
297
}
298
299
add_action( 'give_reports_view_donors', 'give_reports_donors_table' );
300
301
/**
302
 * Renders the Gateways Table
303
 *
304
 * @since 1.3
305
 * @uses  Give_Gateway_Reports_Table::prepare_items()
306
 * @uses  Give_Gateway_Reports_Table::display()
307
 * @return void
308
 */
309
function give_reports_gateways_table() {
310
	include( dirname( __FILE__ ) . '/class-gateways-reports-table.php' );
311
312
	$give_table = new Give_Gateway_Reports_Table();
313
	$give_table->prepare_items();
314
	$give_table->display();
315
}
316
317
add_action( 'give_reports_view_gateways', 'give_reports_gateways_table' );
318
319
/**
320
 * Renders the Reports Earnings Graphs
321
 *
322
 * @since 1.0
323
 * @return void
324
 */
325
function give_reports_earnings() {
326
	?>
327
	<div class="tablenav top reports-table-nav">
328
		<h3 class="alignleft reports-earnings-title"><span><?php esc_html_e( 'Income Report', 'give' ); ?></span></h3>
329
	</div>
330
	<?php
331
	give_reports_graph();
332
}
333
334
add_action( 'give_reports_view_earnings', 'give_reports_earnings' );
335
336
337
/**
338
 * Retrieves estimated monthly earnings and sales
339
 *
340
 * @since 1.0
341
 * @return array
342
 */
343
function give_estimated_monthly_stats() {
344
345
	$estimated = Give_Cache::get( 'give_estimated_monthly_stats', true );
346
347
	if ( false === $estimated ) {
348
349
		$estimated = array(
350
			'earnings' => 0,
351
			'sales'    => 0,
352
		);
353
354
		$stats = new Give_Payment_Stats;
355
356
		$to_date_earnings = $stats->get_earnings( 0, 'this_month' );
0 ignored issues
show
'this_month' is of type string, 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...
357
		$to_date_sales    = $stats->get_sales( 0, 'this_month' );
0 ignored issues
show
'this_month' is of type string, 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...
358
359
		$current_day   = date( 'd', current_time( 'timestamp' ) );
360
		$current_month = date( 'n', current_time( 'timestamp' ) );
361
		$current_year  = date( 'Y', current_time( 'timestamp' ) );
362
		$days_in_month = cal_days_in_month( CAL_GREGORIAN, $current_month, $current_year );
363
364
		$estimated['earnings'] = ( $to_date_earnings / $current_day ) * $days_in_month;
365
		$estimated['sales']    = ( $to_date_sales / $current_day ) * $days_in_month;
366
367
		// Cache for one day
368
		Give_Cache::set( 'give_estimated_monthly_stats', $estimated, DAY_IN_SECONDS, true );
369
	}
370
371
	return maybe_unserialize( $estimated );
372
}
373
374
/**
375
 * Assign Get form method for reporting tabs
376
 *
377
 * @since 1.8.12
378
 *
379
 * @return string
380
 */
381
function give_reports_set_form_method() {
382
	return 'get';
383
}
384
add_filter( 'give-reports_form_method_tab_forms', 'give_reports_set_form_method', 10 );
385
add_filter( 'give-reports_form_method_tab_donors', 'give_reports_set_form_method', 10 );
386
387
// @TODO: After release 1.8 Donations -> Reports generates with new setting api, so we can remove some old code from this file.
388