Completed
Push — develop ( afba25...ca7ea0 )
by Zack
15:50
created

Admin_Request::is_admin()   C

Complexity

Conditions 12
Paths 14

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 12.5669

Importance

Changes 0
Metric Value
cc 12
nc 14
nop 0
dl 0
loc 45
ccs 16
cts 19
cp 0.8421
crap 12.5669
rs 6.9666
c 0
b 0
f 0

How to fix   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 12 and the first side effect is on line 6.

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
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The default Dashboard Request class.
11
 */
12
class Admin_Request extends Request {
13
	/**
14
	 * Check if WordPress is_admin(), and whether we have a GravityView page.
15
	 *
16
	 * @return bool If is in an admin context or not.
17
	 *
18
	 * Accepts optional $hook and $context arguments.
19
	 * @return bool|string If `false`, not a GravityView page. `true` if $page is passed and is the same as current page. Otherwise, the name of the page (`single`, `settings`, or `views`)
20
	 */
21 1
	public static function is_admin() {
22 1
		if ( ! parent::is_admin() ) {
23
			return false;
24
		}
25
26
		/**
27
		 * Regular check.
28
		 */
29 1
		if ( ! ( $args = func_get_args() ) || count( $args ) != 2 ) {
0 ignored issues
show
introduced by
Found "!= 2". Use Yoda Condition checks, you must
Loading history...
30 1
			return true;
31
		}
32
33 1
		$context = $args[1];
34
35
		/**
36
		 * Assume false by default.
37
		 */
38 1
		$is_page = false;
39
40 1
		if ( function_exists( '\get_current_screen' ) || function_exists( 'get_current_screen' ) ) {
41 1
			if ( ( $current_screen = \get_current_screen() ) && $current_screen->post_type == 'gravityview' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
42 1
				if ( $is_gv_edit_list = $current_screen->base == 'edit' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
43
					$is_page = 'views';
44 1
				} else if ( $is_gv_edit_single = $current_screen->base == 'post' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
45
					$is_page = 'single';
46 1
				} else if ( $is_gv_settings = $current_screen->id == 'gravityview_page_gravityview_settings' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
47 1
					$is_page = 'settings';
48
				}
49
			}
50
		}
51
52
		/**
53
		 * @filter `gravityview_is_admin_page` Is the current admin page a GravityView-related page?
54
		 * @param[in,out] string|bool $is_page If false, no. If string, the name of the page (`single`, `settings`, or `views`)
55
		 * @param[in] string $hook The name of the page to check against. Is passed to the method.
56
		 */
57 1
		$is_page = apply_filters( 'gravityview_is_admin_page', $is_page, $args[0] );
58
59
		// If the current page is the same as the compared page
60 1
		if ( ! empty( $context ) ) {
61 1
			return $is_page === $context;
62
		}
63
64 1
		return $is_page;
65
	}
66
}
67