Automattic\WooCommerce\Blocks\BlockTypes
ProductFilterTaxonomy::flatten_terms_list
Flatten hierarchical term tree into flat array maintaining depth-first order.
Method of the class: ProductFilterTaxonomy{}
No Hooks.
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->flatten_terms_list( $terms, $result, $visited_ids, $depth );
- $terms(array) (required)
- Hierarchical terms with children structure.
- $result(array) (required) (passed by reference — &)
- Reference to result array being built.
- $visited_ids(array) (passed by reference — &)
- Reference to array tracking visited term IDs to prevent circular references.
Default: array() - $depth(int)
- Current recursion depth for bounds checking.
ProductFilterTaxonomy::flatten_terms_list() ProductFilterTaxonomy::flatten terms list code WC 10.3.6
private function flatten_terms_list( $terms, &$result, &$visited_ids = array(), $depth = 0 ) {
/**
* This is the safeguard to prevent the memory limit issue. We choose 10 as it
* should cover most of the cases. Typical e-commerce stores have two or three
* levels of category. Extreme cases like Amazon has about 7 levels.
*
* @see https://github.com/woocommerce/woocommerce/pull/60142/files#r2250287050
*/
if ( $depth > 10 ) {
return;
}
if ( ! is_array( $terms ) ) {
return;
}
foreach ( $terms as $term ) {
// Validate term structure.
if ( ! is_array( $term ) || ! isset( $term['term_id'] ) ) {
continue;
}
$term_id = $term['term_id'];
// Prevent circular references.
if ( isset( $visited_ids[ $term_id ] ) ) {
continue;
}
$visited_ids[ $term_id ] = true;
$result[ $term_id ] = $term;
if ( ! empty( $term['children'] ) && is_array( $term['children'] ) ) {
$this->flatten_terms_list( $term['children'], $result, $visited_ids, $depth + 1 );
unset( $result[ $term_id ]['children'] );
}
}
}