From 477031542e0297bea994cb46a522e9ae44ed80fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Mon, 24 Jan 2022 15:09:11 +0100 Subject: [PATCH 1/5] update block registration to happen automatically This changes the block registration flow so that all blocks located within the directioy that have a block.json file get automatically registered. They also automatically get their added if the directory where the file is located also contains a file --- themes/10up-theme/includes/blocks.php | 39 +++++++++++++-- .../includes/blocks/example-block/markup.php | 30 ++++-------- .../blocks/example-block/register.php | 49 ------------------- 3 files changed, 43 insertions(+), 75 deletions(-) delete mode 100644 themes/10up-theme/includes/blocks/example-block/register.php diff --git a/themes/10up-theme/includes/blocks.php b/themes/10up-theme/includes/blocks.php index b6a33a27..b781c247 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,40 @@ 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'; + // get all block.json files within the includes/blocks folder + $old_working_dir = getcwd(); + chdir( ABSPATH ); + $block_json_files = glob( 'wp-content/themes/10up-theme/includes/blocks/*/block.json' ); + chdir( $old_working_dir ); - // Call block register functions for each block. - Example\register(); + // auto register all blocks that were found. + foreach ( $block_json_files as $filename ) { + + $block_folder = '/var/www/html/' . 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; + list( $namespace, $block_name ) = explode( '/', $block->name ); + $class_name = sanitize_html_class( "wp-block-$namespace-$block_name" ); + $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 f30c0602..c63c46f2 100644 --- a/themes/10up-theme/includes/blocks/example-block/markup.php +++ b/themes/10up-theme/includes/blocks/example-block/markup.php @@ -4,29 +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' => [ - 'customTitle' => __( 'Custom title default', 'tenup' ), - ], - 'class_name' => 'wp-block-tenup-example', - ] -); - ?> -
-

- +
"> +

+

diff --git a/themes/10up-theme/includes/blocks/example-block/register.php b/themes/10up-theme/includes/blocks/example-block/register.php deleted file mode 100644 index ec02715e..00000000 --- a/themes/10up-theme/includes/blocks/example-block/register.php +++ /dev/null @@ -1,49 +0,0 @@ - $n( 'render_block_callback' ), - ] - ); -} - -/** - * Render callback method for the block - * - * @param array $attributes The blocks attributes - * @param string $content Data returned from InnerBlocks.Content - * @param array $block Block information such as context. - * - * @return string The rendered block markup. - */ -function render_block_callback( $attributes, $content, $block ) { - ob_start(); - get_template_part( - 'includes/blocks/example-block/markup', - null, - [ - 'class_name' => 'wp-block-tenup-example', - 'attributes' => $attributes, - 'content' => $content, - 'block' => $block, - ] - ); - - return ob_get_clean(); -} From 2aa1d3b9d53c1ae868c7232dbeb8afb3d6bdb33a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Wed, 16 Feb 2022 10:04:28 +0100 Subject: [PATCH 2/5] fix update how blocks are being included --- themes/10up-theme/includes/blocks.php | 49 ++++++++++++++------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/themes/10up-theme/includes/blocks.php b/themes/10up-theme/includes/blocks.php index b781c247..6b3f787b 100644 --- a/themes/10up-theme/includes/blocks.php +++ b/themes/10up-theme/includes/blocks.php @@ -50,39 +50,40 @@ 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 ); - // get all block.json files within the includes/blocks folder - $old_working_dir = getcwd(); - chdir( ABSPATH ); - $block_json_files = glob( 'wp-content/themes/10up-theme/includes/blocks/*/block.json' ); - chdir( $old_working_dir ); + // Register blocks for the active theme, for both parent and child theme, + // if applicable. + foreach ( wp_get_active_and_valid_themes() as $theme ) { + $block_directory_path = $theme . '/includes/blocks/'; + if ( file_exists( $block_directory_path ) ) { + $block_json_files = glob( $block_directory_path . '*/block.json' ); - // auto register all blocks that were found. - foreach ( $block_json_files as $filename ) { + // auto register all blocks that were found. + foreach ( $block_json_files as $filename ) { - $block_folder = '/var/www/html/' . dirname( $filename ); + $block_folder = dirname( $filename ); - $block_options = []; + $block_options = []; - $markup_file_path = $block_folder . '/markup.php'; - if ( file_exists( $markup_file_path ) ) { + $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 ) { + // 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; - list( $namespace, $block_name ) = explode( '/', $block->name ); - $class_name = sanitize_html_class( "wp-block-$namespace-$block_name" ); - $wrapper_attributes = wp_kses_post( get_block_wrapper_attributes() ); + // 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(); + // 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 ); }; }; - - register_block_type_from_metadata( $block_folder, $block_options ); }; // Remove the filter after we register the blocks From f8cec6342fd761324f1eed92ee0f608798b7139f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Fri, 25 Feb 2022 17:08:14 +0100 Subject: [PATCH 3/5] fix remove looping over possible child themes --- themes/10up-theme/includes/blocks.php | 45 +++++++++++++-------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/themes/10up-theme/includes/blocks.php b/themes/10up-theme/includes/blocks.php index 75cd0526..7838ee92 100644 --- a/themes/10up-theme/includes/blocks.php +++ b/themes/10up-theme/includes/blocks.php @@ -50,39 +50,36 @@ 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 ); - // Register blocks for the active theme, for both parent and child theme, - // if applicable. - foreach ( wp_get_active_and_valid_themes() as $theme ) { - $block_directory_path = $theme . '/includes/blocks/'; - if ( file_exists( $block_directory_path ) ) { - $block_json_files = glob( $block_directory_path . '*/block.json' ); + // Register all the blocks in the theme + $block_directory_path = TENUP_THEME_BLOCK_DIR; + if ( file_exists( $block_directory_path ) ) { + $block_json_files = glob( $block_directory_path . '*/block.json' ); - // auto register all blocks that were found. - foreach ( $block_json_files as $filename ) { + // auto register all blocks that were found. + foreach ( $block_json_files as $filename ) { - $block_folder = dirname( $filename ); + $block_folder = dirname( $filename ); - $block_options = []; + $block_options = []; - $markup_file_path = $block_folder . '/markup.php'; - if ( file_exists( $markup_file_path ) ) { + $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 ) { + // 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() ); + // 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(); - }; + // 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 ); }; + + register_block_type_from_metadata( $block_folder, $block_options ); }; }; From 41f46c7b2ef74865789aaf657cb9398863902be1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Fri, 25 Feb 2022 17:08:47 +0100 Subject: [PATCH 4/5] fix reference of block title attribute in markup.php --- themes/10up-theme/includes/blocks/example-block/markup.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/10up-theme/includes/blocks/example-block/markup.php b/themes/10up-theme/includes/blocks/example-block/markup.php index c63c46f2..958bdb9a 100644 --- a/themes/10up-theme/includes/blocks/example-block/markup.php +++ b/themes/10up-theme/includes/blocks/example-block/markup.php @@ -15,6 +15,6 @@ ?>
">

- +

From 02b7a14a0b00c2b776ff7889e39b9cc12c512cf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Fri, 25 Feb 2022 18:00:25 +0100 Subject: [PATCH 5/5] fix use global variable directly --- themes/10up-theme/includes/blocks.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/themes/10up-theme/includes/blocks.php b/themes/10up-theme/includes/blocks.php index 7838ee92..a44a2e07 100644 --- a/themes/10up-theme/includes/blocks.php +++ b/themes/10up-theme/includes/blocks.php @@ -51,9 +51,8 @@ function register_theme_blocks() { add_filter( 'plugins_url', __NAMESPACE__ . '\filter_plugins_url', 10, 2 ); // Register all the blocks in the theme - $block_directory_path = TENUP_THEME_BLOCK_DIR; - if ( file_exists( $block_directory_path ) ) { - $block_json_files = glob( $block_directory_path . '*/block.json' ); + if ( file_exists( TENUP_THEME_BLOCK_DIR ) ) { + $block_json_files = glob( TENUP_THEME_BLOCK_DIR . '*/block.json' ); // auto register all blocks that were found. foreach ( $block_json_files as $filename ) {