dry run (no escribe nada). */ require_once ABSPATH . 'wp-admin/includes/image.php'; $apply = getenv('APPLY') === '1'; $mfile = getenv('MANIFEST') ?: 'manifest_62.json'; $updir = wp_get_upload_dir(); $manifest = json_decode(file_get_contents($updir['basedir'] . '/avatares/' . $mfile), true); $done = $skip = $err = 0; foreach ($manifest as $row) { list($uid, $name, $orig, $kind, $old_attach) = $row; if (!in_array($kind, ['initials','trim','logo','qs'], true)) { $skip++; continue; } $rel = "avatares/autores/autor-{$uid}.png"; $abs = $updir['basedir'] . '/' . $rel; if (!file_exists($abs)) { echo "MISSING file uid=$uid\n"; $err++; continue; } // si foto_perfil ya apunta a este fichero, solo se ha sobrescrito el PNG: no crear attachment nuevo $cur = (int) get_user_meta($uid, 'foto_perfil', true); if ($cur && get_post_meta($cur, '_wp_attached_file', true) === $rel) { if ($apply) wp_update_attachment_metadata($cur, wp_generate_attachment_metadata($cur, $abs)); $skip++; continue; } if (!$apply) { $done++; continue; } // guardar foto_perfil antiguo una sola vez (idempotente) if (get_user_meta($uid, '_foto_perfil_pre62', true) === '') { update_user_meta($uid, '_foto_perfil_pre62', $old_attach); } $attach = [ 'post_mime_type' => 'image/png', 'post_title' => "Avatar {$name}", 'post_status' => 'inherit', 'guid' => $updir['baseurl'] . '/' . $rel, ]; $aid = wp_insert_attachment($attach, $abs, 0, true); if (is_wp_error($aid)) { echo "ERR insert uid=$uid: ".$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 ($apply ? "APLICADO" : "DRY-RUN") . ": procesados=$done omitidos=$skip errores=$err\n";