Skip to content

Commit 0f6e7d6

Browse files
committed
Add block search command
1 parent 4fb8257 commit 0f6e7d6

6 files changed

Lines changed: 824 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ wp block
2424
# List all registered block types
2525
$ wp block type list
2626

27+
# Find posts that use a rounded block style
28+
$ wp block search --style=rounded
29+
2730
# Get a specific block pattern
2831
$ wp block pattern get my-theme/hero
2932

@@ -38,6 +41,123 @@ wp block
3841

3942

4043

44+
### wp block search
45+
46+
Searches posts for block usage.
47+
48+
~~~
49+
wp block search [--block=<block-name>] [--block-namespace=<block-namespace>] [--style=<style-name>] [--pattern=<pattern-name>] [--pattern-namespace=<pattern-namespace>] [--synced-pattern=<post-id>] [--<field>=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>]
50+
~~~
51+
52+
Returns matching posts where the requested block appears anywhere in the
53+
parsed block tree, including nested blocks.
54+
55+
At least one search filter is required: `--block`, `--block-namespace`, `--style`, `--pattern`, `--pattern-namespace`, or `--synced-pattern`.
56+
57+
Pattern filters match embedded block metadata stored in `metadata.patternName`.
58+
59+
The `--synced-pattern` filter matches reusable block references by synced pattern post ID.
60+
61+
**OPTIONS**
62+
63+
[--block=<block-name>]
64+
Block type name to search for (for example, 'core/paragraph').
65+
66+
[--block-namespace=<block-namespace>]
67+
Limit matches to blocks within a specific namespace (for example, 'core').
68+
69+
[--style=<style-name>]
70+
Limit matches to blocks using a specific block style.
71+
72+
[--pattern=<pattern-name>]
73+
Limit matches to blocks embedded from a specific pattern (for example, 'twentytwentyfive/event-rsvp').
74+
75+
[--pattern-namespace=<pattern-namespace>]
76+
Limit matches to blocks embedded from patterns within a specific namespace (for example, 'twentytwentyfive').
77+
78+
[--synced-pattern=<post-id>]
79+
Limit matches to reusable block references for a specific synced pattern post ID.
80+
81+
Note: `--block` and `--block-namespace` are mutually exclusive. `--pattern` and `--pattern-namespace` are also mutually exclusive.
82+
83+
[--<field>=<value>]
84+
One or more args to pass to WP_Query.
85+
86+
[--field=<field>]
87+
Prints the value of a single field for each matching post.
88+
89+
[--fields=<fields>]
90+
Limit the output to specific result fields.
91+
92+
[--format=<format>]
93+
Render output in a particular format.
94+
---
95+
default: table
96+
options:
97+
- table
98+
- csv
99+
- json
100+
- count
101+
- yaml
102+
- ids
103+
---
104+
105+
**AVAILABLE FIELDS**
106+
107+
These fields will be displayed by default for each matching post:
108+
109+
* ID
110+
* post_title
111+
* post_name
112+
* post_date
113+
* post_status
114+
115+
These fields are optionally available:
116+
117+
* post_type
118+
* url
119+
* occurrences
120+
121+
**EXAMPLES**
122+
123+
# Find posts using the paragraph block.
124+
$ wp block search --block=core/paragraph
125+
126+
# Find posts using any block in namespace like 'core'.
127+
$ wp block search --block-namespace=core
128+
129+
# Find posts using any blocks with block style of 'rounded'.
130+
$ wp block search --style=rounded
131+
132+
# Combine namespace and style filters.
133+
$ wp block search --block-namespace=core --style=rounded
134+
135+
# Find posts using blocks embedded from a specific pattern.
136+
$ wp block search --pattern=twentytwentyfive/event-rsvp
137+
138+
# Find posts using blocks embedded from patterns in a specific namespace.
139+
$ wp block search --pattern-namespace=twentytwentyfive
140+
141+
# Find posts using a specific synced pattern.
142+
$ wp block search --synced-pattern=123
143+
144+
# Combine pattern and block filters.
145+
$ wp block search --pattern-namespace=twentytwentyfive --block=core/image
146+
147+
# Limit search to published pages and show selected fields as JSON.
148+
$ wp block search --block=core/image --post_type=page --post_status=publish --fields=ID,post_title,occurrences --format=json
149+
150+
# Restrict search to specific posts and return the match count.
151+
$ wp block search --style=rounded --post__in=21,42,84 --format=count
152+
153+
# Return only matching post IDs.
154+
$ wp block search --block=core/paragraph --format=ids
155+
156+
# Return count of matching posts.
157+
$ wp block search --block=core/heading --format=count
158+
159+
160+
41161
### wp block type
42162

43163
Retrieves details on registered block types.

