t.
*/
protected function renderList( $categories, $attributes, $uid, $depth = 0 ) {
$classes = [
'wc-block-product-categories-list',
'wc-block-product-categories-list--depth-' . absint( $depth ),
];
if ( ! empty( $attributes['hasImage'] ) ) {
$classes[] = 'wc-block-product-categories-list--has-images';
}
$output = '
' . $this->renderListItems( $categories, $attributes, $uid, $depth ) . '
';
return $output;
}
/**
* Render a list of terms.
*
* @param array $categories List of terms.
* @param array $attributes Block attributes. Default empty array.
* @param int $uid Unique ID for the rendered block, used for HTML IDs.
* @param int $depth Current depth.
* @return string Rendered output.
*/
protected function renderListItems( $categories, $attributes, $uid, $depth = 0 ) {
$output = '';
foreach ( $categories as $category ) {
$output .= '
' . $this->get_image_html( $category, $attributes ) . esc_html( $category->name ) . '
' . $this->getCount( $category, $attributes ) . '
' . ( ! empty( $category->children ) ? $this->renderList( $category->children, $attributes, $uid, $depth + 1 ) : '' ) . '
';
}
return $output;
}
/**
* Returns the category image html
*
* @param \WP_Term $category Term object.
* @param array $attributes Block attributes. Default empty array.
* @param string $size Image size, defaults to 'woocommerce_thumbnail'.
* @return string
*/
public function get_image_html( $category, $attributes, $size = 'woocommerce_thumbnail' ) {
if ( empty( $attributes['hasImage'] ) ) {
return '';
}
$image_id = get_term_meta( $category->term_id, 'thumbnail_id', true );
if ( ! $image_id ) {
return '' . wc_placeholder_img( 'woocommerce_thumbnail' ) . '';
}
return '' . wp_get_attachment_image( $image_id, 'woocommerce_thumbnail' ) . '';
}
/**
* Get the count, if displaying.
*
* @param object $category Term object.
* @param array $attributes Block attributes. Default empty array.
* @return string
*/
protected function getCount( $category, $attributes ) {
if ( empty( $attributes['hasCount'] ) ) {
return '';
}
if ( $attributes['isDropdown'] ) {
return '(' . absint( $category->count ) . ')';
}
$screen_reader_text = sprintf(
/* translators: %s number of products in cart. */
_n( '%d product', '%d products', absint( $category->count ), 'woocommerce' ),
absint( $category->count )
);
return ''
. '' . absint( $category->count ) . ''
. '' . esc_html( $screen_reader_text ) . ''
. '';
}
}