Skip to content

Commit ffd5fb1

Browse files
authored
chore: make additional configs 2.3.0+ compatible (#387)
1 parent 5118f38 commit ffd5fb1

File tree

5 files changed

+117
-131
lines changed

5 files changed

+117
-131
lines changed

includes/Blocks/Block.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,32 +120,35 @@ private function register_block_attributes_as_fields(): void {
120120

121121
// For each attribute, register a new object type and attach it to the block type as a field
122122
$block_attribute_type_name = $this->type_name . 'Attributes';
123+
124+
// Stash the type name so we can use it statically.
125+
$type_name = $this->type_name;
123126
register_graphql_object_type(
124127
$block_attribute_type_name,
125128
// @TODO - Remove when WPGraphQL min version is 2.3.0
126129
WPGraphQLRegisterConfig::resolve_graphql_config(
127130
[
128-
'description' => fn () => sprintf(
131+
'description' => static fn () => sprintf(
129132
// translators: %s is the block type name.
130133
__( 'Attributes of the %s Block Type', 'wp-graphql-content-blocks' ),
131-
$this->type_name
134+
$type_name
132135
),
133136
'interfaces' => $this->get_block_attributes_interfaces(),
134137
'fields' => $block_attribute_fields,
135138
]
136139
)
137140
);
138141
register_graphql_field(
139-
$this->type_name,
142+
$type_name,
140143
'attributes',
141144
// @TODO - Remove when WPGraphQL min version is 2.3.0
142145
WPGraphQLRegisterConfig::resolve_graphql_config(
143146
[
144147
'type' => $block_attribute_type_name,
145-
'description' => fn () => sprintf(
148+
'description' => static fn () => sprintf(
146149
// translators: %s is the block type name.
147150
__( 'Attributes of the %s Block Type', 'wp-graphql-content-blocks' ),
148-
$this->type_name
151+
$type_name
149152
),
150153
'resolve' => static function ( $block ) {
151154
return $block;
@@ -269,7 +272,7 @@ private function get_block_attribute_fields( ?array $block_attributes, string $p
269272
// Create the field config.
270273
$fields[ Utils::format_field_name( $attribute_name ) ] = [
271274
'type' => $graphql_type,
272-
'description' => sprintf(
275+
'description' => static fn () => sprintf(
273276
// translators: %1$s is the attribute name, %2$s is the block name.
274277
__( 'The "%1$s" field on the "%2$s" block or block attributes', 'wp-graphql-content-blocks' ),
275278
$attribute_name,
@@ -355,7 +358,7 @@ private function create_attributes_fields( $attributes, $prefix ): array {
355358

356359
$fields[ Utils::format_field_name( $name ) ] = [
357360
'type' => $type,
358-
'description' => sprintf(
361+
'description' => static fn () => sprintf(
359362
// translators: %1$s is the attribute name, %2$s is the block attributes field.
360363
__( 'The "%1$s" field on the "%2$s" block attribute field', 'wp-graphql-content-blocks' ),
361364
$name,
@@ -412,23 +415,23 @@ private function normalize_attribute_value( $value, $type ) {
412415
private function register_type(): void {
413416
register_graphql_object_type(
414417
$this->type_name,
415-
[
416-
'description' => __( 'A block used for editing the site', 'wp-graphql-content-blocks' ),
417-
'interfaces' => $this->get_block_interfaces(),
418-
'eagerlyLoadType' => true,
419-
'fields' => [
420-
// @TODO - Remove when WPGraphQL min version is 2.3.0
421-
'name' => WPGraphQLRegisterConfig::resolve_graphql_config(
422-
[
418+
// @TODO - Remove when WPGraphQL min version is 2.3.0
419+
WPGraphQLRegisterConfig::resolve_graphql_config(
420+
[
421+
'description' => static fn () => __( 'A block used for editing the site', 'wp-graphql-content-blocks' ),
422+
'interfaces' => $this->get_block_interfaces(),
423+
'eagerlyLoadType' => true,
424+
'fields' => [
425+
'name' => [
423426
'type' => 'String',
424427
'description' => static fn () => __( 'The name of the block', 'wp-graphql-content-blocks' ),
425428
'resolve' => static function ( $block ) {
426429
return isset( $block['blockName'] ) ? (string) $block['blockName'] : null;
427430
},
428-
]
429-
),
430-
],
431-
]
431+
],
432+
],
433+
]
434+
)
432435
);
433436
}
434437

includes/Blocks/CoreImage.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,20 @@ class CoreImage extends Block {
4545
public function __construct( WP_Block_Type $block, Registry $block_registry ) {
4646
parent::__construct( $block, $block_registry );
4747

48+
// Stash type name for static methods.
49+
$type_name = $this->type_name;
50+
4851
register_graphql_field(
49-
$this->type_name,
52+
$type_name,
5053
'mediaDetails',
5154
// @TODO - Remove when WPGraphQL min version is 2.3.0
5255
WPGraphQLRegisterConfig::resolve_graphql_config(
5356
[
5457
'type' => 'MediaDetails',
55-
'description' => fn () => sprintf(
58+
'description' => static fn () => sprintf(
5659
// translators: %s is the block type name.
5760
__( 'Media Details of the %s Block Type', 'wp-graphql-content-blocks' ),
58-
$this->type_name
61+
$type_name
5962
),
6063
'resolve' => static function ( $block ) {
6164
$attrs = $block['attrs'];

includes/Field/BlockSupports/Anchor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function register(): void {
2828
'fields' => [
2929
'anchor' => [
3030
'type' => 'string',
31-
'description' => __( 'The anchor field for the block.', 'wp-graphql-content-blocks' ),
31+
'description' => static fn () => __( 'The anchor field for the block.', 'wp-graphql-content-blocks' ),
3232
'resolve' => static function ( $block ) {
3333
$rendered_block = wp_unslash( WPGraphQLHelpers::get_rendered_block( $block ) );
3434
if ( empty( $rendered_block ) ) {

includes/Type/InterfaceType/EditorBlockInterface.php

Lines changed: 44 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public static function register_type(): void {
5555
],
5656
'args' => [
5757
'flat' => [
58-
'description' => __( 'Returns the list of blocks as a flat list if true', 'wp-graphql-content-blocks' ),
58+
'description' => static fn () => __( 'Returns the list of blocks as a flat list if true', 'wp-graphql-content-blocks' ),
5959
'type' => 'Boolean',
6060
],
6161
],
62-
'description' => __( 'List of editor blocks', 'wp-graphql-content-blocks' ),
62+
'description' => static fn () => __( 'List of editor blocks', 'wp-graphql-content-blocks' ),
6363
'resolve' => static function ( $node, $args ) {
6464
return ContentBlocksResolver::resolve_content_blocks( $node, $args );
6565
},
@@ -72,118 +72,91 @@ public static function register_type(): void {
7272
// Register the EditorBlock Interface
7373
register_graphql_interface_type(
7474
'EditorBlock',
75-
[
76-
'eagerlyLoadType' => true,
77-
'description' => static fn () => __( 'Blocks that can be edited to create content and layouts', 'wp-graphql-content-blocks' ),
78-
'fields' => [
79-
// @TODO - Remove when WPGraphQL min version is 2.3.0
80-
'clientId' => WPGraphQLRegisterConfig::resolve_graphql_config(
81-
[
75+
// @TODO - Remove when WPGraphQL min version is 2.3.0
76+
WPGraphQLRegisterConfig::resolve_graphql_config(
77+
[
78+
'eagerlyLoadType' => true,
79+
'description' => static fn () => __( 'Blocks that can be edited to create content and layouts', 'wp-graphql-content-blocks' ),
80+
'fields' => [
81+
'clientId' => [
8282
'type' => 'String',
83-
'description' => __( 'The id of the Block', 'wp-graphql-content-blocks' ),
83+
'description' => static fn () => __( 'The id of the Block', 'wp-graphql-content-blocks' ),
8484
'resolve' => static function ( $block ) {
8585
return isset( $block['clientId'] ) ? $block['clientId'] : uniqid();
8686
},
87-
]
88-
),
89-
// @TODO - Remove when WPGraphQL min version is 2.3.0
90-
'parentClientId' => WPGraphQLRegisterConfig::resolve_graphql_config(
91-
[
87+
],
88+
'parentClientId' => [
9289
'type' => 'String',
93-
'description' => __( 'The parent id of the Block', 'wp-graphql-content-blocks' ),
90+
'description' => static fn () => __( 'The parent id of the Block', 'wp-graphql-content-blocks' ),
9491
'resolve' => static function ( $block ) {
9592
return isset( $block['parentClientId'] ) ? $block['parentClientId'] : null;
9693
},
97-
]
98-
),
99-
// @TODO - Remove when WPGraphQL min version is 2.3.0
100-
'name' => WPGraphQLRegisterConfig::resolve_graphql_config(
101-
[
94+
],
95+
'name' => [
10296
'type' => 'String',
103-
'description' => __( 'The name of the Block', 'wp-graphql-content-blocks' ),
104-
]
105-
),
106-
// @TODO - Remove when WPGraphQL min version is 2.3.0
107-
'blockEditorCategoryName' => WPGraphQLRegisterConfig::resolve_graphql_config(
108-
[
97+
'description' => static fn () => __( 'The name of the Block', 'wp-graphql-content-blocks' ),
98+
],
99+
'blockEditorCategoryName' => [
109100
'type' => 'String',
110-
'description' => __( 'The name of the category the Block belongs to', 'wp-graphql-content-blocks' ),
101+
'description' => static fn () => __( 'The name of the category the Block belongs to', 'wp-graphql-content-blocks' ),
111102
'resolve' => static function ( $block ) {
112103
return isset( self::get_block( $block )->category ) ? self::get_block( $block )->category : null;
113104
},
114-
]
115-
),
116-
// @TODO - Remove when WPGraphQL min version is 2.3.0
117-
'isDynamic' => WPGraphQLRegisterConfig::resolve_graphql_config(
118-
[
105+
],
106+
'isDynamic' => [
119107
'type' => [ 'non_null' => 'Boolean' ],
120-
'description' => __( 'Whether the block is Dynamic (server rendered)', 'wp-graphql-content-blocks' ),
108+
'description' => static fn () => __( 'Whether the block is Dynamic (server rendered)', 'wp-graphql-content-blocks' ),
121109
'resolve' => static function ( $block ) {
122110
return isset( self::get_block( $block )->render_callback );
123111
},
124-
]
125-
),
126-
// @TODO - Remove when WPGraphQL min version is 2.3.0
127-
'apiVersion' => WPGraphQLRegisterConfig::resolve_graphql_config(
128-
[
112+
],
113+
'apiVersion' => [
129114
'type' => 'Integer',
130-
'description' => __( 'The API version of the Gutenberg Block', 'wp-graphql-content-blocks' ),
115+
'description' => static fn () => __( 'The API version of the Gutenberg Block', 'wp-graphql-content-blocks' ),
131116
'resolve' => static function ( $block ) {
132117
return isset( self::get_block( $block )->api_version ) && absint( self::get_block( $block )->api_version ) ? absint( self::get_block( $block )->api_version ) : 2;
133118
},
134-
]
135-
),
136-
// @TODO - Remove when WPGraphQL min version is 2.3.0
137-
'innerBlocks' => WPGraphQLRegisterConfig::resolve_graphql_config(
138-
[
119+
],
120+
'innerBlocks' => [
139121
'type' => [
140122
'list_of' => 'EditorBlock',
141123
],
142-
'description' => __( 'The inner blocks of the Block', 'wp-graphql-content-blocks' ),
124+
'description' => static fn () => __( 'The inner blocks of the Block', 'wp-graphql-content-blocks' ),
143125
'resolve' => static function ( $block ) {
144126
return isset( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ? $block['innerBlocks'] : [];
145127
},
146-
]
147-
),
148-
// @TODO - Remove when WPGraphQL min version is 2.3.0
149-
'cssClassNames' => WPGraphQLRegisterConfig::resolve_graphql_config(
150-
[
128+
],
129+
'cssClassNames' => [
151130
'type' => [ 'list_of' => 'String' ],
152-
'description' => __( 'CSS Classnames to apply to the block', 'wp-graphql-content-blocks' ),
131+
'description' => static fn () => __( 'CSS Classnames to apply to the block', 'wp-graphql-content-blocks' ),
153132
'resolve' => static function ( $block ) {
154133
if ( isset( $block['attrs']['className'] ) ) {
155134
return explode( ' ', $block['attrs']['className'] );
156135
}
157136

158137
return null;
159138
},
160-
]
161-
),
162-
// @TODO - Remove when WPGraphQL min version is 2.3.0
163-
'renderedHtml' => WPGraphQLRegisterConfig::resolve_graphql_config(
164-
[
139+
],
140+
'renderedHtml' => [
165141
'type' => 'String',
166-
'description' => __( 'The rendered HTML for the block', 'wp-graphql-content-blocks' ),
142+
'description' => static fn () => __( 'The rendered HTML for the block', 'wp-graphql-content-blocks' ),
167143
'resolve' => static function ( $block ) {
168144
return WPGraphQLHelpers::get_rendered_block( $block );
169145
},
170-
]
171-
),
172-
// @TODO - Remove when WPGraphQL min version is 2.3.0
173-
'type' => WPGraphQLRegisterConfig::resolve_graphql_config(
174-
[
146+
],
147+
'type' => [
175148
'type' => 'String',
176-
'description' => __( 'The (GraphQL) type of the block', 'wp-graphql-content-blocks' ),
149+
'description' => static fn () => __( 'The (GraphQL) type of the block', 'wp-graphql-content-blocks' ),
177150
'resolve' => static function ( $block ) {
178151
return WPGraphQLHelpers::get_type_name_for_block( $block['blockName'] ?? null );
179152
},
180-
]
181-
),
182-
],
183-
'resolveType' => static function ( $block ) {
184-
return WPGraphQLHelpers::get_type_name_for_block( $block['blockName'] ?? null );
185-
},
186-
]
153+
],
154+
],
155+
'resolveType' => static function ( $block ) {
156+
return WPGraphQLHelpers::get_type_name_for_block( $block['blockName'] ?? null );
157+
},
158+
]
159+
)
187160
);
188161
}
189162
}

0 commit comments

Comments
 (0)