Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions features/block-type.feature
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,18 @@ Feature: Block type commands
--dynamic and --static are mutually exclusive
"""
And the return code should be 1

@require-wp-5.0
Scenario: Check if a block type exists
Given a WP install

When I run `wp block type exists core/archives`
Then STDOUT should be:
"""
Success: Block type 'core/archives' is registered.
"""
And the return code should be 0

When I try `wp block type exists core/nonexistent`
Then STDOUT should be empty
And the return code should be 1
37 changes: 37 additions & 0 deletions src/Block_Type_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,43 @@ public function get( $args, $assoc_args ) {
$formatter->display_item( $data );
}

/**
* Checks whether a block type is registered.
*
* Exits with return code 0 if the block type exists, 1 if it does not.
*
* ## OPTIONS
*
* <name>
* : The block type name, including namespace.
*
* ## EXAMPLES
*
* # Check if a block type exists.
* $ wp block type exists core/paragraph
* Success: Block type 'core/paragraph' is registered.
*
* # Check for a non-existent block type.
* $ wp block type exists core/nonexistent
* $ echo $?
* 1
*
* @subcommand exists
*
* @param array $args Positional arguments.
* @param array $assoc_args Associative arguments.
*/
public function exists_( $args, $assoc_args ) {
$registry = WP_Block_Type_Registry::get_instance();
$name = $args[0];

if ( $registry->is_registered( $name ) ) {
WP_CLI::success( "Block type '{$name}' is registered." );
} else {
WP_CLI::halt( 1 );
}
}

/**
* Converts a WP_Block_Type object to an associative array.
*
Expand Down