- Formulario [ga_final_feliz]: las familias envían texto y foto (solo imágenes reales ≤5 MB, límite de envíos, consentimiento de publicación). Queda pendiente hasta que el equipo la publica; aviso por correo y contador en el menú. - Sección «Finales felices»: historias aprobadas o, si no hay, los adoptados. - Casilla «Busca padrino o madrina», sección «Apadrina» y motivo de contacto (sin pagos). - Páginas «Finales felices / Amaiera zoriontsuak» creadas como borrador. - API: profile.sponsorable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
360 lines
16 KiB
PHP
360 lines
16 KiB
PHP
<?php
|
|
/**
|
|
* Contenido bilingüe (castellano / euskera): idiomas, páginas, especies y menús.
|
|
*
|
|
* Idempotente: sirve para la instalación inicial y para las migraciones.
|
|
* - Las páginas que falten se crean.
|
|
* - Las páginas existentes solo se actualizan si nadie las ha editado
|
|
* (se guarda una huella del contenido original en _ga_seed_hash).
|
|
*
|
|
* Uso: wp eval-file setup-content.php (GA_SEED_MENUS=true para rehacer los menús)
|
|
*/
|
|
|
|
$src = ( getenv( 'GA_SRC' ) ?: '/usr/src/gure-ametsa' ) . '/content';
|
|
$pll = function_exists( 'pll_set_post_language' ) && function_exists( 'PLL' );
|
|
|
|
if ( ! $pll ) {
|
|
WP_CLI::warning( 'Polylang no está activo: se gestiona solo el contenido en castellano.' );
|
|
}
|
|
|
|
/* ---------- 1. Idiomas y ajustes de Polylang ---------- */
|
|
if ( $pll ) {
|
|
$model = PLL()->model;
|
|
$existing = wp_list_pluck( $model->get_languages_list(), 'slug' );
|
|
foreach ( array(
|
|
array( 'locale' => 'es_ES', 'slug' => 'es', 'name' => 'Español', 'flag' => 'es', 'term_group' => 0 ),
|
|
array( 'locale' => 'eu', 'slug' => 'eu', 'name' => 'Euskara', 'flag' => 'basque', 'term_group' => 1 ),
|
|
) as $lang ) {
|
|
if ( ! in_array( $lang['slug'], $existing, true ) ) {
|
|
$result = $model->add_language( $lang );
|
|
if ( is_wp_error( $result ) ) {
|
|
WP_CLI::warning( 'No se pudo crear el idioma ' . $lang['slug'] . ': ' . $result->get_error_message() );
|
|
}
|
|
}
|
|
}
|
|
$model->clean_languages_cache();
|
|
|
|
$settings = array(
|
|
'default_lang' => 'es',
|
|
'force_lang' => 1, // Idioma en el directorio: /eu/...
|
|
'hide_default' => true, // Castellano sin prefijo.
|
|
'rewrite' => true, // /eu/ en lugar de /language/eu/.
|
|
'redirect_lang' => true, // Portada en euskera en /eu/.
|
|
'browser' => false,
|
|
'post_types' => array( 'animal' ),
|
|
'taxonomies' => array( 'especie' ),
|
|
'sync' => array( 'taxonomies', 'post_meta', '_thumbnail_id', 'menu_order', 'post_parent' ),
|
|
);
|
|
$options = PLL()->options;
|
|
if ( is_object( $options ) && method_exists( $options, 'set' ) ) {
|
|
foreach ( $settings as $key => $value ) {
|
|
$options->set( $key, $value );
|
|
}
|
|
if ( method_exists( $options, 'save_all' ) ) {
|
|
$options->save_all();
|
|
}
|
|
} else {
|
|
update_option( 'polylang', array_merge( (array) get_option( 'polylang', array() ), $settings ) );
|
|
}
|
|
if ( class_exists( 'PLL_MO' ) && ( $eu = $model->get_language( 'eu' ) ) ) {
|
|
$mo = new PLL_MO();
|
|
$mo->import_from_db( $eu );
|
|
$mo->add_entry( $mo->make_entry( get_option( 'blogdescription' ), 'Adoptatu eta bi mundu aldatu · Hondarribia' ) );
|
|
$mo->export_to_db( $eu );
|
|
}
|
|
if ( class_exists( 'PLL_Admin_Notices' ) ) {
|
|
PLL_Admin_Notices::dismiss( 'wizard' );
|
|
}
|
|
}
|
|
|
|
/* ---------- 2. Páginas ---------- */
|
|
|
|
/** ¿La página sigue con el contenido que pusimos nosotros? */
|
|
function ga_setup_untouched( WP_Post $post ) {
|
|
$hash = get_post_meta( $post->ID, '_ga_seed_hash', true );
|
|
if ( $hash ) {
|
|
return md5( $post->post_content ) === $hash;
|
|
}
|
|
// Páginas de la primera instalación (sin huella): intactas si nunca se han modificado.
|
|
return $post->post_modified_gmt === $post->post_date_gmt;
|
|
}
|
|
|
|
function ga_setup_page( $slug, $title, $file, $lang, $status = 'publish' ) {
|
|
$content = file_exists( $file ) ? file_get_contents( $file ) : '';
|
|
$page = get_page_by_path( $slug, OBJECT, 'page' );
|
|
|
|
if ( $page && 'trash' !== $page->post_status ) {
|
|
if ( $content && ga_setup_untouched( $page ) && trim( $page->post_content ) !== trim( $content ) ) {
|
|
wp_update_post( array( 'ID' => $page->ID, 'post_content' => $content, 'post_title' => $title ) );
|
|
update_post_meta( $page->ID, '_ga_seed_hash', md5( get_post( $page->ID )->post_content ) );
|
|
WP_CLI::log( "[ga-setup] actualizada: $slug" );
|
|
} elseif ( ! ga_setup_untouched( $page ) ) {
|
|
WP_CLI::log( "[ga-setup] se respeta (editada): $slug" );
|
|
}
|
|
$id = $page->ID;
|
|
} else {
|
|
$id = wp_insert_post( array(
|
|
'post_type' => 'page',
|
|
'post_status' => $status,
|
|
'post_title' => $title,
|
|
'post_name' => $slug,
|
|
'post_content' => $content,
|
|
), true );
|
|
if ( is_wp_error( $id ) ) {
|
|
WP_CLI::error( "No se pudo crear la página $slug: " . $id->get_error_message() );
|
|
}
|
|
update_post_meta( $id, '_ga_seed_hash', md5( get_post( $id )->post_content ) );
|
|
WP_CLI::log( "[ga-setup] creada: $slug" );
|
|
}
|
|
if ( function_exists( 'pll_set_post_language' ) ) {
|
|
pll_set_post_language( $id, $lang );
|
|
}
|
|
return $id;
|
|
}
|
|
|
|
// slug es => [título es, slug eu, título eu]
|
|
$pages = array(
|
|
'inicio' => array( 'Inicio', 'hasiera', 'Hasiera' ),
|
|
'acoge' => array( 'Acoge', 'harrera', 'Harrera' ),
|
|
'colabora' => array( 'Colabora', 'lagundu', 'Lagundu' ),
|
|
'quienes-somos' => array( 'Quiénes somos', 'nor-gara', 'Nor gara' ),
|
|
'contacto' => array( 'Contacto', 'harremanetarako', 'Harremanetarako' ),
|
|
'preguntas-frecuentes' => array( 'Preguntas frecuentes', 'ohiko-galderak', 'Ohiko galderak' ),
|
|
'politica-de-privacidad' => array( 'Política de privacidad', 'pribatutasun-politika', 'Pribatutasun-politika' ),
|
|
'aviso-legal' => array( 'Aviso legal', 'lege-oharra', 'Lege-oharra' ),
|
|
);
|
|
$files = array( 'politica-de-privacidad' => 'privacidad' );
|
|
|
|
$ids = array();
|
|
foreach ( $pages as $slug => [ $title_es, $slug_eu, $title_eu ] ) {
|
|
$ids[ $slug ]['es'] = ga_setup_page( $slug, $title_es, "$src/es/" . ( $files[ $slug ] ?? $slug ) . '.html', 'es' );
|
|
if ( $pll ) {
|
|
$ids[ $slug ]['eu'] = ga_setup_page( $slug_eu, $title_eu, "$src/eu/$slug_eu.html", 'eu' );
|
|
pll_save_post_translations( $ids[ $slug ] );
|
|
}
|
|
}
|
|
|
|
// El hotel ya no forma parte de la web: se retira si nadie lo ha editado.
|
|
foreach ( array( 'hotel', 'hotela' ) as $slug ) {
|
|
$page = get_page_by_path( $slug, OBJECT, 'page' );
|
|
if ( $page && 'trash' !== $page->post_status ) {
|
|
if ( ga_setup_untouched( $page ) ) {
|
|
wp_trash_post( $page->ID );
|
|
WP_CLI::log( "[ga-setup] a la papelera: $slug" );
|
|
} else {
|
|
WP_CLI::warning( "La página «$slug» se ha editado: no se borra, revísala a mano." );
|
|
}
|
|
}
|
|
}
|
|
|
|
// Páginas preparadas pero sin publicar: el equipo las publica cuando quiera.
|
|
$drafts = array( 'finales-felices' => array( 'Finales felices', 'amaiera-zoriontsuak', 'Amaiera zoriontsuak' ) );
|
|
foreach ( $drafts as $slug => [ $title_es, $slug_eu, $title_eu ] ) {
|
|
$exists = get_posts( array( 'post_type' => 'page', 'name' => $slug, 'post_status' => 'any', 'numberposts' => 1, 'lang' => '' ) );
|
|
if ( $exists ) {
|
|
continue;
|
|
}
|
|
$draft_ids = array( 'es' => ga_setup_page( $slug, $title_es, "$src/es/$slug.html", 'es', 'draft' ) );
|
|
if ( $pll ) {
|
|
$draft_ids['eu'] = ga_setup_page( $slug_eu, $title_eu, "$src/eu/$slug_eu.html", 'eu', 'draft' );
|
|
pll_save_post_translations( $draft_ids );
|
|
}
|
|
}
|
|
|
|
update_option( 'show_on_front', 'page' );
|
|
update_option( 'page_on_front', $ids['inicio']['es'] );
|
|
update_option( 'wp_page_for_privacy_policy', $ids['politica-de-privacidad']['es'] );
|
|
WP_CLI::log( '[ga-setup] Páginas listas' );
|
|
|
|
/* ---------- 3. Especies: solo perros y gatos ---------- */
|
|
$term_ids = array();
|
|
foreach ( array( 'perro' => array( 'Perro', 'txakurra', 'Txakurra' ), 'gato' => array( 'Gato', 'katua', 'Katua' ) ) as $slug => [ $name_es, $slug_eu, $name_eu ] ) {
|
|
$pairs = array( 'es' => array( $slug, $name_es ) ) + ( $pll ? array( 'eu' => array( $slug_eu, $name_eu ) ) : array() );
|
|
foreach ( $pairs as $lang => [ $term_slug, $term_name ] ) {
|
|
$term = get_term_by( 'slug', $term_slug, 'especie' );
|
|
if ( ! $term ) {
|
|
$res = wp_insert_term( $term_name, 'especie', array( 'slug' => $term_slug ) );
|
|
$term = is_wp_error( $res ) ? null : get_term( $res['term_id'], 'especie' );
|
|
}
|
|
if ( $term ) {
|
|
$term_ids[ $slug ][ $lang ] = $term->term_id;
|
|
if ( $pll ) {
|
|
pll_set_term_language( $term->term_id, $lang );
|
|
}
|
|
}
|
|
}
|
|
if ( $pll && 2 === count( $term_ids[ $slug ] ?? array() ) ) {
|
|
pll_save_term_translations( $term_ids[ $slug ] );
|
|
}
|
|
}
|
|
foreach ( array( 'otros', 'besteak' ) as $slug ) {
|
|
$term = get_term_by( 'slug', $slug, 'especie' );
|
|
if ( $term && 0 === (int) $term->count ) {
|
|
wp_delete_term( $term->term_id, 'especie' );
|
|
}
|
|
}
|
|
|
|
/* ---------- 4. Menús ---------- */
|
|
function ga_setup_menu( $name, $items, $rebuild ) {
|
|
$menu = wp_get_nav_menu_object( $name );
|
|
if ( $menu && ! $rebuild ) {
|
|
return $menu->term_id;
|
|
}
|
|
if ( $menu ) {
|
|
wp_delete_nav_menu( $menu->term_id );
|
|
}
|
|
$menu_id = wp_create_nav_menu( $name );
|
|
$pos = 0;
|
|
$add = function ( $item, $parent = 0 ) use ( $menu_id, &$pos, &$add ) {
|
|
$args = array(
|
|
'menu-item-status' => 'publish',
|
|
'menu-item-position' => ++$pos,
|
|
'menu-item-title' => $item['title'],
|
|
'menu-item-parent-id' => $parent,
|
|
);
|
|
$args += isset( $item['page'] )
|
|
? array( 'menu-item-type' => 'post_type', 'menu-item-object' => 'page', 'menu-item-object-id' => $item['page'] )
|
|
: array( 'menu-item-type' => 'custom', 'menu-item-url' => $item['url'] );
|
|
$id = wp_update_nav_menu_item( $menu_id, 0, $args );
|
|
foreach ( $item['children'] ?? array() as $child ) {
|
|
$add( $child, $id );
|
|
}
|
|
};
|
|
foreach ( $items as $item ) {
|
|
$add( $item );
|
|
}
|
|
return $menu_id;
|
|
}
|
|
|
|
$rebuild = 'true' === getenv( 'GA_SEED_MENUS' );
|
|
$menus = array(
|
|
'primary' => array(
|
|
'es' => ga_setup_menu( 'Principal', array(
|
|
array( 'title' => 'Inicio', 'page' => $ids['inicio']['es'] ),
|
|
array( 'title' => 'Adopta', 'url' => '/adopta/', 'children' => array(
|
|
array( 'title' => 'Perros', 'url' => '/especie/perro/' ),
|
|
array( 'title' => 'Gatos', 'url' => '/especie/gato/' ),
|
|
) ),
|
|
array( 'title' => 'Acoge', 'page' => $ids['acoge']['es'] ),
|
|
array( 'title' => 'Colabora', 'page' => $ids['colabora']['es'], 'children' => array(
|
|
array( 'title' => 'Voluntariado', 'url' => '/colabora/#voluntariado' ),
|
|
array( 'title' => 'Ayuda material', 'url' => '/colabora/#material' ),
|
|
) ),
|
|
array( 'title' => 'Quiénes somos', 'page' => $ids['quienes-somos']['es'] ),
|
|
array( 'title' => 'Contacto', 'page' => $ids['contacto']['es'] ),
|
|
), $rebuild ),
|
|
),
|
|
'footer' => array(
|
|
'es' => ga_setup_menu( 'Legal', array(
|
|
array( 'title' => 'Aviso legal', 'page' => $ids['aviso-legal']['es'] ),
|
|
array( 'title' => 'Política de privacidad', 'page' => $ids['politica-de-privacidad']['es'] ),
|
|
), $rebuild ),
|
|
),
|
|
);
|
|
if ( $pll ) {
|
|
$menus['primary']['eu'] = ga_setup_menu( 'Nagusia (euskara)', array(
|
|
array( 'title' => 'Hasiera', 'page' => $ids['inicio']['eu'] ),
|
|
array( 'title' => 'Adoptatu', 'url' => '/eu/adopta/', 'children' => array(
|
|
array( 'title' => 'Txakurrak', 'url' => '/eu/especie/txakurra/' ),
|
|
array( 'title' => 'Katuak', 'url' => '/eu/especie/katua/' ),
|
|
) ),
|
|
array( 'title' => 'Harrera', 'page' => $ids['acoge']['eu'] ),
|
|
array( 'title' => 'Lagundu', 'page' => $ids['colabora']['eu'], 'children' => array(
|
|
array( 'title' => 'Boluntariotza', 'url' => '/eu/lagundu/#voluntariado' ),
|
|
array( 'title' => 'Laguntza materiala', 'url' => '/eu/lagundu/#material' ),
|
|
) ),
|
|
array( 'title' => 'Nor gara', 'page' => $ids['quienes-somos']['eu'] ),
|
|
array( 'title' => 'Harremanetarako', 'page' => $ids['contacto']['eu'] ),
|
|
), $rebuild );
|
|
$menus['footer']['eu'] = ga_setup_menu( 'Legala (euskara)', array(
|
|
array( 'title' => 'Lege-oharra', 'page' => $ids['aviso-legal']['eu'] ),
|
|
array( 'title' => 'Pribatutasun-politika', 'page' => $ids['politica-de-privacidad']['eu'] ),
|
|
), $rebuild );
|
|
|
|
$nav_menus = array( get_stylesheet() => $menus );
|
|
if ( is_object( PLL()->options ) && method_exists( PLL()->options, 'set' ) ) {
|
|
PLL()->options->set( 'nav_menus', $nav_menus );
|
|
if ( method_exists( PLL()->options, 'save_all' ) ) {
|
|
PLL()->options->save_all();
|
|
}
|
|
} else {
|
|
$opts = (array) get_option( 'polylang', array() );
|
|
$opts['nav_menus'] = $nav_menus;
|
|
update_option( 'polylang', $opts );
|
|
}
|
|
}
|
|
set_theme_mod( 'nav_menu_locations', array( 'primary' => $menus['primary']['es'], 'footer' => $menus['footer']['es'] ) );
|
|
WP_CLI::log( '[ga-setup] Menús listos' );
|
|
|
|
/* ---------- 5. Animales ---------- */
|
|
|
|
// Estados antiguos → nuevos (disponible / proceso / adoptado).
|
|
foreach ( get_posts( array( 'post_type' => 'animal', 'post_status' => 'any', 'numberposts' => -1, 'fields' => 'ids', 'lang' => '' ) ) as $animal_id ) {
|
|
$estado = get_post_meta( $animal_id, '_ga_estado', true );
|
|
$map = array( 'reservado' => 'proceso', 'urgente' => 'disponible', 'acogida' => 'disponible' );
|
|
if ( isset( $map[ $estado ] ) ) {
|
|
update_post_meta( $animal_id, '_ga_estado', $map[ $estado ] );
|
|
}
|
|
}
|
|
|
|
if ( 'true' === getenv( 'SEED_DEMO_ANIMALS' ) && ! get_posts( array( 'post_type' => 'animal', 'numberposts' => 1, 'post_status' => 'any', 'lang' => '' ) ) ) {
|
|
$demo = array(
|
|
array(
|
|
'es' => array( 'Txuri (ejemplo)', 'Llegó un poco asustada, pero en cuanto coge confianza no se separa de ti.', array( 'caracter' => 'Tranquila en casa y muy cariñosa con quien conoce. Le encantan los paseos largos.', 'hogar' => 'Una familia que respete su ritmo y le acompañe con paciencia.' ) ),
|
|
'eu' => array( 'Txuri (adibidea)', 'Beldur pixka batekin iritsi zen, baina konfiantza hartzen duenean ez da zure ondotik aldentzen.', array( 'caracter' => 'Lasaia etxean eta oso maitekorra ezagutzen dituenekin. Ibilaldi luzeak asko gustatzen zaizkio.', 'hogar' => 'Bere erritmoa errespetatu eta pazientziaz lagunduko dion familia bat.' ) ),
|
|
'sp' => 'perro',
|
|
'meta' => array( 'sexo' => 'hembra', 'edad' => '3', 'edad_unidad' => 'anos', 'tamano' => 'mediano', 'estado' => 'disponible', 'destacado' => '1' ),
|
|
),
|
|
array(
|
|
'es' => array( 'Beltza (ejemplo)', 'Curioso y juguetón, siempre quiere saber qué estás haciendo.', array( 'caracter' => 'Muy sociable con las personas. Le gusta jugar y dormir al sol.' ) ),
|
|
'eu' => array( 'Beltza (adibidea)', 'Jakin-mina eta jostalaria, beti jakin nahi du zer egiten ari zaren.', array( 'caracter' => 'Oso soziala pertsonekin. Jolastea eta eguzkitan lo egitea gustatzen zaizkio.' ) ),
|
|
'sp' => 'gato',
|
|
'meta' => array( 'sexo' => 'macho', 'edad' => '1', 'edad_unidad' => 'anos', 'estado' => 'disponible' ),
|
|
),
|
|
array(
|
|
'es' => array( 'Hartz (ejemplo)', 'Un abuelito noble que merece pasar sus mejores años en familia.', array( 'necesidades' => 'Paseos tranquilos y una casa sin muchas escaleras.' ) ),
|
|
'eu' => array( 'Hartz (adibidea)', 'Aitona noblea, bere urterik onenak familian igarotzea merezi duena.', array( 'necesidades' => 'Ibilaldi lasaiak eta eskailera askorik gabeko etxea.' ) ),
|
|
'sp' => 'perro',
|
|
'meta' => array( 'sexo' => 'macho', 'edad' => '9', 'edad_unidad' => 'anos', 'tamano' => 'grande', 'estado' => 'disponible', 'destacado' => '1' ),
|
|
),
|
|
array(
|
|
'es' => array( 'Ilargi (ejemplo)', 'Una gatita sociable que se lleva bien con otros gatos.', array() ),
|
|
'eu' => array( 'Ilargi (adibidea)', 'Beste katuekin ondo moldatzen den katakume soziala.', array() ),
|
|
'sp' => 'gato',
|
|
'meta' => array( 'sexo' => 'hembra', 'edad' => '4', 'edad_unidad' => 'meses', 'estado' => 'proceso' ),
|
|
),
|
|
);
|
|
foreach ( $demo as $animal ) {
|
|
$translations = array();
|
|
foreach ( $pll ? array( 'es', 'eu' ) : array( 'es' ) as $lang ) {
|
|
[ $title, $text, $texts ] = $animal[ $lang ];
|
|
$id = wp_insert_post( array(
|
|
'post_type' => 'animal',
|
|
'post_status' => 'publish',
|
|
'post_title' => $title,
|
|
'post_content' => "<!-- wp:paragraph -->\n<p>$text</p>\n<!-- /wp:paragraph -->",
|
|
) );
|
|
if ( ! empty( $term_ids[ $animal['sp'] ][ $lang ] ) ) {
|
|
wp_set_object_terms( $id, array( $term_ids[ $animal['sp'] ][ $lang ] ), 'especie' );
|
|
}
|
|
foreach ( $animal['meta'] + $texts as $key => $value ) {
|
|
update_post_meta( $id, "_ga_$key", $value );
|
|
}
|
|
if ( $pll ) {
|
|
pll_set_post_language( $id, $lang );
|
|
}
|
|
$translations[ $lang ] = $id;
|
|
}
|
|
if ( $pll ) {
|
|
pll_save_post_translations( $translations );
|
|
}
|
|
}
|
|
WP_CLI::log( '[ga-setup] Animales de ejemplo creados' );
|
|
}
|
|
|
|
// Polylang guarda en caché la portada de cada idioma: se regenera con las traducciones ya creadas.
|
|
if ( $pll ) {
|
|
PLL()->model->clean_languages_cache();
|
|
}
|
|
flush_rewrite_rules( false );
|