Skip to content

Commit

Permalink
Blocks: Extend registered block types to include default attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Nov 9, 2019
1 parent c1e7d7e commit c7cded6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,39 @@ function gutenberg_get_registered_social_link_blocks() {
return $registered_social_link_blocks;
}

/**
* Registers blocks using block manifests discovered in block library.
*
* @since 6.9.0
*/
function gutenberg_register_block_types() {
$registry = WP_Block_Type_Registry::get_instance();

$block_manifests = glob( dirname( dirname( __FILE__ ) ) . '/packages/block-library/src/*/block.json' );
foreach ( $block_manifests as $block_manifest ) {
$block_settings = json_decode( file_get_contents( $block_manifest ), true );
if ( is_null( $block_settings ) || ! isset( $block_settings['name'] ) ) {
continue;
}

if ( $registry->is_registered( $block_settings['name'] ) ) {
$block_settings = array_merge(
(array) $registry->get_registered( $block_settings['name'] ),
$block_settings
);

$registry->unregister( $block_settings['name'] );
}

register_block_type(
$block_settings['name'],
// Apply default attributes manually, as it isn't currently possible
// to filter block registration.
gutenberg_add_default_attributes( $block_settings )
);
}
}

/**
* Substitutes the implementation of a core-registered block type, if exists,
* with the built result from the plugin.
Expand Down Expand Up @@ -76,6 +109,12 @@ function gutenberg_reregister_core_block_types() {

require $blocks_dir . $file;
}

// Add block library registration only after this is reached, since the
// above `require` calls may attach their own `init` actions which are
// deferred to run at the latest priority. Thus, to correctly merge block
// settings from manifests, it must be the last to run.
add_action( 'init', 'gutenberg_register_block_types', 20 );
}
add_action( 'init', 'gutenberg_reregister_core_block_types' );

Expand Down
44 changes: 44 additions & 0 deletions lib/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,50 @@ function gutenberg_replace_block_parser_class() {
}
add_filter( 'block_parser_class', 'gutenberg_replace_block_parser_class' );

/**
* Given a registered block type settings array, assigns default attributes.
* This must be called manually, as there is currently no way to hook to block
* registration.
*
* @since 6.9.0
*
* @param array $block_settings Block settings.
* @return array Block settings with default attributes.
*/
function gutenberg_add_default_attributes( $block_settings ) {
$supports = isset( $block_settings['supports'] ) ? $block_settings['supports'] : array();
$attributes = isset( $block_settings['attributes'] ) ? $block_settings['attributes'] : array();

if ( ! empty( $supports['align'] ) ) {
if ( ! isset( $attributes['align'] ) ) {
$attributes['align'] = array();
}

$attributes['align']['type'] = 'string';
}

if ( ! empty( $supports['anchor'] ) ) {
if ( ! isset( $attributes['anchor'] ) ) {
$attributes['anchor'] = array();
}

$attributes['anchor']['type'] = 'string';
$attributes['anchor']['source'] = 'attribute';
$attributes['anchor']['attribute'] = 'id';
$attributes['anchor']['selector'] = '*';
}

if ( ! isset( $supports['customClassName'] ) || false !== $supports['customClassName'] ) {
if ( ! isset( $attributes['className'] ) ) {
$attributes['className'] = array();
}

$attributes['className']['type'] = 'string';
}

return $block_settings;
}

/**
* Given a CSS selector string, returns an equivalent XPath selector.
*
Expand Down

0 comments on commit c7cded6

Please sign in to comment.