Skip to content

Commit 27d5c9c

Browse files
swissspidyclaude
andcommitted
Add wp block convert and wp block conversion-support
Add two commands that expose the server-side block conversion from WordPress/gutenberg PR #82013: - `wp block convert [<html>] [--file=<file>]` converts HTML to block markup with `gutenberg_html_to_block_markup()`, reading the HTML from the argument, a file, or STDIN. - `wp block conversion-support [--format=<format>]` lists the blocks a conversion can produce with `gutenberg_get_block_conversion_support()`, one row per block with `converts`, `conditional` or `declines`. Both commands fail with a clear error when the conversion functions are not available. PHPStan gets stubs for those functions so static analysis can type-check the calls. The feature file has two scenarios that run in CI against a plain install and four scenarios tagged @broken that need a Gutenberg build from the pull request, with instructions on how to run them locally. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YZNpaiVywfzpc8Hpm6dMa8
1 parent 309fa21 commit 27d5c9c

9 files changed

Lines changed: 607 additions & 0 deletions

‎.distignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ phpcs.xml.dist
1010
phpunit.xml.dist
1111
bin/
1212
features/
13+
phpstan/
1314
utils/
1415
*.zip
1516
*.tar.gz

‎README.md‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,129 @@ wp block
3838

3939

4040

41+
### wp block convert
42+
43+
Converts HTML to block markup.
44+
45+
~~~
46+
wp block convert [<html>] [--file=<file>]
47+
~~~
48+
49+
Markup that no block claims is kept verbatim inside a Custom HTML block.
50+
Input that already contains block delimiters is parsed as blocks rather
51+
than converted again.
52+
53+
The result is printed as-is and is not sanitized. Run it through the
54+
usual filters, for example by saving it with `wp post update`, before
55+
storing it as post content.
56+
57+
Requires an active Gutenberg build from
58+
https://github.com/WordPress/gutenberg/pull/82013.
59+
60+
**OPTIONS**
61+
62+
[<html>]
63+
The HTML to convert. Reads from --file or STDIN when omitted.
64+
65+
[--file=<file>]
66+
Read the HTML from a file instead of the argument.
67+
68+
**EXAMPLES**
69+
70+
# Convert HTML passed as an argument
71+
$ wp block convert '<h2>Title</h2><p>Text</p>'
72+
<!-- wp:heading -->
73+
<h2 class="wp-block-heading">Title</h2>
74+
<!-- /wp:heading -->
75+
76+
<!-- wp:paragraph -->
77+
<p>Text</p>
78+
<!-- /wp:paragraph -->
79+
80+
# Convert the contents of a file
81+
$ wp block convert --file=page.html
82+
83+
# Convert HTML from STDIN
84+
$ cat page.html | wp block convert
85+
86+
# Convert a post's content in place
87+
$ wp post get 123 --field=post_content | wp block convert | wp post update 123 -
88+
89+
90+
91+
### wp block conversion-support
92+
93+
Lists the blocks a server-side conversion can produce.
94+
95+
~~~
96+
wp block conversion-support [--support=<support>] [--field=<field>] [--fields=<fields>] [--format=<format>]
97+
~~~
98+
99+
Reports one row per block. The `support` column is one of:
100+
101+
* converts: produced from any markup the block matches.
102+
* conditional: produced only from markup the block can save back.
103+
* declines: deliberately not produced by a server-side conversion.
104+
105+
Requires an active Gutenberg build from
106+
https://github.com/WordPress/gutenberg/pull/82013.
107+
108+
**OPTIONS**
109+
110+
[--support=<support>]
111+
Only list blocks with this level of support.
112+
---
113+
options:
114+
- converts
115+
- conditional
116+
- declines
117+
---
118+
119+
[--field=<field>]
120+
Prints the value of a single field for each block.
121+
122+
[--fields=<fields>]
123+
Limit the output to specific fields.
124+
125+
[--format=<format>]
126+
Render output in a particular format.
127+
---
128+
default: table
129+
options:
130+
- table
131+
- csv
132+
- json
133+
- count
134+
- yaml
135+
---
136+
137+
**AVAILABLE FIELDS**
138+
139+
These fields will be displayed by default for each block:
140+
141+
* name
142+
* support
143+
144+
**EXAMPLES**
145+
146+
# List what a conversion can produce
147+
$ wp block conversion-support
148+
+----------------+-------------+
149+
| name | support |
150+
+----------------+-------------+
151+
| core/heading | converts |
152+
| core/paragraph | converts |
153+
| core/table | declines |
154+
+----------------+-------------+
155+
156+
# List only the blocks that decline conversion
157+
$ wp block conversion-support --support=declines --field=name
158+
159+
# Get the support map as JSON
160+
$ wp block conversion-support --format=json
161+
162+
163+
41164
### wp block type
42165

