Merge pull request #24937 from jenkoian/wc-placeholder-attrs

Add support for custom attributes to wc_placeholder_img().
This commit is contained in:
Claudio Sanches 2019-12-04 15:42:41 -03:00 committed by GitHub
commit adbb4fd2d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 8 deletions

View File

@ -1859,7 +1859,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
} }
if ( ! $image && $placeholder ) { if ( ! $image && $placeholder ) {
$image = wc_placeholder_img( $size ); $image = wc_placeholder_img( $size, $attr );
} }
return apply_filters( 'woocommerce_product_get_image', $image, $this, $size, $attr, $placeholder, $image ); return apply_filters( 'woocommerce_product_get_image', $image, $this, $size, $attr, $placeholder, $image );

View File

@ -292,25 +292,36 @@ function wc_placeholder_img_src( $size = 'woocommerce_thumbnail' ) {
* Uses wp_get_attachment_image if using an attachment ID @since 3.6.0 to handle responsiveness. * Uses wp_get_attachment_image if using an attachment ID @since 3.6.0 to handle responsiveness.
* *
* @param string $size Image size. * @param string $size Image size.
* @param string|array $attr Optional. Attributes for the image markup. Default empty.
* @return string * @return string
*/ */
function wc_placeholder_img( $size = 'woocommerce_thumbnail' ) { function wc_placeholder_img( $size = 'woocommerce_thumbnail', $attr = '' ) {
$dimensions = wc_get_image_size( $size ); $dimensions = wc_get_image_size( $size );
$placeholder_image = get_option( 'woocommerce_placeholder_image', 0 ); $placeholder_image = get_option( 'woocommerce_placeholder_image', 0 );
$default_attr = array(
'class' => 'woocommerce-placeholder wp-post-image',
'alt' => __( 'Placeholder', 'woocommerce' ),
);
$attr = wp_parse_args( $attr, $default_attr );
if ( wp_attachment_is_image( $placeholder_image ) ) { if ( wp_attachment_is_image( $placeholder_image ) ) {
$image_html = wp_get_attachment_image( $image_html = wp_get_attachment_image(
$placeholder_image, $placeholder_image,
$size, $size,
false, false,
array( $attr
'alt' => __( 'Placeholder', 'woocommerce' ),
'class' => 'woocommerce-placeholder wp-post-image',
)
); );
} else { } else {
$image = wc_placeholder_img_src( $size ); $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'] ) . '" />'; $hwstring = image_hwstring( $dimensions['width'], $dimensions['height'] );
$attr = array_map( 'esc_attr', $attr );
$image_html = rtrim( '<img src="' . esc_attr( $image ) . '" ' . $hwstring );
foreach ( $attr as $name => $value ) {
$image_html .= " $name=" . '"' . $value . '"';
}
$image_html .= ' />';
} }
return apply_filters( 'woocommerce_placeholder_img', $image_html, $size, $dimensions ); return apply_filters( 'woocommerce_placeholder_img', $image_html, $size, $dimensions );

View File

@ -324,6 +324,10 @@ class WC_Tests_Product_Data extends WC_Unit_Test_Case {
$product = new WC_Product(); $product = new WC_Product();
$this->assertContains( wc_placeholder_img_src(), $product->get_image() ); $this->assertContains( wc_placeholder_img_src(), $product->get_image() );
// Test custom class attribute is honoured.
$image = $product->get_image( 'woocommerce_thumbnail', array( 'class' => 'custom-class' ) );
$this->assertContains( 'class="custom-class"', $image );
} }
/** /**

View File

@ -885,6 +885,10 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
*/ */
public function test_wc_placeholder_img() { public function test_wc_placeholder_img() {
$this->assertTrue( (bool) strstr( wc_placeholder_img(), wc_placeholder_img_src() ) ); $this->assertTrue( (bool) strstr( wc_placeholder_img(), wc_placeholder_img_src() ) );
// Test custom class attribute is honoured.
$attr = array( 'class' => 'custom-class' );
$this->assertContains( 'class="custom-class"', wc_placeholder_img( 'woocommerce_thumbnail', $attr ) );
} }
/** /**