PluginProbe
Code Snippets / 4.0.0-beta.2
Code Snippets v4.0.0-beta.2
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / Admin / Menus / Admin_Menu.php

Admin_Menu.php in Code Snippets 4.0.0-beta.2, at php/Admin/Menus/Admin_Menu.php

191 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\Admin\Menus;
4
5 use function Code_Snippets\code_snippets;
6
7 /**
8 * Base class for a plugin admin menu.
9 */
10 abstract class Admin_Menu {
11
12 /**
13 * The snippet page short name.
14 *
15 * @var string
16 */
17 public string $name;
18
19 /**
20 * The label shown in the admin menu.
21 *
22 * @var string
23 */
24 public string $label;
25
26 /**
27 * The text used for the page title.
28 *
29 * @var string
30 */
31 public string $title;
32
33 /**
34 * The base slug for the top-level admin menu.
35 *
36 * @var string
37 */
38 protected string $base_slug;
39
40 /**
41 * The slug for this admin menu.
42 *
43 * @var string
44 */
45 protected string $slug;
46
47 /**
48 * Common JavaScript dependencies required for React components.
49 *
50 * @var string[]
51 */
52 public static array $script_deps = [
53 'react',
54 'react-dom',
55 'react-jsx-runtime',
56 'wp-url',
57 'wp-i18n',
58 'wp-date',
59 'wp-element',
60 'wp-components',
61 ];
62
63 /**
64 * Common CSS dependencies required for React components.
65 *
66 * @var string[]
67 */
68 public static array $style_deps = [
69 'wp-components',
70 ];
71
72 /**
73 * Constructor.
74 *
75 * @param string $name The snippet page short name.
76 * @param string $label The label shown in the admin menu.
77 * @param string $title The text used for the page title.
78 */
79 public function __construct( string $name, string $label, string $title ) {
80 $this->name = $name;
81 $this->label = $label;
82 $this->title = $title;
83
84 $this->base_slug = code_snippets()->get_menu_slug();
85 $this->slug = code_snippets()->get_menu_slug( $name );
86
87 if ( ! code_snippets()->is_compact_menu() ) {
88 add_action( 'admin_menu', array( $this, 'register' ) );
89 add_action( 'network_admin_menu', array( $this, 'register' ) );
90 }
91 }
92
93 /**
94 * Add a sub-menu to the Snippets menu.
95 *
96 * @param string $slug Menu slug.
97 * @param string $label Label shown in admin menu.
98 * @param string $title Page title.
99 *
100 * @return void
101 */
102 public function add_menu( string $slug, string $label, string $title ) {
103 $hook = add_submenu_page(
104 $this->base_slug,
105 $title,
106 $label,
107 $this->menu_cap(),
108 $slug,
109 array( $this, 'render' )
110 );
111
112 add_action( 'load-' . $hook, array( $this, 'load' ) );
113 }
114
115 /**
116 * Capability required to access this menu.
117 *
118 * Defaults to the snippet access capability. Subclasses such as the
119 * settings menu override this to require the core admin capability so they
120 * remain accessible only to full administrators.
121 *
122 * @return string
123 */
124 protected function menu_cap(): string {
125 return code_snippets()->get_cap();
126 }
127
128 /**
129 * Register the admin menu
130 */
131 public function register() {
132 $this->add_menu( $this->slug, $this->label, $this->title );
133 }
134
135 /**
136 * Retrieve the WordPress hookname computed for this menu's admin page.
137 *
138 * @return string
139 */
140 public function get_hookname(): string {
141 return get_plugin_page_hookname( $this->slug, $this->base_slug );
142 }
143
144 /**
145 * Retrieve every WordPress hookname registered by this menu, including any
146 * additional pages it registers beyond its primary slug.
147 *
148 * @return string[]
149 */
150 public function get_hooknames(): array {
151 return [ $this->get_hookname() ];
152 }
153
154 /**
155 * Render the navigation bar at the top of the admin page.
156 */
157 protected function render_navigation() {
158 echo '<div id="code-snippets-toolbar-container"></div>';
159 }
160
161 /**
162 * Render the menu
163 */
164 abstract public function render();
165
166 /**
167 * Executed when the admin page is loaded
168 */
169 public function load() {
170 // Make sure the user has permission to be here.
171 if ( ! current_user_can( $this->menu_cap() ) ) {
172 wp_die( esc_html__( 'You are not authorized to access this page.', 'code-snippets' ) );
173 }
174
175 // Create the snippet tables if they are missing.
176 $db = code_snippets()->db;
177
178 if ( is_multisite() ) {
179 $db->create_missing_table( $db->ms_table );
180 }
181 $db->create_missing_table( $db->table );
182
183 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
184 }
185
186 /**
187 * Enqueue scripts and stylesheets for the admin page, if necessary
188 */
189 abstract public function enqueue_assets();
190 }
191