Skip to content

Commit f1acf8a

Browse files
swissspidyclaude
andcommitted
Find pattern names with JSON-escaped slashes in the prefilter
`"patternName":"my-theme\/hero"` parses to `my-theme/hero`, but the prefilter only looked for the unescaped slash and dropped the post. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent f459df1 commit f1acf8a

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎features/block-search.feature‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,25 @@ Feature: Search posts by block usage
322322
Wide pattern
323323
"""
324324

325+
Scenario: Prefilter finds pattern names with JSON-escaped slashes
326+
When I run `wp eval 'wp_insert_post( wp_slash( [ "post_title" => "Escaped slash pattern", "post_status" => "publish", "post_content" => "<!-- wp:paragraph {\"metadata\":{\"patternName\":\"my-theme\\/hero\"}} --><p>x</p><!-- /wp:paragraph -->" ] ) ); echo "done";'`
327+
Then STDOUT should be:
328+
"""
329+
done
330+
"""
331+
332+
When I run `wp block search --pattern=my-theme/hero --post_type=post --field=post_title`
333+
Then STDOUT should be:
334+
"""
335+
Escaped slash pattern
336+
"""
337+
338+
When I run `wp block search --pattern-namespace=my-theme --post_type=post --field=post_title`
339+
Then STDOUT should be:
340+
"""
341+
Escaped slash pattern
342+
"""
343+
325344
Scenario: Style names that are escaped when serialized are still found
326345
When I run `wp eval 'wp_insert_post( wp_slash( [ "post_title" => "Escaped style", "post_status" => "publish", "post_content" => "<!-- wp:paragraph {\"className\":\"is-style-a\\u0026b\"} --><p class=\"is-style-a&amp;b\">x</p><!-- /wp:paragraph -->" ] ) ); echo "done";'`
327346
Then STDOUT should be:

‎src/Block_Search_Command.php‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,12 @@ private function build_rough_post_content_prefilter( $block_name, $block_namespa
512512

513513
if ( null !== $pattern_name && '' !== $pattern_name ) {
514514
$markers[] = '"patternName"';
515-
$markers[] = $this->alternatives( [ $pattern_name, $this->encode_attribute_value( $pattern_name ) ] );
515+
$markers[] = $this->alternatives( $this->pattern_name_alternatives( $pattern_name ) );
516516
}
517517

518518
if ( null !== $pattern_namespace && '' !== $pattern_namespace ) {
519519
$markers[] = '"patternName"';
520-
$markers[] = $this->alternatives( [ $pattern_namespace . '/', $this->encode_attribute_value( $pattern_namespace . '/' ) ] );
520+
$markers[] = $this->alternatives( $this->pattern_name_alternatives( $pattern_namespace . '/' ) );
521521
}
522522

523523
if ( null !== $synced_pattern ) {
@@ -561,6 +561,21 @@ private function alternatives( array $alternatives ) {
561561
return 1 === count( $alternatives ) ? $alternatives[0] : $alternatives;
562562
}
563563

564+
/**
565+
* Lists the forms a pattern name can take in serialized block attributes.
566+
*
567+
* Core serializes slashes unescaped, but JSON encoders that escape them
568+
* (`my-theme\/hero`) produce attributes that parse to the same name.
569+
*
570+
* @param string $value Raw pattern name or prefix.
571+
* @return string[]
572+
*/
573+
private function pattern_name_alternatives( $value ) {
574+
$encoded = $this->encode_attribute_value( $value );
575+
576+
return [ $value, $encoded, str_replace( '/', '\\/', $encoded ) ];
577+
}
578+
564579
/**
565580
* Encodes a value the way block attributes are escaped when serialized.
566581
*

0 commit comments

Comments
 (0)