', esc_attr( $styles ) );
+ $output .= sprintf( '
', esc_attr( $wrapper_classes ), esc_attr( $styles ) );
$output .= $this->render_overlay( $attributes );
- if ( ! $attributes['isRepeated'] ) {
+ if ( ! $attributes['isRepeated'] && ! $attributes['hasParallax'] ) {
$output .= $this->render_image( $attributes, $product, $image_url );
}
@@ -154,7 +155,7 @@ private function get_image_url( $attributes, $product ) {
private function render_image( $attributes, $product, $image_url ) {
$style = sprintf( 'object-fit: %s;', $attributes['imageFit'] );
- if ( is_array( $attributes['focalPoint'] ) && 2 === count( $attributes['focalPoint'] ) ) {
+ if ( $this->hasFocalPoint( $attributes ) ) {
$style .= sprintf(
'object-position: %s%% %s%%;',
$attributes['focalPoint']['x'] * 100,
@@ -206,8 +207,16 @@ private function render_overlay( $attributes ) {
public function get_styles( $attributes, $image_url ) {
$style = '';
- if ( $attributes['isRepeated'] ) {
+ if ( $attributes['isRepeated'] || $attributes['hasParallax'] ) {
$style .= "background-image: url($image_url);";
+ }
+
+ if ( ! $attributes['isRepeated'] ) {
+ $style .= "background-repeat: 'no-repeat';";
+ $style .= 'background-size: ' . ( 'cover' === $attributes['imageFit'] ? $attributes['imageFit'] : 'auto' ) . ';';
+ }
+
+ if ( $this->hasFocalPoint( $attributes ) ) {
$style .= sprintf(
'background-position: %s%% %s%%;',
$attributes['focalPoint']['x'] * 100,
@@ -227,6 +236,22 @@ public function get_styles( $attributes, $image_url ) {
return $style;
}
+ /**
+ * Get class names for the block wrapper.
+ *
+ * @param array $attributes Block attributes. Default empty array.
+ * @return string
+ */
+ private function get_wrapper_classes( $attributes ) {
+ $classes[] = 'wc-block-featured-product__wrapper';
+
+ if ( $attributes['hasParallax'] ) {
+ $classes[] = ' has-parallax';
+ }
+
+ return implode( ' ', $classes );
+ }
+
/**
* Get class names for the block container.
*
@@ -287,6 +312,17 @@ public function get_image( $product, $size = 'full' ) {
return $image;
}
+ /**
+ * Returns whether the focal point is defined for the block.
+ *
+ * @param array $attributes Block attributes. Default empty array.
+ *
+ * @return bool
+ */
+ private function hasFocalPoint( $attributes ): bool {
+ return is_array( $attributes['focalPoint'] ) && 2 === count( $attributes['focalPoint'] );
+ }
+
/**
* Extra data passed through from server to client for block.
*