Skip to content

Commit

Permalink
Made changes and removed some of the new schema as not required.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmurphy committed Jan 20, 2025
1 parent 0503245 commit 78c1edc
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 191 deletions.
97 changes: 42 additions & 55 deletions .changeset/khaki-forks-prove.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
---
"@wpengine/wp-graphql-content-blocks": minor
"@wpengine/wp-graphql-content-blocks": patch
---

Added CoreListItem block and new fields to query CoreListItem for the CoreList block.

Notable changes:

Added the field items which retrieves the CoreListItem child blocks for a CoreList.
For the CoreListItem block we added children and value so that we can query values and child blocks if they are core/list-iem blcoks

Example query
Added CoreListItem block for WPGraphQL Content Blocks.

```gql
query postQuery($id: ID!) {
post(id: $id, idType: DATABASE_ID, asPreview: false) {
title
editorBlocks(flat: false) {
name
... on CoreList {
ordered
items {
value
children {
value
children {
value
}
}
}
}
}
}
}
query postQuery($id: ID!) {
post(id: $id, idType: DATABASE_ID, asPreview: false) {
title
editorBlocks(flat: false) {
name
... on CoreList {
type
name
renderedHtml
innerBlocks {
... on CoreListItem {
type
name

renderedHtml
}
}
}
}
}
}
```

This returns an array of items and child items for that block e.g.

```json
{
"data": {
Expand All @@ -44,41 +36,36 @@ This returns an array of items and child items for that block e.g.
"editorBlocks": [
{
"name": "core/list",
"ordered": true,
"items": [
"type": "CoreList",
"renderedHtml": "\n<ol class=\"wp-block-list\">\n<li>List item 1</li>\n\n\n\n<li>List item 2\n<ul class=\"wp-block-list is-style-default\">\n<li>Child list item 1\n<ul class=\"wp-block-list\">\n<li>Third level list item</li>\n</ul>\n</li>\n\n\n\n<li>Child list item 2</li>\n</ul>\n</li>\n\n\n\n<li>List item 3</li>\n\n\n\n<li>List item 4</li>\n</ol>\n",
"innerBlocks": [
{
"value": "<li>List item 1</li>",
"children": []
"type": "CoreListItem",
"name": "core/list-item",
"renderedHtml": "\n<li>List item 1</li>\n"
},
{
"value": "<li>List item 2</li>",
"children": [
{
"value": "<li>Child list item 1</li>",
"children": [
{
"value": "<li>Third level list item</li>"
}
]
},
{
"value": "<li>Child list item 2</li>",
"children": []
}
]
"type": "CoreListItem",
"name": "core/list-item",
"renderedHtml": "\n<li>List item 2\n<ul class=\"wp-block-list is-style-default\">\n<li>Child list item 1\n<ul class=\"wp-block-list\">\n<li>Third level list item</li>\n</ul>\n</li>\n\n\n\n<li>Child list item 2</li>\n</ul>\n</li>\n"
},
{
"value": "<li>List item 3</li>",
"children": []
"type": "CoreListItem",
"name": "core/list-item",
"renderedHtml": "\n<li>List item 3</li>\n"
},
{
"value": "<li>List item 4</li>",
"children": []
"type": "CoreListItem",
"name": "core/list-item",
"renderedHtml": "\n<li>List item 4</li>\n"
}
]
},
{
"name": "core/paragraph"
}
]
}
},
}
}
```
68 changes: 0 additions & 68 deletions includes/Blocks/CoreList.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,72 +27,4 @@ class CoreList extends Block {
'attribute' => 'class',
],
];

/**
* Block constructor.
*
* @param \WP_Block_Type $block The Block Type.
* @param \WPGraphQL\ContentBlocks\Registry\Registry $block_registry The instance of the WPGraphQL block registry.
*/
public function __construct( WP_Block_Type $block, Registry $block_registry ) {
parent::__construct( $block, $block_registry );

register_graphql_field(
$this->type_name,
'ordered',
[
'type' => 'Boolean',
'description' => sprintf(
__( 'Whether the list is ordered or unordered', 'wp-graphql-content-blocks' ),
$this->type_name
),
'resolve' => static function ( $block ) {
return $block['attrs']['ordered'] ?? false;
},
]
);

register_graphql_field(
$this->type_name,
'items',
[
'type' => [ 'list_of' => 'CoreListItem' ],
'description' => sprintf(
__( 'Whether list items', 'wp-graphql-content-blocks' ),
$this->type_name
),
'resolve' => static function ( $block ) {
return self::resolveInnerBlocks( $block['innerBlocks'] );
},
]
);
}

