Skip to content

Commit f459df1

Browse files
swissspidyclaude
andcommitted
Replace the has_block() fast path with a parser-compatible check
has_block() requires a literal space after the block name, so a block comment such as `<!-- wp:my-plugin/card\n/-->` was dropped before parsing. Before WordPress 5.0.8 (and in the first 5.1-5.3 releases) it also did not match `core/quote` against `<!-- wp:quote`. Check the opening comment with the same whitespace rules as the block parser, accepting both the short and the namespaced form of core names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent c7867d3 commit f459df1

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎features/block-search.feature‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,26 @@ Feature: Search posts by block usage
239239
Exact,1
240240
"""
241241

242+
Scenario: Block search accepts any whitespace after the block name
243+
When I run `wp eval 'wp_insert_post( [ "post_title" => "Newline", "post_status" => "publish", "post_content" => "<!-- wp:my-plugin/card\n/-->" ] ); wp_insert_post( [ "post_title" => "Prefix only", "post_status" => "publish", "post_content" => "<!-- wp:my-plugin/card-extra\n/-->" ] ); echo "done";'`
244+
Then STDOUT should be:
245+
"""
246+
done
247+
"""
248+
249+
When I run `wp block search --block=my-plugin/card --post_type=post --field=post_title`
250+
Then STDOUT should be:
251+
"""
252+
Newline
253+
"""
254+
255+
When I run `wp block search --block=my-plugin/card --post_type=post --fields=post_title,occurrences --format=csv`
256+
Then STDOUT should be:
257+
"""
258+
post_title,occurrences
259+
Newline,1
260+
"""
261+
242262
Scenario: Offset without a page size skips posts and returns the rest
243263
When I run `wp eval 'foreach ( [ 1, 2, 3 ] as $i ) { wp_insert_post( [ "post_title" => "Offset $i", "post_status" => "publish", "post_content" => "<!-- wp:my-plugin/offset /-->" ] ); } echo "done";'`
244264
Then STDOUT should be:

‎phpstan.neon.dist‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ parameters:
8888
# `wp block search` is gated on WordPress 5.0.
8989
-
9090
identifier: WPCompat.functionNotAvailable
91-
message: '#^(has_block|parse_blocks)\(\) is only available since WordPress version 5\.0\.0\.$#'
91+
message: '#^parse_blocks\(\) is only available since WordPress version 5\.0\.0\.$#'
9292
path: src/Block_Search_Command.php
9393

9494
# `wp block style` is gated on WordPress 5.3.

‎src/Block_Search_Command.php‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function __invoke( $args, $assoc_args ) {
278278
foreach ( $this->query_posts_in_batches( $query_args, $rough_prefilter ) as $post ) {
279279
$occurrences = null;
280280

281-
if ( $use_fast_path && ! has_block( $block_name, $post ) ) {
281+
if ( $use_fast_path && ! $this->has_block_comment( $post->post_content, $block_name ) ) {
282282
continue;
283283
}
284284

@@ -745,7 +745,7 @@ private function find_matching_blocks( $content, $block_name, $block_namespace,
745745
}
746746

747747
/**
748-
* Checks whether an exact block-only search can use has_block() as a fast precheck.
748+
* Checks whether an exact block-only search can use a block comment check as a fast precheck.
749749
*
750750
* @param string|null $block_name Requested exact block name.
751751
* @param string|null $block_namespace Requested block namespace.
@@ -765,6 +765,27 @@ private function can_use_has_block_fast_path( $block_name, $block_namespace, $st
765765
&& null === $synced_pattern;
766766
}
767767

768+
/**
769+
* Checks whether content contains an opening comment for an exact block name.
770+
*
771+
* Unlike has_block(), this accepts the same whitespace as the block parser
772+
* and the serialized form of core block names on every WordPress version.
773+
*
774+
* @param string $content Post content.
775+
* @param string $block_name Requested exact block name.
776+
* @return bool
777+
*/
778+
private function has_block_comment( $content, $block_name ) {
779+
$serialized_name = $this->strip_core_block_namespace( $block_name );
780+
$name_pattern = preg_quote( $serialized_name, '#' );
781+
782+
if ( $serialized_name !== $block_name ) {
783+
$name_pattern = '(?:core/)?' . $name_pattern;
784+
}
785+
786+
return 1 === preg_match( '#<!--\s+wp:' . $name_pattern . '\s#', $content );
787+
}
788+
768789
/**
769790
* Checks whether a parsed block matches the requested block filter.
770791
*

0 commit comments

Comments
 (0)