Skip to content

Commit f804ead

Browse files
committed
Make index-based operations consistent
1 parent e45b276 commit f804ead

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

features/post-block.feature

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,76 @@ Feature: Manage blocks in post content
13111311
0
13121312
"""
13131313

1314+
@require-wp-5.0
1315+
Scenario: Remove by index with whitespace between blocks
1316+
Given a WP install
1317+
And a spaced-blocks.txt file:
1318+
"""
1319+
<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph -->
1320+
1321+
<!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph -->
1322+
1323+
<!-- wp:paragraph --><p>Third</p><!-- /wp:paragraph -->
1324+
"""
1325+
When I run `wp post create --post_title='Spaced' --porcelain < spaced-blocks.txt`
1326+
Then save STDOUT as {POST_ID}
1327+
1328+
# Index 1 should be "Second", not the whitespace between blocks
1329+
When I run `wp post block remove {POST_ID} --index=1`
1330+
Then STDOUT should contain:
1331+
"""
1332+
Success: Removed 1 block from post {POST_ID}.
1333+
"""
1334+
1335+
When I run `wp post get {POST_ID} --field=post_content`
1336+
Then STDOUT should contain:
1337+
"""
1338+
First
1339+
"""
1340+
And STDOUT should contain:
1341+
"""
1342+
Third
1343+
"""
1344+
And STDOUT should not contain:
1345+
"""
1346+
Second
1347+
"""
1348+
1349+
@require-wp-5.0
1350+
Scenario: Remove multiple indices with whitespace between blocks
1351+
Given a WP install
1352+
And a spaced-blocks.txt file:
1353+
"""
1354+
<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph -->
1355+
1356+
<!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph -->
1357+
1358+
<!-- wp:paragraph --><p>Third</p><!-- /wp:paragraph -->
1359+
"""
1360+
When I run `wp post create --post_title='Spaced' --porcelain < spaced-blocks.txt`
1361+
Then save STDOUT as {POST_ID}
1362+
1363+
# Indices 0 and 2 should be "First" and "Third"
1364+
When I run `wp post block remove {POST_ID} --index=0,2`
1365+
Then STDOUT should contain:
1366+
"""
1367+
Success: Removed 2 blocks from post {POST_ID}.
1368+
"""
1369+
1370+
When I run `wp post get {POST_ID} --field=post_content`
1371+
Then STDOUT should contain:
1372+
"""
1373+
Second
1374+
"""
1375+
And STDOUT should not contain:
1376+
"""
1377+
First
1378+
"""
1379+
And STDOUT should not contain:
1380+
"""
1381+
Third
1382+
"""
1383+
13141384
@require-wp-5.0
13151385
Scenario: Replace block preserves content
13161386
Given a WP install

src/Post_Block_Command.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,19 +1462,43 @@ public function remove( $args, $assoc_args ) {
14621462
WP_CLI::error( 'You must specify either a block name or --index.' );
14631463
}
14641464

1465-
$blocks = parse_blocks( $post->post_content );
1465+
$blocks = parse_blocks( $post->post_content );
1466+
1467+
// Filter out empty blocks but keep track of original indices.
1468+
$filtered_blocks = [];
1469+
$index_map = [];
1470+
foreach ( $blocks as $original_idx => $block ) {
1471+
if ( ! empty( $block['blockName'] ) ) {
1472+
$index_map[ count( $filtered_blocks ) ] = $original_idx;
1473+
$filtered_blocks[] = $block;
1474+
}
1475+
}
1476+
14661477
$removed_count = 0;
14671478

14681479
if ( null !== $indices ) {
14691480
$index_array = array_map( 'intval', explode( ',', $indices ) );
1470-
rsort( $index_array );
14711481

1482+
// Validate all indices first.
14721483
foreach ( $index_array as $idx ) {
1473-
if ( isset( $blocks[ $idx ] ) ) {
1474-
array_splice( $blocks, $idx, 1 );
1475-
++$removed_count;
1484+
if ( $idx < 0 || $idx >= count( $filtered_blocks ) ) {
1485+
WP_CLI::error( "Invalid index: {$idx}. Post has " . count( $filtered_blocks ) . ' block(s) (0-indexed).' );
14761486
}
14771487
}
1488+
1489+
// Map to original indices and sort in reverse order to remove from end first.
1490+
$original_indices = array_map(
1491+
function ( $idx ) use ( $index_map ) {
1492+
return $index_map[ $idx ];
1493+
},
1494+
$index_array
1495+
);
1496+
rsort( $original_indices );
1497+
1498+
foreach ( $original_indices as $original_idx ) {
1499+
array_splice( $blocks, $original_idx, 1 );
1500+
++$removed_count;
1501+
}
14781502
} elseif ( $remove_all && null !== $block_name ) {
14791503
$new_blocks = [];
14801504
foreach ( $blocks as $block ) {

0 commit comments

Comments
 (0)