1
|
|
|
<?php |
|
|
|
|
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 ) { |
|
|
|
|
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' ) { |
|
|
|
|
42
|
1 |
|
if ( $is_gv_edit_list = $current_screen->base == 'edit' ) { |
|
|
|
|
43
|
|
|
$is_page = 'views'; |
44
|
1 |
|
} else if ( $is_gv_edit_single = $current_screen->base == 'post' ) { |
|
|
|
|
45
|
|
|
$is_page = 'single'; |
46
|
1 |
|
} else if ( $is_gv_settings = $current_screen->id == 'gravityview_page_gravityview_settings' ) { |
|
|
|
|
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
|
|
|
|
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.