110 lines
4.7 KiB
PHP
110 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* create_buscar_page.php (#8) — Crea/repara la página dedicada /buscar.
|
|
* La página sirve como destino del enlace «Búsqueda avanzada» y muestra el formulario
|
|
* avanzado aunque no haya consulta activa.
|
|
*
|
|
* Idempotente de verdad:
|
|
* - Si /buscar NO existe → la crea (es) + traducciones (en/fr/it/pt) y las vincula.
|
|
* - Si /buscar YA existe → la deja, pero REPARA traducciones Polylang faltantes o
|
|
* no publicadas (crea las que falten, publica las que estén en borrador, revincula).
|
|
*
|
|
* Uso:
|
|
* wp eval-file scripts/create_buscar_page.php # DRY-RUN
|
|
* APPLY=1 wp eval-file scripts/create_buscar_page.php # aplica
|
|
*/
|
|
$apply = getenv('APPLY') === '1';
|
|
|
|
$titles = [
|
|
'es' => 'Búsqueda avanzada',
|
|
'en' => 'Advanced Search',
|
|
'fr' => 'Recherche avancée',
|
|
'it' => 'Ricerca avanzata',
|
|
'pt' => 'Pesquisa avançada',
|
|
];
|
|
// Contenido mínimo (sin bloques Gutenberg). El formulario se inyecta vía the_content.
|
|
$content = '<p>Utiliza el formulario de búsqueda avanzada para encontrar reflexiones, artículos y más.</p>';
|
|
|
|
$has_pll = function_exists('pll_set_post_language') && function_exists('pll_save_post_translations');
|
|
$pll_langs = (function_exists('pll_languages_list'))
|
|
? pll_languages_list(['fields' => 'slug'])
|
|
: ['es'];
|
|
|
|
/** Crea (o devuelve si existe) una página por slug, con idioma Polylang. */
|
|
function fea_buscar_ensure_page(string $slug, string $title, string $content, string $lang, bool $apply, bool $has_pll) {
|
|
$existing = get_page_by_path($slug, OBJECT, 'page');
|
|
if ($existing) {
|
|
// Asegurar que está publicada
|
|
if ($apply && $existing->post_status !== 'publish') {
|
|
wp_update_post(['ID' => $existing->ID, 'post_status' => 'publish']);
|
|
echo " · ({$lang}) página '{$slug}' existía en estado {$existing->post_status} → publicada (ID {$existing->ID})\n";
|
|
} else {
|
|
echo " · ({$lang}) página '{$slug}' ya existe y publicada (ID {$existing->ID})\n";
|
|
}
|
|
// Asegurar idioma asignado
|
|
if ($apply && $has_pll && function_exists('pll_get_post_language')) {
|
|
$cur = pll_get_post_language($existing->ID);
|
|
if ($cur !== $lang) { pll_set_post_language($existing->ID, $lang); echo " idioma → {$lang}\n"; }
|
|
}
|
|
return (int) $existing->ID;
|
|
}
|
|
|
|
echo ($apply ? " · ({$lang}) creando" : " · ({$lang}) [dry] crearía") . " página '{$slug}'\n";
|
|
if (!$apply) return 0;
|
|
|
|
$id = wp_insert_post([
|
|
'post_type' => 'page',
|
|
'post_status' => 'publish',
|
|
'post_name' => $slug,
|
|
'post_title' => $title,
|
|
'post_content' => $content,
|
|
'post_author' => 1,
|
|
], true);
|
|
if (is_wp_error($id)) { echo " ERROR: " . $id->get_error_message() . "\n"; return 0; }
|
|
if ($has_pll) pll_set_post_language($id, $lang);
|
|
echo " creada (ID {$id})\n";
|
|
return (int) $id;
|
|
}
|
|
|
|
echo ($apply ? "APLICANDO" : "DRY-RUN") . " — crear/reparar /buscar\n";
|
|
|
|
// 1) Página ES (slug 'buscar')
|
|
$translations = [];
|
|
$es_id = fea_buscar_ensure_page('buscar', $titles['es'], $content, 'es', $apply, $has_pll);
|
|
if ($es_id) $translations['es'] = $es_id;
|
|
|
|
// 2) Traducciones (en/fr/it/pt) sólo si Polylang activo y el idioma existe
|
|
if ($has_pll) {
|
|
foreach (['en', 'fr', 'it', 'pt'] as $lang) {
|
|
if (!in_array($lang, $pll_langs, true)) { echo " · ({$lang}) idioma no configurado en Polylang, omitido\n"; continue; }
|
|
|
|
// Si ya hay traducción vinculada a la ES, reusarla
|
|
$linked = ($es_id && function_exists('pll_get_post')) ? (int) pll_get_post($es_id, $lang) : 0;
|
|
if ($linked) {
|
|
$p = get_post($linked);
|
|
if ($p && $p->post_status !== 'publish' && $apply) {
|
|
wp_update_post(['ID' => $linked, 'post_status' => 'publish']);
|
|
echo " · ({$lang}) traducción vinculada (ID {$linked}) estaba {$p->post_status} → publicada\n";
|
|
} else {
|
|
echo " · ({$lang}) traducción ya vinculada (ID {$linked})\n";
|
|
}
|
|
$translations[$lang] = $linked;
|
|
continue;
|
|
}
|
|
|
|
// Crear/reparar por slug
|
|
$tl_id = fea_buscar_ensure_page('buscar-' . $lang, $titles[$lang], $content, $lang, $apply, $has_pll);
|
|
if ($tl_id) $translations[$lang] = $tl_id;
|
|
}
|
|
|
|
// 3) Revincular todas las traducciones
|
|
if ($apply && count($translations) > 1) {
|
|
pll_save_post_translations($translations);
|
|
echo " · traducciones revinculadas: " . implode(', ', array_map(
|
|
fn($l, $id) => "{$l}={$id}", array_keys($translations), $translations)) . "\n";
|
|
}
|
|
}
|
|
|
|
if (function_exists('wp_cache_flush')) wp_cache_flush();
|
|
echo ($apply ? "APLICADO" : "DRY-RUN") . "\n";
|