-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathAdmin_Menu.php
More file actions
158 lines (135 loc) · 3.17 KB
/
Admin_Menu.php
File metadata and controls
158 lines (135 loc) · 3.17 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
<?php
namespace Code_Snippets\Admin\Menus;
use function Code_Snippets\code_snippets;
/**
* Base class for a plugin admin menu.
*/
abstract class Admin_Menu {
/**
* The snippet page short name.
*
* @var string
*/
public string $name;
/**
* The label shown in the admin menu.
*
* @var string
*/
public string $label;
/**
* The text used for the page title.
*
* @var string
*/
public string $title;
/**
* The base slug for the top-level admin menu.
*
* @var string
*/
protected string $base_slug;
/**
* The slug for this admin menu.
*
* @var string
*/
protected string $slug;
/**
* Common JavaScript dependencies required for React components.
*
* @var string[]
*/
public static array $script_deps = [
'react',
'react-dom',
'react-jsx-runtime',
'wp-url',
'wp-i18n',
'wp-date',
'wp-element',
'wp-components',
];
/**
* Common CSS dependencies required for React components.
*
* @var string[]
*/
public static array $style_deps = [
'wp-components',
];
/**
* Constructor.
*
* @param string $name The snippet page short name.
* @param string $label The label shown in the admin menu.
* @param string $title The text used for the page title.
*/
public function __construct( string $name, string $label, string $title ) {
$this->name = $name;
$this->label = $label;
$this->title = $title;
$this->base_slug = code_snippets()->get_menu_slug();
$this->slug = code_snippets()->get_menu_slug( $name );
if ( ! code_snippets()->is_compact_menu() ) {
add_action( 'admin_menu', array( $this, 'register' ) );
add_action( 'network_admin_menu', array( $this, 'register' ) );
}
}
/**
* Add a sub-menu to the Snippets menu.
*
* @param string $slug Menu slug.
* @param string $label Label shown in admin menu.
* @param string $title Page title.
*
* @return void
*/
public function add_menu( string $slug, string $label, string $title ) {
$hook = add_submenu_page(
$this->base_slug,
$title,
$label,
code_snippets()->get_cap(),
$slug,
array( $this, 'render' )
);
add_action( 'load-' . $hook, array( $this, 'load' ) );
}
/**
* Register the admin menu
*/
public function register() {
$this->add_menu( $this->slug, $this->label, $this->title );
}
/**
* Render the navigation bar at the top of the admin page.
*/
protected function render_navigation() {
echo '<div id="code-snippets-toolbar-container"></div>';
}
/**
* Render the menu
*/
abstract public function render();
/**
* Executed when the admin page is loaded
*/
public function load() {
// Make sure the user has permission to be here.
if ( ! current_user_can( code_snippets()->get_cap() ) ) {
wp_die( esc_html__( 'You are not authorized to access this page.', 'code-snippets' ) );
}
// Create the snippet tables if they are missing.
$db = code_snippets()->db;
if ( is_multisite() ) {
$db->create_missing_table( $db->ms_table );
}
$db->create_missing_table( $db->table );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
}
/**
* Enqueue scripts and stylesheets for the admin page, if necessary
*/
abstract public function enqueue_assets();
}