Completed
Push — issues/1796 ( 71eafd...b53179 )
by Ravinder
24:17 queued 04:47
created

admin-pages.php ➔ give_add_options_links()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 83
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 58
nc 1
nop 0
dl 0
loc 83
rs 8.7468
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 32 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     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
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_reports_page
26
 * @global $give_add_ons_page
27
 * @global $give_upgrades_screen
28
 * @global $give_donors_page
29
 *
30
 * @return void
31
 */
32
function give_add_options_links() {
33
	global $give_settings_page, $give_payments_page, $give_reports_page, $give_add_ons_page, $give_upgrades_screen, $give_donors_page, $give_tools_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...
34
35
	//Payments
36
	$give_payment       = get_post_type_object( 'give_payment' );
37
	$give_payments_page = add_submenu_page(
38
		'edit.php?post_type=give_forms',
39
		$give_payment->labels->name,
40
		$give_payment->labels->menu_name,
41
		'edit_give_payments',
42
		'give-payment-history',
43
		'give_payment_history_page'
44
	);
45
46
	//Donors
47
	$give_donors_page = add_submenu_page(
48
		'edit.php?post_type=give_forms',
49
		esc_html__( 'Donors', 'give' ),
50
		esc_html__( 'Donors', 'give' ),
51
		'view_give_reports',
52
		'give-donors',
53
		'give_donors_page'
54
	);
55
56
	//Reports`
57
	$give_reports_page = add_submenu_page(
58
		'edit.php?post_type=give_forms',
59
		esc_html__( 'Donation Reports', 'give' ),
60
		esc_html__( 'Reports', 'give' ),
61
		'view_give_reports',
62
		'give-reports',
63
		array(
64
			Give()->give_settings,
65
			'output',
66
		)
67
	);
68
69
	//Settings
70
	$give_settings_page = add_submenu_page(
71
		'edit.php?post_type=give_forms',
72
		esc_html__( 'Give Settings', 'give' ),
73
		esc_html__( 'Settings', 'give' ),
74
		'manage_give_settings',
75
		'give-settings',
76
		array(
77
			Give()->give_settings,
78
			'output',
79
		)
80
	);
81
82
	//Tools.
83
	$give_tools_page = add_submenu_page(
84
		'edit.php?post_type=give_forms',
85
		esc_html__( 'Give Tools', 'give' ),
86
		esc_html__( 'Tools', 'give' ),
87
		'manage_give_settings',
88
		'give-tools',
89
		array(
90
			Give()->give_settings,
91
			'output',
92
		)
93
	);
94
95
	//Add-ons
96
	$give_add_ons_page = add_submenu_page(
97
		'edit.php?post_type=give_forms',
98
		esc_html__( 'Give Add-ons', 'give' ),
99
		esc_html__( 'Add-ons', 'give' ),
100
		'install_plugins',
101
		'give-addons',
102
		'give_add_ons_page'
103
	);
104
105
	//Upgrades
106
	$give_upgrades_screen = add_submenu_page(
107
		null,
108
		esc_html__( 'Give Upgrades', 'give' ),
109
		esc_html__( 'Give Upgrades', 'give' ),
110
		'manage_give_settings',
111
		'give-upgrades',
112
		'give_upgrades_screen'
113
	);
114
}
115
116
add_action( 'admin_menu', 'give_add_options_links', 10 );
117
118
/**
119
 *  Determines whether the current admin page is a Give admin page.
120
 *
121
 *  Only works after the `wp_loaded` hook, & most effective
122
 *  starting on `admin_menu` hook.
123
 *
124
 * @since 1.0
125
 *
126
 * @param string $passed_page Optional. Main page's slug
127
 * @param string $passed_view Optional. Page view ( ex: `edit` or `delete` )
128
 *
129
 * @return bool True if Give admin page.
130
 */
131
function give_is_admin_page( $passed_page = '', $passed_view = '' ) {
132
133
	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...
134
135
	$found     = false;
136
	$post_type = isset( $_GET['post_type'] ) ? strtolower( $_GET['post_type'] ) : false;
137
	$action    = isset( $_GET['action'] ) ? strtolower( $_GET['action'] ) : false;
138
	$taxonomy  = isset( $_GET['taxonomy'] ) ? strtolower( $_GET['taxonomy'] ) : false;
139
	$page      = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : false;
140
	$view      = isset( $_GET['view'] ) ? strtolower( $_GET['view'] ) : false;
141
	$tab       = isset( $_GET['tab'] ) ? strtolower( $_GET['tab'] ) : false;
142
143
	switch ( $passed_page ) {
144
		case 'give_forms':
145
			switch ( $passed_view ) {
146
				case 'list-table':
147
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' ) {
148
						$found = true;
149
					}
150
					break;
151
				case 'edit':
152
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'post.php' ) {
153
						$found = true;
154
					}
155
					break;
156
				case 'new':
157
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'post-new.php' ) {
158
						$found = true;
159
					}
160
					break;
161
				default:
162
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) || 'give_forms' === $post_type || ( 'post-new.php' == $pagenow && 'give_forms' === $post_type ) ) {
163
						$found = true;
164
					}
