Skip to content

Commit

Permalink
Registers 'prefix', 'suffix' and 'term' string fields for the 'CorePo…
Browse files Browse the repository at this point in the history
…stTerms' block.
  • Loading branch information
whoami-pwd committed Feb 5, 2025
1 parent 2769e88 commit 9323cfb
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions includes/Blocks/CorePostTerms.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,51 @@
* 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 );
}

$this->register_list_of_terms_field();
}

/**
* Registers 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;
},
]
);
}

/**
* Registers a list of terms field for the block.
*
Expand Down

0 comments on commit 9323cfb

Please sign in to comment.