Use wp_get_attachment_image if we have an attachment based placeholder

This commit is contained in:
Mike Jolley 2019-01-14 13:55:12 +00:00
parent 198fdce1bf
commit 1e563aea29
1 changed files with 17 additions and 60 deletions

View File

@ -296,77 +296,34 @@ function wc_placeholder_img_src( $size = 'woocommerce_thumbnail' ) {
return apply_filters( 'woocommerce_placeholder_img_src', $src );
}
/**
* Get the placeholder attachment image src, falling back to the default if unset.
*
* @since 3.6.0
* @param string $size Thumbnail size to use.
* @return bool|array Returns an array (url, width, height, is_intermediate), or false, if no image is available.
*/
function wc_get_placeholder_image_src( $size ) {
$placeholder_image = get_option( 'woocommerce_placeholder_image', 0 );
if ( ! empty( $placeholder_image ) && is_numeric( $placeholder_image ) ) {
return wp_get_attachment_image_src( $placeholder_image, $size );
}
// Fallback to non-attachment placeholder.
return array( wc_placeholder_img_src( $size ), $dimensions['width'], $dimensions['height'] );
}
/**
* Get the placeholder attachment srcset property for responsiveness.
*
* @since 3.6.0
* @param string $size Thumbnail size to use.
* @return bool|string Image srcset for the img tag, or false if unavailable.
*/
function wc_get_placeholder_image_srcset( $size ) {
$placeholder_image = get_option( 'woocommerce_placeholder_image', 0 );
if ( ! empty( $placeholder_image ) && is_numeric( $placeholder_image ) && function_exists( 'wp_get_attachment_image_srcset' ) ) {
return wp_get_attachment_image_srcset( $placeholder_image, $size );
}
return false;
}
/**
* Get the placeholder attachment sizes property for responsiveness.
*
* @since 3.6.0
* @param string $size Thumbnail size to use.
* @return bool|string Image sizes for the img tag, or false if unavailable.
*/
function wc_get_placeholder_image_sizes( $size ) {
$placeholder_image = get_option( 'woocommerce_placeholder_image', 0 );
if ( ! empty( $placeholder_image ) && is_numeric( $placeholder_image ) && function_exists( 'wp_get_attachment_image_sizes' ) ) {
return wp_get_attachment_image_sizes( $placeholder_image, $size );
}
return false;
}
/**
* Get the placeholder image.
*
* Uses wp_get_attachment_image if using an attachment ID @since 3.6.0 to handle responsiveness.
*
* @param string $size Image size.
* @return string
*/
function wc_placeholder_img( $size = 'woocommerce_thumbnail' ) {
$dimensions = wc_get_image_size( $size );
$image = wc_get_placeholder_image_src( $size );
$image_srcset = wc_get_placeholder_image_srcset( $size );
$image_sizes = wc_get_placeholder_image_sizes( $size );
$dimensions = wc_get_image_size( $size );
$placeholder_image = get_option( 'woocommerce_placeholder_image', 0 );
if ( $image_srcset && $image_sizes ) {
$placeholder_image_html = '<img src="' . esc_attr( $image[0] ) . '" alt="' . esc_attr__( 'Placeholder', 'woocommerce' ) . '" width="' . esc_attr( $image[1] ) . '" class="woocommerce-placeholder wp-post-image" height="' . esc_attr( $image[2] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
if ( ! empty( $placeholder_image ) && is_numeric( $placeholder_image ) ) {
$image_html = wp_get_attachment_image(
$placeholder_image,
$size,
false,
array(
'alt' => __( 'Placeholder', 'woocommerce' ),
'class' => 'woocommerce-placeholder wp-post-image',
)
);
} else {
$placeholder_image_html = '<img src="' . esc_attr( $image[0] ) . '" alt="' . esc_attr__( 'Placeholder', 'woocommerce' ) . '" width="' . esc_attr( $image[1] ) . '" class="woocommerce-placeholder wp-post-image" height="' . esc_attr( $image[2] ) . '" />';
$image = wc_placeholder_img_src( $size );
$image_html = '<img src="' . esc_attr( $image ) . '" alt="' . esc_attr__( 'Placeholder', 'woocommerce' ) . '" width="' . esc_attr( $dimensions['width'] ) . '" class="woocommerce-placeholder wp-post-image" height="' . esc_attr( $dimensions['height'] ) . '" />';
}
return apply_filters( 'woocommerce_placeholder_img', $placeholder_image_html, $size, $dimensions );
return apply_filters( 'woocommerce_placeholder_img', $image_html, $size, $dimensions );
}
/**