Skip to content

Commit 46cc3f1

Browse files
authored
Merge pull request #8 from wp-cli/add/block-type-exists
2 parents 2f015cb + 40ccc07 commit 46cc3f1

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎features/block-type.feature‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,18 @@ Feature: Block type commands
159159
--dynamic and --static are mutually exclusive
160160
"""
161161
And the return code should be 1
162+
163+
@require-wp-5.0
164+
Scenario: Check if a block type exists
165+
Given a WP install
166+
167+
When I run `wp block type exists core/archives`
168+
Then STDOUT should be:
169+
"""
170+
Success: Block type 'core/archives' is registered.
171+
"""
172+
And the return code should be 0
173+
174+
When I try `wp block type exists core/nonexistent`
175+
Then STDOUT should be empty
176+
And the return code should be 1

‎src/Block_Type_Command.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,43 @@ public function get( $args, $assoc_args ) {
253253
$formatter->display_item( $data );
254254
}
255255

256+
/**
257+
* Checks whether a block type is registered.
258+
*
259+
* Exits with return code 0 if the block type exists, 1 if it does not.
260+
*
261+
* ## OPTIONS
262+
*
263+
* <name>
264+
* : The block type name, including namespace.
265+
*
266+
* ## EXAMPLES
267+
*
268+
* # Check if a block type exists.
269+
* $ wp block type exists core/paragraph
270+
* Success: Block type 'core/paragraph' is registered.
271+
*
272+
* # Check for a non-existent block type.
273+
* $ wp block type exists core/nonexistent
274+
* $ echo $?
275+
* 1
276+
*
277+
* @subcommand exists
278+
*
279+
* @param array $args Positional arguments.
280+
* @param array $assoc_args Associative arguments.
281+
*/
282+
public function exists_( $args, $assoc_args ) {
283+
$registry = WP_Block_Type_Registry::get_instance();
284+
$name = $args[0];
285+
286+
if ( $registry->is_registered( $name ) ) {
287+
WP_CLI::success( "Block type '{$name}' is registered." );
288+
} else {
289+
WP_CLI::halt( 1 );
290+
}
291+
}
292+
256293
/**
257294
* Converts a WP_Block_Type object to an associative array.
258295
*

0 commit comments

Comments
 (0)