Ensure alt text is set for product attachments

@claudiosmweb Fixes #10822
This commit is contained in:
Mike Jolley 2016-05-03 16:41:51 +01:00
parent 0782b869f5
commit fce8dc0868
3 changed files with 44 additions and 22 deletions

View File

@ -697,3 +697,32 @@ function wc_get_product_cat_ids( $product_id ) {
return $product_cats;
}
/**
* Gets data about an attachment, such as alt text and captions.
* @since 2.6.0
* @param int $thumbnail_id
* @param object|bool $product
* @return array
*/
function wc_get_product_attachment_props( $attachment_id, $product = false ) {
$props = array(
'title' => '',
'caption' => '',
'url' => '',
'alt' => '',
);
if ( $attachment_id ) {
$attachment = get_post( $attachment_id );
$props['title'] = trim( strip_tags( $attachment->post_title ) );
$props['caption'] = trim( strip_tags( $attachment->post_excerpt ) );
$props['url'] = wp_get_attachment_url( $attachment_id );
$props['alt'] = trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );
// Alt text fallbacks
$props['alt'] = empty( $props['alt'] ) ? $props['caption'] : $props['alt'];
$props['alt'] = empty( $props['alt'] ) ? trim( strip_tags( $attachment->post_title ) ) : $props['alt'];
$props['alt'] = empty( $props['alt'] ) && $product ? trim( strip_tags( get_the_title( $product->ID ) ) ) : $props['alt'];
}
return $props;
}

View File

@ -730,7 +730,11 @@ if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
global $post;
if ( has_post_thumbnail() ) {
return get_the_post_thumbnail( $post->ID, $size );
$props = wc_get_product_attachment_props( get_post_thumbnail_id(), $post );
return get_the_post_thumbnail( $post->ID, $size, array(
'title' => $props['title'],
'alt' => $props['alt'],
) );
} elseif ( wc_placeholder_img_src() ) {
return wc_placeholder_img( $size );
}

View File

@ -20,34 +20,23 @@ if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $post, $woocommerce, $product;
global $post, $product;
?>
<div class="images">
<?php
if ( has_post_thumbnail() ) {
$image_caption = get_post( get_post_thumbnail_id() )->post_excerpt;
$image_link = wp_get_attachment_url( get_post_thumbnail_id() );
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
'title' => get_the_title( get_post_thumbnail_id() )
) );
$attachment_count = count( $product->get_gallery_attachment_ids() );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s" data-rel="prettyPhoto' . $gallery . '">%s</a>', $image_link, $image_caption, $image ), $post->ID );
$gallery = $attachment_count > 0 ? '[product-gallery]' : '';
$props = wc_get_product_attachment_props( get_post_thumbnail_id(), $post );
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), array(
'title' => $props['title'],
'alt' => $props['alt'],
) );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s" data-rel="prettyPhoto' . $gallery . '">%s</a>', $props['url'], $props['caption'], $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="%s" />', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );
}
?>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
do_action( 'woocommerce_product_thumbnails' );
?>
</div>