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

admin-pages.php ➔ give_add_options_links()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.4551

Importance

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