n string */ private function render_image( $product, $attributes ) { $image_size = 'single' === $attributes['imageSizing'] ? 'woocommerce_single' : 'woocommerce_thumbnail'; $image_style = 'max-width:none;'; if ( ! empty( $attributes['height'] ) ) { $image_style .= sprintf( 'height:%s;', $attributes['height'] ); } if ( ! empty( $attributes['width'] ) ) { $image_style .= sprintf( 'width:%s;', $attributes['width'] ); } if ( ! empty( $attributes['scale'] ) ) { $image_style .= sprintf( 'object-fit:%s;', $attributes['scale'] ); } if ( ! empty( $attributes['aspectRatio'] ) ) { $image_style .= sprintf( 'aspect-ratio:%s;', $attributes['aspectRatio'] ); } $image_id = $product->get_image_id(); $alt_text = ''; $title = ''; if ( $image_id ) { $alt_text = get_post_meta( $image_id, '_wp_attachment_image_alt', true ); $title = get_the_title( $image_id ); } return $product->get_image( $image_size, array( 'alt' => empty( $alt_text ) ? $product->get_title() : $alt_text, 'data-testid' => 'product-image', 'style' => $image_style, 'title' => $title, ) ); } /** * Extra data passed through from server to client for block. * * @param array $attributes Any attributes that currently are available from the block. * Note, this will be empty in the editor context when the block is * not in the post content on editor load. */ protected function enqueue_data( array $attributes = [] ) { $this->asset_data_registry->add( 'isBlockThemeEnabled', wc_current_theme_is_fse_theme() ); } /** * Include and render the block * * @param array $attributes Block attributes. Default empty array. * @param string $content Block content. Default empty string. * @param WP_Block $block Block instance. * @return string Rendered block type output. */ protected function render( $attributes, $content, $block ) { if ( ! empty( $content ) ) { parent::register_block_type_assets(); $this->register_chunk_translations( [ $this->block_name ] ); return $content; } $parsed_attributes = $this->parse_attributes( $attributes ); $classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes, array(), array( 'extra_classes' ) ); $post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : ''; $product = wc_get_product( $post_id ); $classes = implode( ' ', array_filter( array( 'wc-block-components-product-image wc-block-grid__product-image', esc_attr( $classes_and_styles['classes'] ), ) ) ); $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes, 'style' => esc_attr( $classes_and_styles['styles'] ), ) ); if ( $product ) { return sprintf( '
%2$s
', $wrapper_attributes, $this->render_anchor( $product, $this->render_on_sale_badge( $product, $parsed_attributes ), $this->render_image( $product, $parsed_attributes ), $parsed_attributes ) ); } } }