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

Do advanced rendering by hooking into render_block for navigation block #19991

Merged
merged 3 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What relevance does this change have in the original goal of using the filter instead of a new argument?

Copy link
Contributor Author

@draganescu draganescu Feb 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why, but in my local setup while testing, I got a notice of this not being defined. Also I think this check is better than the initial code, if ( $attributes['showSubmenuIcon'] assumes the item exists.

$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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if the convention is codified anywhere, but typically I would expect to see the add_filter to be placed immediately following the callback function (render_block_navigation in this case).