prefix . 'aitmt_translations'; } private static function table_sql(): string { $table = self::table(); if (!preg_match('/^[A-Za-z0-9_]+$/', $table)) { return ''; } return esc_sql($table); } public static function normalize_text(string $text): string { if (function_exists('aitmt_fallback_normalize_text')) { return aitmt_fallback_normalize_text($text); } $text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8'); $text = preg_replace('/\s+/u', ' ', trim($text)); return null === $text ? '' : $text; } public static function hash(string $text): string { return hash('sha256', self::normalize_text($text)); } public static function get_existing_map(array $segments, string $source_language, string $target_language, string $context = 'html'): array { global $wpdb; if (empty($segments)) { return array(); } $hashes = array_keys($segments); $placeholders = implode(',', array_fill(0, count($hashes), '%s')); $args = array_merge($hashes, array($source_language, $target_language, $context)); $table = self::table_sql(); if ('' === $table) { return array(); } // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- Dynamic IN placeholders and table name are controlled above; all values are prepared. $prepared = $wpdb->prepare( "SELECT source_hash, translated_text FROM {$table} WHERE source_hash IN ({$placeholders}) AND source_language = %s AND target_language = %s AND context = %s AND status IN ('published', 'manual', 'import', 'draft')", ...$args ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber $rows = $wpdb->get_results($prepared, ARRAY_A); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Prepared immediately above. $map = array(); foreach ((array) $rows as $row) { $map[$row['source_hash']] = $row['translated_text']; } return $map; } public static function save(string $source_text, string $translated_text, string $source_language, string $target_language, string $context = 'html', string $status = 'published', string $provider = 'manual', $object_id = null, string $source_type = 'string', string $field_key = ''): bool { global $wpdb; $source_text = self::normalize_text($source_text); $translated_text = trim($translated_text); if ('' === $source_text || '' === $translated_text) { return false; } $now = current_time('mysql'); $table = self::table_sql(); if ('' === $table) { return false; } $source_type = function_exists('aitmt_fallback_normalize_source_type') ? aitmt_fallback_normalize_source_type($source_type) : sanitize_key($source_type); if ('' === $source_type) { return false; } $sql = $wpdb->prepare( "INSERT INTO {$table} (source_hash, source_language, target_language, context, object_id, field_key, source_type, source_text, translated_text, status, provider, created_at, updated_at) VALUES (%s, %s, %s, %s, %d, %s, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE translated_text = CASE WHEN VALUES(status) IN ('manual', 'import') THEN VALUES(translated_text) WHEN status IN ('published', 'manual', 'import', 'draft') AND translated_text <> '' THEN translated_text ELSE VALUES(translated_text) END, status = CASE WHEN VALUES(status) IN ('manual', 'import') THEN VALUES(status) WHEN status IN ('published', 'manual', 'import', 'draft') AND translated_text <> '' THEN status ELSE VALUES(status) END, provider = CASE WHEN VALUES(status) IN ('manual', 'import') THEN VALUES(provider) WHEN status IN ('published', 'manual', 'import', 'draft') AND translated_text <> '' THEN provider ELSE VALUES(provider) END, source_type = CASE WHEN source_type = '' OR source_type = 'unknown' THEN VALUES(source_type) ELSE source_type END, object_id = CASE WHEN object_id = 0 THEN VALUES(object_id) ELSE object_id END, field_key = CASE WHEN field_key = '' THEN VALUES(field_key) ELSE field_key END, updated_at = VALUES(updated_at)", self::hash($source_text), AITMT_Languages::normalize_code($source_language), AITMT_Languages::normalize_code($target_language), sanitize_key($context), absint($object_id), sanitize_key($field_key), $source_type, $source_text, $translated_text, sanitize_key($status), sanitize_key($provider), $now, $now ); $result = $wpdb->query($sql); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Query is prepared above and table name is validated. return false !== $result; } public static function save_batch(array $source_segments, array $translated_segments, string $source_language, string $target_language, string $context = 'html', string $status = 'published', string $provider = 'openai') { foreach ($translated_segments as $hash => $translation) { if (!isset($source_segments[$hash])) { continue; } self::save($source_segments[$hash], (string) $translation, $source_language, $target_language, $context, $status, $provider); } } }