Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Convert preset to css variable for padding
Browse files Browse the repository at this point in the history
We are getting padding value in preset format like this:
"var:preset|spacing|50"
Therefore I added a function to convert it to CSS variable like this:
"var(--wp--preset--spacing--50)"

i.e. "var:preset|spacing|50" -> "var(--wp--preset--spacing--50)"
  • Loading branch information
imanish003 committed Nov 23, 2022
1 parent 713ae70 commit f859dcd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/BlockTypes/ProductSaleBadge.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ protected function render( $attributes, $content, $block ) {
}

$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
$classname = isset( $attributes['className'] ) ? $attributes['className'] : '';

$output = '';
$output .= '<div class="wc-block-components-product-sale-badge ' . $classes_and_styles['classes'] . ' ' . $attributes['className'] . '" style="' . $classes_and_styles['styles'] . '"">';
$output .= '<div class="wc-block-components-product-sale-badge ' . $classes_and_styles['classes'] . ' ' . $classname . '" style="' . $classes_and_styles['styles'] . '"">';
$output .= '<span class="wc-block-components-product-sale-badge__text" aria-hidden="true">' . __( 'Sale', 'woo-gutenberg-products-block' ) . '</span>';
$output .= '<span class="screen-reader-text">' . __(
'Product on sale',
Expand Down
37 changes: 36 additions & 1 deletion src/Utils/StyleAttributesUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,27 @@ public static function get_align_class_and_style( $attributes ) {
return null;
}


/**
* If spacing value is in preset format, convert it to a CSS var. Else return same value
* For example:
* "var:preset|spacing|50" -> "var(--wp--preset--spacing--50)"
* "50px" -> "50px"
*
* @param string $spacing_value value to be processed.
*
* @return (string)
*/
public static function get_spacing_value( $spacing_value ) {
// If value is in format: var:preset|spacing|50.
if ( is_string( $spacing_value ) && str_contains( $spacing_value, 'var:preset|spacing|' ) ) {
$spacing_value = str_replace( 'var:preset|spacing|', '', 'var:preset|spacing|50' );
return sprintf( 'var(--wp--preset--spacing--%s)', $spacing_value );
}

return $spacing_value;
}

/**
* Get class and style for padding from attributes.
*
Expand All @@ -341,9 +362,23 @@ public static function get_padding_class_and_style( $attributes ) {
return null;
}

$padding_top = $padding['top'] ? self::get_spacing_value( $padding['top'] ) : null;
$padding_right = $padding['right'] ? self::get_spacing_value( $padding['right'] ) : null;
$padding_bottom = $padding['bottom'] ? self::get_spacing_value( $padding['bottom'] ) : null;
$padding_left = $padding['left'] ? self::get_spacing_value( $padding['left'] ) : null;

return array(
'class' => null,
'style' => sprintf( 'padding: %s;', implode( ' ', $padding ) ),
'style' => sprintf(
'padding-top:%s;
padding-right:%s;
padding-bottom:%s;
padding-left:%s;',
$padding_top,
$padding_right,
$padding_bottom,
$padding_left
),
);
}

Expand Down

0 comments on commit f859dcd

Please sign in to comment.