*/
private const FIELD_CAPS = array(
'meta_title' => 100,
'product_subtitle' => 500,
'product_brand' => 200,
'client_category_identifier' => 200,
);
/**
* Truncate a meta value to the middleware Postgres column cap for the given field key.
*
* Uses mb_substr with an explicit UTF-8 encoding to count characters rather than
* bytes, regardless of the process-level mb_internal_encoding() setting.
* Fields not listed in FIELD_CAPS are returned unchanged.
*
* @param string $value Raw meta value.
* @param string $field_key Middleware field key (e.g. 'meta_title').
* @return string Truncated value, or original value when no cap is defined.
*/
public static function truncate_meta( string $value, string $field_key ): string {
$cap = self::FIELD_CAPS[ $field_key ] ?? null;
if ( null === $cap ) {
return $value;
}
return mb_substr( $value, 0, $cap, 'UTF-8' );
}
/**
* Strip HTML from a string while converting block-level elements to newlines.
*
* Builders (Elementor, Divi, Bricks, etc.) read post_excerpt as plain text
* and do not pass it through wp_autop / the_excerpt filters, so HTML written
* there renders as literal tags on the frontend.
*
* This method converts
,
,
, list, and heading tags to newlines * before stripping all remaining HTML, so content does not run together * (e.g. "Foo
Bar
" becomes "Foo\n\nBar"). * * @param string $html The HTML string to sanitize. * @return string Plain-text string with block-level breaks preserved as newlines. */ public static function html_to_plain_text( string $html ): string { if ( '' === $html || ! preg_match( '#?[a-z][^>]*>#i', $html ) ) { return $html; } // Convert
(including attributed variants) to single newline. $text = preg_replace( '#
]*/?\s*>#i', "\n", $html ); // Convert block-level open/close tags to newlines. $text = preg_replace( '#?(p|div|li|ul|ol|h[1-6]|blockquote)[^>]*>#i', "\n", $text ); // Strip any remaining HTML. $text = wp_strip_all_tags( $text ); // Collapse runs of 3+ newlines to a double newline. $text = preg_replace( '/\n{3,}/', "\n\n", $text ); return trim( $text ); } }