|
106 | 106 | * # Show selected fields as JSON for further processing. |
107 | 107 | * $ wp block search --block=core/heading --post_status=publish --fields=ID,post_type,occurrences --format=json |
108 | 108 | * |
| 109 | + * # Limit the candidate posts scanned with a native query argument. |
| 110 | + * $ wp block search --block=core/paragraph --showposts=50 --format=ids |
| 111 | + * |
109 | 112 | * # Restrict search to specific posts and return the count. |
110 | 113 | * $ wp block search --style=rounded --post__in=21,42,84 --format=count |
111 | 114 | * |
@@ -187,8 +190,17 @@ public function __invoke( $args, $assoc_args ) { |
187 | 190 | $query_args['post_type'] = explode( ',', $query_args['post_type'] ); |
188 | 191 | } |
189 | 192 |
|
| 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 | + |
190 | 202 | $results = []; |
191 | | - $query = new \WP_Query( $query_args ); |
| 203 | + $query = $this->run_query_with_rough_prefilter( $query_args, $rough_prefilter ); |
192 | 204 |
|
193 | 205 | foreach ( $query->posts as $post ) { |
194 | 206 | if ( ! $post instanceof \WP_Post ) { |
@@ -278,6 +290,151 @@ private static function process_csv_arguments_to_arrays( array $assoc_args ) { |
278 | 290 | return $assoc_args; |
279 | 291 | } |
280 | 292 |
|
| 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 | + |
281 | 438 | /** |
282 | 439 | * Finds matching blocks in post content. |
283 | 440 | * |
|
0 commit comments