diff --git a/themes/10up-theme/includes/blocks.php b/themes/10up-theme/includes/blocks.php index b7f23153..a44a2e07 100644 --- a/themes/10up-theme/includes/blocks.php +++ b/themes/10up-theme/includes/blocks.php @@ -42,7 +42,7 @@ 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 */ @@ -50,11 +50,37 @@ 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 ); diff --git a/themes/10up-theme/includes/blocks/example-block/markup.php b/themes/10up-theme/includes/blocks/example-block/markup.php index 80d8b6d1..958bdb9a 100644 --- a/themes/10up-theme/includes/blocks/example-block/markup.php +++ b/themes/10up-theme/includes/blocks/example-block/markup.php @@ -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(); - ?> -