Skip to content

Commit

Permalink
feat: Add support for CoreNavigation blocks (#334)
Browse files Browse the repository at this point in the history
* feat: Add support for CoreNavigation blocks

* Chore: phpstan fix

* Chore: phpcs fix

* Refactor: move logic into ContentBlocksResolver

* chore: phpcs fix

* chore: wp coding standards fix

* Update thirty-ducks-check.md

* Update includes/Data/ContentBlocksResolver.php

Co-authored-by: Alex K. <33621842+whoami-pwd@users.noreply.github.com>

* chore: phpcs fix

* chore: update version to minor

* refactor: cast ref value to int

* chore: revert coercion check

---------

Co-authored-by: Alex K. <33621842+whoami-pwd@users.noreply.github.com>
  • Loading branch information
theodesp and whoami-pwd authored Jan 22, 2025
1 parent b133a1b commit b813352
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
63 changes: 63 additions & 0 deletions .changeset/thirty-ducks-check.md
Original file line number Diff line number Diff line change
@@ -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": [
{}
]
}
]
}
},
}
```
29 changes: 29 additions & 0 deletions includes/Data/ContentBlocksResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ) {
Expand Down Expand Up @@ -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<string,mixed> $block The block to populate.
*
* @return array<string,mixed> 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.
*
Expand Down

0 comments on commit b813352

Please sign in to comment.