' . esc_html__( 'No WP Advanced Ads found. Cannot run retroactive update.', 'adfusion-ad-manager-and-monetization' ) . '
';
}
);
return;
}
// Index Advanced Ads by title for matching.
$adv_ads_by_title = array();
foreach ( $adv_ads as $adv_ad ) {
$title = $adv_ad->post_title;
if ( ! isset( $adv_ads_by_title[ $title ] ) ) {
$adv_ads_by_title[ $title ] = array();
}
$adv_ads_by_title[ $title ][] = $adv_ad;
}
foreach ( $adfusion_ads as $adfusion_ad ) {
$title = $adfusion_ad->post_title;
if ( empty( $title ) || ! isset( $adv_ads_by_title[ $title ] ) ) {
continue;
}
// Use the first matching Advanced Ad (exact title match).
$adv_ad = $adv_ads_by_title[ $title ][0];
$ad_options = get_post_meta( $adv_ad->ID, 'advanced_ads_ad_options', true );
$ad_options = is_array( $ad_options ) ? $ad_options : array();
$image_id = $this->extract_image_id( $ad_options );
$dest_url = $this->extract_destination_url( $ad_options );
// Only update if we have an image to attach.
if ( $image_id && absint( $image_id ) ) {
// Verify attachment exists.
if ( wp_attachment_is_image( absint( $image_id ) ) ) {
set_post_thumbnail( $adfusion_ad->ID, absint( $image_id ) );
update_post_meta( $adfusion_ad->ID, '_adf_ad_type', 'image' );
update_post_meta( $adfusion_ad->ID, '_adf_destination_url', esc_url_raw( $dest_url ) );
++$updated_count;
}
}
}
$count = $updated_count;
add_action(
'admin_notices',
function () use ( $count ) {
$class = $count > 0 ? 'notice-success' : 'notice-warning';
$message = $count > 0
? sprintf(
/* translators: 1: number of ads updated */
__( 'Retroactive update complete: %1$d ad(s) now have images and destination URLs attached.', 'adfusion-ad-manager-and-monetization' ),
$count
)
: __( 'No ads were updated. Either no matching ads were found (by title) or the source Advanced Ads had no image data.', 'adfusion-ad-manager-and-monetization' );
printf( '