Automattic\WooCommerce\EmailEditor\Integrations\Utils
Html_Processing_Helper::normalize_rel_attribute
Normalize rel attribute by lowercasing, deduplicating tokens, and ensuring required tokens.
Method of the class: Html_Processing_Helper{}
No Hooks.
Returns
String. Normalized rel attribute value.
Usage
$result = Html_Processing_Helper::normalize_rel_attribute( ?string $rel_value, $require_security_tokens ): string;
- ?string $rel_value(required)
- .
- $require_security_tokens(true|false)
- Whether to require noopener and noreferrer tokens.
Default:false
Html_Processing_Helper::normalize_rel_attribute() Html Processing Helper::normalize rel attribute code WC 10.4.3
private static function normalize_rel_attribute( ?string $rel_value, bool $require_security_tokens = false ): string {
$allowed_tokens = array( 'noopener', 'noreferrer', 'nofollow', 'external' );
$required_tokens = $require_security_tokens ? array( 'noopener', 'noreferrer' ) : array();
// If no rel value and no required tokens, return empty.
if ( null === $rel_value && empty( $required_tokens ) ) {
return '';
}
// Start with required tokens.
$tokens = $required_tokens;
// If rel value exists, parse and normalize it.
if ( null !== $rel_value ) {
$existing_tokens = preg_split( '/\s+/', trim( $rel_value ) );
if ( false !== $existing_tokens ) {
// Normalize existing tokens: lowercase, remove empty, filter allowed.
$normalized_existing = array_filter(
array_map( 'strtolower', $existing_tokens ),
function ( $token ) use ( $allowed_tokens ) {
return ! empty( $token ) && in_array( $token, $allowed_tokens, true );
}
);
// Merge with required tokens, removing duplicates.
$tokens = array_unique( array_merge( $tokens, $normalized_existing ) );
}
}
// Return normalized rel attribute or empty string if no valid tokens.
return empty( $tokens ) ? '' : implode( ' ', $tokens );
}