165
					break;
166
			}
167
			break;
168
		case 'categories':
169
			switch ( $passed_view ) {
170
				case 'list-table':
171
				case 'new':
172
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'edit' !== $action && 'give_forms_category' === $taxonomy ) {
173
						$found = true;
174
					}
175
					break;
176
				case 'edit':
177
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'edit' === $action && 'give_forms_category' === $taxonomy ) {
178
						$found = true;
179
					}
180
					break;
181
				default:
182
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'give_forms_category' === $taxonomy ) {
183
						$found = true;
184
					}
185
					break;
186
			}
187
			break;
188
		case 'tags':
189
			switch ( $passed_view ) {
190
				case 'list-table':
191
				case 'new':
192
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'edit' !== $action && 'give_forms_tag' === $taxonomy ) {
193
						$found = true;
194
					}
195
					break;
196
				case 'edit':
197
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'edit' === $action && 'give_forms_tag' === $taxonomy ) {
198
						$found = true;
199
					}
200
					break;
201
				default:
202
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit-tags.php' && 'give_forms_tag' === $taxonomy ) {
203
						$found = true;
204
					}
205
					break;
206
			}
207
			break;
208
		case 'payments':
209
			switch ( $passed_view ) {
210
				case 'list-table':
211
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-payment-history' === $page && false === $view ) {
212
						$found = true;
213
					}
214
					break;
215
				case 'edit':
216
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-payment-history' === $page && 'view-payment-details' === $view ) {
217
						$found = true;
218
					}
219
					break;
220
				default:
221
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-payment-history' === $page ) {
222
						$found = true;
223
					}
224
					break;
225
			}
226
			break;
227
		case 'reports':
228
			switch ( $passed_view ) {
229
				// If you want to do something like enqueue a script on a particular report's duration, look at $_GET[ 'range' ]
230
				case 'earnings':
231
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page && ( 'earnings' === $view || '-1' === $view || false === $view ) ) {
232
						$found = true;
233
					}
234
					break;
235
				case 'donors':
236
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page && 'customers' === $view ) {
237
						$found = true;
238
					}
239
					break;
240
				case 'gateways':
241
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page && 'gateways' === $view ) {
242
						$found = true;
243
					}
244
					break;
245
				case 'export':
246
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page && 'export' === $view ) {
247
						$found = true;
248
					}
249
					break;
250
				case 'logs':
251
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page && 'logs' === $view ) {
252
						$found = true;
253
					}
254
					break;
255
				default:
256
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page ) {
257
						$found = true;
258
					}
259
					break;
260
			}
261
			break;
262
		case 'settings':
263
			switch ( $passed_view ) {
264
				case 'general':
265
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && ( 'general' === $tab || false === $tab ) ) {
266
						$found = true;
267
					}
268
					break;
269
				case 'gateways':
270
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'gateways' === $tab ) {
271
						$found = true;
272
					}
273
					break;
274
				case 'emails':
275
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'emails' === $tab ) {
276
						$found = true;
277
					}
278
					break;
279
				case 'display':
280
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'display' === $tab ) {
281
						$found = true;
282
					}
283
					break;
284
				case 'licenses':
285
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'licenses' === $tab ) {
286
						$found = true;
287
					}
288
					break;
289
				case 'api':
290
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'api' === $tab ) {
291
						$found = true;
292
					}
293
					break;
294
				case 'advanced':
295
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'advanced' === $tab ) {
296
						$found = true;
297
					}
298
					break;
299
				case 'system_info':
300
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page && 'system_info' === $tab ) {
301
						$found = true;
302
					}
303
					break;
304
				default:
305
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-settings' === $page ) {
306
						$found = true;
307
					}
308
					break;
309
			}
310
			break;
311
		case 'addons':
312
			if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-addons' === $page ) {
313
				$found = true;
314
			}
315
			break;
316
		case 'donors':
317
			switch ( $passed_view ) {
318
				case 'list-table':
319
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-donors' === $page && false === $view ) {
320
						$found = true;
321
					}
322
					break;
323
				case 'overview':
324
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-donors' === $page && 'overview' === $view ) {
325
						$found = true;
326
					}
327
					break;
328
				case 'notes':
329
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-donors' === $page && 'notes' === $view ) {
330
						$found = true;
331
					}
332
					break;
333
				default:
334
					if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-donors' === $page ) {
335
						$found = true;
336
					}
337
					break;
338
			}
339
			break;
340
		case 'reports':
341
			if ( ( 'give_forms' == $typenow || 'give_forms' === $post_type ) && $pagenow == 'edit.php' && 'give-reports' === $page ) {
342
				$found = true;
343
			}
344
			break;
345
		default:
346
			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_donors_page, $give_tools_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...
347
348
			$admin_pages = apply_filters( 'give_admin_pages', array(
349
				$give_payments_page,
350
				$give_settings_page,
351
				$give_reports_page,
352
				$give_system_info_page,
353
				$give_add_ons_page,
354
				$give_upgrades_screen,
355
				$give_settings_export,
356
				$give_donors_page,
357
				$give_tools_page,
358
				'widgets.php'
359
		) );
