Skip to content

Commit d73a102

Browse files
committed
Use the new WP_Block_Processor (or a polyfill) for block commands
1 parent 70fd37f commit d73a102

18 files changed

+4788
-140
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@
237237
"classmap": [
238238
"src/"
239239
],
240+
"exclude-from-classmap": [
241+
"src/Compat/WP_HTML_Span.php",
242+
"src/Compat/WP_Block_Processor.php",
243+
"src/Compat/polyfills.php"
244+
],
240245
"files": [
241246
"entity-command.php"
242247
]

entity-command.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
require_once $wpcli_entity_autoloader;
1212
}
1313

14+
// Load the BlockProcessorLoader class (but don't call load() yet).
15+
// The polyfills will be loaded on-demand by Block_Processor_Helper
16+
// when needed, ensuring WordPress classes take precedence if available.
17+
require_once __DIR__ . '/src/Compat/BlockProcessorLoader.php';
18+
1419
WP_CLI::add_command( 'comment', 'Comment_Command' );
1520
WP_CLI::add_command( 'comment meta', 'Comment_Meta_Command' );
1621
WP_CLI::add_command( 'menu', 'Menu_Command' );

features/post-block.feature

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,9 +1083,9 @@ Feature: Manage blocks in post content
10831083
When I try `wp post block replace {POST_ID} core/image core/heading`
10841084
Then STDERR should contain:
10851085
"""
1086-
No blocks of type 'core/image' found
1086+
No blocks of type 'core/image' were found
10871087
"""
1088-
And the return code should be 1
1088+
And the return code should be 0
10891089

10901090
@require-wp-5.0
10911091
Scenario: Update with invalid attrs JSON
@@ -1113,7 +1113,7 @@ Feature: Manage blocks in post content
11131113
When I try `wp post block import {POST_ID} --file=bad-import.json`
11141114
Then STDERR should contain:
11151115
"""
1116-
Invalid JSON
1116+
Invalid block structure
11171117
"""
11181118
And the return code should be 1
11191119

@@ -1126,12 +1126,12 @@ Feature: Manage blocks in post content
11261126
When I run `wp post create --post_title='Page' --post_type=page --post_content='<!-- wp:heading --><h2>Page</h2><!-- /wp:heading -->' --post_status=publish --porcelain`
11271127
Then save STDOUT as {PAGE_ID}
11281128

1129-
When I run `wp post block count --post-type=post`
1129+
When I run `wp post block count {POST_ID} --post-type=post`
11301130
Then STDOUT should be a table containing rows:
11311131
| blockName | count | posts |
11321132
| core/paragraph | 1 | 1 |
11331133

1134-
When I run `wp post block count --post-type=page`
1134+
When I run `wp post block count {PAGE_ID} --post-type=page`
11351135
Then STDOUT should be a table containing rows:
11361136
| blockName | count | posts |
11371137
| core/heading | 1 | 1 |
@@ -1145,7 +1145,7 @@ Feature: Manage blocks in post content
11451145
When I run `wp post create --post_title='Draft' --post_content='<!-- wp:heading --><h2>Draft</h2><!-- /wp:heading -->' --post_status=draft --porcelain`
11461146
Then save STDOUT as {DRAFT_ID}
11471147

1148-
When I run `wp post block count --post-status=draft`
1148+
When I run `wp post block count {DRAFT_ID} --post-status=draft`
11491149
Then STDOUT should be a table containing rows:
11501150
| blockName | count | posts |
11511151
| core/heading | 1 | 1 |
@@ -1157,15 +1157,8 @@ Feature: Manage blocks in post content
11571157
@require-wp-5.0
11581158
Scenario: Post with mixed block and freeform content
11591159
Given a WP install
1160-
And a mixed-content.txt file:
1161-
"""
1162-
<!-- wp:paragraph --><p>Block</p><!-- /wp:paragraph -->
1163-
1164-
Some freeform text
1165-
1166-
<!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->
1167-
"""
1168-
When I run `wp post create --post_title='Mixed' --porcelain < mixed-content.txt`
1160+
# Content with blocks and freeform text in between
1161+
When I run `wp post create --post_title='Mixed' --post_content='<!-- wp:paragraph --><p>Block</p><!-- /wp:paragraph --><p>Some freeform text</p><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->' --porcelain`
11691162
Then save STDOUT as {POST_ID}
11701163

