Blocks: Introduce `register_block_type()`, `unregister_block_type()`, and `get_dynamic_blocks()` functions.

These helper functions allow easy access to the global block registry.

Merges [43743] from the 5.0 branch to trunk.

See #45109.

Built from https://develop.svn.wordpress.org/trunk@44109


git-svn-id: http://core.svn.wordpress.org/trunk@43939 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast 2018-12-13 09:54:25 +00:00
parent 2d3f734847
commit 0f1544dabc
2 changed files with 54 additions and 1 deletions

View File

@ -7,6 +7,39 @@
* @since 5.0.0
*/
/**
* Registers a block type.
*
* @since 5.0.0
*
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
* complete WP_Block_Type instance. In case a WP_Block_Type
* is provided, the $args parameter will be ignored.
* @param array $args {
* Optional. Array of block type arguments. Any arguments may be defined, however the
* ones described below are supported by default. Default empty array.
*
* @type callable $render_callback Callback used to render blocks of this block type.
* }
* @return WP_Block_Type|false The registered block type on success, or false on failure.
*/
function register_block_type( $name, $args = array() ) {
return WP_Block_Type_Registry::get_instance()->register( $name, $args );
}
/**
* Unregisters a block type.
*
* @since 5.0.0
*
* @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
* complete WP_Block_Type instance.
* @return WP_Block_Type|false The unregistered block type on success, or false on failure.
*/
function unregister_block_type( $name ) {
return WP_Block_Type_Registry::get_instance()->unregister( $name );
}
/**
* Determine whether a post or content string has blocks.
*
@ -59,3 +92,23 @@ function has_block( $block_type, $post = null ) {
return false !== strpos( $post, '<!-- wp:' . $block_type . ' ' );
}
/**
* Returns an array of the names of all registered dynamic block types.
*
* @since 5.0.0
*
* @return array Array of dynamic block names.
*/
function get_dynamic_block_names() {
$dynamic_block_names = array();
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
foreach ( $block_types as $block_type ) {
if ( $block_type->is_dynamic() ) {
$dynamic_block_names[] = $block_type->name;
}
}
return $dynamic_block_names;
}

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.1-alpha-44108';
$wp_version = '5.1-alpha-44109';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.