class-admin-page.php
7.4 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* Controller for admin pages.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\Core\Controllers
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\Core\Controllers;
// Abort if called directly.
defined( 'WPINC' ) || die;
use WPMUDEV_BLC\Core\Utils\Abstracts\Base;
use WPMUDEV_BLC\Core\Traits\Enqueue;
use WPMUDEV_BLC\Core\Utils\Utilities;
use function get_current_screen;
/**
* Class Admin_Page
*
* @package WPMUDEV_BLC\Core\Controllers
*/
abstract class Admin_Page extends Base {
/**
* Use the Enqueue Trait.
*
* @since 2.0.0
*/
use Enqueue;
/**
* The Admin Page's Menu Type.
*
* @since 2.0.0
* @var bool $is_submenu Set to true if page uses submenu.
*
*/
protected $is_submenu = false;
/**
* The Admin SubPage's Parent Slug.
*
* @since 2.0.0
* @var string $parent_slug The slug of the parent admin menu.
*
*/
protected $parent_slug;
/**
* The Admin Page's Title.
*
* @since 2.0.0
* @var string $page_title The text to be displayed in the title tags of the page when the menu is selected.
*
*/
protected $page_title;
/**
* The Admin Menu's Title.
*
* @since 2.0.0
* @var string $menu_title The text to be used for the menu.
*
*/
protected $menu_title;
/**
* Unique ID. Used for unique DOM element id.
*
* @since 2.0.0
* @var string|int $unique_id Unique ID.
*
*/
protected $unique_id;
/**
* The Admin Menu's capability.
*
* @since 2.0.0
* @var string $capability The capability required for this menu to be displayed to the user.
*
*/
protected $capability = 'manage_options';
/**
* The Admin Menu's Slug.
*
* @since 2.0.0
* @var string $menu_slug The slug name to refer to this menu by. Should be unique.
*
*/
protected $menu_slug;
/**
* The Admin Menu's Icon.
*
* @since 2.0.0
* @var string $icon_url The URL to the icon to be used for this menu.
*
*/
protected $icon_url = '';
/**
* The Admin Menu's position.
*
* @since 2.0.0
* @var int $position The position in the menu order this item should appear.
*
*/
protected $position = null;
/**
* Init Admin Page
*
* @since 2.0.0
*
* @return void Initialize the Admin_Page.
*/
public function init() {
if (
apply_filters(
'wpmudev_blc_admin_page_abort_load',
! is_admin() || wp_doing_ajax() || ( defined( 'WP_CLI' ) && WP_CLI ),
$this::instance()
)
) {
return;
}
$this->prepare_props();
$this->actions();
}
/**
* Prepares the properties of the Admin Page.
*
* @since 2.0.0
*
* @return void Prepares of the admin page.
*/
abstract public function prepare_props();
/**
* Add Actions
*
* @since 2.0.0
*
* @return void Add the Actions.
*/
public function actions() {
/**
* Until multisites are officially supported, BLC v2 menus are disabled in subsites.
* Legacy menus are loaded instead.
*/
if ( Utilities::is_subsite() ) {
return;
}
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_menu', array( $this, 'admin_submenu' ), 99 );
add_action( 'admin_init', array( $this, 'boot' ) );
}
/**
* Set the admin_menu for the Admin Page.
*
* @since 2.0.0
*
* @return void Creates the admin page menu(s). Should check if menu or submenu.
*/
public function admin_menu() {
if ( ! $this->is_submenu() && ! empty( $this->page_prop( 'menu_slug' ) ) ) {
add_menu_page(
$this->page_prop( 'page_title' ),
$this->page_prop( 'menu_title' ),
$this->page_prop( 'capability' ),
$this->page_prop( 'menu_slug' ),
array( $this, 'output' ),
$this->page_prop( 'icon_url' ),
$this->page_prop( 'position' )
);
}
}
/**
* Returns true if page uses submenu.
*
* @since 2.0.0
*
* @return bool Returns true if page uses submenu..
*/
private function is_submenu() {
return $this->is_submenu;
}
/**
* Get Admin Page's properties.
*
* @since 2.0.0
*
* @return string|int|null Returns true if page uses submenu.
*/
private function page_prop( $prop ) {
$properties = $this->get_properties();
return isset( $properties[ $prop ] ) ? $properties[ $prop ] : null;
}
/**
* Admin Page's Properties.
*
* @since 2.0.0
*
* @return array An array with all Page's Properties.
*/
public function get_properties() {
return apply_filters(
'wpmudev_blc_admin_page_props',
array(
'parent_slug' => $this->parent_slug,
'page_title' => $this->page_title,
'menu_title' => $this->menu_title,
'capability' => $this->capability,
'menu_slug' => $this->menu_slug,
'icon_url' => $this->icon_url,
'position' => $this->position,
),
$this->menu_slug,
$this
);
}
/**
* Set the admin_submenu for the Admin Page.
*
* @since 2.0.0
*
* @return void Creates the admin sub menu(s). Should check if menu or submenu.
*/
public function admin_submenu() {
global $submenu;
if ( isset( $submenu[ $this->page_prop( 'parent_slug' ) ] ) ) {
remove_submenu_page( $this->page_prop( 'parent_slug' ), $this->page_prop( 'parent_slug' ) );
}
if ( $this->is_submenu() && ! empty( $this->page_prop( 'parent_slug' ) ) ) {
add_submenu_page(
$this->page_prop( 'parent_slug' ),
$this->page_prop( 'page_title' ),
$this->page_prop( 'menu_title' ),
$this->page_prop( 'capability' ),
$this->page_prop( 'menu_slug' ),
array( $this, 'output' ),
$this->page_prop( 'position' )
);
}
}
/**
* Admin init actions.
*
* @since 2.0.0
*
* @return void Admin init actions.
*/
public function boot() {
add_action( 'current_screen', array( $this, 'current_screen_actions' ) );
}
/**
* Current screen actions.
*
* @since 2.0.0
*
* @return void Current screen actions.
*/
public function current_screen_actions() {
if ( $this->can_boot() ) {
$this->prepare_scripts();
$this->page_hooks();
add_filter( 'admin_body_class', array( $this, 'admin_body_classes' ), 999 );
}
}
/**
* Checks if admin page actions/scripts should load in current screen.
*
* @since 2.0.0
*
* @return boolean Checks if admin page actions/scripts should load. Useful for enqueuing scripts.
*/
public function can_boot() {
// Using strpos of the menu_slug, so it can be checked dynamically for toplevel or not.
// Should also take care of translated menu_slugs.
return (
is_admin() &&
is_callable( '\get_current_screen' ) &&
isset( get_current_screen()->id ) &&
strpos( get_current_screen()->id, $this->menu_slug )
);
}
/**
* Adds Page specific hooks. Extends $this->actions.
*
* @since 2.0.0
*
* @return void
*/
public function page_hooks() {
}
/**
* Adds SUI admin body class. It will be used in all admin pages.
*
* @param $classes
*
* @return string
*/
public function admin_body_classes( $classes ) {
$sui_classes = explode( ' ', $classes );
$sui_classes[] = BLC_SHARED_UI_VERSION;
if ( apply_filters( 'wpmudev_branding_hide_branding', false ) ) {
$sui_classes[] = 'wpmudev-hide-branding';
}
return join( ' ', $sui_classes );
}
/**
* Register scripts for the admin page.
*
* @since 2.0.0
*
* @return array Register scripts for the admin page.
*/
public function set_scripts() {
return array();
}
/**
* Admin Menu Output.
*
* @since 2.0.0
*
* @return void The output function of the Admin Menu Page.
*/
abstract public function output();
}