Skip to content

Commit 9280759

Browse files
committed
Add wp post convert-to-blocks for server-side block conversion
Adds a `post convert-to-blocks <id>... [--dry-run]` subcommand that turns classic post content into block markup using the server-side conversion from WordPress/gutenberg#82013 (`gutenberg_html_to_block_markup()`). Posts that cannot be found count as failures, posts without content or that already contain blocks are skipped, and the run ends with the usual batch summary. Without that Gutenberg build active, the command fails with a clear error pointing at the pull request. Only the "not available" scenario runs in CI; the scenarios that need the Gutenberg build are tagged @broken with instructions for running them locally. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ECHX5L8q3jXJTxQqX94rH7
1 parent 741ad38 commit 9280759

4 files changed

Lines changed: 317 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4696,6 +4696,62 @@ or return code 1 if it does not.
46964696

46974697

46984698

4699+
### wp post convert-to-blocks
4700+
4701+
Converts the classic (non-block) content of one or more posts to block markup.
4702+
4703+
~~~
4704+
wp post convert-to-blocks <id>... [--dry-run]
4705+
~~~
4706+
4707+
Uses the server-side block conversion provided by the Gutenberg plugin
4708+
(see https://github.com/WordPress/gutenberg/pull/82013) to turn classic
4709+
HTML content into serialized block markup. Markup that no block claims
4710+
is kept verbatim inside a Custom HTML block, so nothing is lost. Posts
4711+
that already contain blocks or have no content are skipped.
4712+
4713+
The conversion does not sanitize the markup. Saving the converted content
4714+
applies the usual kses filtering for the current user context, so a run
4715+
without `--user` is filtered as an untrusted author would be. Run the
4716+
command with `--user=<administrator>` to keep markup that requires the
4717+
`unfiltered_html` capability, such as iframes or scripts.
4718+
4719+
Requires a Gutenberg build that provides `gutenberg_html_to_block_markup()`.
4720+
4721+
**OPTIONS**
4722+
4723+
<id>...
4724+
One or more IDs of posts to convert.
4725+
4726+
[--dry-run]
4727+
Preview which posts would be converted, without saving any changes.
4728+
4729+
**EXAMPLES**
4730+
4731+
# Convert a single post.
4732+
$ wp post convert-to-blocks 123
4733+
Converted post 123.
4734+
Success: Converted 1 of 1 posts.
4735+
4736+
# Convert every post of a post type.
4737+
$ wp post list --post_type=post --format=ids | xargs wp post convert-to-blocks
4738+
Converted post 123.
4739+
Converted post 124.
4740+
Warning: Post 125 already contains blocks.
4741+
Success: Converted 2 of 3 posts (1 skipped).
4742+
4743+
# Preview a conversion without saving.
4744+
$ wp post convert-to-blocks 123 --dry-run
4745+
Would convert post 123.
4746+
Success: Would convert 1 of 1 posts.
4747+
4748+
# Run as an administrator to keep markup that requires unfiltered_html.
4749+
$ wp post convert-to-blocks 123 --user=admin
4750+
Converted post 123.
4751+
Success: Converted 1 of 1 posts.
4752+
4753+
4754+
46994755
### wp post block
47004756

47014757
Manages blocks within post content.

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
"post url-to-id",
149149
"post has-blocks",
150150
"post has-block",
151+
"post convert-to-blocks",
151152
"post block",
152153
"post block clone",
153154
"post block count",
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
Feature: Convert classic post content to blocks
2+
3+
# The conversion depends on the server-side block conversion from
4+
# https://github.com/WordPress/gutenberg/pull/82013 (branch
5+
# `try/13163-php-block-conversion`), which no released Gutenberg build
6+
# provides yet. The scenarios tagged @broken below need a Gutenberg build
7+
# of that PR. To run them locally, build the plugin ZIP from that branch,
8+
# remove the @broken tags, and add the following step after the
9+
# `Given a WP install` step in each scenario:
10+
#
11+
# And I run `wp plugin install /path/to/gutenberg.zip --activate`
12+
13+
Scenario: Conversion requires the Gutenberg build with server-side block conversion
14+
Given a WP install
15+
16+
When I try `wp post convert-to-blocks 1`
17+
Then STDERR should be:
18+
"""
19+
Error: Server-side block conversion is not available. Activate the Gutenberg plugin from https://github.com/WordPress/gutenberg/pull/82013.
20+
"""
21+
And STDOUT should be empty
22+
And the return code should be 1
23+
24+
@broken
25+
Scenario: Convert a classic post to blocks
26+
Given a WP install
27+
28+
When I run `wp post create --post_title='Classic post' --post_content='<h2>Title</h2><p>Text</p>' --porcelain`
29+
Then save STDOUT as {POST_ID}
30+
31+
When I run `wp post convert-to-blocks {POST_ID}`
32+
Then STDOUT should be:
33+
"""
34+
Converted post {POST_ID}.
35+
Success: Converted 1 of 1 posts.
36+
"""
37+
And STDERR should be empty
38+
39+
When I run `wp post get {POST_ID} --field=post_content`
40+
Then STDOUT should contain:
41+
"""
42+
<!-- wp:heading -->
43+
"""
44+
And STDOUT should contain:
45+
"""
46+
<!-- wp:paragraph -->
47+
"""
48+
49+
When I run `wp post has-blocks {POST_ID}`
50+
Then STDOUT should be:
51+
"""
52+
Success: Post {POST_ID} contains blocks.
53+
"""
54+
55+
@broken
56+
Scenario: Preview a conversion with --dry-run
57+
Given a WP install
58+
59+
When I run `wp post create --post_title='Classic post' --post_content='<h2>Title</h2><p>Text</p>' --porcelain`
60+
Then save STDOUT as {POST_ID}
61+
62+
When I run `wp post convert-to-blocks {POST_ID} --dry-run`
63+
Then STDOUT should be:
64+
"""
65+
Would convert post {POST_ID}.
66+
Success: Would convert 1 of 1 posts.
67+
"""
68+
And STDERR should be empty
69+
70+
When I run `wp post get {POST_ID} --field=post_content`
71+
Then STDOUT should be:
72+
"""
73+
<h2>Title</h2><p>Text</p>
74+
"""
75+
76+
@broken
77+
Scenario: Skip posts that already contain blocks
78+
Given a WP install
79+
80+
When I run `wp post create --post_title='Block post' --post_content='<!-- wp:paragraph --><p>Text</p><!-- /wp:paragraph -->' --porcelain`
81+
Then save STDOUT as {POST_ID}
82+
83+
When I try `wp post convert-to-blocks {POST_ID}`
84+
Then STDERR should be:
85+
"""
86+
Warning: Post {POST_ID} already contains blocks.
87+
"""
88+
And STDOUT should be:
89+
"""
90+
Success: Converted 0 of 1 posts (1 skipped).
91+
"""
92+
And the return code should be 0
93+
94+
@broken
95+
Scenario: Skip posts without content
96+
Given a WP install
97+
98+
When I run `wp post create --post_title='Empty post' --post_content='' --porcelain`
99+
Then save STDOUT as {POST_ID}
100+
101+
When I try `wp post convert-to-blocks {POST_ID}`
102+
Then STDERR should be:
103+
"""
104+
Warning: Post {POST_ID} has no content to convert.
105+
"""
106+
And STDOUT should be:
107+
"""
108+
Success: Converted 0 of 1 posts (1 skipped).
109+
"""
110+
And the return code should be 0
111+
112+
@broken
113+
Scenario: Report missing posts as failures
114+
Given a WP install
115+
116+
When I run `wp post create --post_title='Classic post' --post_content='<p>Text</p>' --porcelain`
117+
Then save STDOUT as {POST_ID}
118+
119+
When I try `wp post convert-to-blocks {POST_ID} 99999`
120+
Then STDERR should contain:
121+
"""
122+
Warning: Could not find the post with ID 99999.
123+
"""
124+
And STDERR should contain:
125+
"""
126+
Error: Only converted 1 of 2 posts (1 failed).
127+
"""
128+
And STDOUT should be:
129+
"""
130+
Converted post {POST_ID}.
131+
"""
132+
And the return code should be 1

‎src/Post_Command.php‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,134 @@ public function has_block( $args, $assoc_args ) {
14431443
}
14441444
}
14451445

