-
Notifications
You must be signed in to change notification settings - Fork 17
Replace core/block with core/synced-pattern (Changes GraphQL response structure) #347
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| --- | ||
| "@wpengine/wp-graphql-content-blocks": major | ||
| --- | ||
|
|
||
| Replaced core/block with core/synced-pattern for reusable blocks, aligning with WP 6.3's synced patterns. | ||
|
|
||
| **🚨Breaking change🚨** This update does not break functionality for WP < 6.3, but it alters the GraphQL response structure. | ||
|
|
||
| Query: | ||
| ``` | ||
| { | ||
| posts { | ||
| nodes { | ||
| editorBlocks { | ||
| name | ||
| clientId | ||
| parentClientId | ||
| ... on CoreSyncedPattern { | ||
| attributes { | ||
| slug | ||
| } | ||
| name | ||
| innerBlocks { | ||
| name | ||
| clientId | ||
| parentClientId | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
| Response: | ||
| ``` | ||
| { | ||
| "data": { | ||
| "posts": { | ||
| "nodes": [ | ||
| { | ||
| "editorBlocks": [ | ||
| { | ||
| "name": "core/synced-pattern", | ||
| "clientId": "67b317909b801", | ||
| "parentClientId": null, | ||
| "attributes": { | ||
| "slug": "my-synced-pattern" | ||
| }, | ||
| "innerBlocks": [ | ||
| { | ||
| "name": "core/group", | ||
| "clientId": "67b317909b89a", | ||
| "parentClientId": null | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "name": "core/group", | ||
| "clientId": "67b317909b89a", | ||
| "parentClientId": "67b317909b801" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,7 @@ public function init(): void { | |
| $this->register_interface_types(); | ||
| $this->register_scalar_types(); | ||
| $this->register_support_block_types(); | ||
| $this->register_custom_block_types(); | ||
| $this->register_block_types(); | ||
| } | ||
|
|
||
|
|
@@ -257,6 +258,41 @@ static function ( $post_type ) { | |
| }//end foreach | ||
| } | ||
|
|
||
| /** | ||
| * Registers custom block types. | ||
| * | ||
| * The 'core/synced-pattern' block is a reusable synced pattern block and | ||
| * is not meant to appear in the block editor selection but is used | ||
| * internally for resolving reusable content. | ||
| */ | ||
| protected function register_custom_block_types(): void { | ||
| $registry = \WP_Block_Type_Registry::get_instance(); | ||
| $block_name = 'core/synced-pattern'; | ||
|
|
||
| if ( ! $registry->is_registered( $block_name ) ) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems very fragile and maybe also unnecessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @justlevine How can we rename an existing block in GraphQL? I'm not familiar with this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @justlevine how should we handle the mapping in GraphQL to reflect core/synced-pattern without re-registering it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pseudocode: add_filter( 'graphql_type_name', static function( $type_name ) {
// Bail if not our Block. (str_starts_with() would probably be too loose).
if ( 'CoreBlock' !== $type_name && 'CoreBlockAttributes' !== $type_name ) { return $type_name; }
// or to whatever you're calling it.
return str_replace( 'CoreBlock', 'CoreSyncedPatternBlock', $type_name );
} );Or you could probably use |
||
| $registry->register( | ||
| $block_name, | ||
| [ | ||
| 'name' => $block_name, | ||
| 'title' => __( 'Synced Pattern', 'wp-graphql-content-blocks' ), | ||
| 'icon' => null, | ||
| 'category' => 'reusable', | ||
| 'attributes' => [ | ||
| 'ref' => [ | ||
| 'type' => 'number', | ||
| ], | ||
| 'slug' => [ | ||
| 'type' => 'string', | ||
| ], | ||
| ], | ||
| 'render_callback' => static function ( $attributes, $content ) { | ||
| return $content; | ||
| }, | ||
| ] | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Register Scalar types to the GraphQL Schema | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,10 +101,16 @@ public function tearDown(): void { | |
| public function test_resolve_content_blocks_resolves_reusable_blocks() { | ||
| $post_model = new Post( get_post( $this->reusable_post_id ) ); | ||
| $actual = $this->instance->resolve_content_blocks( $post_model, [ 'flat' => true ] ); | ||
|
|
||
| // There should return only the non-empty blocks | ||
| $this->assertEquals( 3, count( $actual ) ); | ||
| $this->assertEquals( 'core/columns', $actual[0]['blockName'] ); | ||
|
|
||
| $this->assertNotEmpty( $actual ); | ||
| $this->assertEquals( 'core/synced-pattern', $actual[0]['blockName'] ); | ||
|
|
||
| $this->assertArrayHasKey( 'attrs', $actual[0] ); | ||
| $this->assertArrayHasKey( 'ref', $actual[0]['attrs'] ); | ||
| $this->assertArrayHasKey( 'slug', $actual[0]['attrs'] ); | ||
|
|
||
| $this->assertEquals( 'core/columns', $actual[1]['blockName'] ); | ||
| $this->assertCount( 4, $actual ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now count the 'synced pattern` block as well. |
||
| } | ||
|
|
||
| public function test_resolve_content_blocks_filters_empty_blocks() { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.