From 2e7f2e854546d94d71d2f586003d77983b91edad Mon Sep 17 00:00:00 2001 From: Theofanis Despoudis <328805+theodesp@users.noreply.github.com> Date: Wed, 23 Aug 2023 10:16:48 +0100 Subject: [PATCH] Refactor: (Performance) `register_graphql_interfaces` no longer used 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. --- .changeset/thin-insects-sparkle.md | 8 ++++++ includes/Blocks/Block.php | 18 ++++++------ includes/Field/BlockSupports/Anchor.php | 30 +++++++++++++++++++ includes/Registry/Registry.php | 38 +++++++++++++++++++++++-- 4 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 .changeset/thin-insects-sparkle.md diff --git a/.changeset/thin-insects-sparkle.md b/.changeset/thin-insects-sparkle.md new file mode 100644 index 00000000..1a647a1f --- /dev/null +++ b/.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. + diff --git a/includes/Blocks/Block.php b/includes/Blocks/Block.php index c5faaea2..e25e519f 100644 --- a/includes/Blocks/Block.php +++ b/includes/Blocks/Block.php @@ -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(); } /** @@ -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, ) ); @@ -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. * @@ -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 * diff --git a/includes/Field/BlockSupports/Anchor.php b/includes/Field/BlockSupports/Anchor.php index 90c232a5..c685068b 100644 --- a/includes/Field/BlockSupports/Anchor.php +++ b/includes/Field/BlockSupports/Anchor.php @@ -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 ) ) ); diff --git a/includes/Registry/Registry.php b/includes/Registry/Registry.php index e7fed3a6..1559348d 100644 --- a/includes/Registry/Registry.php +++ b/includes/Registry/Registry.php @@ -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(); } /** @@ -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; } /**