43166
Retrieves details on registered block types.

‎block-command.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@
5454
WP_CLI::add_command( 'block binding', WP_CLI\Block\Block_Binding_Command::class, [ 'before_invoke' => $wpcli_block_before_invoke_6_5 ] );
5555
WP_CLI::add_command( 'block template', WP_CLI\Block\Block_Template_Command::class, [ 'before_invoke' => $wpcli_block_before_invoke_5_9 ] );
5656
WP_CLI::add_command( 'block synced-pattern', WP_CLI\Block\Block_Synced_Pattern_Command::class, [ 'before_invoke' => $wpcli_block_before_invoke_5_0 ] );
57+
58+
// Server-side block conversion, provided by https://github.com/WordPress/gutenberg/pull/82013.
59+
// The commands check for the conversion functions themselves on top of the version gate.
60+
WP_CLI::add_command( 'block convert', [ WP_CLI\Block\Block_Conversion_Command::class, 'convert' ], [ 'before_invoke' => $wpcli_block_before_invoke_5_0 ] );
61+
WP_CLI::add_command( 'block conversion-support', [ WP_CLI\Block\Block_Conversion_Command::class, 'conversion_support' ], [ 'before_invoke' => $wpcli_block_before_invoke_5_0 ] );

‎composer.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"bundled": true,
3030
"commands": [
3131
"block",
32+
"block convert",
33+
"block conversion-support",
3234
"block type",
3335
"block type exists",
3436
"block type list",

‎features/block-convert.feature‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
Feature: Server-side block conversion commands
2+
3+
# The conversion functions ship with the Gutenberg build from
4+
# https://github.com/WordPress/gutenberg/pull/82013 and are not part of any
5+
# released WordPress or Gutenberg version yet, so on a plain install every
6+
# command fails with the same error.
7+
8+
@require-wp-5.0
9+
Scenario: Convert fails without the server-side block conversion
10+
Given a WP install
11+
12+
When I try `wp block convert '<p>Text</p>'`
13+
Then STDERR should be:
14+
"""
15+
Error: Server-side block conversion is not available. Activate the Gutenberg plugin from https://github.com/WordPress/gutenberg/pull/82013.
16+
"""
17+
And STDOUT should be empty
18+
And the return code should be 1
19+
20+
@require-wp-5.0
21+
Scenario: Conversion support fails without the server-side block conversion
22+
Given a WP install
23+
24+
When I try `wp block conversion-support`
25+
Then STDERR should be:
26+
"""
27+
Error: Server-side block conversion is not available. Activate the Gutenberg plugin from https://github.com/WordPress/gutenberg/pull/82013.
28+
"""
29+
And STDOUT should be empty
30+
And the return code should be 1
31+
32+
# The scenarios below need a Gutenberg build from that pull request, which
33+
# CI cannot install, so they are tagged @broken and `composer behat` skips
34+
# them. To run them locally:
35+
#
36+
# 1. Build the plugin zip from the `try/13163-php-block-conversion` branch
37+
# (`npm run build:plugin-zip` in a Gutenberg checkout).
38+
# 2. Copy it to `gutenberg.zip` in the root of this package. Git ignores it.
39+
# 3. Prepare the test database once with `composer prepare-tests`.
40+
# 4. Run the scenarios by tag, bypassing the @broken exclusion:
41+
#
42+
# vendor/bin/behat --tags=@broken features/block-convert.feature
43+
#
44+
# Each scenario installs the zip with `wp plugin install gutenberg.zip --activate`.
45+
46+
@broken
47+
Scenario: Convert HTML passed as an argument
48+
Given a WP install
49+
And I run `wp plugin install {PROJECT_DIR}/gutenberg.zip --activate`
50+
51+
When I run `wp block convert '<h2>Title</h2><p>Text</p>'`
52+
Then STDOUT should contain:
53+
"""
54+
<!-- wp:heading -->
55+
"""
56+
And STDOUT should contain:
57+
"""
58+
<!-- wp:paragraph -->
59+
"""
60+
And STDOUT should contain:
61+
"""
62+
<p>Text</p>
63+
"""
64+
And STDERR should be empty
65+
66+
@broken
67+
Scenario: Convert HTML from STDIN and from a file
68+
Given a WP install
69+
And I run `wp plugin install {PROJECT_DIR}/gutenberg.zip --activate`
70+
And a page.html file:
71+
"""
72+
<h2>Title</h2>
73+
<p>Text</p>
74+
"""
75+
76+
When I run `cat page.html | wp block convert`
77+
Then STDOUT should contain:
78+
"""
79+
<!-- wp:heading -->
80+
"""
81+
And STDOUT should contain:
82+
"""
83+
<!-- wp:paragraph -->
84+
"""
85+
86+
When I run `wp block convert --file=page.html`
87+
Then STDOUT should contain:
88+
"""
89+
<!-- wp:heading -->
90+
"""
91+
And STDOUT should contain:
92+
"""
93+
<!-- wp:paragraph -->
94+
"""
95+
96+
When I try `wp block convert --file=missing.html`
97+
Then STDERR should contain:
98+
"""
99+
Error: File 'missing.html' does not exist or is not readable.
100+
"""
101+
And the return code should be 1
102+
103+
When I try `wp block convert '<p>Text</p>' --file=page.html`
104+
Then STDERR should contain:
105+
"""
106+
Error: Pass the HTML either as an argument or with --file, not both.
107+
"""
108+
And the return code should be 1
109+
110+
When I try `wp block convert < /dev/null`
111+
Then STDERR should contain:
112+
"""
113+
Error: No HTML given. Pass it as an argument, with --file, or on STDIN.
114+
"""
115+
And the return code should be 1
116+
117+
@broken
118+
Scenario: Existing block markup is left alone and unknown markup becomes a Custom HTML block
119+
Given a WP install
120+
And I run `wp plugin install {PROJECT_DIR}/gutenberg.zip --activate`
121+
122+
When I run `wp block convert '<!-- wp:paragraph --><p>Text</p><!-- /wp:paragraph -->'`
123+
Then STDOUT should be:
124+
"""
125+
<!-- wp:paragraph --><p>Text</p><!-- /wp:paragraph -->
126+
"""
127+
128+
When I run `wp block convert '<marquee>Text</marquee>'`
129+
Then STDOUT should contain:
130+
"""
131+
<!-- wp:html -->
132+
"""
133+
134+
@broken
135+
Scenario: List the blocks a conversion can produce
136+
Given a WP install
137+
And I run `wp plugin install {PROJECT_DIR}/gutenberg.zip --activate`
138+
139+
When I run `wp block conversion-support`
140+
Then STDOUT should be a table containing rows:
141+
| name | support |
142+
| core/paragraph | converts |
143+
| core/table | declines |
144+
145+
When I run `wp block conversion-support --support=declines --field=name`
146+
Then STDOUT should contain:
147+
"""
148+
core/table
149+
"""
150+
And STDOUT should not contain:
151+
"""
152+
core/paragraph
153+
"""
154+
155+
When I run `wp block conversion-support --format=json`
156+
Then STDOUT should contain:
157+
"""
158+
{"name":"core\/paragraph","support":"converts"}
159+
"""
160+
161+
When I run `wp block conversion-support --format=count`
162+
Then STDOUT should be a number

‎phpcs.xml.dist‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
<!-- What to scan. -->
66
<file>.</file>
7+
<!-- PHPStan stubs for functions provided by other packages. -->
8+
<exclude-pattern>*/phpstan/*</exclude-pattern>
79

810
<!-- Show progress. -->
911
<arg value="p"/>

‎phpstan.neon.dist‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parameters:
77
- vendor/wp-cli/wp-cli/php
88
scanFiles:
99
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
- phpstan/gutenberg-block-conversion-stubs.php
1011
treatPhpDocTypesAsCertain: false
1112
ignoreErrors:
1213
# Standard WP-CLI ignores (same as entity-command)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* PHPStan stubs for the server-side block conversion from
4+
* https://github.com/WordPress/gutenberg/pull/82013.
5+
*
6+
* The functions only exist while a Gutenberg build from that pull request is
7+
* active, so `wp block convert` and `wp block conversion-support` check for
8+
* them at runtime. This file gives PHPStan their signatures.
9+
*/
10+
11+
/**
12+
* Converts HTML into blocks.
13+
*
14+
* @param string $html HTML to convert.
15+
* @return array<int, array<string, mixed>> Parsed block arrays, in the shape returned by `parse_blocks()`.
16+
*/
17+
function gutenberg_html_to_blocks( $html ) {}
18+
19+
/**
20+
* Converts HTML into serialized block markup.
21+
*
22+
* @param string $html HTML to convert.
23+
* @return string Block markup.
24+
*/
25+
function gutenberg_html_to_block_markup( $html ) {}
26+
27+
/**
28+
* Reports which blocks a server-side conversion can produce.
29+
*
30+
* @return array{converts: string[], conditional: string[], declines: string[]}
31+
*/
32+
function gutenberg_get_block_conversion_support() {}

0 commit comments

Comments
 (0)