Automattic\WooCommerce\Blocks\BlockTypes
ProductFilterTaxonomy::render │ protected │ WC 1.0
Render the block.
Method of the class: ProductFilterTaxonomy{}
No Hooks.
Returns
String. Rendered block type output.
Usage
// protected - for code of main (parent) or child class $result = $this->render( $block_attributes, $content, $block );
- $block_attributes(array) (required)
- Block attributes.
- $content(string) (required)
- Block content.
- $block(WP_Block) (required)
- Block instance.
ProductFilterTaxonomy::render() ProductFilterTaxonomy::render code WC 10.3.6
protected function render( $block_attributes, $content, $block ) {
// Skip rendering in admin or during AJAX requests.
if ( is_admin() || wp_doing_ajax() || empty( $block_attributes['taxonomy'] ) ) {
return '';
}
$taxonomy = $block_attributes['taxonomy'];
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! $taxonomy_object || ! taxonomy_exists( $taxonomy ) ) {
return '';
}
// Validate that this taxonomy is configured in the parameter map.
$container = wc_get_container();
$params_handler = $container->get( \Automattic\WooCommerce\Internal\ProductFilters\Params::class );
$taxonomy_params = $params_handler->get_param( 'taxonomy' );
if ( ! isset( $taxonomy_params[ $taxonomy ] ) ) {
return '';
}
// Pass taxonomy parameter mapping to frontend via interactivity config.
wp_interactivity_config(
'woocommerce/product-filters',
array(
'taxonomyParamsMap' => $taxonomy_params,
)
);
$filter_context = array(
'showCounts' => $block_attributes['showCounts'] ?? false,
'items' => array(),
'groupLabel' => $taxonomy_object->labels->singular_name,
);
$taxonomy_counts = $this->get_taxonomy_term_counts( $block, $taxonomy );
if ( ! empty( $taxonomy_counts ) ) {
$hide_empty = $block_attributes['hideEmpty'] ?? true;
$orderby = $block_attributes['sortOrder'] ? explode( '-', $block_attributes['sortOrder'] )[0] : 'name';
$order = $block_attributes['sortOrder'] ? strtoupper( explode( '-', $block_attributes['sortOrder'] )[1] ) : 'DESC';
$taxonomy_terms = $this->get_sorted_terms( $taxonomy, $taxonomy_counts, $hide_empty, $orderby, $order );
if ( is_wp_error( $taxonomy_terms ) ) {
return '';
}
// Get selected terms from filter params.
$filter_params = $block->context['filterParams'] ?? array();
$selected_terms = array();
$param_key = $taxonomy_params[ $taxonomy ];
if ( $filter_params && ! empty( $filter_params[ $param_key ] ) && is_string( $filter_params[ $param_key ] ) ) {
$selected_terms = array_filter( array_map( 'sanitize_title', explode( ',', $filter_params[ $param_key ] ) ) );
}
$taxonomy_options = array_map(
function ( $term ) use ( $taxonomy_counts, $selected_terms, $taxonomy ) {
$term = (array) $term;
$term['count'] = $taxonomy_counts[ $term['term_id'] ] ?? 0;
$option = array(
'label' => $term['name'],
'value' => $term['slug'],
'selected' => in_array( $term['slug'], $selected_terms, true ),
'count' => $term['count'],
'type' => 'taxonomy/' . $taxonomy,
);
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
$option['id'] = $term['term_id'];
if ( isset( $term['depth'] ) && $term['depth'] > 0 ) {
$option['depth'] = $term['depth'];
}
if ( isset( $term['parent'] ) && $term['parent'] > 0 ) {
$option['parent'] = $term['parent'];
}
}
return $option;
},
$taxonomy_terms
);
$filter_context['items'] = $taxonomy_options;
}
$wrapper_attributes = array(
'data-wp-interactive' => 'woocommerce/product-filters',
'data-wp-key' => wp_unique_prefixed_id( $this->get_block_type() ),
'data-wp-context' => wp_json_encode(
array(
'activeLabelTemplate' => $taxonomy_object->labels->singular_name . ': {{label}}',
'filterType' => 'taxonomy/' . $taxonomy,
),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
),
);
if ( empty( $filter_context['items'] ) ) {
$wrapper_attributes['hidden'] = true;
$wrapper_attributes['class'] = 'wc-block-product-filter--hidden';
}
return sprintf(
'<div %1$s>%2$s</div>',
get_block_wrapper_attributes( $wrapper_attributes ),
array_reduce(
$block->parsed_block['innerBlocks'],
function ( $carry, $parsed_block ) use ( $filter_context ) {
$carry .= ( new \WP_Block( $parsed_block, array( 'filterData' => $filter_context ) ) )->render();
return $carry;
},
''
)
);
}