From 5b2cf729ac9ae14441e88f1157c7b8856eab9557 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Mon, 12 Aug 2019 15:10:21 +1000 Subject: [PATCH] Navigation: Remove hierarchy attribute, pass $block to render_callback --- lib/compat.php | 44 +++++++++++++++++++ .../src/navigation-menu/block.json | 14 ------ .../block-library/src/navigation-menu/edit.js | 42 +----------------- .../src/navigation-menu/index.php | 11 +---- 4 files changed, 47 insertions(+), 64 deletions(-) delete mode 100644 packages/block-library/src/navigation-menu/block.json diff --git a/lib/compat.php b/lib/compat.php index 0ecae1ca55ad5..a98013d04c3d8 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -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 ); diff --git a/packages/block-library/src/navigation-menu/block.json b/packages/block-library/src/navigation-menu/block.json deleted file mode 100644 index b48c74c7004b2..0000000000000 --- a/packages/block-library/src/navigation-menu/block.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "core/navigation-menu", - "category": "layout", - "attributes": { - "automaticallyAdd": { - "type": "boolean", - "default": false - }, - "hierarchy": { - "type": "array", - "default": [] - } - } -} diff --git a/packages/block-library/src/navigation-menu/edit.js b/packages/block-library/src/navigation-menu/edit.js index 619dfb4a58212..cfda7b514deb0 100644 --- a/packages/block-library/src/navigation-menu/edit.js +++ b/packages/block-library/src/navigation-menu/edit.js @@ -12,8 +12,6 @@ import { CheckboxControl, PanelBody, } from '@wordpress/components'; -import { withSelect } from '@wordpress/data'; -import { compose } from '@wordpress/compose'; /** * Internal dependencies @@ -22,14 +20,8 @@ import { __ } from '@wordpress/i18n'; function NavigationMenu( { attributes, - clientId, setAttributes, - getBlockInnerHierarchy, } ) { - const onChange = () => { - setAttributes( { hierarchy: getBlockInnerHierarchy( clientId ) } ); - }; - return ( @@ -44,11 +36,6 @@ function NavigationMenu( { label={ __( 'Automatically add new pages' ) } help={ __( 'Automatically add new top level pages to this menu.' ) } /> -
@@ -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; diff --git a/packages/block-library/src/navigation-menu/index.php b/packages/block-library/src/navigation-menu/index.php index c548d9272b4ad..ee35a4fae8ba6 100644 --- a/packages/block-library/src/navigation-menu/index.php +++ b/packages/block-library/src/navigation-menu/index.php @@ -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'] ) ); } /** @@ -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', )