Skip to content

Commit

Permalink
Do advanced rendering by hooking into render_block for navigation blo…
Browse files Browse the repository at this point in the history
…ck (#19991)

* do advanced rendering by hooking into render_block for navigation block

* removes superfluous comment, lint fixes
  • Loading branch information
draganescu authored Feb 3, 2020
1 parent 6de983f commit 1e35ca3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 56 deletions.
50 changes: 0 additions & 50 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +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;
if ( 'core/navigation' !== $block['blockName'] ) {
return $pre_render;
}

$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.
*
Expand Down
16 changes: 10 additions & 6 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,18 @@ 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.
*
* @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'] ) ) {
Expand Down Expand Up @@ -234,7 +239,7 @@ function build_navigation_html( $attributes, $block, $colors, $font_sizes, $is_l
$html .= '</span>';

// 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 .= '<span class="wp-block-navigation-link__submenu-icon">' . render_submenu_icon() . '</span>';
}

Expand All @@ -261,7 +266,7 @@ function register_block_core_navigation() {
register_block_type(
'core/navigation',
array(
'attributes' => array(
'attributes' => array(
'className' => array(
'type' => 'string',
),
Expand Down Expand Up @@ -291,9 +296,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 );

0 comments on commit 1e35ca3

Please sign in to comment.