From 78487964a977c4b7762f3fa928f65639edc5674b Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Fri, 31 Jan 2020 21:09:09 +0200 Subject: [PATCH 1/2] do advanced rendering by hooking into render_block for navigation block --- lib/compat.php | 47 ------------------- .../block-library/src/navigation/index.php | 13 +++-- 2 files changed, 9 insertions(+), 51 deletions(-) diff --git a/lib/compat.php b/lib/compat.php index 3ab82002b2f92..4df03c8bf99b9 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -30,53 +30,6 @@ function gutenberg_safe_style_css_column_flex_basis( $attr ) { } add_filter( 'safe_style_css', 'gutenberg_safe_style_css_column_flex_basis' ); -/** - * Shim that hooks into `pre_render_block` so as to override `render_block` - * with a function that passes `render_callback` the block object as the - * argument. - * - * @see https://core.trac.wordpress.org/ticket/48104 - * - * @param string $pre_render The pre-rendered content. Default null. - * @param array $block The block being rendered. - * - * @return string String of rendered HTML. - */ -function gutenberg_provide_render_callback_with_block_object( $pre_render, $block ) { - global $post; - - $source_block = $block; - - /** This filter is documented in src/wp-includes/blocks.php */ - $block = apply_filters( 'render_block_data', $block, $source_block ); - - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); - $is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic(); - $block_content = ''; - $index = 0; - - foreach ( $block['innerContent'] as $chunk ) { - $block_content .= is_string( $chunk ) ? $chunk : render_block( $block['innerBlocks'][ $index++ ] ); - } - - if ( ! is_array( $block['attrs'] ) ) { - $block['attrs'] = array(); - } - - if ( $is_dynamic ) { - $global_post = $post; - - $prepared_attributes = $block_type->prepare_attributes_for_render( $block['attrs'] ); - $block_content = (string) call_user_func( $block_type->render_callback, $prepared_attributes, $block_content, $block ); - - $post = $global_post; - } - - /** This filter is documented in src/wp-includes/blocks.php */ - return apply_filters( 'render_block', $block_content, $block ); -} -add_filter( 'pre_render_block', 'gutenberg_provide_render_callback_with_block_object', 10, 2 ); - /** * Sets the current post for usage in template blocks. * diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index ddf0e2a23662d..a851614ef7577 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -128,7 +128,13 @@ function render_submenu_icon() { * * @return string Returns the post content with the legacy widget added. */ -function render_block_navigation( $attributes, $content, $block ) { +function render_block_navigation( $content, $block ) { + + if ( 'core/navigation' !== $block['blockName'] ) { + return $content; + } + + $attributes = $block['attrs']; $block['innerBlocks'] = gutenberg_remove_empty_navigation_links_recursive( $block['innerBlocks'] ); if ( empty( $block['innerBlocks'] ) ) { @@ -234,7 +240,7 @@ function build_navigation_html( $attributes, $block, $colors, $font_sizes, $is_l $html .= ''; // Append submenu icon to top-level item. - if ( $attributes['showSubmenuIcon'] && $is_level_zero && $has_submenu ) { + if ( ! empty( $attributes['showSubmenuIcon'] ) && $is_level_zero && $has_submenu ) { $html .= '' . render_submenu_icon() . ''; } @@ -291,9 +297,8 @@ function register_block_core_navigation() { 'default' => false, ), ), - - 'render_callback' => 'render_block_navigation', ) ); } add_action( 'init', 'register_block_core_navigation' ); +add_filter( 'render_block', 'render_block_navigation', 10, 2 ); From 1cfdf425223b58487b8a0bb0f6c0c786da554763 Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Sat, 1 Feb 2020 10:26:00 +0200 Subject: [PATCH 2/2] removes superfluous comment, lint fixes --- packages/block-library/src/navigation/index.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index a851614ef7577..6270db2d86bff 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -122,7 +122,6 @@ function render_submenu_icon() { /** * Renders the `core/navigation` block on server. * - * @param array $attributes The block attributes. * @param array $content The saved content. * @param array $block The parsed block. * @@ -267,7 +266,7 @@ function register_block_core_navigation() { register_block_type( 'core/navigation', array( - 'attributes' => array( + 'attributes' => array( 'className' => array( 'type' => 'string', ),