From ef90d290eeccb6e3d93815376f52b343f25de578 Mon Sep 17 00:00:00 2001 From: David Levine Date: Tue, 17 Sep 2024 12:26:09 +0000 Subject: [PATCH] feat: expose `EditorBlock.type` field --- .changeset/ten-bulldogs-cover.md | 5 +++++ .../Type/InterfaceType/EditorBlockInterface.php | 12 +++++------- .../InterfaceType/PostTypeBlockInterface.php | 8 +------- includes/Utilities/WPGraphQLHelpers.php | 17 +++++++++++++++++ tests/unit/EditorBlockInterfaceTest.php | 1 + tests/unit/PostTypeBlockInterfaceTest.php | 1 + 6 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 .changeset/ten-bulldogs-cover.md diff --git a/.changeset/ten-bulldogs-cover.md b/.changeset/ten-bulldogs-cover.md new file mode 100644 index 00000000..8dc1d6a0 --- /dev/null +++ b/.changeset/ten-bulldogs-cover.md @@ -0,0 +1,5 @@ +--- +"@wpengine/wp-graphql-content-blocks": minor +--- + +feat: expose `EditorBlock.type` field diff --git a/includes/Type/InterfaceType/EditorBlockInterface.php b/includes/Type/InterfaceType/EditorBlockInterface.php index 1157fbd9..d12f86d9 100644 --- a/includes/Type/InterfaceType/EditorBlockInterface.php +++ b/includes/Type/InterfaceType/EditorBlockInterface.php @@ -138,15 +138,13 @@ public static function register_type(): void { return render_block( $block ); }, ], + 'type' => [ + 'type' => 'String', + 'description' => __( 'The (GraphQL) type of the block', 'wp-graphql-content-blocks' ), + ], ], 'resolveType' => static function ( $block ) { - if ( empty( $block['blockName'] ) ) { - $block['blockName'] = 'core/freeform'; - } - - $type_name = lcfirst( ucwords( $block['blockName'], '/' ) ); - - return WPGraphQLHelpers::format_type_name( $type_name ); + return WPGraphQLHelpers::get_type_name_for_block( $block['blockName'] ?? null ); }, ] ); diff --git a/includes/Type/InterfaceType/PostTypeBlockInterface.php b/includes/Type/InterfaceType/PostTypeBlockInterface.php index b5da76e7..f65e4d88 100644 --- a/includes/Type/InterfaceType/PostTypeBlockInterface.php +++ b/includes/Type/InterfaceType/PostTypeBlockInterface.php @@ -36,13 +36,7 @@ public static function register_type( string $post_type, array $block_names = [] ], ], 'resolveType' => static function ( $block ) { - if ( empty( $block['blockName'] ) ) { - $block['blockName'] = 'core/freeform'; - } - - $type_name = lcfirst( ucwords( $block['blockName'], '/' ) ); - - return WPGraphQLHelpers::format_type_name( $type_name ); + return WPGraphQLHelpers::get_type_name_for_block( $block['blockName'] ?? null ); }, ] ); diff --git a/includes/Utilities/WPGraphQLHelpers.php b/includes/Utilities/WPGraphQLHelpers.php index e4dbd247..9cac2635 100644 --- a/includes/Utilities/WPGraphQLHelpers.php +++ b/includes/Utilities/WPGraphQLHelpers.php @@ -32,4 +32,21 @@ public static function format_type_name( string $name ): string { return ! empty( $type_name ) ? Utils::format_type_name( $type_name ) : $type_name; } + + /** + * Gets the GraphQL type name for a provided block name. + * + * @internal This method will likely be removed in the future in favor of a block Model. + * + * @param ?string $block_name The name of the block. + */ + public static function get_type_name_for_block( ?string $block_name ): string { + if ( empty( $block_name ) ) { + $block_name = 'core/freeform'; + } + + $type_name = lcfirst( ucwords( $block_name, '/' ) ); + + return self::format_type_name( $type_name ); + } } diff --git a/tests/unit/EditorBlockInterfaceTest.php b/tests/unit/EditorBlockInterfaceTest.php index 938f6eb2..d7291bab 100644 --- a/tests/unit/EditorBlockInterfaceTest.php +++ b/tests/unit/EditorBlockInterfaceTest.php @@ -97,6 +97,7 @@ public function test_register_type() { 'clientId', 'parentClientId', 'renderedHtml', + 'type', ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $actual = array_map( diff --git a/tests/unit/PostTypeBlockInterfaceTest.php b/tests/unit/PostTypeBlockInterfaceTest.php index 400fdc4f..ced2a6ec 100644 --- a/tests/unit/PostTypeBlockInterfaceTest.php +++ b/tests/unit/PostTypeBlockInterfaceTest.php @@ -89,6 +89,7 @@ public function test_register_type() { 'clientId', 'parentClientId', 'renderedHtml', + 'type', ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $actual = array_map(