run_main_import(); return; } if ( 'update_advanced_ads_images' === $action ) { check_admin_referer( 'adfusion_update_advanced_ads_images', 'adfusion_update_nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'adfusion-ad-manager-and-monetization' ) ); } $this->run_retroactive_update(); return; } } /** * Run the main import from WP Advanced Ads. * * @return void */ private function run_main_import() { $imported_groups_count = 0; $imported_ads_count = 0; $group_mapping = array(); // Step 1: Import groups (taxonomy). $adv_groups = get_terms( array( 'taxonomy' => 'advanced_ads_groups', 'hide_empty' => false, ) ); if ( ! is_wp_error( $adv_groups ) && ! empty( $adv_groups ) ) { foreach ( $adv_groups as $old_term ) { $existing_term = term_exists( $old_term->name, 'adfusion_group' ); if ( ! $existing_term ) { $new_term = wp_insert_term( $old_term->name, 'adfusion_group', array( 'description' => $old_term->description, 'slug' => $old_term->slug, ) ); if ( ! is_wp_error( $new_term ) ) { $group_mapping[ $old_term->term_id ] = $new_term['term_id']; ++$imported_groups_count; } } else { $group_mapping[ $old_term->term_id ] = is_array( $existing_term ) ? $existing_term['term_id'] : $existing_term; } } } // Step 2: Import ads and assign to groups. $args = array( 'post_type' => 'advanced_ads', 'posts_per_page' => -1, 'post_status' => 'any', ); $adv_ads = get_posts( $args ); if ( $adv_ads ) { foreach ( $adv_ads as $ad ) { if ( get_post_meta( $ad->ID, '_adfusion_imported', true ) ) { continue; } $new_ad_data = array( 'post_title' => sanitize_text_field( $ad->post_title ), 'post_status' => $ad->post_status, 'post_type' => 'adfusion_ad', ); $new_ad_id = wp_insert_post( $new_ad_data ); if ( $new_ad_id && ! is_wp_error( $new_ad_id ) ) { $ad_options = get_post_meta( $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 ); if ( $image_id && absint( $image_id ) ) { set_post_thumbnail( $new_ad_id, absint( $image_id ) ); update_post_meta( $new_ad_id, '_adf_ad_type', 'image' ); update_post_meta( $new_ad_id, '_adf_destination_url', esc_url_raw( $dest_url ) ); } else { update_post_meta( $new_ad_id, '_adf_ad_type', 'code' ); update_post_meta( $new_ad_id, '_adf_ad_code', $ad->post_content ); } update_post_meta( $ad->ID, '_adfusion_imported', true ); $old_ad_terms = wp_get_post_terms( $ad->ID, 'advanced_ads_groups' ); if ( ! is_wp_error( $old_ad_terms ) && ! empty( $old_ad_terms ) ) { $new_terms_to_assign = array(); foreach ( $old_ad_terms as $old_term ) { if ( isset( $group_mapping[ $old_term->term_id ] ) ) { $new_terms_to_assign[] = (int) $group_mapping[ $old_term->term_id ]; } } if ( ! empty( $new_terms_to_assign ) ) { wp_set_object_terms( $new_ad_id, $new_terms_to_assign, 'adfusion_group' ); } } ++$imported_ads_count; } } } $groups_count = $imported_groups_count; $ads_count = $imported_ads_count; add_action( 'admin_notices', function () use ( $ads_count, $groups_count ) { $class = ( $ads_count > 0 || $groups_count > 0 ) ? 'notice-success' : 'notice-warning'; $message = ( $ads_count > 0 || $groups_count > 0 ) ? sprintf( /* translators: 1: number of groups imported, 2: number of ads imported */ __( 'Successfully imported %1$d groups and %2$d ads. You can now assign these groups to placements in the AdFusion Settings page.', 'adfusion-ad-manager-and-monetization' ), $groups_count, $ads_count ) : __( 'No new WP Advanced Ads or Groups found to import.', 'adfusion-ad-manager-and-monetization' ); printf( '

%2$s

', esc_attr( $class ), esc_html( $message ) ); } ); do_action( 'adfusion_after_import', $imported_groups_count, $imported_ads_count ); } /** * Run the retroactive update: attach images and URLs to previously imported ads. * * @return void */ private function run_retroactive_update() { $updated_count = 0; $adfusion_ads = get_posts( array( 'post_type' => 'adfusion_ad', 'posts_per_page' => -1, 'post_status' => 'any', ) ); $adv_ads = get_posts( array( 'post_type' => 'advanced_ads', 'posts_per_page' => -1, 'post_status' => 'any', ) ); if ( empty( $adv_ads ) ) { add_action( 'admin_notices', function () { echo '

' . 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( '

%2$s

', esc_attr( $class ), esc_html( $message ) ); } ); do_action( 'adfusion_after_retroactive_update', $updated_count ); } /** * Extract image ID from Advanced Ads ad options. * * @param array $ad_options Advanced Ads ad options. * @return int|false Image attachment ID or false. */ private function extract_image_id( $ad_options ) { if ( empty( $ad_options ) || ! is_array( $ad_options ) ) { return false; } if ( ! empty( $ad_options['output']['image_id'] ) ) { return absint( $ad_options['output']['image_id'] ); } if ( ! empty( $ad_options['image_id'] ) ) { return absint( $ad_options['image_id'] ); } return false; } /** * Extract destination URL from Advanced Ads ad options. * * @param array $ad_options Advanced Ads ad options. * @return string URL or empty string. */ private function extract_destination_url( $ad_options ) { if ( empty( $ad_options ) || ! is_array( $ad_options ) ) { return ''; } $output = isset( $ad_options['output'] ) && is_array( $ad_options['output'] ) ? $ad_options['output'] : $ad_options; $url_keys = array( 'url', 'link', 'output_url' ); foreach ( $url_keys as $key ) { if ( ! empty( $output[ $key ] ) && is_string( $output[ $key ] ) ) { return esc_url_raw( $output[ $key ] ); } } foreach ( $url_keys as $key ) { if ( ! empty( $ad_options[ $key ] ) && is_string( $ad_options[ $key ] ) ) { return esc_url_raw( $ad_options[ $key ] ); } } return ''; } }