Screen.php
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace AC;
use AC\Admin\RequestHandlerInterface;
use WP_Screen;
class Screen implements Registrable {
/**
* @var WP_Screen
*/
protected $screen;
public function register() {
add_action( 'current_screen', [ $this, 'init' ] );
}
/**
* @param WP_Screen $screen
*/
public function init( WP_Screen $screen ) {
$this->set_screen( $screen );
do_action( 'ac/screen', $this, $this->screen->id );
}
/**
* @param $id
*
* @return bool
*/
public function is_screen( $id ) {
return $this->has_screen() && $this->get_screen()->id === $id;
}
/**
* @param WP_Screen $screen
*
* @return $this
*/
public function set_screen( WP_Screen $screen ) {
$this->screen = $screen;
return $this;
}
/**
* @return WP_Screen
*/
public function get_screen() {
return $this->screen;
}
/**
* @return bool
*/
public function has_screen() {
return $this->screen instanceof WP_Screen;
}
/**
* @return string
*/
public function get_id() {
return $this->screen->id;
}
/**
* @return string
*/
public function get_base() {
return $this->screen->base;
}
/**
* @return string
*/
public function get_post_type() {
return $this->screen->post_type;
}
/**
* @return string|null
*/
public function get_list_screen() {
foreach ( ListScreenTypes::instance()->get_list_screens() as $list_screen ) {
if ( $list_screen->is_current_screen( $this->screen ) ) {
return $list_screen->get_key();
}
}
return null;
}
/**
* @return bool
*/
private function is_admin_network() {
return $this->screen->in_admin( 'network' );
}
/**
* @return bool
*/
public function is_list_screen() {
return null !== $this->get_list_screen();
}
/**
* Check if current screen is plugins screen
* @return bool
*/
public function is_plugin_screen() {
$id = 'plugins';
if ( $this->is_admin_network() ) {
$id .= '-network';
}
return $this->is_screen( $id );
}
/**
* @param string|null $slug
*
* @return bool
*/
public function is_admin_screen( $slug = null ) {
if ( null !== $slug ) {
return $this->is_main_admin_screen() && $slug === filter_input( INPUT_GET, RequestHandlerInterface::PARAM_TAB );
}
return $this->is_main_admin_screen();
}
/**
* @return bool
*/
private function is_main_admin_screen() {
$id = 'settings_page_' . Admin\Admin::NAME;
if ( $this->is_admin_network() ) {
$id .= '-network';
}
return $this->is_screen( $id );
}
}