Block.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\ProductCollectionImage; |
| 4 | |
| 5 | use SureCartBlocks\Blocks\BaseBlock; |
| 6 | |
| 7 | /** |
| 8 | * Product Collection Image Block |
| 9 | */ |
| 10 | class Block extends BaseBlock { |
| 11 | |
| 12 | /** |
| 13 | * Get image styles. |
| 14 | * |
| 15 | * @param array $attributes |
| 16 | * |
| 17 | * @return string |
| 18 | */ |
| 19 | private function getImageStyle( $attributes ): string { |
| 20 | $style = ''; |
| 21 | |
| 22 | // Aspect ratio with a height set needs to override the default width/height. |
| 23 | if ( ! empty( $attributes['aspectRatio'] ) ) { |
| 24 | $style .= 'width:100%;height:100%;'; |
| 25 | } elseif ( ! empty( $attributes['height'] ) ) { |
| 26 | $style .= "height:{$attributes['height']};"; |
| 27 | } |
| 28 | |
| 29 | if ( ! empty( $attributes['scale'] ) ) { |
| 30 | $style .= "object-fit:{$attributes['scale']};"; |
| 31 | } |
| 32 | |
| 33 | return $style; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Render the block |
| 38 | * |
| 39 | * @param array $attributes Block attributes. |
| 40 | * @param string $content Post content. |
| 41 | * |
| 42 | * @return string |
| 43 | */ |
| 44 | public function render( $attributes, $content ) { |
| 45 | $collection = get_query_var( 'surecart_current_collection' ); |
| 46 | if ( empty( $collection ) || empty( $collection->image ) ) { |
| 47 | return ''; |
| 48 | } |
| 49 | |
| 50 | // Wrapper attributes. |
| 51 | $aspect_ratio = ! empty( $attributes['aspectRatio'] ) ? esc_attr( safecss_filter_attr( 'aspect-ratio:' . $attributes['aspectRatio'] ) ) . ';' : ''; |
| 52 | $width = ! empty( $attributes['width'] ) ? esc_attr( safecss_filter_attr( 'width:' . $attributes['width'] ) ) . ';' : ''; |
| 53 | $height = ! empty( $attributes['height'] ) ? esc_attr( safecss_filter_attr( 'height:' . $attributes['height'] ) ) . ';' : ''; |
| 54 | |
| 55 | if ( ! $height && ! $width && ! $aspect_ratio ) { |
| 56 | $wrapper_attributes = get_block_wrapper_attributes(); |
| 57 | } else { |
| 58 | $wrapper_attributes = get_block_wrapper_attributes( [ 'style' => $aspect_ratio . $width . $height ] ); |
| 59 | } |
| 60 | |
| 61 | // if % is used, we need to set the cdn_image_size = 0, so that it will use the full size image. |
| 62 | if ( ! empty( $attributes['width'] ) ) { |
| 63 | $cdn_image_size = strpos( $attributes['width'], '%' ) !== false ? 0 : $attributes['width'] ?? 0; |
| 64 | } else { |
| 65 | $cdn_image_size = 0; |
| 66 | } |
| 67 | |
| 68 | $collection_image = sprintf( |
| 69 | '<img src="%1$s" alt="%2$s" style="%3$s" srcset="%4$s" width="%5$s" height="%6$s" />', |
| 70 | esc_url( $collection->getImageUrl( $cdn_image_size ) ), |
| 71 | esc_attr( $collection->name ?? '' ), |
| 72 | $this->getImageStyle( $attributes ), |
| 73 | $collection->getImageUrl( $cdn_image_size, 'dpr=2' ) . ' 2x', |
| 74 | ! empty( $attributes['width'] ) ? (int) $attributes['width'] : null, |
| 75 | ! empty( $attributes['height'] ) ? (int) $attributes['height'] : null |
| 76 | ); |
| 77 | |
| 78 | return "<figure {$wrapper_attributes}>{$collection_image}</figure>"; |
| 79 | } |
| 80 | } |
| 81 |