From 1bd300aaa9fbd5f4cf8622c92b94e49ae8ad80c3 Mon Sep 17 00:00:00 2001 From: DDEV User Date: Sat, 21 Sep 2024 22:42:19 +0000 Subject: [PATCH] fix: resolve missing bool/rich-text attributes --- includes/Blocks/Block.php | 10 ++++++---- includes/Data/BlockAttributeResolver.php | 5 +++-- tests/unit/CoreVideoTest.php | 6 +++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/includes/Blocks/Block.php b/includes/Blocks/Block.php index 85360087..94258282 100644 --- a/includes/Blocks/Block.php +++ b/includes/Blocks/Block.php @@ -279,9 +279,10 @@ private function create_attributes_fields( $attributes, $prefix ): array { $name, $prefix ), - 'resolve' => function ( $attributes ) use ( $name, $default_value ) { + 'resolve' => function ( $attributes ) use ( $name, $default_value, $type ) { $value = $attributes[ $name ] ?? $default_value; - return $this->normalize_attribute_value( $value, $attributes['__type'][ $name ]['type'] ); + + return $this->normalize_attribute_value( $value, $type ); }, ]; } @@ -305,10 +306,11 @@ private function normalize_attribute_value( $value, $type ) { } switch ( $type ) { - case 'rich-text': case 'array': // If we're here, we want an array type, even though the value is not an array. - return isset( $value ) ? [ $value ] : []; + // @todo This should return null if the value is empty. + return ! empty( $value ) ? [ $value ] : []; + case 'rich-text': case 'string': return (string) $value; case 'number': diff --git a/includes/Data/BlockAttributeResolver.php b/includes/Data/BlockAttributeResolver.php index c84eecd0..10e4ec6e 100644 --- a/includes/Data/BlockAttributeResolver.php +++ b/includes/Data/BlockAttributeResolver.php @@ -59,14 +59,15 @@ public static function resolve_block_attribute( $attribute, string $html, $attri $value = intval( $value ); break; case 'boolean': - $value = ! empty( $value ); + // Boolean attributes can be an empty string. + $value = ( ! is_array( $value ) && isset( $value ) ) || ! empty( $value ); break; } } } // Fallback to the attributes or default value if the result is empty. - if ( empty( $value ) ) { + if ( null === $value ) { $default = $attribute['default'] ?? null; $value = $attribute_value ?? $default; diff --git a/tests/unit/CoreVideoTest.php b/tests/unit/CoreVideoTest.php index 7a455b38..5796e36b 100644 --- a/tests/unit/CoreVideoTest.php +++ b/tests/unit/CoreVideoTest.php @@ -85,12 +85,12 @@ public function test_retrieve_core_video_attributes() { 'anchor' => null, 'autoplay' => true, 'tracks' => [], - 'muted' => null, - 'caption' => null, + 'muted' => false, + 'caption' => '', 'preload' => 'auto', 'src' => 'http://mysite.local/wp-content/uploads/2023/07/pexels_videos_1860684-1440p.mp4', 'playsInline' => true, - 'controls' => true, + 'controls' => false, 'loop' => true, 'poster' => 'http://mysite.local/wp-content/uploads/2023/05/pexels-egor-komarov-14420089-scaled.jpg', 'id' => 1636.0,