Skip to content

Commit

Permalink
Navigation: Remove hierarchy attribute, pass $block to render_callback
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Aug 12, 2019
1 parent 4809a29 commit 5b2cf72
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 64 deletions.
44 changes: 44 additions & 0 deletions lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,47 @@ function gutenberg_filter_wp_insert_post_data( $data, $postarr ) {
return $data;
}
add_filter( 'wp_insert_post_data', 'gutenberg_filter_wp_insert_post_data', 10, 2 );


/**
* 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.
*
* @param array $block A single parsed block object.
* @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;

$block_type->prepare_attributes_for_render( $block['attrs'] );
$block_content = (string) call_user_func( $block_type->render_callback, $block['attrs'], $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 );
14 changes: 0 additions & 14 deletions packages/block-library/src/navigation-menu/block.json

This file was deleted.

42 changes: 1 addition & 41 deletions packages/block-library/src/navigation-menu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
CheckboxControl,
PanelBody,
} from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -22,14 +20,8 @@ import { __ } from '@wordpress/i18n';

function NavigationMenu( {
attributes,
clientId,
setAttributes,
getBlockInnerHierarchy,
} ) {
const onChange = () => {
setAttributes( { hierarchy: getBlockInnerHierarchy( clientId ) } );
};

return (
<Fragment>
<InspectorControls>
Expand All @@ -44,11 +36,6 @@ function NavigationMenu( {
label={ __( 'Automatically add new pages' ) }
help={ __( 'Automatically add new top level pages to this menu.' ) }
/>
<CheckboxControl
value={ false }
onChange={ onChange }
label={ __( 'Set Tree' ) }
/>
</PanelBody>
</InspectorControls>
<div className="wp-block-navigation-menu">
Expand All @@ -61,31 +48,4 @@ function NavigationMenu( {
);
}

export default compose(
withSelect( ( select ) => {
const {
getBlock,
} = select( 'core/block-editor' );
/**
* Given a block client ID, returns the nested hierarchy from the given block, return the block itself for root level blocks.
*
* @param {string} clientId Block from which to find hierarchy.
*
* @return {Array} Hierarchy of menu item blocks
*/
const getBlockInnerHierarchy = ( clientId ) => {
const block = getBlock( clientId );
const getBlockChildren = ( innerBlock ) => {
return {
label: innerBlock.attributes.label,
destination: innerBlock.attributes.destination,
submenu: innerBlock.innerBlocks.map( getBlockChildren ),
};
};
return block.innerBlocks.map( getBlockChildren );
};
return {
getBlockInnerHierarchy,
};
} )
)( NavigationMenu );
export default NavigationMenu;
11 changes: 2 additions & 9 deletions packages/block-library/src/navigation-menu/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*
* @return string Returns the post content with the legacy widget added.
*/
function render_block_navigation_menu( $attributes, $content ) {
return "Content";
function render_block_navigation_menu( $attributes, $content, $block ) {
return sprintf( 'Navigation menu with %d items', count( $block['innerBlocks'] ) );
}

/**
Expand All @@ -31,13 +31,6 @@ function register_block_core_navigation_menu() {
'type' => 'boolean',
'default' => 'false',
),
'hierarchy' => array(
'type' => 'array',
'default' => array(),
'items' => array(
'type' => 'object',
),
),
),
'render_callback' => 'render_block_navigation_menu',
)
Expand Down

0 comments on commit 5b2cf72

Please sign in to comment.