Skip to content

Commit 56c51c9

Browse files
committed
Replaces registering string fields with register_graphql_fields and eliminates abstraction and class constant.
1 parent 0ba35f4 commit 56c51c9

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

includes/Blocks/CorePostTerms.php

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,40 @@
1616
* Class CorePostTerms.
1717
*/
1818
class CorePostTerms extends Block {
19-
/**
20-
* String fields for the block.
21-
*/
22-
public const STRING_FIELDS = [ 'prefix', 'suffix', 'term' ];
23-
2419
/**
2520
* {@inheritDoc}
2621
*/
2722
public function __construct( WP_Block_Type $block, Registry $block_registry ) {
2823
parent::__construct( $block, $block_registry );
2924

30-
foreach ( self::STRING_FIELDS as $field ) {
31-
$this->register_string_field( $field );
32-
}
25+
register_graphql_fields(
26+
$this->type_name,
27+
[
28+
'prefix' => $this->get_string_field_config( 'prefix' ),
29+
'suffix' => $this->get_string_field_config( 'suffix' ),
30+
'term' => $this->get_string_field_config( 'term' ),
31+
]
32+
);
3333

3434
$this->register_list_of_terms_field();
3535
}
3636

3737
/**
38-
* Registers a string field for the block.
38+
* Gets a string field for the block.
3939
*
4040
* @param string $name The name of the field.
41-
*
42-
* @return void
43-
* @throws \Exception
4441
*/
45-
protected function register_string_field( $name ) {
46-
register_graphql_field(
47-
$this->type_name,
48-
$name,
49-
[
50-
'type' => 'String',
51-
'description' => sprintf(
52-
// translators: %1$s is the field name, %2$s is the block type name.
53-
__( '%1$s of the "%2$s" Block Type', 'wp-graphql-content-blocks' ),
54-
ucfirst( $name ),
55-
$this->type_name
56-
),
57-
'resolve' => static function ( $block ) use ( $name ) {
58-
return isset( $block['attrs'][ $name ] ) ? (string) $block['attrs'][ $name ] : null;
59-
},
60-
]
61-
);
42+
private function get_string_field_config( string $name ): array {
43+
return [
44+
'type' => 'String',
45+
'description' => sprintf(
46+
// translators: %1$s is the field name, %2$s is the block type name.
47+
__( '%1$s of the "%2$s" Block Type', 'wp-graphql-content-blocks' ),
48+
ucfirst( $name ),
49+
$this->type_name
50+
),
51+
'resolve' => static fn ( $block ) => isset( $block['attrs'][ $name ] ) ? (string) $block['attrs'][ $name ] : null,
52+
];
6253
}
6354

6455
/**

0 commit comments

Comments
 (0)