Completed
Push — develop ( 91529b...8dd1dd )
by Zack
18:37
created

Admin_Request   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 76.67%

Importance

Changes 0
Metric Value
dl 0
loc 68
ccs 23
cts 30
cp 0.7667
rs 10
c 0
b 0
f 0
wmc 14
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
C is_admin() 0 58 14
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() ) ) {
30 1
			return true;
31
		}
32
33 1
		$hook    = \GV\Utils::get( $args, 0, '' );
34 1
		$context = \GV\Utils::get( $args, 1, null );
35
36
		/**
37
		 * Assume false by default.
38
		 */
39 1
		$is_page = false;
40
41 1
		if( function_exists( '\get_current_screen' ) ) {
42 1
			$current_screen = \get_current_screen();
43
		} else {
44
			$current_screen = false;
45
		}
46
47 1
		if ( $current_screen && $current_screen->post_type == 'gravityview' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
48 1
			if ( $is_gv_edit_list = 'edit' === $current_screen->base ) {
49
				$is_page = 'views';
50 1
			} elseif ( $is_gv_edit_single = 'post' === $current_screen->base ) {
51
				$is_page = 'single';
52 1
			} elseif ( $is_gv_settings = 'gravityview_page_gravityview_settings' === $current_screen->id ) {
53 1
				$is_page = 'settings';
54 1
			} elseif( $is_extensions = 'gravityview_page_gv-admin-installer' === $current_screen->id ) {
55 1
				$is_page = 'downloads';
56 1
			} elseif( $is_changelog = 'gravityview_page_gv-changelog' === $current_screen->id ) {
57
				$is_page = 'changelog';
58 1
			} elseif( $is_getting_started = 'gravityview_page_gv-getting-started' === $current_screen->id ) {
59 1
				$is_page = 'getting-started';
60
			} elseif( $is_credits = 'gravityview_page_gv-credits' === $current_screen->id ) {
61
				$is_page = 'credits';
62
			}
63
		}
64
65
		/**
66
		 * @filter `gravityview_is_admin_page` Is the current admin page a GravityView-related page?
67
		 * @param[in,out] string|bool $is_page If false, no. If string, the name of the page (`single`, `settings`, or `views`)
68
		 * @param[in] string $hook The name of the page to check against. Is passed to the method.
69
		 */
70 1
		$is_page = apply_filters( 'gravityview_is_admin_page', $is_page, $hook );
71
72
		// If the current page is the same as the compared page
73 1
		if ( ! empty( $context ) ) {
74 1
			return $is_page === $context;
75
		}
76
77 1
		return $is_page;
78
	}
79
}
80