1446+
/**
1447+
* Converts the classic (non-block) content of one or more posts to block markup.
1448+
*
1449+
* Uses the server-side block conversion provided by the Gutenberg plugin
1450+
* (see https://github.com/WordPress/gutenberg/pull/82013) to turn classic
1451+
* HTML content into serialized block markup. Markup that no block claims
1452+
* is kept verbatim inside a Custom HTML block, so nothing is lost. Posts
1453+
* that already contain blocks or have no content are skipped.
1454+
*
1455+
* The conversion does not sanitize the markup. Saving the converted content
1456+
* applies the usual kses filtering for the current user context, so a run
1457+
* without `--user` is filtered as an untrusted author would be. Run the
1458+
* command with `--user=<administrator>` to keep markup that requires the
1459+
* `unfiltered_html` capability, such as iframes or scripts.
1460+
*
1461+
* Requires a Gutenberg build that provides `gutenberg_html_to_block_markup()`.
1462+
*
1463+
* ## OPTIONS
1464+
*
1465+
* <id>...
1466+
* : One or more IDs of posts to convert.
1467+
*
1468+
* [--dry-run]
1469+
* : Preview which posts would be converted, without saving any changes.
1470+
*
1471+
* ## EXAMPLES
1472+
*
1473+
* # Convert a single post.
1474+
* $ wp post convert-to-blocks 123
1475+
* Converted post 123.
1476+
* Success: Converted 1 of 1 posts.
1477+
*
1478+
* # Convert every post of a post type.
1479+
* $ wp post list --post_type=post --format=ids | xargs wp post convert-to-blocks
1480+
* Converted post 123.
1481+
* Converted post 124.
1482+
* Warning: Post 125 already contains blocks.
1483+
* Success: Converted 2 of 3 posts (1 skipped).
1484+
*
1485+
* # Preview a conversion without saving.
1486+
* $ wp post convert-to-blocks 123 --dry-run
1487+
* Would convert post 123.
1488+
* Success: Would convert 1 of 1 posts.
1489+
*
1490+
* # Run as an administrator to keep markup that requires unfiltered_html.
1491+
* $ wp post convert-to-blocks 123 --user=admin
1492+
* Converted post 123.
1493+
* Success: Converted 1 of 1 posts.
1494+
*
1495+
* @subcommand convert-to-blocks
1496+
*/
1497+
public function convert_to_blocks( $args, $assoc_args ) {
1498+
if ( ! function_exists( 'gutenberg_html_to_block_markup' ) ) {
1499+
WP_CLI::error( 'Server-side block conversion is not available. Activate the Gutenberg plugin from https://github.com/WordPress/gutenberg/pull/82013.' );
1500+
}
1501+
1502+
$dry_run = (bool) Utils\get_flag_value( $assoc_args, 'dry-run', false );
1503+
1504+
$total = count( $args );
1505+
$successes = 0;
1506+
$errors = 0;
1507+
$skips = 0;
1508+
1509+
foreach ( $args as $post_id ) {
1510+
$post = $this->fetcher->get( $post_id );
1511+
1512+
if ( ! $post ) {
1513+
WP_CLI::warning( "Could not find the post with ID {$post_id}." );
1514+
++$errors;
1515+
continue;
1516+
}
1517+
1518+
if ( '' === trim( (string) $post->post_content ) ) {
1519+
WP_CLI::warning( "Post {$post->ID} has no content to convert." );
1520+
++$skips;
1521+
continue;
1522+
}
1523+
1524+
if ( Block_Processor_Helper::has_blocks( $post->post_content ) ) {
1525+
WP_CLI::warning( "Post {$post->ID} already contains blocks." );
1526+
++$skips;
1527+
continue;
1528+
}
1529+
1530+
$markup = gutenberg_html_to_block_markup( $post->post_content );
1531+
1532+
if ( $dry_run ) {
1533+
WP_CLI::log( "Would convert post {$post->ID}." );
1534+
++$successes;
1535+
continue;
1536+
}
1537+
1538+
$result = wp_update_post(
1539+
[
1540+
'ID' => $post->ID,
1541+
'post_content' => $markup,
1542+
],
1543+
true
1544+
);
1545+
1546+
if ( is_wp_error( $result ) ) {
1547+
WP_CLI::warning( "Failed converting post {$post->ID}: " . $result->get_error_message() );
1548+
++$errors;
1549+
continue;
1550+
}
1551+
1552+
WP_CLI::log( "Converted post {$post->ID}." );
1553+
++$successes;
1554+
}
1555+
1556+
if ( ! $dry_run ) {
1557+
Utils\report_batch_operation_results( 'post', 'convert', $total, $successes, $errors, $skips );
1558+
return;
1559+
}
1560+
1561+
$skipped_message = $skips ? " ({$skips} skipped)" : '';
1562+
1563+
if ( $errors ) {
1564+
$failed_skipped_message = " ({$errors} failed" . ( $skips ? ", {$skips} skipped" : '' ) . ')';
1565+
if ( $successes ) {
1566+
WP_CLI::error( "Would only convert {$successes} of {$total} posts{$failed_skipped_message}." );
1567+
}
1568+
WP_CLI::error( "No posts would be converted{$failed_skipped_message}." );
1569+
}
1570+
1571+
WP_CLI::success( "Would convert {$successes} of {$total} posts{$skipped_message}." );
1572+
}
1573+
14461574
/**
14471575
* Convert a date-time string with a hyphen separator to a space separator.
14481576
*

0 commit comments

Comments
 (0)