/**
* @param array $inner_blocks An array of inner blocks.
*
* @return array An array of parsed inner blocks.
*/
public static function resolveInnerBlocks( array $inner_blocks ): array {

$items = [];

foreach ( $inner_blocks as $block ) {
switch ( $block['blockName'] ) {
case 'core/list-item':
$items[] = [
'value' => trim( $block['innerHTML'], "\n" ),
'children' => self::resolveInnerBlocks( $block['innerBlocks'] ),
];
break;

case 'core/list':
$nested_items = self::resolveInnerBlocks( $block['innerBlocks'] );
$items = array_merge( $items, $nested_items );
break;
}
}

return $items;
}
}
34 changes: 0 additions & 34 deletions includes/Blocks/CoreListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,4 @@ class CoreListItem extends Block {
'attribute' => 'class',
],
];

/**
* Block constructor.
*
* @param \WP_Block_Type $block The Block Type.
* @param \WPGraphQL\ContentBlocks\Registry\Registry $block_registry The instance of the WPGraphQL block registry.
*/
public function __construct( WP_Block_Type $block, Registry $block_registry ) {
parent::__construct( $block, $block_registry );

register_graphql_field(
$this->type_name,
'value',
[
'type' => 'String',
'description' => sprintf(
__( 'The content of the list item', 'wp-graphql-content-blocks' ),
$this->type_name
),
]
);

register_graphql_field(
$this->type_name,
'children',
[
'type' => [ 'list_of' => 'CoreListItem' ],
'description' => sprintf(
__( 'The content of the list item', 'wp-graphql-content-blocks' ),
$this->type_name
),
]
);
}
}
80 changes: 46 additions & 34 deletions tests/unit/CoreListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,14 +754,15 @@ public function test_retrieve_core_list_item_values(): void {
editorBlocks(flat: false) {
name
... on CoreList {
ordered
items {
value
children {
value
children {
value
}
type
name
renderedHtml
innerBlocks {
... on CoreListItem {
type
name
renderedHtml
}
}
}
Expand All @@ -783,33 +784,44 @@ public function test_retrieve_core_list_item_values(): void {
$editorBlocks = $actual['data']['post']['editorBlocks'];
$this->assertEquals( 1, count($editorBlocks));

$this->assertArrayHasKey('items', $editorBlocks[0]);
$this->assertEquals( 4, count($editorBlocks[0]['items']));
$this->assertEquals( true, $editorBlocks[0]['ordered']);

$items = $editorBlocks[0]['items'];
$itemOne = $items[0];
$itemTwo = $items[1];
$itemThree = $items[2];
$itemFour = $items[3];

$this->assertEquals( '<li>List item 1</li>', $itemOne['value']);
$this->assertEmpty( $itemOne['children'] );

$this->assertEquals( '<li>List item 2</li>', $itemTwo['value']);
$this->assertEquals( 2, count($itemTwo['children']));

$this->assertEquals( '<li>List item 3</li>',$itemThree['value']);
$this->assertEmpty( $itemThree['children'] );

$this->assertEquals( '<li>List item 4</li>', $itemFour['value']);
$this->assertEmpty( $itemFour['children'] );
$editorBlock = $editorBlocks[0];
$this->assertEquals( 'core/list', $editorBlock['name'], 'The block name should be core/list' );
$this->assertEquals( 'CoreList', $editorBlock['type'], 'The block type should be CoreList' );
$this->assertArrayHasKey( 'renderedHtml', $editorBlock);
$this->assertArrayHasKey( 'innerBlocks', $editorBlock);

$innerBlocks = $editorBlock['innerBlocks'];
$this->assertEquals( 4, count($innerBlocks));
$firstBlock = $innerBlocks[0];
$secondBlock = $innerBlocks[0];

/**
* No child list items
*/
$this->assertEquals('CoreListItem', $firstBlock['type'], 'The block type should be CoreListItem');
$this->assertEquals('core/list-item', $firstBlock['name'], 'The block name should be core/list-item');
$this->assertEquals('<li>List item 1</li>', trim($firstBlock['renderedHtml']), 'The block should have valid HTML block');


/**
* Child list items
*/
$html = <<<HTML
<li>List item 2
<ul class="wp-block-list is-style-checkmark-list">
<li>Child list item 1
<ul class="wp-block-list">
<li>Third level list item</li>
</ul>
</li>
<li>Child list item 2</li>
</ul>
</li>
HTML;

$children = $itemTwo['children'];
$this->assertEquals( '<li>Child list item 1</li>', $children[0]['value']);
$this->assertEquals( '<li>Third level list item</li>', $children[0]['children'][0]['value']);
$html = trim(preg_replace('/\s+/', ' ', $html));
$renderedHtml = trim(preg_replace('/\s+/', ' ', $innerBlocks[1]['renderedHtml'] ));
$this->assertEquals($html, $renderedHtml, 'The block should have valid HTML block');

$this->assertEquals( '<li>Child list item 2</li>', $children[1]['value']);
$this->assertEmpty( $children[1]['children'] );
}
}

0 comments on commit 78c1edc

Please sign in to comment.