File: //proc/thread-self/cwd/fix-donate-images.php
<?php
/**
* Upload donate images to media library and patch the donate block inline data.
*/
// Register images in media library
$upload_dir = wp_upload_dir();
$images = [
'donate-bg' => ['file' => 'donate-bg.jpg', 'title' => 'Donate Background'],
'thank-you' => ['file' => 'thank-you.jpg', 'title' => 'Thank You Image'],
];
$ids = [];
foreach ($images as $key => $img) {
$file_path = $upload_dir['path'] . '/' . $img['file'];
if (!file_exists($file_path)) {
WP_CLI::warning("File not found: {$file_path}");
continue;
}
$filetype = wp_check_filetype($img['file']);
$attach_id = wp_insert_attachment([
'post_mime_type' => $filetype['type'],
'post_title' => $img['title'],
'post_content' => '',
'post_status' => 'inherit',
], $file_path);
if (!is_wp_error($attach_id)) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
wp_generate_attachment_metadata($attach_id, $file_path);
$ids[$key] = $attach_id;
WP_CLI::log("Uploaded: {$img['title']} (ID: {$attach_id})");
}
}
// Patch donate block on page 45
$post = get_post(45);
$blocks = parse_blocks($post->post_content);
foreach ($blocks as &$block) {
if ($block['blockName'] === 'acf/donate' && isset($block['attrs']['data'])) {
if (isset($ids['donate-bg'])) {
$block['attrs']['data']['donate_cta_image'] = $ids['donate-bg'];
$block['attrs']['data']['_donate_cta_image'] = 'field_donate_cta_image';
WP_CLI::log('Set CTA background image');
}
}
}
$new_content = serialize_blocks($blocks);
wp_update_post(['ID' => 45, 'post_content' => $new_content]);
wp_cache_flush();
// Verify
$rendered = apply_filters('the_content', get_post(45)->post_content);
$has_upload = strpos($rendered, 'uploads/') !== false;
WP_CLI::success("Images in rendered output: " . ($has_upload ? 'YES' : 'NO'));