Plugins bloques actualizados

This commit is contained in:
Jose Ibáñez
2026-02-14 18:21:10 +01:00
parent 26d7f11378
commit cc68c21d9a
7 changed files with 315 additions and 280 deletions
@@ -16,7 +16,7 @@ if (!defined('ABSPATH')) {
class Bloques_Actuaciones_Importer {
private const CSV_DELIMITER = ';';
private const CSV_DELIMITER_FALLBACK = ';';
private const NOMINATIM_DELAY_US = 1100000; // 1.1 s
private const NOMINATIM_USER_AGENT = 'BloquesTransicion/1.0 (WordPress; tangente.coop)';
@@ -411,6 +411,44 @@ class Bloques_Actuaciones_Importer {
/* ── CSV parser ── */
/**
* Detectar el delimitador del CSV analizando la primera línea.
* Prueba con ';' y ',' y elige el que produce más columnas.
*/
private static function detect_delimiter($path) {
$handle = fopen($path, 'r');
if (!$handle) {
return self::CSV_DELIMITER_FALLBACK;
}
// Saltar BOM
$bom = fread($handle, 3);
if ($bom !== "\xEF\xBB\xBF") {
rewind($handle);
}
$first_line = fgets($handle);
fclose($handle);
if ($first_line === false) {
return self::CSV_DELIMITER_FALLBACK;
}
$candidates = [',', ';', "\t"];
$best = self::CSV_DELIMITER_FALLBACK;
$best_count = 0;
foreach ($candidates as $delim) {
$count = count(str_getcsv($first_line, $delim));
if ($count > $best_count) {
$best_count = $count;
$best = $delim;
}
}
return $best;
}
private static function parse_csv($path, &$errors = []) {
$handle = fopen($path, 'r');
if (!$handle) {
@@ -424,7 +462,10 @@ class Bloques_Actuaciones_Importer {
rewind($handle);
}
$header = fgetcsv($handle, 0, self::CSV_DELIMITER);
// Auto-detectar delimitador
$delimiter = self::detect_delimiter($path);
$header = fgetcsv($handle, 0, $delimiter);
if ($header === false || empty($header)) {
fclose($handle);
$errors[] = __('Cabecera CSV no válida.', 'bloques-actuaciones-import');
@@ -434,7 +475,7 @@ class Bloques_Actuaciones_Importer {
$header = array_map('trim', $header);
$rows = [];
while (($raw = fgetcsv($handle, 0, self::CSV_DELIMITER)) !== false) {
while (($raw = fgetcsv($handle, 0, $delimiter)) !== false) {
$padded = array_pad($raw, count($header), '');
$row = array_combine($header, array_slice($padded, 0, count($header)));
if (is_array($row)) {