Repository navigation
Expand file tree
/
Copy pathclass-command-palette.php
More file actions
270 lines (228 loc) · 6.67 KB
/
Copy pathclass-command-palette.php
File metadata and controls
270 lines (228 loc) · 6.67 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
<?php
/**
* Admin command palette helper.
*
* @package WebberZone\Settings_API
*/
namespace WebberZone\Settings_API\Admin;
use WebberZone\Settings_API\Util\Hook_Registry;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Registers wp-admin command palette commands for a plugin.
*
* Copy-paste friendly: adjust namespaces and configuration per plugin.
*
* @since 3.0.0
*/
class Command_Palette {
private const SCRIPT_HANDLE = 'wz-command-palette-v1';
/**
* Resolved configuration.
*
* @var array<string, mixed>
*/
public array $config = array();
/**
* Constructor.
*
* @param array<string, mixed> $config Configuration arguments.
*/
public function __construct( array $config ) {
$this->config = wp_parse_args(
$config,
array(
'capability' => 'manage_options',
'allow_network' => false,
'prefix' => '',
'product_label' => '',
'commands' => array(),
)
);
$this->hooks();
}
/**
* Register hooks.
*/
public function hooks(): void {
Hook_Registry::add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ), 20 );
}
/**
* Enqueue the shared script and register this plugin's commands.
*/
public function enqueue_assets(): void {
global $wp_version;
if ( version_compare( $wp_version, '6.9', '<' ) || ! wp_script_is( 'wp-core-commands', 'enqueued' ) ) {
return;
}
if ( is_network_admin() && ! $this->config['allow_network'] ) {
return;
}
$commands = $this->get_commands();
if ( empty( $commands ) ) {
return;
}
$this->register_shared_script();
wp_enqueue_script( self::SCRIPT_HANDLE );
$payload = wp_json_encode(
array(
'commands' => $commands,
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
),
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
);
if ( ! is_string( $payload ) ) {
return;
}
wp_add_inline_script(
self::SCRIPT_HANDLE,
'window.wzRegisterCommandPaletteV1(' . $payload . ');',
'after'
);
}
/**
* Build the command list after sanitization and filters.
*
* @return array<int, array<string, mixed>>
*/
public function get_commands(): array {
$prefix = sanitize_key( (string) $this->config['prefix'] );
if ( '' === $prefix || '' === trim( (string) $this->config['product_label'], " \f\n\r\t\v\0" ) ) {
return array();
}
/**
* Filter command definitions before validation.
*
* @since 3.0.0
*
* @param array<int, array<string, mixed>> $commands Command definitions.
*/
$commands = apply_filters( "{$prefix}_command_palette_commands", $this->config['commands'] );
return $this->sanitize_commands( (array) $commands );
}
/**
* Register compatible script copies under the same handle.
*/
private function register_shared_script(): void {
if ( wp_script_is( self::SCRIPT_HANDLE, 'registered' ) ) {
return;
}
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_register_script(
self::SCRIPT_HANDLE,
plugin_dir_url( __FILE__ ) . 'js/command-palette' . $suffix . '.js',
array( 'wp-commands', 'wp-data', 'wp-a11y' ),
'3.0.0',
true
);
}
/**
* Sanitize command definitions and drop entries the user cannot run.
*
* @param array<int, array<string, mixed>> $commands Raw commands.
*
* @return array<int, array<string, mixed>>
*/
public function sanitize_commands( array $commands ): array {
$sanitized = array();
foreach ( $commands as $command ) {
$entry = $this->sanitize_command( $command );
if ( null === $entry ) {
continue;
}
$sanitized[] = $entry;
}
return $sanitized;
}
/**
* Validate one command and prepare its client payload.
*
* @param mixed $command Raw command definition.
* @return array<string, mixed>|null
*/
private function sanitize_command( $command ): ?array {
if ( ! is_array( $command ) ) {
return null;
}
$slug = $command['slug'] ?? '';
$label = $command['label'] ?? '';
if ( ! is_string( $slug ) || ! preg_match( '/^[a-z0-9_-]+$/D', $slug ) || ! is_string( $label ) || '' === trim( $label, " \f\n\r\t\v\0" ) ) {
return null;
}
$capability = $command['capability'] ?? $this->config['capability'];
if ( ! is_string( $capability ) || '' === $capability || ! current_user_can( $capability ) ) {
return null;
}
$label = $this->config['product_label'] . ': ' . $label;
$entry = array(
'name' => 'webberzone/' . sanitize_key( $this->config['prefix'] ) . '/' . $slug,
'label' => $label,
'keywords' => $this->sanitize_keywords( $command['keywords'] ?? array() ),
);
switch ( $command['type'] ?? 'navigate' ) {
case 'navigate':
$url = $command['url'] ?? '';
if ( ! is_string( $url ) ) {
return null;
}
$url = esc_url_raw( $url, array( 'http', 'https' ) );
if ( ! in_array( wp_parse_url( $url, PHP_URL_SCHEME ), array( 'http', 'https' ), true ) || ! wp_parse_url( $url, PHP_URL_HOST ) ) {
return null;
}
$entry['url'] = $url;
$entry['openInNewTab'] = ! empty( $command['open_in_new_tab'] );
return $entry;
case 'action':
$action = $this->sanitize_action( $command );
return null === $action ? null : array_merge( $entry, $action );
}
return null;
}
/**
* Validate the configuration for an authenticated AJAX handler.
*
* @param array<string, mixed> $command Raw action definition.
* @return array<string, string>|null
*/
private function sanitize_action( array $command ): ?array {
foreach ( array( 'ajax_action', 'nonce_action', 'nonce_field', 'confirm_message', 'success_message', 'error_message' ) as $field ) {
if ( ! isset( $command[ $field ] ) || ! is_string( $command[ $field ] ) || '' === trim( $command[ $field ], " \f\n\r\t\v\0" ) ) {
return null;
}
}
if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/D', $command['ajax_action'] ) ||
! preg_match( '/^[a-zA-Z0-9_-]+$/D', $command['nonce_field'] ) ||
'action' === $command['nonce_field'] ) {
return null;
}
return array(
'action' => $command['ajax_action'],
'nonce' => wp_create_nonce( $command['nonce_action'] ),
'nonceField' => $command['nonce_field'],
'confirmMessage' => $command['confirm_message'],
'successMessage' => $command['success_message'],
'errorMessage' => $command['error_message'],
);
}
/**
* Sanitize keyword strings.
*
* @param mixed $keywords Raw keywords.
*
* @return array<int, string>
*/
private function sanitize_keywords( $keywords ): array {
if ( ! is_array( $keywords ) ) {
return array();
}
$sanitized = array();
foreach ( $keywords as $keyword ) {
if ( ! is_string( $keyword ) || '' === $keyword ) {
continue;
}
$sanitized[] = sanitize_text_field( $keyword );
}
return array_values( array_unique( $sanitized ) );
}
}