57 lines
2.5 KiB
PHP
57 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* import_avatars_143.php (#143) — da de alta los avatares de INICIALES de los
|
|
* usuarios nuevos creados al corregir la autoría K2 (ver fix_k2_authors.php).
|
|
*
|
|
* Crea/actualiza el attachment del PNG uploads/avatares/autores/autor-<uid>.png
|
|
* y reapunta foto_perfil (ACF user_meta). Backup del valor previo en
|
|
* user_meta _foto_perfil_pre143. Idempotente (si ya apunta, solo regenera metadata).
|
|
*
|
|
* Entrada: TSV «uid<TAB>display_name» (env FEA_TSV, por defecto /tmp/users29.tsv).
|
|
* Uso (en el servidor, dentro de /web/wp-nuevo):
|
|
* FEA_TSV=/tmp/users29.tsv wp eval-file scripts/import_avatars_143.php # dry-run
|
|
* APPLY=1 FEA_TSV=/tmp/users29.tsv wp eval-file scripts/import_avatars_143.php # aplica
|
|
*/
|
|
require_once ABSPATH . 'wp-admin/includes/image.php';
|
|
|
|
$apply = getenv('APPLY') === '1';
|
|
$tsv = getenv('FEA_TSV') ?: '/tmp/users29.tsv';
|
|
$updir = wp_get_upload_dir();
|
|
if (!is_readable($tsv)) { echo "No puedo leer TSV: $tsv\n"; return; }
|
|
|
|
$done = $regen = $err = 0;
|
|
foreach (file($tsv) as $line) {
|
|
$p = explode("\t", rtrim($line, "\n"));
|
|
if (count($p) < 2) continue;
|
|
$uid = (int) $p[0];
|
|
$name = trim($p[1]);
|
|
$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);
|
|
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_pre143', true) === '') {
|
|
update_user_meta($uid, '_foto_perfil_pre143', $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";
|