Automattic\WooCommerce\Admin

PluginsHelper::normalize_reason │ private static │ WC 11.2.0

Make a reason safe to show, store and transmit.

Reasons come from WordPress and from values that reach us through filters, so they can carry markup or be arbitrarily long. WordPress also writes some messages as multiple paragraphs, so block boundaries become spaces first: stripping the tags would otherwise run the last word of one paragraph into the first word of the next.

Method of the class: PluginsHelper{}

No Hooks.

Returns

string. The reason, HTML-stripped, whitespace-collapsed and length-capped.

Usage

$result = PluginsHelper::normalize_reason( $text ): string;
$text(string) (required)
The raw reason.

Changelog

Since 11.2.0 Introduced.

PluginsHelper::normalize_reason() code WC 11.2.1

private static function normalize_reason( string $text ): string {
	$spaced = preg_replace( '#<(?:/p|/div|/li|/h[1-6]|br\s*/?)>#i', ' ', $text ) ?? $text;
	$reason = trim( wp_strip_all_tags( $spaced, true ) );

	if ( mb_strlen( $reason ) > self::MAX_ERROR_REASON_LENGTH ) {
		$reason = trim( mb_substr( $reason, 0, self::MAX_ERROR_REASON_LENGTH ) ) . "\u{2026}";
	}

	return $reason;
}