Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Block Registration to Happen Automatically for any blocks containing a block.json file #78

Merged
merged 8 commits into from
Feb 25, 2022
36 changes: 31 additions & 5 deletions themes/10up-theme/includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,45 @@ function setup() {
}

/**
* Add in blocks that are registered in this theme
* Automatically registers all blocks that are located within the includes/blocks directory
*
* @return void
*/
function register_theme_blocks() {
// Filter the plugins URL to allow us to have blocks in themes with linked assets. i.e editorScripts
add_filter( 'plugins_url', __NAMESPACE__ . '\filter_plugins_url', 10, 2 );

// Require custom blocks.
require_once TENUP_THEME_BLOCK_DIR . '/example-block/register.php';
// Register all the blocks in the theme
if ( file_exists( TENUP_THEME_BLOCK_DIR ) ) {
$block_json_files = glob( TENUP_THEME_BLOCK_DIR . '*/block.json' );

// Call block register functions for each block.
Example\register();
// auto register all blocks that were found.
foreach ( $block_json_files as $filename ) {

$block_folder = dirname( $filename );

$block_options = [];

$markup_file_path = $block_folder . '/markup.php';
if ( file_exists( $markup_file_path ) ) {

// only add the render callback if the block has a file called markdown.php in it's directory
$block_options['render_callback'] = function( $attributes, $content, $block ) use ( $block_folder ) {

// create helpful variables that will be accessible in markup.php file
$context = $block->context;
$wrapper_attributes = wp_kses_post( get_block_wrapper_attributes() );

// get the actual markup from the markup.php file
ob_start();
include $block_folder . '/markup.php';
return ob_get_clean();
};
};

register_block_type_from_metadata( $block_folder, $block_options );
};
};

// Remove the filter after we register the blocks
remove_filter( 'plugins_url', __NAMESPACE__ . '\filter_plugins_url', 10, 2 );
Expand Down
31 changes: 9 additions & 22 deletions themes/10up-theme/includes/blocks/example-block/markup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,17 @@
*
* @package TenUpScaffold\Blocks\Example
*
* @var array $args {
* $args is provided by get_template_part.
*
* @type array $attributes Block attributes.
* @type array $content Block content.
* @type array $block Block instance.
* }
* @var array $attributes Block attributes.
* @var string $content Block content.
* @var WP_Block $block Block instance.
* @var array $context BLock context.
* @var string $class_name Generated class name for concatenation.
* @var string $wrapper_attributes Block Wrapper Attributes. To be applied to the outermost element.
*/

// Set defaults.
$args = wp_parse_args(
$args,
[
'attributes' => [
'title' => __( 'Custom title default', 'tenup' ),
],
]
);

$wrapper_attributes = get_block_wrapper_attributes();

?>
<div <?php echo wp_kses_post( $wrapper_attributes ); ?>>
<h2 class="wp-block-example-block__title">
<?php echo wp_kses_post( $args['attributes']['title'] ); ?>
<div <?php echo wp_kses_post( $wrapper_attributes ); ?>">
<h2 class="<?php echo sanitize_html_class( $class_name ); ?>__title">
<?php echo wp_kses_post( $attributes['title'] ); ?>
</h2>
</div>
48 changes: 0 additions & 48 deletions themes/10up-theme/includes/blocks/example-block/register.php

This file was deleted.