PluginProbe
Code Snippets / 3.6.3
Code Snippets v3.6.3
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 3.0.1 All 64 releases
code-snippets / php / admin-menus / class-admin-menu.php

class-admin-menu.php in Code Snippets 3.6.3, at php/admin-menus/class-admin-menu.php

238 lines 5.4 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;
4
5 /**
6 * Base class for a plugin admin menu.
7 */
8 abstract class Admin_Menu {
9
10 /**
11 * The snippet page short name.
12 *
13 * @var string
14 */
15 public $name;
16
17 /**
18 * The label shown in the admin menu.
19 *
20 * @var string
21 */
22 public $label;
23
24 /**
25 * The text used for the page title.
26 *
27 * @var string
28 */
29 public $title;
30
31 /**
32 * The base slug for the top-level admin menu.
33 *
34 * @var string
35 */
36 protected $base_slug;
37
38 /**
39 * The slug for this admin menu.
40 *
41 * @var string
42 */
43 protected $slug;
44
45 /**
46 * Constructor.
47 *
48 * @param string $name The snippet page short name.
49 * @param string $label The label shown in the admin menu.
50 * @param string $title The text used for the page title.
51 */
52 public function __construct( string $name, string $label, string $title ) {
53 $this->name = $name;
54 $this->label = $label;
55 $this->title = $title;
56
57 $this->base_slug = code_snippets()->get_menu_slug();
58 $this->slug = code_snippets()->get_menu_slug( $name );
59 }
60
61 /**
62 * Register action and filter hooks.
63 *
64 * @return void
65 */
66 public function run() {
67 if ( ! code_snippets()->is_compact_menu() ) {
68 add_action( 'admin_menu', array( $this, 'register' ) );
69 add_action( 'network_admin_menu', array( $this, 'register' ) );
70 }
71 }
72
73 /**
74 * Add a sub-menu to the Snippets menu.
75 *
76 * @param string $slug Menu slug.
77 * @param string $label Label shown in admin menu.
78 * @param string $title Page title.
79 *
80 * @return void
81 */
82 public function add_menu( string $slug, string $label, string $title ) {
83 $hook = add_submenu_page(
84 $this->base_slug,
85 $title,
86 $label,
87 code_snippets()->get_cap(),
88 $slug,
89 array( $this, 'render' )
90 );
91
92 add_action( 'load-' . $hook, array( $this, 'load' ) );
93 }
94
95 /**
96 * Register the admin menu
97 */
98 public function register() {
99 $this->add_menu( $this->slug, $this->label, $this->title );
100 }
101
102 /**
103 * Render the content of a vew template
104 *
105 * @param string $name Name of view template to render.
106 */
107 protected function render_view( string $name ) {
108 include dirname( PLUGIN_FILE ) . '/php/views/' . $name . '.php';
109 }
110
111 /**
112 * Render the menu
113 */
114 public function render() {
115 $this->render_view( $this->name );
116 }
117
118 /**
119 * Print the status and error messages
120 */
121 protected function print_messages() {
122 // None required by default.
123 }
124
125 /**
126 * Retrieve a result message based on a posted status
127 *
128 * @param array<string, string> $messages List of possible messages to display.
129 * @param string $request_var Name of $_REQUEST variable to check.
130 * @param string $class_name Class to use on buttons. Default 'updated'.
131 *
132 * @return bool Whether a result message was printed.
133 */
134 protected function print_result_message( array $messages, string $request_var = 'result', string $class_name = 'updated' ): bool {
135
136 if ( empty( $_REQUEST[ $request_var ] ) ) {
137 return false;
138 }
139
140 $result = sanitize_key( $_REQUEST[ $request_var ] );
141
142 if ( isset( $messages[ $result ] ) ) {
143 printf(
144 '<div id="message" class="%2$s fade"><p>%1$s</p></div>',
145 wp_kses_post( $messages[ $result ] ),
146 esc_attr( $class_name )
147 );
148
149 return true;
150 }
151
152 return false;
153 }
154
155 /**
156 * Executed when the admin page is loaded
157 */
158 public function load() {
159 // Make sure the user has permission to be here.
160 if ( ! current_user_can( code_snippets()->get_cap() ) ) {
161 wp_die( esc_html__( 'You are not authorized to access this page.', 'code-snippets' ) );
162 }
163
164 // Create the snippet tables if they are missing.
165 $db = code_snippets()->db;
166
167 if ( is_multisite() ) {
168 $db->create_missing_table( $db->ms_table );
169 }
170 $db->create_missing_table( $db->table );
171
172 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
173 }
174
175 /**
176 * Enqueue scripts and stylesheets for the admin page, if necessary
177 */
178 abstract public function enqueue_assets();
179
180 /**
181 * Generate a list of page title links for passing to React.
182 *
183 * @param array<string> $actions List of actions to convert into links, as array values.
184 *
185 * @return array<string, string> Link labels keyed to link URLs.
186 */
187 protected function page_title_action_links( array $actions ): array {
188 $plugin = code_snippets();
189 $links = [];
190
191 foreach ( $actions as $action ) {
192 if ( 'settings' === $action && ! isset( $plugin->admin->menus['settings'] ) ) {
193 continue;
194 }
195
196 $url = $plugin->get_menu_url( $action );
197
198 if ( isset( $_GET['type'] ) && in_array( $_GET['type'], Snippet::get_types(), true ) ) {
199 $url = add_query_arg( 'type', sanitize_key( wp_unslash( $_GET['type'] ) ), $url );
200 }
201
202 switch ( $action ) {
203 case 'manage':
204 $label = _x( 'Manage', 'snippets', 'code-snippets' );
205 break;
206 case 'add':
207 $label = _x( 'Add New', 'snippet', 'code-snippets' );
208 break;
209 case 'import':
210 $label = _x( 'Import', 'snippets', 'code-snippets' );
211 break;
212 case 'settings':
213 $label = _x( 'Settings', 'snippets', 'code-snippets' );
214 break;
215 default:
216 $label = '';
217 }
218
219 if ( $label && $url ) {
220 $links[ $label ] = $url;
221 }
222 }
223
224 return $links;
225 }
226
227 /**
228 * Render a list of links to other pages in the page title
229 *
230 * @param array<string> $actions List of actions to render as links, as array values.
231 */
232 protected function render_page_title_actions( array $actions ) {
233 foreach ( $this->page_title_action_links( $actions ) as $label => $url ) {
234 printf( '<a href="%s" class="page-title-action">%s</a>', esc_url( $url ), esc_html( $label ) );
235 }
236 }
237 }
238