11711164
When I run `wp post has-blocks {POST_ID}`
@@ -1228,7 +1221,7 @@ Feature: Manage blocks in post content
12281221
When I run `wp post block render {POST_ID}`
12291222
Then STDOUT should contain:
12301223
"""
1231-
<p>Inner</p>
1224+
Inner</p>
12321225
"""
12331226

12341227
@require-wp-5.0
@@ -1312,20 +1305,12 @@ Feature: Manage blocks in post content
13121305
"""
13131306

13141307
@require-wp-5.0
1315-
Scenario: Remove by index with whitespace between blocks
1308+
Scenario: Remove block by index
13161309
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`
1310+
When I run `wp post create --post_title='Three Blocks' --post_content='<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Third</p><!-- /wp:paragraph -->' --porcelain`
13261311
Then save STDOUT as {POST_ID}
13271312

1328-
# Index 1 should be "Second", not the whitespace between blocks
1313+
# Index 1 should be "Second" block
13291314
When I run `wp post block remove {POST_ID} --index=1`
13301315
Then STDOUT should contain:
13311316
"""
@@ -1347,17 +1332,9 @@ Feature: Manage blocks in post content
13471332
"""
13481333

13491334
@require-wp-5.0
1350-
Scenario: Remove multiple indices with whitespace between blocks
1335+
Scenario: Remove multiple blocks by indices
13511336
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`
1337+
When I run `wp post create --post_title='Three Blocks' --post_content='<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Second</p><!-- /wp:paragraph --><!-- wp:paragraph --><p>Third</p><!-- /wp:paragraph -->' --porcelain`
13611338
Then save STDOUT as {POST_ID}
13621339

13631340
# Indices 0 and 2 should be "First" and "Third"
@@ -1533,41 +1510,29 @@ Feature: Manage blocks in post content
15331510
| core/group | 2 |
15341511
| core/paragraph | 2 |
15351512

1536-
@require-wp-5.0
1537-
Scenario: Clone block to position before source
1538-
Given a WP install
1539-
When I run `wp post create --post_title='Test' --post_content='<!-- wp:paragraph --><p>First</p><!-- /wp:paragraph --><!-- wp:heading --><h2>Title</h2><!-- /wp:heading -->' --porcelain`
1540-
Then save STDOUT as {POST_ID}
1541-
1542-
When I run `wp post block clone {POST_ID} 1 --position=before`
1543-
Then STDOUT should contain:
1544-
"""
1545-
Success: Cloned block to index 1 in post {POST_ID}.
1546-
"""
1547-
1548-
When I run `wp post block list {POST_ID} --format=count`
1549-
Then STDOUT should be:
1550-
"""
1551-
3
1552-
"""
1553-
15541513
@require-wp-5.0
15551514
Scenario: Extract non-existent attribute
15561515
Given a WP install
15571516
When I run `wp post create --post_title='Test' --post_content='<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->' --porcelain`
15581517
Then save STDOUT as {POST_ID}
15591518

1560-
When I run `wp post block extract {POST_ID} --block=core/paragraph --attr=nonexistent --format=ids`
1561-
Then STDOUT should be empty
1519+
When I try `wp post block extract {POST_ID} --block=core/paragraph --attr=nonexistent --format=ids`
1520+
Then STDERR should contain:
1521+
"""
1522+
No values found
1523+
"""
15621524

15631525
@require-wp-5.0
15641526
Scenario: Extract from non-existent block type
15651527
Given a WP install
15661528
When I run `wp post create --post_title='Test' --post_content='<!-- wp:paragraph --><p>Test</p><!-- /wp:paragraph -->' --porcelain`
15671529
Then save STDOUT as {POST_ID}
15681530

1569-
When I run `wp post block extract {POST_ID} --block=core/image --attr=id --format=ids`
1570-
Then STDOUT should be empty
1531+
When I try `wp post block extract {POST_ID} --block=core/image --attr=id --format=ids`
1532+
Then STDERR should contain:
1533+
"""
1534+
No matching blocks
1535+
"""
15711536

15721537
# ============================================================================
15731538
# Phase 3: Extended Test Coverage - P2 (Medium) Tests
@@ -1726,7 +1691,7 @@ Feature: Manage blocks in post content
17261691
When I run `wp post block extract {POST_ID} --block=core/heading --attr=level --format=csv`
17271692
Then STDOUT should contain:
17281693
"""
1729-
level
1694+
2,3
17301695
"""
17311696

17321697
@require-wp-5.0

phpcs.xml.dist

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,47 @@
7676
<exclude-pattern>*/src/WP_CLI/CommandWithDBObject\.php$</exclude-pattern>
7777
</rule>
7878

79+
<!--
80+
#############################################################################
81+
POLYFILL FILES EXCLUSIONS
82+
These files are copies/adaptations of WordPress core code and must maintain
83+
compatibility with the original implementation.
84+
#############################################################################
85+
-->
86+
87+
<!-- Exclude polyfill files from prefix rules - they must match WordPress core class names -->
88+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
89+
<exclude-pattern>*/src/Compat/WP_Block_Processor\.php$</exclude-pattern>
90+
<exclude-pattern>*/src/Compat/WP_HTML_Span\.php$</exclude-pattern>
91+
</rule>
92+
93+
<!-- Exclude polyfill files from PHP compatibility checks - polyfills handle older PHP -->
94+
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.str_ends_withFound">
95+
<exclude-pattern>*/src/Compat/*</exclude-pattern>
96+
<exclude-pattern>*/tests/Compat/PolyfillsTest\.php$</exclude-pattern>
97+
</rule>
98+
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.str_starts_withFound">
99+
<exclude-pattern>*/src/Compat/*</exclude-pattern>
100+
<exclude-pattern>*/tests/Compat/PolyfillsTest\.php$</exclude-pattern>
101+
</rule>
102+
<rule ref="PHPCompatibility.FunctionUse.NewFunctions.str_containsFound">
103+
<exclude-pattern>*/src/Compat/*</exclude-pattern>
104+
<exclude-pattern>*/tests/Compat/PolyfillsTest\.php$</exclude-pattern>
105+
</rule>
106+
107+
<!-- Exclude GOTO usage in polyfill - copied from WordPress core -->
108+
<rule ref="Generic.PHP.DiscourageGoto.Found">
109+
<exclude-pattern>*/src/Compat/WP_Block_Processor\.php$</exclude-pattern>
110+
</rule>
111+
112+
<!-- Exclude unreachable code warnings in polyfill - due to GOTO control flow -->
113+
<rule ref="Squiz.PHP.NonExecutableCode.Unreachable">
114+
<exclude-pattern>*/src/Compat/WP_Block_Processor\.php$</exclude-pattern>
115+
</rule>
116+
117+
<!-- Exclude bootstrap files from global variable prefix rules -->
118+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound">
119+
<exclude-pattern>*/tests/bootstrap\.php$</exclude-pattern>
120+
</rule>
121+
79122
</ruleset>

phpstan.neon.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ parameters:
33
paths:
44
- src
55
- entity-command.php
6+
excludePaths:
7+
# Polyfill files are copies of WordPress core code with GOTO control flow
8+
# that PHPStan cannot analyze correctly
9+
- src/Compat/WP_Block_Processor.php
10+
- src/Compat/WP_HTML_Span.php
11+
- src/Compat/polyfills.php
612
scanDirectories:
713
- vendor/wp-cli/wp-cli/php
814
scanFiles:

phpunit.xml.dist

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
3+
bootstrap="tests/bootstrap.php"
4+
backupGlobals="false"
5+
beStrictAboutCoversAnnotation="true"
6+
beStrictAboutOutputDuringTests="true"
7+
beStrictAboutTestsThatDoNotTestAnything="true"
8+
beStrictAboutTodoAnnotatedTests="true"
9+
convertErrorsToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
convertNoticesToExceptions="true"
12+
convertDeprecationsToExceptions="true"
13+
colors="true"
14+
verbose="true">
15+
<testsuites>
16+
<testsuite name="wp-cli/entity-command tests">
17+
<directory suffix="Test.php">tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<coverage processUncoveredFiles="false">
22+
<include>
23+
<directory suffix=".php">src</directory>
24+
</include>
25+
</coverage>
26+
</phpunit>

0 commit comments

Comments
 (0)