Skip to content

Commit a6a1543

Browse files
swissspidyclaude
andcommitted
Keep backslashes when saving converted post content
wp_update_post() expects slashed data, so saving the converted markup as is stripped backslashes, including those of escaped block attributes such as \u003c. Slash the markup first, and cover the save with a scenario that stands in for the Gutenberg conversion so it runs in CI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 74836b4 commit a6a1543

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎features/post-convert-to-blocks.feature‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,37 @@ Feature: Convert classic post content to blocks
2121
And STDOUT should be empty
2222
And the return code should be 1
2323

24+
Scenario: Keep backslashes when saving the converted content
25+
Given a WP install
26+
# Stands in for the Gutenberg conversion, to check how the command saves the result.
27+
And a wp-content/mu-plugins/fake-block-conversion.php file:
28+
"""
29+
<?php
30+
function gutenberg_html_to_block_markup( $html ) {
31+
return '<!-- wp:paragraph {"className":"\u003cb\u003e"} -->' . trim( $html ) . '<!-- /wp:paragraph -->';
32+
}
33+
"""
34+
And a content.txt file:
35+
"""
36+
<p>C:\Users\me</p>
37+
"""
38+
39+
When I run `wp post create content.txt --post_title='Classic post' --porcelain`
40+
Then save STDOUT as {POST_ID}
41+
42+
When I run `wp post convert-to-blocks {POST_ID}`
43+
Then STDOUT should be:
44+
"""
45+
Converted post {POST_ID}.
46+
Success: Converted 1 of 1 posts.
47+
"""
48+
49+
When I run `wp post get {POST_ID} --field=post_content`
50+
Then STDOUT should be:
51+
"""
52+
<!-- wp:paragraph {"className":"\u003cb\u003e"} --><p>C:\Users\me</p><!-- /wp:paragraph -->
53+
"""
54+
2455
@broken
2556
Scenario: Convert a classic post to blocks
2657
Given a WP install

‎src/Post_Command.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,7 @@ public function convert_to_blocks( $args, $assoc_args ) {
16891689
continue;
16901690
}
16911691

1692+
/** @var string $markup */
16921693
$markup = gutenberg_html_to_block_markup( $post->post_content );
16931694

16941695
if ( $dry_run ) {
@@ -1700,7 +1701,8 @@ public function convert_to_blocks( $args, $assoc_args ) {
17001701
$result = wp_update_post(
17011702
[
17021703
'ID' => $post->ID,
1703-
'post_content' => $markup,
1704+
// wp_update_post() expects slashed data and would strip the backslashes of escaped block attributes.
1705+
'post_content' => wp_slash( $markup ),
17041706
],
17051707
true
17061708
);

0 commit comments

Comments
 (0)