Skip to content

Commit

Permalink
fix: [PHPStan] $block_attributes is always set (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
justlevine authored May 22, 2023
1 parent f0bc286 commit b075a98
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-avocados-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpengine/wp-graphql-content-blocks": patch
---

fix: Correctly check if `$block_attributes` are set when attempting to register the block attribute fields to WPGraphQL.
100 changes: 54 additions & 46 deletions includes/Blocks/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,54 +141,62 @@ private function register_block_support_fields(): void {
/**
* Gets the WPGraphQL field registration config for the block attributes.
*
* @param array $block_attributes The block attributes.
* @param ?array $block_attributes The block attributes.
*/
private function get_block_attribute_fields( $block_attributes ): array {
private function get_block_attribute_fields( ?array $block_attributes ): array {
$block_attribute_fields = array();
if ( isset( $block_attributes ) ) {
foreach ( $block_attributes as $attribute_name => $attribute_config ) {
$graphql_type = null;
if ( ! isset( $attribute_config['type'] ) ) {
return $block_attribute_fields;
}

switch ( $attribute_config['type'] ) {
case 'string':
$graphql_type = 'String';
break;
case 'number':
$graphql_type = 'Float';
break;
case 'integer':
$graphql_type = 'Int';
break;
case 'boolean':
$graphql_type = 'Boolean';
break;
case 'array':
case 'object':
$graphql_type = Scalar::get_block_attributes_object_type_name();
break;
}

if ( empty( $graphql_type ) ) {
continue;
}

$block_attribute_fields[ Utils::format_field_name( $attribute_name ) ] = array(
'type' => $graphql_type,
'description' => sprintf(
// translators: %1$s is the attribute name, %2$s is the block name.
__( 'The "%1$s" field on the "%2$s" block', 'wp-graphql-content-blocks' ),
$attribute_name,
$this->type_name
),
'resolve' => function ( $block ) use ( $attribute_name, $attribute_config ) {
return $this->resolve_block_attributes( $block, $attribute_name, $attribute_config );
},
);
}//end foreach
}//end if

// Bail early if no attributes are defined.
if ( null === $block_attributes ) {
return $block_attribute_fields;
}

foreach ( $block_attributes as $attribute_name => $attribute_config ) {
$graphql_type = null;

if ( ! isset( $attribute_config['type'] ) ) {
return $block_attribute_fields;
}

switch ( $attribute_config['type'] ) {
case 'string':
$graphql_type = 'String';
break;
case 'number':
$graphql_type = 'Float';
break;
case 'integer':
$graphql_type = 'Int';
break;
case 'boolean':
$graphql_type = 'Boolean';
break;
case 'array':
case 'object':
$graphql_type = Scalar::get_block_attributes_object_type_name();
break;
}

// Skip if there's no valid type.
if ( empty( $graphql_type ) ) {
continue;
}

// Create the field config.
$block_attribute_fields[ Utils::format_field_name( $attribute_name ) ] = array(
'type' => $graphql_type,
'description' => sprintf(
// translators: %1$s is the attribute name, %2$s is the block name.
__( 'The "%1$s" field on the "%2$s" block', 'wp-graphql-content-blocks' ),
$attribute_name,
$this->type_name
),
'resolve' => function ( $block ) use ( $attribute_name, $attribute_config ) {
return $this->resolve_block_attributes( $block, $attribute_name, $attribute_config );
},
);
}//end foreach

return $block_attribute_fields;
}

Expand Down

0 comments on commit b075a98

Please sign in to comment.