-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock_Type_Command.php
More file actions
353 lines (330 loc) · 9.79 KB
/
Block_Type_Command.php
File metadata and controls
353 lines (330 loc) · 9.79 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
<?php
namespace WP_CLI\Block;
use WP_CLI;
use WP_CLI\Formatter;
use WP_CLI_Command;
use WP_Block_Type;
use WP_Block_Type_Registry;
/**
* Retrieves details on registered block types.
*
* Get information on WordPress' built-in and custom block types from the
* WP_Block_Type_Registry.
*
* ## EXAMPLES
*
* # List all registered block types
* $ wp block type list
*
* # Get details about a specific block type
* $ wp block type get core/paragraph --format=json
*
* # List all core blocks
* $ wp block type list --namespace=core
*
* @package wp-cli
*/
class Block_Type_Command extends WP_CLI_Command {
/**
* Default fields to display.
*
* @var array
*/
private $fields = [
'name',
'title',
'description',
'category',
'is_dynamic',
];
/**
* Lists registered block types.
*
* ## OPTIONS
*
* [--namespace=<namespace>]
* : Filter by block namespace (e.g., 'core', 'my-plugin').
*
* [--dynamic]
* : Only show dynamic blocks (blocks with render_callback).
*
* [--static]
* : Only show static blocks (blocks without render_callback).
*
* [--field=<field>]
* : Prints the value of a single field for each block type.
*
* [--fields=<fields>]
* : Limit the output to specific block type fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* - yaml
* - ids
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each block type:
*
* * name
* * title
* * description
* * category
* * is_dynamic
*
* These fields are optionally available:
*
* * icon
* * keywords
* * parent
* * ancestor
* * supports
* * attributes
* * provides_context
* * uses_context
* * block_hooks
* * editor_script_handles
* * script_handles
* * view_script_handles
* * editor_style_handles
* * style_handles
* * view_style_handles
* * api_version
*
* ## EXAMPLES
*
* # List all registered block types
* $ wp block type list
*
* # List all core blocks
* $ wp block type list --namespace=core --fields=name,title,category
*
* # List only dynamic blocks
* $ wp block type list --dynamic --format=json
*
* # Get count of registered block types
* $ wp block type list --format=count
*
* @subcommand list
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ) {
// Validate mutually exclusive flags.
$dynamic = isset( $assoc_args['dynamic'] );
$static = isset( $assoc_args['static'] );
if ( $dynamic && $static ) {
WP_CLI::error( '--dynamic and --static are mutually exclusive.' );
}
$registry = WP_Block_Type_Registry::get_instance();
$block_types = $registry->get_all_registered();
// Filter by namespace.
if ( ! empty( $assoc_args['namespace'] ) ) {
$namespace = $assoc_args['namespace'];
$block_types = array_filter(
$block_types,
function ( $block_type ) use ( $namespace ) {
return strpos( $block_type->name, $namespace . '/' ) === 0;
}
);
unset( $assoc_args['namespace'] );
}
// Filter by dynamic/static.
if ( isset( $assoc_args['dynamic'] ) ) {
$block_types = array_filter(
$block_types,
function ( $block_type ) {
return $block_type->is_dynamic();
}
);
unset( $assoc_args['dynamic'] );
} elseif ( isset( $assoc_args['static'] ) ) {
$block_types = array_filter(
$block_types,
function ( $block_type ) {
return ! $block_type->is_dynamic();
}
);
unset( $assoc_args['static'] );
}
$formatter = $this->get_formatter( $assoc_args );
if ( 'ids' === $formatter->format ) {
$ids = array_keys( $block_types );
echo implode( ' ', $ids );
return;
}
$items = [];
foreach ( $block_types as $block_type ) {
$items[] = $this->block_type_to_array( $block_type );
}
$formatter->display_items( $items );
}
/**
* Gets details about a registered block type.
*
* ## OPTIONS
*
* <name>
* : Block type name (e.g., 'core/paragraph').
*
* [--field=<field>]
* : Instead of returning the whole block type, returns the value of a single field.
*
* [--fields=<fields>]
* : Limit the output to specific fields. Defaults to all fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* # Get details about the paragraph block
* $ wp block type get core/paragraph
*
* # Get the supports field as JSON
* $ wp block type get core/paragraph --field=supports --format=json
*
* # Get specific fields
* $ wp block type get core/image --fields=name,title,supports --format=json
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function get( $args, $assoc_args ) {
$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( $args[0] );
if ( ! $block_type ) {
WP_CLI::error( "Block type '{$args[0]}' is not registered." );
}
if ( empty( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = array_merge(
$this->fields,
[
'icon',
'keywords',
'parent',
'ancestor',
'supports',
'attributes',
'provides_context',
'uses_context',
'block_hooks',
'api_version',
]
);
}
$formatter = $this->get_formatter( $assoc_args );
$data = $this->block_type_to_array( $block_type );
$formatter->display_item( $data );
}
/**
* Checks whether a block type is registered.
*
* Exits with return code 0 if the block type exists, 1 if it does not.
*
* ## OPTIONS
*
* <name>
* : The block type name, including namespace.
*
* ## EXAMPLES
*
* # Check if a block type exists.
* $ wp block type exists core/paragraph
* Success: Block type 'core/paragraph' is registered.
*
* # Check for a non-existent block type.
* $ wp block type exists core/nonexistent
* $ echo $?
* 1
*
* @subcommand exists
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function exists_( $args, $assoc_args ) {
$registry = WP_Block_Type_Registry::get_instance();
$name = $args[0];
if ( $registry->is_registered( $name ) ) {
WP_CLI::success( "Block type '{$name}' is registered." );
} else {
WP_CLI::halt( 1 );
}
}
/**
* Converts a WP_Block_Type object to an associative array.
*
* @param WP_Block_Type $block_type Block type object.
* @return array
*/
private function block_type_to_array( $block_type ) {
return [
'name' => $block_type->name,
'title' => $this->get_block_type_property( $block_type, 'title' ),
'description' => $this->get_block_type_property( $block_type, 'description' ),
'category' => $this->get_block_type_property( $block_type, 'category' ),
'is_dynamic' => $block_type->is_dynamic(),
'icon' => $this->get_block_type_property( $block_type, 'icon' ),
'keywords' => $this->get_block_type_property( $block_type, 'keywords' ),
'parent' => $this->get_block_type_property( $block_type, 'parent' ),
'ancestor' => $this->get_block_type_property( $block_type, 'ancestor' ),
'allowed_blocks' => $this->get_block_type_property( $block_type, 'allowed_blocks' ),
'supports' => $this->get_block_type_property( $block_type, 'supports' ),
'attributes' => $this->get_block_type_property( $block_type, 'attributes' ),
'provides_context' => $this->get_block_type_property( $block_type, 'provides_context' ),
'uses_context' => $this->get_block_type_property( $block_type, 'uses_context' ),
'block_hooks' => $this->get_block_type_property( $block_type, 'block_hooks' ),
'selectors' => $this->get_block_type_property( $block_type, 'selectors' ),
'styles' => $this->get_block_type_property( $block_type, 'styles' ),
'example' => $this->get_block_type_property( $block_type, 'example' ),
'editor_script_handles' => $this->get_block_type_property( $block_type, 'editor_script_handles' ),
'script_handles' => $this->get_block_type_property( $block_type, 'script_handles' ),
'view_script_handles' => $this->get_block_type_property( $block_type, 'view_script_handles' ),
'view_script_module_ids' => $this->get_block_type_property( $block_type, 'view_script_module_ids' ),
'editor_style_handles' => $this->get_block_type_property( $block_type, 'editor_style_handles' ),
'style_handles' => $this->get_block_type_property( $block_type, 'style_handles' ),
'view_style_handles' => $this->get_block_type_property( $block_type, 'view_style_handles' ),
'api_version' => $this->get_block_type_property( $block_type, 'api_version' ),
];
}
/**
* Safely gets a property from a WP_Block_Type object.
*
* Some properties may not exist on all block types depending on WordPress
* version or how the block was registered.
*
* @param WP_Block_Type $block_type Block type object.
* @param string $property Property name.
* @return mixed|null Property value or null if not set.
*/
private function get_block_type_property( $block_type, $property ) {
return isset( $block_type->$property ) ? $block_type->$property : null;
}
/**
* Gets the formatter instance.
*
* @param array $assoc_args Associative arguments.
* @return Formatter
*/
private function get_formatter( &$assoc_args ) {
return new Formatter( $assoc_args, $this->fields, 'block-type' );
}
}