Skip to content

Commit a337f73

Browse files
Copilotswissspidy
andcommitted
Add block and pattern commands with WordPress 5.0+ support
Co-authored-by: swissspidy <[email protected]>
1 parent 29b129e commit a337f73

File tree

5 files changed

+596
-0
lines changed

5 files changed

+596
-0
lines changed

composer.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
"comment unspam",
6464
"comment untrash",
6565
"comment update",
66+
"block",
67+
"block get",
68+
"block list",
6669
"menu",
6770
"menu create",
6871
"menu delete",
@@ -96,13 +99,18 @@
9699
"option update",
97100
"option set-autoload",
98101
"option get-autoload",
102+
"pattern",
103+
"pattern get",
104+
"pattern list",
99105
"post",
100106
"post create",
101107
"post delete",
102108
"post edit",
103109
"post exists",
104110
"post generate",
105111
"post get",
112+
"post has-block",
113+
"post has-blocks",
106114
"post list",
107115
"post meta",
108116
"post meta add",
@@ -113,6 +121,8 @@
113121
"post meta patch",
114122
"post meta pluck",
115123
"post meta update",
124+
"post parse-blocks",
125+
"post render-blocks",
116126
"post term",
117127
"post term add",
118128
"post term list",

entity-command.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,34 @@
9292
},
9393
)
9494
);
95+
96+
// Block and pattern commands require WordPress 5.0+.
97+
WP_CLI::add_command(
98+
'block',
99+
'Block_Command',
100+
array(
101+
'before_invoke' => function () {
102+
if ( Utils\wp_version_compare( '5.0', '<' ) ) {
103+
WP_CLI::error( 'The block commands require WordPress 5.0 or greater.' );
104+
}
105+
if ( ! class_exists( 'WP_Block_Type_Registry' ) ) {
106+
WP_CLI::error( 'WP_Block_Type_Registry class not found.' );
107+
}
108+
},
109+
)
110+
);
111+
112+
WP_CLI::add_command(
113+
'pattern',
114+
'Pattern_Command',
115+
array(
116+
'before_invoke' => function () {
117+
if ( Utils\wp_version_compare( '5.5', '<' ) ) {
118+
WP_CLI::error( 'The pattern commands require WordPress 5.5 or greater.' );
119+
}
120+
if ( ! class_exists( 'WP_Block_Patterns_Registry' ) ) {
121+
WP_CLI::error( 'WP_Block_Patterns_Registry class not found.' );
122+
}
123+
},
124+
)
125+
);

