Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added CoreListItem Block and updated CoreList block #331

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions .changeset/khaki-forks-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
"@wpengine/wp-graphql-content-blocks": minor
---

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

```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
}
}
}
}
}
}
}
```

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

```json
{
"data": {
"post": {
"title": "Hello world!",
"editorBlocks": [
{
"name": "core/list",
"ordered": true,
"items": [
{
"value": "<li>List item 1</li>",
"children": []
},
{
"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": []
}
]
},
{
"value": "<li>List item 3</li>",
"children": []
},
{
"value": "<li>List item 4</li>",
"children": []
}
]
}
]
}
},
}
```
71 changes: 71 additions & 0 deletions includes/Blocks/CoreList.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace WPGraphQL\ContentBlocks\Blocks;

use WPGraphQL\ContentBlocks\Registry\Registry;
use WP_Block_Type;

/**
* Class CoreList
*/
Expand All @@ -24,4 +27,72 @@ 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;
}
}
64 changes: 64 additions & 0 deletions includes/Blocks/CoreListItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Core List Item Block
*
* @package WPGraphQL\ContentBlocks\Blocks
*/

namespace WPGraphQL\ContentBlocks\Blocks;

use WPGraphQL\ContentBlocks\Registry\Registry;
use WP_Block_Type;

/**
* Class CoreListItem
*/
class CoreListItem extends Block {
/**
* {@inheritDoc}
*
* @var array|null
*/
protected ?array $additional_block_attributes = [
'cssClassName' => [
'type' => 'string',
'selector' => 'li',
'source' => 'attribute',
'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
),
]
);
}
}
111 changes: 111 additions & 0 deletions tests/unit/CoreListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace WPGraphQL\ContentBlocks\Unit;

/**
* @group block
* @group core-list
*/
final class CoreListTest extends PluginTestCase {
/**
* The ID of the post created for the test.
Expand Down Expand Up @@ -701,4 +705,111 @@ className
$block['attributes'],
);
}


public function test_retrieve_core_list_item_values(): void {
$block_content = <<<HTML
<!-- wp:list {"ordered":true} -->
<ol class="wp-block-list"><!-- wp:list-item -->
<li>List item 1</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>List item 2<!-- wp:list {"className":"is-style-checkmark-list"} -->
<ul class="wp-block-list is-style-checkmark-list"><!-- wp:list-item -->
<li>Child list item 1<!-- wp:list -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li>Third level list item</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Child list item 2</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>List item 3</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>List item 4</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->
HTML;
// Set post content.
wp_update_post(
[
'ID' => $this->post_id,
'post_content' => $block_content,
]
);

$query = '
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
}
}
}
}
}
}
}
';

$variables = [
'id' => $this->post_id,
];

$actual = graphql( compact( 'query', 'variables' ) );

$this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' );
$this->assertArrayHasKey( 'data', $actual, 'The data key should be present' );
$this->assertArrayHasKey( 'post', $actual['data'], 'The post key should be present' );

$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'] );

$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']);

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