File: //proc/self/cwd/upload-support-cae-images.php
<?php
/**
* Upload support-cae images to WordPress media library and populate ACF fields.
* Images are in the theme at assets/images/support-cae/
*/
$post_id = 25;
$theme_dir = get_template_directory();
$image_dir = $theme_dir . '/assets/images/support-cae';
/**
* Upload a file to the WordPress media library.
*/
function upload_image_to_media($file_path, $title) {
if (!file_exists($file_path)) {
WP_CLI::warning("File not found: {$file_path}");
return false;
}
$filetype = wp_check_filetype(basename($file_path));
$attachment = [
'post_mime_type' => $filetype['type'],
'post_title' => $title,
'post_content' => '',
'post_status' => 'inherit',
];
// Copy to uploads dir
$upload_dir = wp_upload_dir();
$dest = $upload_dir['path'] . '/' . basename($file_path);
copy($file_path, $dest);
$attach_id = wp_insert_attachment($attachment, $dest);
if (is_wp_error($attach_id)) {
WP_CLI::warning("Failed to insert: {$title}");
return false;
}
require_once(ABSPATH . 'wp-admin/includes/image.php');
$metadata = wp_generate_attachment_metadata($attach_id, $dest);
wp_update_attachment_metadata($attach_id, $metadata);
WP_CLI::log(" Uploaded: {$title} (ID: {$attach_id})");
return $attach_id;
}
// Upload banner
$banner_id = upload_image_to_media("{$image_dir}/ways-to-give-banner.jpg", 'Ways to Give Banner');
// Upload accordion thumbnails
$thumb_ids = [];
for ($i = 1; $i <= 6; $i++) {
$thumb_ids[$i - 1] = upload_image_to_media("{$image_dir}/accordion-{$i}.jpg", "Ways to Give - Accordion {$i}");
}
// Upload callout image
$callout_id = upload_image_to_media("{$image_dir}/stock-callout.jpg", 'After You Donate Stock');
// Now populate ACF fields
// Intro banner
if ($banner_id) {
update_field('support_cae_intro_image', $banner_id, $post_id);
WP_CLI::log(" Set intro banner image");
}
// Accordion thumbnails and callout
$items = get_field('support_cae_items', $post_id);
if (is_array($items)) {
foreach ($items as $i => &$item) {
if (isset($thumb_ids[$i]) && $thumb_ids[$i]) {
$item['item_image'] = $thumb_ids[$i];
}
// Callout image on item 2 (stock donation)
if ($i === 2 && $callout_id) {
$item['item_callout_image'] = $callout_id;
}
}
update_field('support_cae_items', $items, $post_id);
WP_CLI::log(" Set accordion thumbnails and callout image");
}
// Also populate CTA image
$cta_image_path = $theme_dir . '/assets/images/blocks/cta/default.png';
if (file_exists($cta_image_path)) {
$cta_img_id = upload_image_to_media($cta_image_path, 'CTA Default Image');
if ($cta_img_id) {
// CTA stores data inline in block attrs, so we need to update post content
// For now just note it — CTA already has a fallback default image in the template
WP_CLI::log(" CTA image uploaded (ID: {$cta_img_id}) — template already uses fallback");
}
}
WP_CLI::success("Support CAE images uploaded and ACF fields populated.");