$1', $escaped );
$escaped = preg_replace( '/__(.+?)__/s', '$1', $escaped );
// Italic (look-arounds prevent matching inside bold markers)
$escaped = preg_replace( '/(?$1', $escaped );
$escaped = preg_replace( '/(?$1', $escaped );
// Inline code
$escaped = preg_replace( '/`([^`]+)`/', '$1', $escaped );
// Markdown links [text](url) – only http/https URLs
$escaped = preg_replace(
'/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/',
'$1',
$escaped
);
// ── Block-level conversion (headings, lists, paragraphs). ──
$lines = explode( "\n", $escaped );
$html_lines = [];
$list_stack = []; // Tracks nesting: e.g. ['ol', 'ul']
$close_all = static function () use ( &$html_lines, &$list_stack ) {
while ( ! empty( $list_stack ) ) {
$html_lines[] = '' . array_pop( $list_stack ) . '>';
}
};
foreach ( $lines as $line ) {
$trimmed = trim( $line );
// Blank line → close any open lists.
if ( $trimmed === '' ) {
$close_all();
continue;
}
// ── Headings ──
if ( preg_match( '/^(#{1,6})\s+(.+)$/', $trimmed, $hm ) ) {
$close_all();
$level = strlen( $hm[1] );
$html_lines[] = "
' . $trimmed . '
'; } $close_all(); return wp_kses( implode( "\n", $html_lines ), $allowed_html ); } } if ( ! function_exists( 'ai_app_onsite_get_response_allowed_html' ) ) { /** * Return the wp_kses allowed-HTML array used by the response formatter. * * Centralised so that email, stats, and PDF paths use the same allowlist. * * @return array */ function ai_app_onsite_get_response_allowed_html(): array { return [ 'a' => [ 'href' => true, 'target' => true, 'rel' => true ], 'strong' => [], 'b' => [], 'em' => [], 'i' => [], 'u' => [], 'br' => [], 'p' => [], 'ul' => [], 'ol' => [], 'li' => [], 'blockquote' => [], 'h1' => [], 'h2' => [], 'h3' => [], 'h4' => [], 'h5' => [], 'h6' => [], 'code' => [], ]; } } if ( ! function_exists( 'ai_app_onsite_apply_anchor_links' ) ) { /** * Replace configured anchor-text keywords in HTML with links. * * @param string $html Formatted HTML string. * @param array $anchor_texts Array of ['anchor_text' => …, 'link_url' => …]. * @return string HTML with anchor links applied. */ function ai_app_onsite_apply_anchor_links( string $html, array $anchor_texts ): string { if ( empty( $anchor_texts ) ) { return $html; } foreach ( $anchor_texts as $map ) { if ( empty( $map['anchor_text'] ) || empty( $map['link_url'] ) ) { continue; } $anchor = preg_quote( $map['anchor_text'], '/' ); $url = esc_url( $map['link_url'] ); $html = preg_replace_callback( '/' . $anchor . '/i', static function ( $matches ) use ( $url ) { return '' . $matches[0] . ''; }, $html ); } return $html; } } if ( ! function_exists( 'ai_app_onsite_truncate_words' ) ) { /** * Truncate plain text to a given word count with ellipsis. * * @param string $text Raw text (tags are stripped internally). * @param int $word_limit Maximum number of words. * @return string Truncated plain text. */ function ai_app_onsite_truncate_words( string $text, int $word_limit = 100 ): string { $text = wp_strip_all_tags( $text ); if ( $word_limit < 1 ) { return trim( $text ); } $tokens = preg_split( '/(\s+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); if ( ! $tokens ) { return trim( $text ); } $count = 0; $output = ''; foreach ( $tokens as $token ) { if ( trim( $token ) === '' ) { $output .= $token; continue; } $count++; if ( $count > $word_limit ) { $output = rtrim( $output ) . ' ...'; break; } $output .= $token; } return trim( $output ); } }