HEX
Server: Apache/2.4.66 (Debian)
System: Linux 6dfabc3b2241 6.8.0-71-generic #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025 x86_64
User: (1000)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: /var/www/html/patch-support-cae-images.php
<?php
/**
 * Patch support-cae block inline data with image attachment IDs.
 * Images were already uploaded — this adds their IDs to the block attrs.
 */

$post_id = 25;

// Find uploaded image IDs by title
function find_attachment_by_title($title) {
    $args = [
        'post_type'      => 'attachment',
        'post_status'    => 'inherit',
        'title'          => $title,
        'posts_per_page' => 1,
    ];
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        return $query->posts[0]->ID;
    }
    return false;
}

$banner_id = find_attachment_by_title('Ways to Give Banner');
$callout_id = find_attachment_by_title('After You Donate Stock');
$thumb_ids = [];
for ($i = 1; $i <= 6; $i++) {
    $thumb_ids[$i - 1] = find_attachment_by_title("Ways to Give - Accordion {$i}");
}

WP_CLI::log("Banner: " . ($banner_id ?: 'NOT FOUND'));
WP_CLI::log("Callout: " . ($callout_id ?: 'NOT FOUND'));
foreach ($thumb_ids as $i => $id) {
    WP_CLI::log("Thumb {$i}: " . ($id ?: 'NOT FOUND'));
}

// Parse the post content and update the block data
$post = get_post($post_id);
$blocks = parse_blocks($post->post_content);

foreach ($blocks as &$block) {
    if ($block['blockName'] === 'acf/support-cae' && isset($block['attrs']['data'])) {
        $data = &$block['attrs']['data'];

        // Add banner image
        if ($banner_id) {
            $data['support_cae_intro_image'] = $banner_id;
            $data['_support_cae_intro_image'] = 'field_support_cae_intro_image';
        }

        // Add thumbnails to each item
        $item_count = intval($data['support_cae_items'] ?? 0);
        for ($i = 0; $i < $item_count; $i++) {
            if (isset($thumb_ids[$i]) && $thumb_ids[$i]) {
                $data["support_cae_items_{$i}_item_image"] = $thumb_ids[$i];
                $data["_support_cae_items_{$i}_item_image"] = 'field_support_cae_item_image';
            }
            // Callout image on item 2
            if ($i === 2 && $callout_id) {
                $data["support_cae_items_{$i}_item_callout_image"] = $callout_id;
                $data["_support_cae_items_{$i}_item_callout_image"] = 'field_support_cae_item_callout_image';
            }
        }
    }
}

// Serialize back to post content
$new_content = serialize_blocks($blocks);

// Verify the blocks still parse
$test = parse_blocks($new_content);
$has_data = isset($test[0]['attrs']['data']['support_cae_intro_image']);
if (!$has_data) {
    WP_CLI::error('Block serialization failed — intro image not found in re-parsed data.');
    exit(1);
}

wp_update_post(['ID' => $post_id, 'post_content' => $new_content]);
wp_cache_flush();

// Verify rendering
$post = get_post($post_id);
$content = apply_filters('the_content', $post->post_content);
$has_uploads = strpos($content, 'uploads/') !== false;

WP_CLI::success("Block data patched. Images in rendered output: " . ($has_uploads ? 'YES' : 'NO'));