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

admin-pages.php ➔ give_is_admin_page()   F

Complexity

Conditions 208
Paths 4864

Size

Total Lines 241
Code Lines 188

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 25786.6923
Metric Value
cc 208
eloc 188
nc 4864
nop 2
dl 0
loc 241
ccs 36
cts 224
cp 0.1607
crap 25786.6923
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 29 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
 * Admin Pages
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Pages
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 1
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Creates the admin submenu pages under the Give menu and assigns their
19
 * links to global variables
20
 *
21
 * @since 1.0
22
 *
23
 * @global $give_settings_page
24
 * @global $give_payments_page
25
 * @global $give_campaigns_page
26
 *
27
 * @return void
28
 */
29
function give_add_options_links() {
30
	global $give_settings_page, $give_payments_page, $give_reports_page, $give_add_ons_page, $give_upgrades_screen, $give_donors_page;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
31
32
	//Campaigns
33
	//	$give_campaigns      = get_post_type_object( 'give_campaigns' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
34
	//	$give_campaigns_page = add_submenu_page( 'edit.php?post_type=give_forms', $give_campaigns->labels->menu_name, $give_campaigns->labels->add_new, 'edit_' . $give_campaigns->capability_type . 's', 'post-new.php?post_type=give_campaigns', null );
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% 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...
35
36
	//Payments
37
	$give_payment       = get_post_type_object( 'give_payment' );
38
	$give_payments_page = add_submenu_page( 'edit.php?post_type=give_forms', $give_payment->labels->name, $give_payment->labels->menu_name, 'edit_give_payments', 'give-payment-history', 'give_payment_history_page' );
39
40
	//Donors
41
	$give_donors_page = add_submenu_page( 'edit.php?post_type=give_forms', __( 'Donors', 'give' ), __( 'Donors', 'give' ), 'view_give_reports', 'give-donors', 'give_customers_page' );
42
43
	//Reports
44
	$give_reports_page = add_submenu_page( 'edit.php?post_type=give_forms', __( 'Donation Reports', 'give' ), __( 'Reports', 'give' ), 'view_give_reports', 'give-reports', 'give_reports_page' );
45
46
	//Settings
47
	$give_settings_page = add_submenu_page( 'edit.php?post_type=give_forms', __( 'Give Settings', 'give' ), __( 'Settings', 'give' ), 'manage_give_settings', 'give-settings', array(
48
		Give()->give_settings,
49
		'admin_page_display'
50
	) );
51
52
	//Add-ons
53
	$give_add_ons_page = add_submenu_page( 'edit.php?post_type=give_forms', __( 'Give Add-ons', 'give' ), __( 'Add-ons', 'give' ), 'install_plugins', 'give-addons', 'give_add_ons_page' );
54
55
	//Upgrades
56
	$give_upgrades_screen = add_submenu_page( null, __( 'Give Upgrades', 'give' ), __( 'Give Upgrades', 'give' ), 'manage_give_settings', 'give-upgrades', 'give_upgrades_screen' );
57
58
59
}
60
61 1
add_action( 'admin_menu', 'give_add_options_links', 10 );
62
63
/**
64
 *  Determines whether the current admin page is a Give admin page.
65
 *
66
 *  Only works after the `wp_loaded` hook, & most effective
67
 *  starting on `admin_menu` hook.
68
 *
69
 * @since 1.0
70
 *
71
 * @param string $passed_page Optional. Main page's slug
72
 * @param string $passed_view Optional. Page view ( ex: `edit` or `delete` )
73
 *
74
 * @return bool True if Give admin page.
75
 */
76
function give_is_admin_page( $passed_page = '', $passed_view = '' ) {
77
78 2
	global $pagenow, $typenow;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
79
80 2
	$found     = false;
81 2
	$post_type = isset( $_GET['post_type'] ) ? strtolower( $_GET['post_type'] ) : false;
82 2
	$action    = isset( $_GET['action'] ) ? strtolower( $_GET['action'] ) : false;
83 2
	$taxonomy  = isset( $_GET['taxonomy'] ) ? strtolower( $_GET['taxonomy'] ) : false;
84 2
	$page      = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : false;
85 2
	$view      = isset( $_GET['view'] ) ? strtolower( $_GET['view'] ) : false;
86 2
	$tab       = isset( $_GET['tab'] ) ? strtolower( $_GET['tab'] ) : false;
87
88
	switch ( $passed_page ) {
89 2
		case 'give_forms':
90
			switch ( $passed_view ) {
91
				case 'list-table':
92
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' ) {
93
						$found = true;
94
					}
95
					break;
96
				case 'edit':
97
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'post.php' ) {
98
						$found = true;
99
					}
100
					break;
101
				case 'new':
102
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'post-new.php' ) {
103
						$found = true;
104
					}
105
					break;
106
				default:
107
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) || 'give_forms' === $post_type || ( 'post-new.php' == $pagenow && 'give_forms' === $post_type ) ) {
108
						$found = true;
109
					}
110
					break;
111
			}
112
			break;
113 2
		case 'categories':
114
			switch ( $passed_view ) {
115
				case 'list-table':
116
				case 'new':
117
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'edit' !== $action && 'give_forms_category' === $taxonomy ) {
118
						$found = true;
119
					}
120
					break;
121
				case 'edit':
