From 72b32109c964d1a841a628516e022ac8ee37679e Mon Sep 17 00:00:00 2001 From: theodesp <328805+theodesp@users.noreply.github.com> Date: Tue, 2 Jan 2024 11:32:05 +0000 Subject: [PATCH] Fix: Properly extract innerHTML from source attrs --- includes/Blocks/Block.php | 22 ++++++++++++++-------- includes/Utilities/DomHelpers.php | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/includes/Blocks/Block.php b/includes/Blocks/Block.php index 9ee6d771..8cf0e3c8 100644 --- a/includes/Blocks/Block.php +++ b/includes/Blocks/Block.php @@ -129,7 +129,7 @@ private function register_block_attributes_as_fields(): void { * @param object $attribute The block attribute config * @param string $prefix Current prefix string to use for the get_query_type */ - private function get_attribute_type( $name, $attribute, $prefix ) { + private function get_attribute_type( $name, $attribute, $prefix ): mixed { $type = null; if ( isset( $attribute['type'] ) ) { @@ -185,6 +185,8 @@ private function get_attribute_type( $name, $attribute, $prefix ) { * * @param ?array $block_attributes The block attributes. * @param string $prefix The current prefix string to use for the get_query_type + * + * @return array */ private function get_block_attribute_fields( ?array $block_attributes, $prefix = null ): array { $fields = []; @@ -193,7 +195,7 @@ private function get_block_attribute_fields( ?array $block_attributes, $prefix = if ( null === $block_attributes ) { return $fields; } - + foreach ( $block_attributes as $attribute_name => $attribute_config ) { $graphql_type = self::get_attribute_type( $attribute_name, $attribute_config, $prefix ); @@ -230,7 +232,7 @@ private function get_block_attribute_fields( ?array $block_attributes, $prefix = * @param array $query The block query config * @param string $prefix The current prefix string to use for registering the new query attribute type */ - private function get_query_type( $name, $query, $prefix ) { + private function get_query_type( $name, $query, $prefix ): string { $type = $prefix . ucfirst( $name ); $fields = $this->create_attributes_fields( $query, $type ); @@ -256,8 +258,10 @@ private function get_query_type( $name, $query, $prefix ) { * * @param array $attributes The query attributes config * @param string $prefix The current prefix string to use for registering the new query attribute type + * + * @return array */ - private function create_attributes_fields( $attributes, $prefix ) { + private function create_attributes_fields( $attributes, $prefix ): array { $fields = []; foreach ( $attributes as $name => $attribute ) { $type = $this->get_attribute_type( $name, $attribute, $prefix ); @@ -284,7 +288,7 @@ private function create_attributes_fields( $attributes, $prefix ) { * @param array|string $value The value * @param string $type The type of the value */ - private function normalize_attribute_value( $value, $type ) { + private function normalize_attribute_value( $value, $type ): mixed { switch ( $type ) { case 'string': return (string) $value; @@ -358,8 +362,10 @@ private function resolve( $block ) { * @param array $attributes The block current attributes value * @param string $html The block rendered html * @param array $config The block current attribute configuration + * + * @return array */ - private function resolve_block_attributes_recursive( $attributes, $html, $config ) { + private function resolve_block_attributes_recursive( $attributes, $html, $config ): array { $result = []; foreach ( $config as $key => $value ) { // Get default value. @@ -428,14 +434,14 @@ private function resolve_block_attributes_recursive( $attributes, $html, $config * @param string $html The html value * @param string $source The source type */ - private function parse_single_source( $html, $source ) { + private function parse_single_source( $html, $source ): string|null { $value = null; if ( empty( $html ) ) { return $value; } switch ( $source ) { case 'html': - $value = $html; + $value = DOMHelpers::findNodes( $html )->innerHTML(); break; } return $value; diff --git a/includes/Utilities/DomHelpers.php b/includes/Utilities/DomHelpers.php index aca0277f..50679908 100644 --- a/includes/Utilities/DomHelpers.php +++ b/includes/Utilities/DomHelpers.php @@ -136,7 +136,7 @@ public static function parseText( $html, $selector ) { * @param string $html The HTML string to parse. * @param string|null $selector The selector to use. * - * @return \DOMElement[] + * @return \DOMElement[]|\WPGraphQL\ContentBlocks\Utilities\DOMElement */ public static function findNodes( $html, $selector = null ) { $value = null;