src/Block_Command.php

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
<?php
2+
3+
use WP_CLI\Formatter;
4+
5+
/**
6+
* Manages block types.
7+
*
8+
* Lists and gets information about registered block types in the WordPress block editor.
9+
*
10+
* ## EXAMPLES
11+
*
12+
* # List all registered block types
13+
* $ wp block list --format=csv
14+
* name,title,description,category
15+
* core/paragraph,Paragraph,"Start with the building block of all narrative.",text
16+
*
17+
* # Get details about a specific block type
18+
* $ wp block get core/paragraph --fields=name,title,category
19+
* +----------+-----------+------+
20+
* | name | title | category |
21+
* +----------+-----------+------+
22+
* | core/paragraph | Paragraph | text |
23+
* +----------+-----------+------+
24+
*
25+
* @package wp-cli
26+
*/
27+
class Block_Command extends WP_CLI_Command {
28+
29+
private $fields = array(
30+
'name',
31+
'title',
32+
'description',
33+
'category',
34+
);
35+
36+
/**
37+
* Lists registered block types.
38+
*
39+
* ## OPTIONS
40+
*
41+
* [--field=<field>]
42+
* : Prints the value of a single field for each block type.
43+
*
44+
* [--fields=<fields>]
45+
* : Limit the output to specific block type fields.
46+
*
47+
* [--format=<format>]
48+
* : Render output in a particular format.
49+
* ---
50+
* default: table
51+
* options:
52+
* - table
53+
* - csv
54+
* - json
55+
* - count
56+
* - yaml
57+
* ---
58+
*
59+
* ## AVAILABLE FIELDS
60+
*
61+
* These fields will be displayed by default for each block type:
62+
*
63+
* * name
64+
* * title
65+
* * description
66+
* * category
67+
*
68+
* These fields are optionally available:
69+
*
70+
* * parent
71+
* * icon
72+
* * keywords
73+
* * textdomain
74+
* * supports
75+
* * styles
76+
* * variations
77+
* * api_version
78+
* * editor_script
79+
* * editor_style
80+
* * script
81+
* * style
82+
*
83+
* ## EXAMPLES
84+
*
85+
* # List all registered block types
86+
* $ wp block list
87+
* +-------------------+-------------------+----------------------------------------+----------+
88+
* | name | title | description | category |
89+
* +-------------------+-------------------+----------------------------------------+----------+
90+
* | core/paragraph | Paragraph | Start with the building block of all.. | text |
91+
* | core/heading | Heading | Introduce new sections and organize... | text |
92+
* +-------------------+-------------------+----------------------------------------+----------+
93+
*
94+
* # List all block types with 'text' category
95+
* $ wp block list --format=csv
96+
* name,title,description,category
97+
* core/paragraph,Paragraph,"Start with the building block of all narrative.",text
98+
*
99+
* @subcommand list
100+
*/
101+
public function list_( $args, $assoc_args ) {
102+
$registry = WP_Block_Type_Registry::get_instance();
103+
$blocks = $registry->get_all_registered();
104+
105+
$items = array();
106+
foreach ( $blocks as $block ) {
107+
$items[] = $this->prepare_block_for_output( $block );
108+
}
109+
110+
$formatter = $this->get_formatter( $assoc_args );
111+
$formatter->display_items( $items );
112+
}
113+
114+
/**
115+
* Gets details about a registered block type.
116+
*
117+
* ## OPTIONS
118+
*
119+
* <name>
120+
* : Block type name (e.g., core/paragraph).
121+
*
122+
* [--field=<field>]
123+
* : Instead of returning the whole block type, returns the value of a single field.
124+
*
125+
* [--fields=<fields>]
126+
* : Limit the output to specific fields. Defaults to all fields.
127+
*
128+
* [--format=<format>]
129+
* : Render output in a particular format.
130+
* ---
131+
* default: table
132+
* options:
133+
* - table
134+
* - csv
135+
* - json
136+
* - yaml
137+
* ---
138+
*
139+
* ## AVAILABLE FIELDS
140+
*
141+
* * name
142+
* * title
143+
* * description
144+
* * category
145+
* * parent
146+
* * icon
147+
* * keywords
148+
* * textdomain
149+
* * supports
150+
* * styles
151+
* * variations
152+
* * api_version
153+
* * editor_script
154+
* * editor_style
155+
* * script
156+
* * style
157+
*
158+
* ## EXAMPLES
159+
*
160+
* # Get details about the core/paragraph block type.
161+
* $ wp block get core/paragraph --fields=name,title,category
162+
* +----------------+-----------+----------+
163+
* | name | title | category |
164+
* +----------------+-----------+----------+
165+
* | core/paragraph | Paragraph | text |
166+
* +----------------+-----------+----------+
167+
*/
168+
public function get( $args, $assoc_args ) {
169+
$block_name = $args[0];
170+
$registry = WP_Block_Type_Registry::get_instance();
171+
$block = $registry->get_registered( $block_name );
172+
173+
if ( ! $block ) {
174+
WP_CLI::error( "Block type '{$block_name}' is not registered." );
175+
}
176+
177+
$data = $this->prepare_block_for_output( $block );
178+
179+
$formatter = $this->get_formatter( $assoc_args );
180+
$formatter->display_item( $data );
181+
}
182+
183+
/**
184+
* Prepares block data for output.
185+
*
186+
* @param WP_Block_Type $block Block type object.
187+
* @return array Prepared block data.
188+
*/
189+
private function prepare_block_for_output( $block ) {
190+
return array(
191+
'name' => $block->name,
192+
'title' => $block->title ?? '',
193+
'description' => $block->description ?? '',
194+
'category' => $block->category ?? '',
195+
'parent' => $block->parent ?? null,
196+
'icon' => $block->icon ?? '',
197+
'keywords' => $block->keywords ?? array(),
198+
'textdomain' => $block->textdomain ?? '',
199+
'supports' => $block->supports ?? array(),
200+
'styles' => $block->styles ?? array(),
201+
'variations' => $block->variations ?? array(),
202+
'api_version' => $block->api_version ?? 1,
203+
'editor_script' => $block->editor_script ?? '',
204+
'editor_style' => $block->editor_style ?? '',
205+
'script' => $block->script ?? '',
206+
'style' => $block->style ?? '',
207+
);
208+
}
209+
210+
/**
211+
* Gets a formatter instance.
212+
*
213+
* @param array $assoc_args Associative arguments.
214+
* @return Formatter Formatter instance.
215+
*/
216+
private function get_formatter( &$assoc_args ) {
217+
return new Formatter( $assoc_args, $this->fields, 'block' );
218+
}
219+
}

0 commit comments

Comments
 (0)