Automattic\WooCommerce\Blocks\Utils

ProductDescriptionUtils::guarded_format │ public static │ WC 1.0

Format an accessible product description while preventing same-product recursion.

Method of the class: ProductDescriptionUtils{}

No Hooks.

Returns

string. Formatted product description.

Usage

$result = ProductDescriptionUtils::guarded_format( $product, $format_callback ): string;
$product(WC_Product) (required)
Product object.
$format_callback(callable) (required)
Callback that formats the product description.

ProductDescriptionUtils::guarded_format() code WC 11.3.0

public static function guarded_format( \WC_Product $product, callable $format_callback ): string {
	$product_id = $product->get_id();

	if ( ! self::is_description_accessible( $product ) || isset( self::$formatting_product_descriptions[ $product_id ] ) ) {
		return '';
	}

	self::$formatting_product_descriptions[ $product_id ] = true;

	try {
		$result = $format_callback();

		return is_string( $result ) ? $result : '';
	} finally {
		unset( self::$formatting_product_descriptions[ $product_id ] );
	}
}