Completed
Pull Request — master (#1907)
by
unknown
19:56
created

reports.php ➔ give_reports_page()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 63
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 31
nc 16
nop 0
dl 0
loc 63
rs 7.2689
c 0
b 0
f 0

How to fix   Long Method   

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 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     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';
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>
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>
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'];
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'];
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';
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>
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'] ) ) {
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'] ) ) {
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'] ) );
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
function give_reports_donors_table() {
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' );
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...
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' );
357
		$to_date_sales    = $stats->get_sales( 0, 'this_month' );
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