122
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'edit' === $action && 'give_forms_category' === $taxonomy ) {
123
						$found = true;
124
					}
125
					break;
126
				default:
127
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'give_forms_category' === $taxonomy ) {
128
						$found = true;
129
					}
130
					break;
131
			}
132
			break;
133 2
		case 'tags':
134
			switch ( $passed_view ) {
135
				case 'list-table':
136
				case 'new':
137
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'edit' !== $action && 'give_forms_tag' === $taxonomy ) {
138
						$found = true;
139
					}
140
					break;
141
				case 'edit':
142
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'edit' === $action && 'give_forms_tag' === $taxonomy ) {
143
						$found = true;
144
					}
145
					break;
146
				default:
147
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'give_forms_tag' === $taxonomy ) {
148
						$found = true;
149
					}
150
					break;
151
			}
152
			break;
153 2
		case 'payments':
154
			switch ( $passed_view ) {
155
				case 'list-table':
156
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-payment-history' === $page && false === $view ) {
157
						$found = true;
158
					}
159
					break;
160
				case 'edit':
161
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-payment-history' === $page && 'view-order-details' === $view ) {
162
						$found = true;
163
					}
164
					break;
165
				default:
166
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-payment-history' === $page ) {
167
						$found = true;
168
					}
169
					break;
170
			}
171
			break;
172 2
		case 'reports':
173
			switch ( $passed_view ) {
174
				// If you want to do something like enqueue a script on a particular report's duration, look at $_GET[ 'range' ]
175
				case 'earnings':
176
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page && ( 'earnings' === $view || '-1' === $view || false === $view ) ) {
177
						$found = true;
178
					}
179
					break;
180
				case 'donors':
181
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page && 'customers' === $view ) {
182
						$found = true;
183
					}
184
					break;
185
				case 'gateways':
186
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page && 'gateways' === $view ) {
187
						$found = true;
188
					}
189
					break;
190
				case 'export':
191
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page && 'export' === $view ) {
192
						$found = true;
193
					}
194
					break;
195
				case 'logs':
196
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page && 'logs' === $view ) {
197
						$found = true;
198
					}
199
					break;
200
				default:
201
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page ) {
202
						$found = true;
203
					}
204
					break;
205
			}
206
			break;
207 2
		case 'settings':
208
			switch ( $passed_view ) {
209
				case 'general':
210
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && ( 'general' === $tab || false === $tab ) ) {
211
						$found = true;
212
					}
213
					break;
214
				case 'gateways':
215
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'gateways' === $tab ) {
216
						$found = true;
217
					}
218
					break;
219
				case 'emails':
220
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'emails' === $tab ) {
221
						$found = true;
222
					}
223
					break;
224
				case 'display':
225
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'display' === $tab ) {
226
						$found = true;
227
					}
228
					break;
229
				case 'licenses':
230
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'licenses' === $tab ) {
231
						$found = true;
232
					}
233
					break;
234
				case 'api':
235
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'api' === $tab ) {
236
						$found = true;
237
					}
238
					break;
239
				case 'advanced':
240
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'advanced' === $tab ) {
241
						$found = true;
242
					}
243
					break;
244
				case 'system_info':
245
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'system_info' === $tab ) {
246
						$found = true;
247
					}
248
					break;
249
				default:
250
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page ) {
251
						$found = true;
252
					}
253
					break;
254
			}
255
			break;
256 2
		case 'addons':
257
			if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-addons' === $page ) {
258
				$found = true;
259
			}
260
			break;
261 2
		case 'donors':
262
			switch ( $passed_view ) {
263
				case 'list-table':
264
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-donors' === $page && false === $view ) {
265
						$found = true;
266
					}
267
					break;
268
				case 'overview':
269
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-donors' === $page && 'overview' === $view ) {
270
						$found = true;
271
					}
272
					break;
273
				case 'notes':
274
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-donors' === $page && 'notes' === $view ) {
275
						$found = true;
276
					}
277
					break;
278
				default:
279
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-donors' === $page ) {
280
						$found = true;
281
					}
282
					break;
283
			}
284
			break;
285 2
		case 'reports':
286
			if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page ) {
287
				$found = true;
288
			}
289
			break;
290 2
		default:
291 2
			global $give_payments_page, $give_settings_page, $give_reports_page, $give_system_info_page, $give_add_ons_page, $give_settings_export, $give_upgrades_screen, $give_customers_page;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
292
293 2
			$admin_pages = apply_filters( 'give_admin_pages', array(
294 2
				$give_payments_page,
295 2
				$give_settings_page,
296 2
				$give_reports_page,
297 2
				$give_system_info_page,
298 2
				$give_add_ons_page,
299 2
				$give_upgrades_screen,
300 2
				$give_settings_export,
301
				$give_customers_page
302 2
			) );
303 2
			if ( 'give_forms' == $typenow || 'index.php' == $pagenow || 'post-new.php' == $pagenow || 'post.php' == $pagenow ) {
304
				$found = true;
305
				if ( 'give-upgrades' === $page ) {
306
					$found = false;
307
				}
308 2
			} elseif ( in_array( $pagenow, $admin_pages ) ) {
309 1
				$found = true;
310 1
			}
311 2
			break;
312 2
	}
313
314 2
	return (bool) apply_filters( 'give_is_admin_page', $found, $page, $view, $passed_page, $passed_view );
315
316
}