|
16 | 16 | * Class CorePostTerms. |
17 | 17 | */ |
18 | 18 | class CorePostTerms extends Block { |
19 | | - /** |
20 | | - * String fields for the block. |
21 | | - */ |
22 | | - public const STRING_FIELDS = [ 'prefix', 'suffix', 'term' ]; |
23 | | - |
24 | 19 | /** |
25 | 20 | * {@inheritDoc} |
26 | 21 | */ |
27 | 22 | public function __construct( WP_Block_Type $block, Registry $block_registry ) { |
28 | 23 | parent::__construct( $block, $block_registry ); |
29 | 24 |
|
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 | + ); |
33 | 33 |
|
34 | 34 | $this->register_list_of_terms_field(); |
35 | 35 | } |
36 | 36 |
|
37 | 37 | /** |
38 | | - * Registers a string field for the block. |
| 38 | + * Gets a string field for the block. |
39 | 39 | * |
40 | 40 | * @param string $name The name of the field. |
41 | | - * |
42 | | - * @return void |
43 | | - * @throws \Exception |
44 | 41 | */ |
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 | + ]; |
62 | 53 | } |
63 | 54 |
|
64 | 55 | /** |
|
0 commit comments