Make WordPress Core

Changeset 44109 for trunk


Ignore:
Timestamp:
12/13/2018 09:53:10 AM (6 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/blocks.php

    r44108 r44109  
    77 * @since 5.0.0
    88 */
     9
     10/**
     11 * Registers a block type.
     12 *
     13 * @since 5.0.0
     14 *
     15 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
     16 *                                   complete WP_Block_Type instance. In case a WP_Block_Type
     17 *                                   is provided, the $args parameter will be ignored.
     18 * @param array                $args {
     19 *     Optional. Array of block type arguments. Any arguments may be defined, however the
     20 *     ones described below are supported by default. Default empty array.
     21 *
     22 *     @type callable $render_callback Callback used to render blocks of this block type.
     23 * }
     24 * @return WP_Block_Type|false The registered block type on success, or false on failure.
     25 */
     26function register_block_type( $name, $args = array() ) {
     27    return WP_Block_Type_Registry::get_instance()->register( $name, $args );
     28}
     29
     30/**
     31 * Unregisters a block type.
     32 *
     33 * @since 5.0.0
     34 *
     35 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
     36 *                                   complete WP_Block_Type instance.
     37 * @return WP_Block_Type|false The unregistered block type on success, or false on failure.
     38 */
     39function unregister_block_type( $name ) {
     40    return WP_Block_Type_Registry::get_instance()->unregister( $name );
     41}
    942
    1043/**
     
    6093    return false !== strpos( $post, '<!-- wp:' . $block_type . ' ' );
    6194}
     95
     96/**
     97 * Returns an array of the names of all registered dynamic block types.
     98 *
     99 * @since 5.0.0
     100 *
     101 * @return array Array of dynamic block names.
     102 */
     103function get_dynamic_block_names() {
     104    $dynamic_block_names = array();
     105
     106    $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
     107    foreach ( $block_types as $block_type ) {
     108        if ( $block_type->is_dynamic() ) {
     109            $dynamic_block_names[] = $block_type->name;
     110        }
     111    }
     112
     113    return $dynamic_block_names;
     114}
Note: See TracChangeset for help on using the changeset viewer.