diff --git a/.changeset/thirty-ducks-check.md b/.changeset/thirty-ducks-check.md new file mode 100644 index 00000000..c73f64c2 --- /dev/null +++ b/.changeset/thirty-ducks-check.md @@ -0,0 +1,63 @@ +--- +"@wpengine/wp-graphql-content-blocks": minor +--- + +Adds support for resolving and returning navigation items within the CoreNavigation innerBlocks for WPGraphQL Content Blocks. + +```graphql +{ + posts { + nodes { + editorBlocks { + ... on CoreNavigation { + type + name + innerBlocks { + type + name + } + attributes { + ref + } + } + } + } + } +} +``` +```json +{ + "data": { + "posts": { + "nodes": [ + { + "editorBlocks": [ + { + "type": "CoreNavigation", + "name": "core/navigation", + "innerBlocks": [ + { + "type": "CorePageList", + "name": "core/page-list" + }, + { + "type": "CoreNavigationLink", + "name": "core/navigation-link" + } + ], + "attributes": { + "ref": 31 + } + }, + ] + }, + { + "editorBlocks": [ + {} + ] + } + ] + } + }, +} +``` diff --git a/includes/Data/ContentBlocksResolver.php b/includes/Data/ContentBlocksResolver.php index e4c589ad..0188f6c8 100644 --- a/includes/Data/ContentBlocksResolver.php +++ b/includes/Data/ContentBlocksResolver.php @@ -152,6 +152,7 @@ private static function handle_do_block( array $block ): ?array { $block = self::populate_post_content_inner_blocks( $block ); $block = self::populate_reusable_blocks( $block ); $block = self::populate_pattern_inner_blocks( $block ); + $block = self::populate_navigation_blocks( $block ); // Prepare innerBlocks. if ( ! empty( $block['innerBlocks'] ) ) { @@ -242,6 +243,34 @@ private static function populate_post_content_inner_blocks( array $block ): arra return $block; } + /** + * Populates the innerBlocks of this block with navigation item blocks from the referenced navigation post. + * + * @param array $block The block to populate. + * + * @return array The populated block. + */ + private static function populate_navigation_blocks( array $block ): array { + if ( 'core/navigation' !== $block['blockName'] || ! isset( $block['attrs']['ref'] ) ) { + return $block; + } + + $ref = absint( $block['attrs']['ref'] ); + $navigation_post = get_post( $ref ); + + if ( ! $navigation_post || 'publish' !== $navigation_post->post_status ) { + return $block; + } + + $parsed_blocks = ! empty( $navigation_post->post_content ) ? parse_blocks( $navigation_post->post_content ) : null; + + if ( empty( $parsed_blocks ) ) { + return $block; + } + $block['innerBlocks'] = $parsed_blocks; + return $block; + } + /** * Populates reusable blocks with the blocks from the reusable ref ID. *