Skip to content

Commit

Permalink
Replaces registering string fields with register_graphql_fields and…
Browse files Browse the repository at this point in the history
… eliminates abstraction and class constant.
  • Loading branch information
whoami-pwd committed Feb 5, 2025
1 parent 0ba35f4 commit 56c51c9
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions includes/Blocks/CorePostTerms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}

/**
Expand Down

0 comments on commit 56c51c9

Please sign in to comment.