wc_get_product_attachment_props()
Gets data about an attachment, such as alt text and captions.
Хуки из функции
Возвращает
Массив.
Использование
wc_get_product_attachment_props( $attachment_id, $product );
- $attachment_id(int|null)
- Attachment ID.
По умолчанию:null - $product(WC_Product|true|false)
- WC_Product object.
По умолчанию:false
Список изменений
| С версии 2.6.0 | Введена. |
Код wc_get_product_attachment_props() wc get product attachment props WC 10.7.0
function wc_get_product_attachment_props( $attachment_id = null, $product = false ) {
$props = array(
'title' => '',
'caption' => '',
'url' => '',
'alt' => '',
'src' => '',
'srcset' => false,
'sizes' => false,
);
$attachment = get_post( $attachment_id );
if ( $attachment && 'attachment' === $attachment->post_type ) {
$props['title'] = wp_strip_all_tags( $attachment->post_title );
$props['caption'] = wp_strip_all_tags( $attachment->post_excerpt );
$props['url'] = wp_get_attachment_url( $attachment_id );
// Alt text.
$alt_text = array( wp_strip_all_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ), $props['caption'], wp_strip_all_tags( $attachment->post_title ) );
if ( $product && $product instanceof WC_Product ) {
$alt_text[] = wp_strip_all_tags( get_the_title( $product->get_id() ) );
}
$alt_text = array_filter( $alt_text );
$props['alt'] = $alt_text ? reset( $alt_text ) : '';
/**
* Filters the size for the full gallery image.
*
* @param string $size Image size name.
*
* @since 2.6.0
*/
$full_size = apply_filters( 'woocommerce_gallery_full_size', apply_filters( 'woocommerce_product_thumbnails_large_size', 'full' ) );
$src = wp_get_attachment_image_src( $attachment_id, $full_size );
$props['full_src'] = $src[0] ?? null;
$props['full_src_w'] = $src[1] ?? null;
$props['full_src_h'] = $src[2] ?? null;
$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
/**
* Filters the size for the gallery thumbnail.
*
* @param array $size Array containing width and height dimensions.
*
* @since 2.6.0
*/
$gallery_thumbnail_size = apply_filters( 'woocommerce_gallery_thumbnail_size', array( $gallery_thumbnail['width'], $gallery_thumbnail['height'] ) );
$src = wp_get_attachment_image_src( $attachment_id, $gallery_thumbnail_size );
$props['gallery_thumbnail_src'] = $src[0] ?? null;
$props['gallery_thumbnail_src_w'] = $src[1] ?? null;
$props['gallery_thumbnail_src_h'] = $src[2] ?? null;
/**
* Filters the thumbnail size.
*
* @param string $size Image size name.
*
* @since 2.6.0
*/
$thumbnail_size = apply_filters( 'woocommerce_thumbnail_size', 'woocommerce_thumbnail' );
$src = wp_get_attachment_image_src( $attachment_id, $thumbnail_size );
$props['thumb_src'] = $src[0] ?? null;
$props['thumb_src_w'] = $src[1] ?? null;
$props['thumb_src_h'] = $src[2] ?? null;
/**
* Filters the size for the gallery image.
*
* @param string $size Image size name.
*
* @since 2.6.0
*/
$image_size = apply_filters( 'woocommerce_gallery_image_size', 'woocommerce_single' );
$src = wp_get_attachment_image_src( $attachment_id, $image_size );
$props['src'] = $src[0] ?? null;
$props['src_w'] = $src[1] ?? null;
$props['src_h'] = $src[2] ?? null;
$props['srcset'] = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $attachment_id, $image_size ) : false;
$props['sizes'] = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $attachment_id, $image_size ) : false;
}
return $props;
}