]*>(.*?)<\/\1>/is', $content);
if ($containsHtml) {
$tags = [];
$content = preg_replace_callback('/<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>/is', function($matches) use (&$tags) {
$placeholder = '##HTML_TAG_' . count($tags) . '##';
$tags[$placeholder] = $matches[0];
return $placeholder;
}, $content);
}
$content = self::formatVariables($content);
$direct_replacements = [
'"\\u2013"' => '"-"',
'"\\u2019"' => '"\'',
'\\u2013' => '-',
'\\u2019' => '\'',
'\u2013' => '-',
'\u2019' => '\'',
"\\u2013" => "-",
"\\u2019" => "'"
];
foreach ($direct_replacements as $search => $replace) {
$content = str_replace($search, $replace, $content);
}
$content = preg_replace_callback('/(?:\\\\u|\\\\\\\\u)([0-9a-fA-F]{4})/', function($matches) {
return html_entity_decode('' . $matches[1] . ';', ENT_QUOTES, 'UTF-8');
}, $content);
$specialChars = [
'\u2019' => "'",
'\u2018' => "'",
'\u201C' => '"',
'\u201D' => '"',
'\u2013' => '-',
'\u2014' => '--',
'\u00A0' => ' ',
'\u00AD' => '-',
'\u2026' => '...',
];
foreach ($specialChars as $unicodeChar => $replacement) {
$content = str_replace($unicodeChar, $replacement, $content);
}
$content = preg_replace('/[-\*]?\s*(Attributes|Categories|Tags|Price):\s*$/m', '', $content);
$content = preg_replace('/[-\*]?\s*(Attributes|Categories|Tags|Price):\s*\n/m', '', $content);
if ($repairMarkdown) {
$content = preg_replace('/\*\*(.+?)\*\*/s', '$1', $content);
$content = preg_replace('/\*(?!\s)([^*\n]+?)(? '-',
'\u2019' => "'",
'\\u2013' => '-',
'\\u2019' => "'"
];
foreach ($final_replacements as $search => $replace) {
$content = str_replace($search, $replace, $content);
}
if ($containsHtml && !empty($tags)) {
foreach ($tags as $placeholder => $originalTag) {
$content = str_replace($placeholder, $originalTag, $content);
}
}
return trim($content);
}
/**
* Değişkenleri formatlayarak ön ekler ekleyen fonksiyon
*
* Örnek: {PRICE} değişkenini Price: {PRICE} şeklinde formatlar.
* Eğer değişken boş ise hiçbir ön ek eklemez.
*
* @param string $content Formatlama yapılacak içerik
* @param array $replacements Değişkenlerin gerçek değerlerini içeren assoc array (opsiyonel)
* @return string Değişkenleri formatlanmış içerik
*/
public static function formatVariables(string $content, array $replacements = []): string {
preg_match_all('/{([A-Za-z0-9_]+)}/', $content, $matches);
if (empty($matches[0])) {
return $content;
}
foreach ($matches[0] as $index => $fullMatch) {
$variableName = $matches[1][$index];
$variableValue = "";
if (isset($replacements[$fullMatch])) {
$variableValue = $replacements[$fullMatch];
} else {
$variableValue = $fullMatch;
}
if (empty($variableValue) || $variableValue === $fullMatch) {
continue;
}
$systemVariables = ['max_length', 'max_short_length', 'language', 'style', 'title'];
if (in_array(strtolower($variableName), $systemVariables, true)) {
$content = str_replace($fullMatch, $variableValue, $content);
continue;
}
$formattedName = implode(' ', array_map('ucfirst', array_map('strtolower', explode('_', $variableName))));
$replacement = $formattedName . ': ' . $variableValue;
$content = str_replace($fullMatch, $replacement, $content);
}
return $content;
}
/**
* AI'dan gelen metni WordPress editörü için düzgün formatlama
*
* @param string $text AI'dan gelen ham metin
* @return string WordPress editörü için formatlanmış metin
*/
public static function formatAiResponseForWordPress($text) {
$text = preg_replace('/^\s*```[a-zA-Z]*\s*\n/', '', $text);
$text = preg_replace('/\n\s*```\s*$/', '', $text);
$trimmed = ltrim( $text );
if ( '' !== $trimmed && '<' === $trimmed[0] && preg_match( '/^<[a-zA-Z][a-zA-Z0-9]*\b/', $trimmed ) ) {
$first_gt = strpos( $trimmed, '>', 0 );
$first_lt = strpos( $trimmed, '<', 1 );
if ( false === $first_gt || ( false !== $first_lt && $first_lt < $first_gt ) ) {
$text = ltrim( $text );
$text = substr( $text, 1 );
}
}
$hasHtml = preg_match('/<[a-zA-Z][^>]*>/', $text);
if ($hasHtml) {
return wp_kses_post($text);
}
$lines = explode("\n", $text);
$in_list = false;
$list_type = '';
$formatted_lines = [];
foreach ($lines as $line) {
$line = preg_replace('/```[a-zA-Z]*/', '', $line);
$line = preg_replace('/```/', '', $line);
$trimmed = trim($line);
if (!empty($trimmed)) {
$trimmed = sanitize_text_field($trimmed);
if (preg_match('/^(#+)\s+(.+)$/', $trimmed, $matches)) {
$headerLevel = strlen($matches[1]);
if ($headerLevel >= 1 && $headerLevel <= 3) {
if ($in_list) {
$in_list = false;
$formatted_lines[] = $list_type === 'ul' ? '' : '';
$list_type = '';
}
$formatted_lines[] = '
' . self::formatInlineMarkdown(esc_html($trimmed)) . '
'; } } } elseif (empty($trimmed) && !empty($line)) { $formatted_lines[] = '