Skip to content

Commit 960a796

Browse files
committed
Enhance performance with prefiltering
1 parent f6ea8fc commit 960a796

3 files changed

Lines changed: 209 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ wp block search [--block=<block-name>] [--block-namespace=<block-namespace>] [--
5252
Returns matching posts where the requested block appears anywhere in the
5353
parsed block tree, including nested blocks.
5454

55+
To reduce unnecessary parsing on large datasets, the command first applies a coarse `post_content` prefilter when a safe marker is available, then confirms matches by parsing blocks.
56+
5557
At least one search filter is required: `--block`, `--block-namespace`, `--style`, `--pattern`, `--pattern-namespace`, or `--synced-pattern`.
5658

5759
Pattern filters match embedded block metadata stored in `metadata.patternName`.
@@ -147,6 +149,9 @@ These fields are optionally available:
147149
# Limit search to published pages and show selected fields as JSON.
148150
$ wp block search --block=core/image --post_type=page --post_status=publish --fields=ID,post_title,occurrences --format=json
149151

152+
# Limit the candidate posts scanned with a native query argument.
153+
$ wp block search --block=core/paragraph --showposts=50 --format=ids
154+
150155
# Restrict search to specific posts and return the match count.
151156
$ wp block search --style=rounded --post__in=21,42,84 --format=count
152157

‎features/block-search.feature‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ Feature: Search posts by block usage
3434
{CORE_NAMESPACE_POST_ID}
3535
"""
3636

37+
Scenario: Search posts by custom block namespace
38+
Given a custom-namespace-post.html file:
39+
"""
40+
<!-- wp:my-plugin/card --><div class="wp-block-my-plugin-card">Hello custom</div><!-- /wp:my-plugin/card -->
41+
"""
42+
When I run `wp post create custom-namespace-post.html --post_type=post --post_title='Custom Namespace Post' --post_status=publish --porcelain`
43+
Then STDOUT should be a number
44+
And save STDOUT as {CUSTOM_NAMESPACE_POST_ID}
45+
46+
When I run `wp block search --block-namespace=my-plugin --post_type=post --field=ID`
47+
Then STDOUT should be:
48+
"""
49+
{CUSTOM_NAMESPACE_POST_ID}
50+
"""
51+
3752
Scenario: Search nested block usage
3853
Given a nested-image-post.html file:
3954
"""
@@ -90,6 +105,17 @@ Feature: Search posts by block usage
90105
{ROUNDED_QUOTE_POST_ID}
91106
"""
92107

108+
Scenario: Style search ignores plain text false positives
109+
When I run `wp post create --post_type=post --post_title='Style Token Plain Text Post' --post_status=publish --post_content='This post mentions is-style-rounded in plain text only.' --porcelain`
110+
Then STDOUT should be a number
111+
And save STDOUT as {STYLE_TOKEN_TEXT_POST_ID}
112+
113+
When I run `wp block search --style=rounded --field=ID --format=ids`
114+
Then STDOUT should not contain:
115+
"""
116+
{STYLE_TOKEN_TEXT_POST_ID}
117+
"""
118+
93119
Scenario: Search block usage count
94120
Given a counted-quote-one.html file:
95121
"""
@@ -156,12 +182,25 @@ Feature: Search posts by block usage
156182
Then STDOUT should be a number
157183
And save STDOUT as {PATTERN_HEADER_POST_ID}
158184

185+
Given a spaced-pattern-rsvp-post.html file:
186+
"""
187+
<!-- wp:group {"metadata":{"categories":["call-to-action"],"patternName": "twentytwentyfive/event-rsvp","name":"Event RSVP Spaced"}} --><div class="wp-block-group"><!-- wp:paragraph --><p>RSVP later</p><!-- /wp:paragraph --></div><!-- /wp:group -->
188+
"""
189+
When I run `wp post create spaced-pattern-rsvp-post.html --post_type=post --post_title='Pattern RSVP Spaced Post' --post_status=publish --porcelain`
190+
Then STDOUT should be a number
191+
And save STDOUT as {SPACED_PATTERN_RSVP_POST_ID}
192+
159193
When I run `wp block search --pattern=twentytwentyfive/event-rsvp --field=ID`
160-
Then STDOUT should be:
194+
Then STDOUT should contain:
161195
"""
162196
{PATTERN_RSVP_POST_ID}
163197
"""
164198

199+
And STDOUT should contain:
200+
"""
201+
{SPACED_PATTERN_RSVP_POST_ID}
202+
"""
203+
165204
When I run `wp block search --pattern=twentytwentyfive/event-rsvp --field=ID --format=ids`
166205
Then STDOUT should not contain:
167206
"""
@@ -174,6 +213,11 @@ Feature: Search posts by block usage
174213
{PATTERN_RSVP_POST_ID}
175214
"""
176215

216+
And STDOUT should contain:
217+
"""
218+
{SPACED_PATTERN_RSVP_POST_ID}
219+
"""
220+
177221
And STDOUT should contain:
178222
"""
179223
{PATTERN_HEADER_POST_ID}
@@ -242,7 +286,7 @@ Feature: Search posts by block usage
242286
Then STDOUT should be a number
243287

244288
When I run `wp block search --synced-pattern={SYNCED_PATTERN_ID} --field=ID`
245-
Then STDOUT should be:
289+
Then STDOUT should contain:
246290
"""
247291
{SYNCED_PATTERN_POST_ID}
248292
"""

‎src/Block_Search_Command.php‎

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
* # Show selected fields as JSON for further processing.
107107
* $ wp block search --block=core/heading --post_status=publish --fields=ID,post_type,occurrences --format=json
108108
*
109+
* # Limit the candidate posts scanned with a native query argument.
110+
* $ wp block search --block=core/paragraph --showposts=50 --format=ids
111+
*
109112
* # Restrict search to specific posts and return the count.
110113
* $ wp block search --style=rounded --post__in=21,42,84 --format=count
111114
*
@@ -187,8 +190,17 @@ public function __invoke( $args, $assoc_args ) {
187190
$query_args['post_type'] = explode( ',', $query_args['post_type'] );
188191
}
189192

193+
$rough_prefilter = $this->build_rough_post_content_prefilter(
194+
$block_name,
195+
$block_namespace,
196+
$style_name,
197+
$pattern_name,
198+
$pattern_ns,
199+
$synced_pattern
200+
);
201+
190202
$results = [];
191-
$query = new \WP_Query( $query_args );
203+
$query = $this->run_query_with_rough_prefilter( $query_args, $rough_prefilter );
192204

193205
foreach ( $query->posts as $post ) {
194206
if ( ! $post instanceof \WP_Post ) {
@@ -278,6 +290,151 @@ private static function process_csv_arguments_to_arrays( array $assoc_args ) {
278290
return $assoc_args;
279291
}
280292

293+
/**
294+
* Builds a rough post_content prefilter marker.
295+
*
296+
* The prefilter is advisory only and must never exclude valid matches.
297+
*
298+
* @param string|null $block_name Requested exact block name.
299+
* @param string|null $block_namespace Requested block namespace.
300+
* @param string $style_name Requested style name.
301+
* @param string|null $pattern_name Requested exact pattern name.
302+
* @param string|null $pattern_namespace Requested pattern namespace.
303+
* @param int|null $synced_pattern Requested synced pattern post ID.
304+
* @return string[]
305+
*/
306+
private function build_rough_post_content_prefilter( $block_name, $block_namespace, $style_name, $pattern_name, $pattern_namespace, $synced_pattern ) {
307+
$markers = [];
308+
309+
if ( null !== $block_name && '' !== $block_name ) {
310+
$markers[] = '<!-- wp:' . $this->strip_core_block_namespace( $block_name );
311+
}
312+
313+
if ( null !== $block_namespace && '' !== $block_namespace ) {
314+
$markers[] = $this->build_block_namespace_prefilter_marker( $block_namespace );
315+
}
316+
317+
if ( '' !== $style_name ) {
318+
$markers[] = 'is-style-' . $style_name;
319+
}
320+
321+
if ( null !== $pattern_name && '' !== $pattern_name ) {
322+
$markers[] = '"patternName"';
323+
$markers[] = $pattern_name;
324+
}
325+
326+
if ( null !== $pattern_namespace && '' !== $pattern_namespace ) {
327+
$markers[] = '"patternName"';
328+
$markers[] = $pattern_namespace . '/';
329+
}
330+
331+
if ( null !== $synced_pattern ) {
332+
$markers[] = '"ref"';
333+
$markers[] = (string) $synced_pattern;
334+
}
335+
336+
return array_values( array_unique( $markers ) );
337+
}
338+
339+
/**
340+
* Runs a query with an optional rough post_content prefilter.
341+
*
342+
* @param array $query_args WP_Query arguments.
343+
* @param string[] $markers Rough prefilter markers.
344+
* @return \WP_Query
345+
*/
346+
private function run_query_with_rough_prefilter( array $query_args, array $markers ) {
347+
if ( [] === $markers ) {
348+
return new \WP_Query( $query_args );
349+
}
350+
351+
$query_args['wp_cli_block_search_rough_markers'] = $markers;
352+
353+
$posts_where = [ $this, 'filter_posts_where_for_rough_post_content_prefilter' ];
354+
add_filter( 'posts_where', $posts_where, 10, 2 );
355+
356+
try {
357+
return new \WP_Query( $query_args );
358+
} finally {
359+
remove_filter( 'posts_where', $posts_where, 10 );
360+
}
361+
}
362+
363+
/**
364+
* Applies the rough post_content prefilter to this command's query only.
365+
*
366+
* @param string $where The WHERE clause.
367+
* @param \WP_Query $query Query instance.
368+
* @return string
369+
*/
370+
public function filter_posts_where_for_rough_post_content_prefilter( $where, $query ) {
371+
global $wpdb;
372+
373+
$markers = $query->get( 'wp_cli_block_search_rough_markers' );
374+
375+
if ( ! is_array( $markers ) || [] === $markers ) {
376+
return $where;
377+
}
378+
379+
$clauses = [];
380+
381+
foreach ( $markers as $marker ) {
382+
if ( ! is_string( $marker ) || '' === $marker ) {
383+
continue;
384+
}
385+
386+
$clauses[] = $wpdb->prepare(
387+
"{$wpdb->posts}.post_content LIKE %s",
388+
'%' . $wpdb->esc_like( $marker ) . '%'
389+
);
390+
}
391+
392+
if ( [] === $clauses ) {
393+
return $where;
394+
}
395+
396+
return $where . ' AND ' . implode(
397+
' AND ',
398+
array_map(
399+
static function ( $clause ) {
400+
return '(' . $clause . ')';
401+
},
402+
$clauses
403+
)
404+
);
405+
}
406+
407+
/**
408+
* Normalizes core block names to their serialized comment marker form.
409+
*
410+
* @param string $block_name Requested exact block name.
411+
* @return string
412+
*/
413+
private function strip_core_block_namespace( $block_name ) {
414+
if ( 0 === strpos( $block_name, 'core/' ) ) {
415+
return substr( $block_name, 5 );
416+
}
417+
418+
return $block_name;
419+
}
420+
421+
/**
422+
* Builds a rough serialized comment marker for a block namespace.
423+
*
424+
* Core blocks omit the namespace in serialized comments, so the safest
425+
* coarse marker for core is the generic block comment prefix.
426+
*
427+
* @param string $block_namespace Requested block namespace.
428+
* @return string
429+
*/
430+
private function build_block_namespace_prefilter_marker( $block_namespace ) {
431+
if ( 'core' === $block_namespace ) {
432+
return '<!-- wp:';
433+
}
434+
435+
return '<!-- wp:' . $block_namespace . '/';
436+
}
437+
281438
/**
282439
* Finds matching blocks in post content.
283440
*

0 commit comments

Comments
 (0)