Files
feadulta/scripts/fix_carta_content_links.php
T

66 lines
3.2 KiB
PHP

<?php
/**
* Arregla los enlaces internos de la carta que apuntan a contenido com_content
* de Joomla (multimedia, vídeos, cantoral...) con forma legacy
* es/<seccion>/<joomla_content_id>-<slug>.html
* que fix_carta_joomla_links.php NO mapea (solo trata /item/<k2>-...). Resuelve
* el número (id de contenido Joomla) por meta `_fgj2wp_old_content_id` y, en su
* defecto, `_fgj2wp_old_id` (contenido migrado en el bulk original) → permalink
* WP en el idioma de cada carta (degrada a ES si no hay traducción).
*
* Deja intactos los enlaces absolutos a feadulta.com (navegación externa) y los
* índices de sección (tablon-de-anuncios.html, noticias-de-alcance.html, etc.).
*
* Uso: CARTA=<es_id> php fix_carta_content_links.php (dry-run)
* APPLY=1 CARTA=<es_id> php fix_carta_content_links.php
*/
require getenv('FEA_WP_LOAD') ?: '/var/www/html/wp-load.php';
global $wpdb;
$APPLY = getenv('APPLY') === '1';
$CARTA = (int)(getenv('CARTA') ?: 0);
if (!$CARTA) { fwrite(STDERR, "Falta CARTA=<es_id>\n"); exit(1); }
$BAK = "/tmp/fix_carta_content_bak"; if ($APPLY) @mkdir($BAK, 0777, true);
function content_es_post($jid) {
global $wpdb;
foreach (['_fgj2wp_old_content_id', '_fgj2wp_old_id'] as $mk) {
$pid = $wpdb->get_var($wpdb->prepare(
"SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key=%s AND meta_value=%s LIMIT 1", $mk, (string)$jid));
if ($pid) return (int)$pid;
}
return 0;
}
$tot = 0;
foreach (pll_get_post_translations($CARTA) as $lang => $pid) {
$post = get_post($pid); if (!$post) continue;
$chg = 0; $miss = [];
$new = preg_replace_callback('~href="([^"]+)"~i', function($m) use ($lang, &$chg, &$miss) {
$href = html_entity_decode(trim($m[1]));
if (stripos($href, '.html') === false) return $m[0]; // solo legacy .html
if (stripos($href, 'feadulta.com') !== false) return $m[0]; // absoluto externo → dejar
if (stripos($href, '/item/') !== false) return $m[0]; // K2 lo trata otro script
if (!preg_match('~/(\d+)-[^/"]+\.html$~i', $href, $mm)) return $m[0]; // necesita <id>-slug.html
$es = content_es_post((int)$mm[1]);
if (!$es) { $miss[] = $href; return $m[0]; }
$t = function_exists('pll_get_post') ? (pll_get_post($es, $lang) ?: $es) : $es;
$url = get_permalink($t);
if (!$url || strpos($url, '?p=') !== false) return $m[0];
$chg++;
return 'href="' . esc_url($url) . '"';
}, $post->post_content);
printf("#%d [%s] «%s» — %d enlaces de contenido%s\n", $pid, $lang, mb_substr($post->post_title,0,26), $chg,
$miss ? (" | sin mapear: " . implode(", ", array_slice($miss,0,3))) : "");
$tot += $chg;
if ($APPLY && $chg) {
file_put_contents("$BAK/$pid.html", $post->post_content);
wp_update_post(['ID'=>$pid, 'post_content'=>$new]);
clean_post_cache($pid);
}
}
if ($APPLY) {
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_fea_carta_sections_%'");
$wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_timeout_fea_carta_sections_%'");
}
echo ($APPLY ? "APLICADO" : "DRY-RUN") . ": $tot enlaces.\n";