Skip to content

Commit

Permalink
Refactor: (Performance) register_graphql_interfaces no longer used …
Browse files Browse the repository at this point in the history
…in `register_block_types` (#146)

* Refactor: (Performance) Removed usage of register_graphql_interfaces_to_types in register_block_types

* Style: PHPcs fix.

* Update thin-insects-sparkle.md

* Refactor: (Comments)

* Update thin-insects-sparkle.md

* Chore: Deprecate `register_to_block` instead.

* Update thin-insects-sparkle.md

* Review: Add `_deprecated_function` as well.
  • Loading branch information
theodesp committed Aug 23, 2023
1 parent b8008ab commit 2e7f2e8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .changeset/thin-insects-sparkle.md
@@ -0,0 +1,8 @@
---
"@wpengine/wp-graphql-content-blocks": patch
---

Refactored `register_block_types` to remove usages of `register_graphql_interfaces_to_types` to improve performance.

Deprecated `Anchor::register_to_block` public static method.

18 changes: 10 additions & 8 deletions includes/Blocks/Block.php
Expand Up @@ -76,7 +76,6 @@ public function __construct( WP_Block_Type $block, Registry $block_registry ) {
private function register_block_type() {
$this->register_block_attributes_as_fields();
$this->register_type();
$this->register_block_support_fields();
}

/**
Expand All @@ -100,6 +99,7 @@ private function register_block_attributes_as_fields(): void {
__( 'Attributes of the %s Block Type', 'wp-graphql-content-blocks' ),
$this->type_name
),
'interfaces' => $this->get_block_attributes_interfaces(),
'fields' => $block_attribute_fields,
)
);
Expand All @@ -122,13 +122,6 @@ private function register_block_attributes_as_fields(): void {
}//end if
}

/**
* Registers fields for the block supports.
*/
private function register_block_support_fields(): void {
Anchor::register_to_block( $this->block );
}

/**
* Gets the WPGraphQL field registration config for the block attributes.
*
Expand Down Expand Up @@ -200,6 +193,15 @@ private function get_block_interfaces(): array {
return $this->block_registry->get_block_interfaces( $this->block->name );
}

/**
* Gets the GraphQL interfaces that should be implemented by the block attributes object.
*
* @return string[]
*/
private function get_block_attributes_interfaces(): array {
return $this->block_registry->get_block_attributes_interfaces( $this->block->name );
}

/**
* Register the Type for the block
*
Expand Down
30 changes: 30 additions & 0 deletions includes/Field/BlockSupports/Anchor.php
Expand Up @@ -40,13 +40,43 @@ public static function register(): void {
);
}

/**
* Checks if the block spec supports the anchor field and returns the relevant interface
*
* @param array $existing An existing list of a block interfaces.
* @param \WP_Block_Type $block_spec The block type to register the anchor field against.
*
* @return string[]
*/
public static function get_block_interfaces( $existing, \WP_Block_Type $block_spec ): array {
if ( isset( $block_spec ) && isset( $block_spec->supports['anchor'] ) && true === $block_spec->supports['anchor'] ) {
$existing[] = 'BlockWithSupportsAnchor';
}
return $existing;
}

/**
* Checks if the block spec supports the anchor field and returns the relevant interface
*
* @param array $existing An existing list of a block attribute interfaces.
* @param \WP_Block_Type $block_spec The block type to register the anchor field against.
*
* @return string[]
*/
public static function get_block_attributes_interfaces( $existing, \WP_Block_Type $block_spec ): array {
return self::get_block_interfaces( $existing, $block_spec );
}

/**
* Registers an Anchor field on a block if it supports it.
*
* @param \WP_Block_Type $block_spec The block type to register the anchor field against.
* @return void
*
* @deprecated 1.1.4 No longer used by internal code and not recommended.
*/
public static function register_to_block( \WP_Block_Type $block_spec ): void {
_deprecated_function( __METHOD__, '1.1.4' );
if ( isset( $block_spec->supports['anchor'] ) && true === $block_spec->supports['anchor'] ) {
register_graphql_interfaces_to_types( 'BlockWithSupportsAnchor', array( WPGraphQLHelpers::format_type_name( $block_spec->name ) . 'Attributes' ) );
register_graphql_interfaces_to_types( 'BlockWithSupportsAnchor', array( WPGraphQLHelpers::format_type_name( $block_spec->name ) ) );
Expand Down
38 changes: 35 additions & 3 deletions includes/Registry/Registry.php
Expand Up @@ -70,8 +70,8 @@ public function __construct( TypeRegistry $type_registry, $block_type_registry )
public function init(): void {
$this->register_interface_types();
$this->register_scalar_types();
$this->register_block_types();
$this->register_support_block_types();
$this->register_block_types();
}

/**
Expand Down Expand Up @@ -140,8 +140,40 @@ public function get_block_interfaces( string $block_name ): array {
// Get the interfaces a block should implement based on the context a block is available to be accessed from.
$context_interfaces = $this->get_block_context_interfaces( $block_name );

// @todo: if blocks need to implement other interfaces (i.e. "BlockSupports" interfaces, that could be handled here as well)
return array_merge( array( 'EditorBlock' ), $context_interfaces );
// Get additional interfaces a block should implement.
$additional_interfaces = $this->get_block_additional_interfaces( $block_name );

return array_merge( array( 'EditorBlock' ), $context_interfaces, $additional_interfaces );
}

/**
* Given the name of a Block, return interfaces the Block object should implement.
*
* @param string $block_name The name of the block to get the interfaces for.
*
* @return string[]
*/
public function get_block_additional_interfaces( string $block_name ): array {
$block_spec = $this->block_type_registry->get_registered( $block_name );
$block_interfaces = array();
// NOTE: Using add_filter here creates a performance penalty.
$block_interfaces = Anchor::get_block_interfaces( $block_interfaces, $block_spec );
return $block_interfaces;
}

/**
* Given the name of a Block, return interfaces the Block attributes object should implement.
*
* @param string $block_name The name of the block to get the interfaces for.
*
* @return string[]
*/
public function get_block_attributes_interfaces( string $block_name ): array {
$block_spec = $this->block_type_registry->get_registered( $block_name );
$block_interfaces = array();
// NOTE: Using add_filter here creates a performance penalty.
$block_interfaces = Anchor::get_block_attributes_interfaces( $block_interfaces, $block_spec );
return $block_interfaces;
}

/**
Expand Down

0 comments on commit 2e7f2e8

Please sign in to comment.