* Refactor import and add padding support in image block

In this commit, the import statement for __experimentalGetSpacingClassesAndStyles has been refactored to use an alias getSpacingClassesAndStyles for better readability. Additionally, padding support has been added to the image block's spacing configuration.

* Refactor ProductImage to use consolidated style classes and attributes

This commit refactors the ProductImage block by using the consolidated classes and styles provided by the StyleAttributesUtils::get_classes_and_styles_by_attributes() function. This change simplifies the code, improves readability, and ensures consistent handling of styles across different block elements.
This commit is contained in:
Manish Menaria 2023-03-30 12:35:32 +05:30 committed by GitHub
parent a0a6f0914c
commit 392f7e17fe
2 changed files with 9 additions and 7 deletions

View File

@ -3,7 +3,7 @@
* External dependencies
*/
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import { __experimentalGetSpacingClassesAndStyles } from '@wordpress/block-editor';
import { __experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles } from '@wordpress/block-editor';
/**
* Internal dependencies
@ -20,9 +20,10 @@ export const supports = {
fontSize: true,
__experimentalSkipSerialization: true,
},
...( typeof __experimentalGetSpacingClassesAndStyles === 'function' && {
...( typeof getSpacingClassesAndStyles === 'function' && {
spacing: {
margin: true,
padding: true,
__experimentalSkipSerialization: true,
},
} ),

View File

@ -188,19 +188,20 @@ class ProductImage extends AbstractBlock {
}
$parsed_attributes = $this->parse_attributes( $attributes );
$border_radius = StyleAttributesUtils::get_border_radius_class_and_style( $attributes );
$margin = StyleAttributesUtils::get_margin_class_and_style( $attributes );
$border_radius = StyleAttributesUtils::get_border_radius_class_and_style( $attributes );
$margin = StyleAttributesUtils::get_margin_class_and_style( $attributes );
$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
$post_id = $block->context['postId'];
$product = wc_get_product( $post_id );
if ( $product ) {
return sprintf(
'<div class="wc-block-components-product-image wc-block-grid__product-image" style="%1$s %2$s">
'<div class="wc-block-components-product-image wc-block-grid__product-image %1$s" style="%2$s">
%3$s
</div>',
isset( $border_radius['style'] ) ? esc_attr( $border_radius['style'] ) : '',
isset( $margin['style'] ) ? esc_attr( $margin['style'] ) : '',
esc_attr( $classes_and_styles['classes'] ),
esc_attr( $classes_and_styles['styles'] ),
$this->render_anchor(
$product,
$this->render_on_sale_badge( $product, $parsed_attributes ),