Files
feadulta/scripts/import_avatars_75.php

60 lines
2.3 KiB
PHP

<?php
/**
* Issue #81 — avatares nuevos de 6 colaboradores habituales.
* Reapunta foto_perfil (user_meta) al PNG uploads/avatares/autores/autor-<uid>.png.
* Si foto_perfil ya apuntaba a ese fichero (caso #62), solo regenera metadata.
* Backup del foto_perfil anterior en user_meta _foto_perfil_pre75 (revertible).
*
* Uso (dentro del contenedor):
* php import_avatars_75.php -> DRY-RUN
* APPLY=1 php import_avatars_75.php -> aplica
*/
require "/var/www/html/wp-load.php";
require_once ABSPATH . 'wp-admin/includes/image.php';
$apply = getenv('APPLY') === '1';
$updir = wp_get_upload_dir();
$authors = [
[384, "Enrique Martínez Lozano"],
[583, "Fidel Aizpurúa"],
[1138, "Guadalupe Labrador"],
[383, "José Antonio Pagola"],
[774, "José Luis Sicre"],
[775, "Miguel A. Munárriz"],
];
$done = $regen = $err = 0;
foreach ($authors as [$uid, $name]) {
$rel = "avatares/autores/autor-{$uid}.png";
$abs = $updir['basedir'] . '/' . $rel;
if (!file_exists($abs)) { echo "MISSING uid=$uid ($name)\n"; $err++; continue; }
$cur = (int) get_user_meta($uid, 'foto_perfil', true);
// foto_perfil ya apunta a este fichero -> solo se sobrescribió el PNG
if ($cur && get_post_meta($cur, '_wp_attached_file', true) === $rel) {
echo "REGEN #$uid $name (attachment $cur ya apunta al PNG)\n";
if ($apply) wp_update_attachment_metadata($cur, wp_generate_attachment_metadata($cur, $abs));
$regen++; continue;
}
echo "NUEVO #$uid $name (foto_perfil actual: " . ($cur ?: 'ninguna') . ")\n";
if (!$apply) { $done++; continue; }
if (get_user_meta($uid, '_foto_perfil_pre75', true) === '') {
update_user_meta($uid, '_foto_perfil_pre75', $cur);
}
$aid = wp_insert_attachment([
'post_mime_type' => 'image/png',
'post_title' => "Avatar {$name}",
'post_status' => 'inherit',
'guid' => $updir['baseurl'] . '/' . $rel,
], $abs, 0, true);
if (is_wp_error($aid)) { echo " ERR: " . $aid->get_error_message() . "\n"; $err++; continue; }
wp_update_attachment_metadata($aid, wp_generate_attachment_metadata($aid, $abs));
update_user_meta($uid, 'foto_perfil', $aid);
$done++;
}
echo "\n" . ($apply ? "APLICADO" : "DRY-RUN") . ": nuevos=$done regen=$regen errores=$err\n";