diff --git a/includes/Blocks/CorePostTerms.php b/includes/Blocks/CorePostTerms.php index 29e6bdf7..6cbdd1dd 100644 --- a/includes/Blocks/CorePostTerms.php +++ b/includes/Blocks/CorePostTerms.php @@ -16,49 +16,40 @@ * Class CorePostTerms. */ class CorePostTerms extends Block { - /** - * String fields for the block. - */ - public const STRING_FIELDS = [ 'prefix', 'suffix', 'term' ]; - /** * {@inheritDoc} */ public function __construct( WP_Block_Type $block, Registry $block_registry ) { parent::__construct( $block, $block_registry ); - foreach ( self::STRING_FIELDS as $field ) { - $this->register_string_field( $field ); - } + register_graphql_fields( + $this->type_name, + [ + 'prefix' => $this->get_string_field_config( 'prefix' ), + 'suffix' => $this->get_string_field_config( 'suffix' ), + 'term' => $this->get_string_field_config( 'term' ), + ] + ); $this->register_list_of_terms_field(); } /** - * Registers a string field for the block. + * Gets a string field for the block. * * @param string $name The name of the field. - * - * @return void - * @throws \Exception */ - protected function register_string_field( $name ) { - register_graphql_field( - $this->type_name, - $name, - [ - 'type' => 'String', - 'description' => sprintf( - // translators: %1$s is the field name, %2$s is the block type name. - __( '%1$s of the "%2$s" Block Type', 'wp-graphql-content-blocks' ), - ucfirst( $name ), - $this->type_name - ), - 'resolve' => static function ( $block ) use ( $name ) { - return isset( $block['attrs'][ $name ] ) ? (string) $block['attrs'][ $name ] : null; - }, - ] - ); + private function get_string_field_config( string $name ): array { + return [ + 'type' => 'String', + 'description' => sprintf( + // translators: %1$s is the field name, %2$s is the block type name. + __( '%1$s of the "%2$s" Block Type', 'wp-graphql-content-blocks' ), + ucfirst( $name ), + $this->type_name + ), + 'resolve' => static fn ( $block ) => isset( $block['attrs'][ $name ] ) ? (string) $block['attrs'][ $name ] : null, + ]; } /**