‎block-command.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@
5454
WP_CLI::add_command( 'block binding', WP_CLI\Block\Block_Binding_Command::class, [ 'before_invoke' => $wpcli_block_before_invoke_6_5 ] );
5555
WP_CLI::add_command( 'block template', WP_CLI\Block\Block_Template_Command::class, [ 'before_invoke' => $wpcli_block_before_invoke_5_9 ] );
5656
WP_CLI::add_command( 'block synced-pattern', WP_CLI\Block\Block_Synced_Pattern_Command::class, [ 'before_invoke' => $wpcli_block_before_invoke_5_0 ] );
57+
WP_CLI::add_command( 'block search', WP_CLI\Block\Block_Search_Command::class, [ 'before_invoke' => $wpcli_block_before_invoke_5_0 ] );

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"bundled": true,
3030
"commands": [
3131
"block",
32+
"block search",
3233
"block type",
3334
"block type exists",
3435
"block type list",

‎features/block-search.feature‎

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
@require-wp-5.0
2+
Feature: Search posts by block usage
3+
4+
Background:
5+
Given a WP install
6+
7+
Scenario: Search posts by block type usage
8+
When I run `wp post create --post_title='Quote Post' --post_status=publish --post_type=post --post_content='<!-- wp:quote --><blockquote class="wp-block-quote"><p>Hello world</p></blockquote><!-- /wp:quote -->' --porcelain`
9+
Then STDOUT should be a number
10+
And save STDOUT as {QUOTE_POST_ID}
11+
12+
When I run `wp block search --block=core/quote --post_type=post --field=ID`
13+
Then STDOUT should be:
14+
"""
15+
{QUOTE_POST_ID}
16+
"""
17+
18+
Scenario: Search posts by block namespace
19+
When I run `wp post create --post_title='Core Namespace Post' --post_status=publish --post_type=post --post_content='<!-- wp:quote --><blockquote class="wp-block-quote"><p>Hello core</p></blockquote><!-- /wp:quote -->' --porcelain`
20+
Then STDOUT should be a number
21+
And save STDOUT as {CORE_NAMESPACE_POST_ID}
22+
23+
When I run `wp block search --block-namespace=core --post_type=post --field=ID`
24+
Then STDOUT should contain:
25+
"""
26+
{CORE_NAMESPACE_POST_ID}
27+
"""
28+
29+
Scenario: Search nested block usage
30+
When I run `wp post create --post_title='Nested Image Post' --post_status=publish --post_type=page --post_content='<!-- wp:group --><div class="wp-block-group"><!-- wp:image {"sizeSlug":"large"} --><figure class="wp-block-image size-large"><img alt="" /></figure><!-- /wp:image --></div><!-- /wp:group -->' --porcelain`
31+
Then STDOUT should be a number
32+
And save STDOUT as {NESTED_POST_ID}
33+
34+
When I run `wp block search --block=core/image --post_type=page --field=ID`
35+
Then STDOUT should be:
36+
"""
37+
{NESTED_POST_ID}
38+
"""
39+
40+
Scenario: Search block usage by style
41+
When I run `wp post create --post_title='Rounded Image Post' --post_status=publish --post_type=post --post_content='<!-- wp:image {"className":"is-style-rounded"} --><figure class="wp-block-image is-style-rounded"><img alt="" /></figure><!-- /wp:image -->' --porcelain`
42+
Then STDOUT should be a number
43+
And save STDOUT as {ROUNDED_IMAGE_POST_ID}
44+
45+
When I run `wp post create --post_title='Plain Image Post' --post_status=publish --post_type=post --post_content='<!-- wp:image --><figure class="wp-block-image"><img alt="" /></figure><!-- /wp:image -->' --porcelain`
46+
Then STDOUT should be a number
47+
48+
When I run `wp post create --post_title='Rounded Quote Post' --post_status=publish --post_type=post --post_content='<!-- wp:quote {"className":"is-style-rounded"} --><blockquote class="wp-block-quote is-style-rounded"><p>Hello quote</p></blockquote><!-- /wp:quote -->' --porcelain`
49+
Then STDOUT should be a number
50+
And save STDOUT as {ROUNDED_QUOTE_POST_ID}
51+
52+
When I run `wp block search --block=core/image --style=rounded --field=ID`
53+
Then STDOUT should be:
54+
"""
55+
{ROUNDED_IMAGE_POST_ID}
56+
"""
57+
58+
When I run `wp block search --style=rounded --field=ID`
59+
Then STDOUT should contain:
60+
"""
61+
{ROUNDED_IMAGE_POST_ID}
62+
"""
63+
64+
And STDOUT should contain:
65+
"""
66+
{ROUNDED_QUOTE_POST_ID}
67+
"""
68+
69+
Scenario: Search block usage count
70+
When I run `wp post create --post_title='Counted Quote One' --post_status=publish --post_type=post --post_content='<!-- wp:quote --><blockquote class="wp-block-quote"><p>One</p></blockquote><!-- /wp:quote -->' --porcelain`
71+
Then STDOUT should be a number
72+
And save STDOUT as {COUNTED_QUOTE_ONE_ID}
73+
74+
When I run `wp post create --post_title='Counted Quote Two' --post_status=publish --post_type=post --post_content='<!-- wp:quote --><blockquote class="wp-block-quote"><p>Two</p></blockquote><!-- /wp:quote -->' --porcelain`
75+
Then STDOUT should be a number
76+
And save STDOUT as {COUNTED_QUOTE_TWO_ID}
77+
78+
When I run `wp block search --block=core/quote --post_type=post --format=count`
79+
Then STDOUT should be:
80+
"""
81+
2
82+
"""
83+
84+
When I run `wp block search --block=core/quote --post__in={COUNTED_QUOTE_ONE_ID},{COUNTED_QUOTE_TWO_ID} --format=ids`
85+
Then STDOUT should contain:
86+
"""
87+
{COUNTED_QUOTE_ONE_ID}
88+
"""
89+
90+
And STDOUT should contain:
91+
"""
92+
{COUNTED_QUOTE_TWO_ID}
93+
"""
94+
95+
Scenario: Search block usage with selected fields in JSON
96+
When I run `wp post create --post_title='Double Quote Post' --post_status=publish --post_type=post --post_content='<!-- wp:quote --><blockquote class="wp-block-quote"><p>One</p></blockquote><!-- /wp:quote --><!-- wp:quote --><blockquote class="wp-block-quote"><p>Two</p></blockquote><!-- /wp:quote -->' --porcelain`
97+
Then STDOUT should be a number
98+
And save STDOUT as {DOUBLE_QUOTE_POST_ID}
99+
100+
When I run `wp block search --block=core/quote --post__in={DOUBLE_QUOTE_POST_ID} --fields=ID,occurrences --format=json`
101+
Then STDOUT should be JSON containing:
102+
"""
103+
[{"ID":{DOUBLE_QUOTE_POST_ID},"occurrences":2}]
104+
"""
105+
106+
Scenario: Search blocks embedded from a specific pattern
107+
When I run `wp post create --post_title='Pattern RSVP Post' --post_status=publish --post_type=post --post_content='<!-- wp:group {"metadata":{"categories":["call-to-action"],"patternName":"twentytwentyfive/event-rsvp","name":"Event RSVP"}} --><div class="wp-block-group"><!-- wp:paragraph --><p>RSVP now</p><!-- /wp:paragraph --></div><!-- /wp:group -->' --porcelain`
108+
Then STDOUT should be a number
109+
And save STDOUT as {PATTERN_RSVP_POST_ID}
110+
111+
When I run `wp post create --post_title='Pattern Header Post' --post_status=publish --post_type=post --post_content='<!-- wp:group {"metadata":{"categories":["header"],"patternName":"twentytwentyfive/site-header","name":"Site Header"}} --><div class="wp-block-group"><!-- wp:paragraph --><p>Header block</p><!-- /wp:paragraph --></div><!-- /wp:group -->' --porcelain`
112+
Then STDOUT should be a number
113+
And save STDOUT as {PATTERN_HEADER_POST_ID}
114+
115+
When I run `wp block search --pattern=twentytwentyfive/event-rsvp --field=ID`
116+
Then STDOUT should be:
117+
"""
118+
{PATTERN_RSVP_POST_ID}
119+
"""
120+
121+
When I run `wp block search --pattern=twentytwentyfive/event-rsvp --field=ID --format=ids`
122+
Then STDOUT should not contain:
123+
"""
124+
{PATTERN_HEADER_POST_ID}
125+
"""
126+
127+
When I run `wp block search --pattern-namespace=twentytwentyfive --field=ID`
128+
Then STDOUT should contain:
129+
"""
130+
{PATTERN_RSVP_POST_ID}
131+
"""
132+
133+
And STDOUT should contain:
134+
"""
135+
{PATTERN_HEADER_POST_ID}
136+
"""
137+
138+
Scenario: Search pattern blocks with an additional block filter
139+
When I run `wp post create --post_title='Patterned Image Post' --post_status=publish --post_type=post --post_content='<!-- wp:group {"metadata":{"categories":["media"],"patternName":"twentytwentyfive/media-highlight","name":"Media Highlight"}} --><div class="wp-block-group"><!-- wp:image --><figure class="wp-block-image"><img alt="" /></figure><!-- /wp:image --></div><!-- /wp:group -->' --porcelain`
140+
Then STDOUT should be a number
141+
And save STDOUT as {PATTERN_IMAGE_POST_ID}
142+
143+
When I run `wp block search --pattern-namespace=twentytwentyfive --block=core/image --field=ID`
144+
Then STDOUT should be:
145+
"""
146+
{PATTERN_IMAGE_POST_ID}
147+
"""
148+
149+
Scenario: Search uses the nearest pattern ancestor for nested patterns
150+
When I run `wp post create --post_title='Nested Pattern Image Post' --post_status=publish --post_type=post --post_content='<!-- wp:group {"metadata":{"categories":["outer"],"patternName":"twentytwentyfive/outer-shell","name":"Outer Shell"}} --><div class="wp-block-group"><!-- wp:group {"metadata":{"categories":["inner"],"patternName":"twentytwentyfive/inner-media","name":"Inner Media"}} --><div class="wp-block-group"><!-- wp:image --><figure class="wp-block-image"><img alt="" /></figure><!-- /wp:image --></div><!-- /wp:group --></div><!-- /wp:group -->' --porcelain`
151+
Then STDOUT should be a number
152+
And save STDOUT as {NESTED_PATTERN_IMAGE_POST_ID}
153+
154+
When I run `wp block search --pattern=twentytwentyfive/inner-media --block=core/image --field=ID`
155+
Then STDOUT should be:
156+
"""
157+
{NESTED_PATTERN_IMAGE_POST_ID}
158+
"""
159+
160+
When I run `wp block search --pattern=twentytwentyfive/outer-shell --block=core/image --field=ID --format=ids`
161+
Then STDOUT should not contain:
162+
"""
163+
{NESTED_PATTERN_IMAGE_POST_ID}
164+
"""
165+
166+
Scenario: Search posts by synced pattern reference
167+
Given a pattern.html file:
168+
"""
169+
<!-- wp:paragraph --><p>Reusable</p><!-- /wp:paragraph -->
170+
"""
171+
When I run `wp block synced-pattern create pattern.html --title='Reusable Search Pattern' --porcelain`
172+
Then STDOUT should be a number
173+
And save STDOUT as {SYNCED_PATTERN_ID}
174+
175+
When I run `wp post create --post_title='Reusable Pattern Post' --post_status=publish --post_type=post --post_content='<!-- wp:block {"ref":{SYNCED_PATTERN_ID}} /-->' --porcelain`
176+
Then STDOUT should be a number
177+
And save STDOUT as {SYNCED_PATTERN_POST_ID}
178+
179+
When I run `wp post create --post_title='Different Reusable Pattern Post' --post_status=publish --post_type=post --post_content='<!-- wp:quote --><blockquote class="wp-block-quote"><p>Not reusable</p></blockquote><!-- /wp:quote -->' --porcelain`
180+
Then STDOUT should be a number
181+
182+
When I run `wp block search --synced-pattern={SYNCED_PATTERN_ID} --field=ID`
183+
Then STDOUT should be:
184+
"""
185+
{SYNCED_PATTERN_POST_ID}
186+
"""
187+
188+
Scenario: Pattern and pattern namespace are mutually exclusive
189+
When I try `wp block search --pattern=twentytwentyfive/event-rsvp --pattern-namespace=twentytwentyfive`
190+
Then STDERR should contain:
191+
"""
192+
The --pattern and --pattern-namespace parameters are mutually exclusive.
193+
"""
194+
And the return code should be 1
195+
196+
Scenario: Block name and namespace are mutually exclusive
197+
When I try `wp block search --block=core/quote --block-namespace=core`
198+
Then STDERR should contain:
199+
"""
200+
The --block and --block-namespace parameters are mutually exclusive.
201+
"""
202+
And the return code should be 1
203+
204+
Scenario: Synced pattern parameter must be a positive integer
205+
When I try `wp block search --synced-pattern=0`
206+
Then STDERR should contain:
207+
"""
208+
The --synced-pattern parameter must be a positive integer post ID.
209+
"""
210+
And the return code should be 1
211+
212+
Scenario: Search requires at least one filter
213+
When I try `wp block search`
214+
Then STDERR should contain:
215+
"""
216+
At least one block filter is required: --block, --block-namespace, --style, --pattern, --pattern-namespace, or --synced-pattern.
217+
"""
218+
And the return code should be 1

‎src/Block_Command.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*
1212
* # List all registered block types
1313
* $ wp block type list
14+
15+
* # Find posts by block usage
16+
* $ wp block search --block=core/button
1417
*
1518
* # Get a specific block pattern
1619
* $ wp block pattern get my-theme/hero

0 commit comments

Comments
 (0)