360
			if ( 'give_forms' == $typenow || 'index.php' == $pagenow || 'post-new.php' == $pagenow || 'post.php' == $pagenow ) {
361
				$found = true;
362
				if ( 'give-upgrades' === $page ) {
363
					$found = false;
364
				}
365
			} elseif ( in_array( $pagenow, $admin_pages ) ) {
366
				$found = true;
367
			}
368
			break;
369
	}
370
371
	return (bool) apply_filters( 'give_is_admin_page', $found, $page, $view, $passed_page, $passed_view );
372
373
}
374
375
376
/**
377
 * Add setting tab to give-settings page
378
 *
379
 * @since  1.8
380
 * @param  array $settings
381
 * @return array
382
 */
383
function give_settings_page_pages( $settings ) {
384
	include( 'abstract-admin-settings-page.php' );
385
	include( 'settings/class-settings-cmb2-backward-compatibility.php' );
386
387
	$settings = array(
388
		// General settings.
389
		include( GIVE_PLUGIN_DIR . 'includes/admin/settings/class-settings-general.php' ),
390
391
		// Payment Gateways Settings.
392
		include( GIVE_PLUGIN_DIR . 'includes/admin/settings/class-settings-gateways.php' ),
393
394
		// Display settings.
395
		include( GIVE_PLUGIN_DIR . 'includes/admin/settings/class-settings-display.php' ),
396
397
		// Emails settings.
398
		include( GIVE_PLUGIN_DIR . 'includes/admin/settings/class-settings-email.php' ),
399
400
		// Addons settings.
401
		include( GIVE_PLUGIN_DIR . 'includes/admin/settings/class-settings-addon.php' ),
402
403
		// License settings.
404
		include( GIVE_PLUGIN_DIR . 'includes/admin/settings/class-settings-license.php' ),
405
406
		// Advanced settings.
407
		include( GIVE_PLUGIN_DIR . 'includes/admin/settings/class-settings-advanced.php' )
408
	);
409
410
	// Output.
411
	return $settings;
412
}
413
add_filter( 'give-settings_get_settings_pages', 'give_settings_page_pages', 0, 1 );
414
415
416
/**
417
 * Add setting tab to give-settings page
418
 *
419
 * @since  1.8
420
 * @param  array $settings
421
 * @return array
422
 */
423
function give_reports_page_pages( $settings ) {
424
	include( 'abstract-admin-settings-page.php' );
425
426
	$settings = array(
427
		// Earnings.
428
		include( 'reporting/class-settings-earnings.php' ),
429
430
		// Forms.
431
		include( 'reporting/class-settings-forms.php' ),
432
433
		// Donors.
434
		include( 'reporting/class-settings-donors.php' ),
435
436
		// Gateways.
437
		include( 'reporting/class-settings-gateways.php' ),
438
439
	);
440
441
	// Output.
442
	return $settings;
443
}
444
add_filter( 'give-reports_get_settings_pages', 'give_reports_page_pages', 0, 1 );
445
446
/**
447
 * Add setting tab to give-settings page
448
 *
449
 * @since  1.8
450
 * @param  array $settings
451
 * @return array
452
 */
453
function give_tools_page_pages( $settings ) {
454
	include( 'abstract-admin-settings-page.php' );
455
456
	$settings = array(
457
		// System Info.
458
		include( GIVE_PLUGIN_DIR . 'includes/admin/tools/class-settings-system-info.php' ),
459
460
		// Logs.
461
		include( GIVE_PLUGIN_DIR . 'includes/admin/tools/class-settings-logs.php' ),
462
463
		// API.
464
		include( GIVE_PLUGIN_DIR . 'includes/admin/tools/class-settings-api.php' ),
465
466
		// Data.
467
		include( GIVE_PLUGIN_DIR . 'includes/admin/tools/class-settings-data.php' ),
468
469
		// Export.
470
		include( GIVE_PLUGIN_DIR . 'includes/admin/tools/class-settings-export.php' ),
471
	);
472
473
	// Output.
474
	return $settings;
475
}
476
add_filter( 'give-tools_get_settings_pages', 'give_tools_page_pages', 0, 1 );
477
478
/**
479
 * Set default tools page tab.
480
 *
481
 * @since  1.8
482
 * @param  string $default_tab Default tab name.
483
 * @return string
484
 */
485
function give_set_default_tab_form_tools_page( $default_tab ) {
0 ignored issues
show
Unused Code introduced by
The parameter $default_tab 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...
486
	return 'system-info';
487
}
488
add_filter( 'give_default_setting_tab_give-tools', 'give_set_default_tab_form_tools_page', 10, 1 );
489
490
491
/**
492
 * Set default reports page tab.
493
 *
494
 * @since  1.8
495
 * @param  string $default_tab Default tab name.
496
 * @return string
497
 */
498
function give_set_default_tab_form_reports_page( $default_tab ) {
0 ignored issues
show
Unused Code introduced by
The parameter $default_tab 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...
499
	return 'earnings';
500
}
501
add_filter( 'give_default_setting_tab_give-reports', 'give_set_default_tab_form_reports_page', 10, 1 );