Skip to content

Commit

Permalink
fix: resolve missing bool/rich-text attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
DDEV User authored and justlevine committed Sep 23, 2024
1 parent 23bd8b4 commit 1bd300a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
10 changes: 6 additions & 4 deletions includes/Blocks/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
},
];
}
Expand All @@ -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':
Expand Down
5 changes: 3 additions & 2 deletions includes/Data/BlockAttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/CoreVideoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1bd300a

Please sign in to comment.