array( 'option_name' => 'adfuz_global_ads_popup', 'title' => 'Global Popup', 'slug' => 'global-popup-space', ), 'exit_popup' => array( 'option_name' => 'adfuz_global_ads_exit_popup', 'title' => 'Global Exit Popup', 'slug' => 'global-exit-popup-space', ), 'background' => array( 'option_name' => 'adfuz_global_ads_background', 'title' => 'Global Background', 'slug' => 'global-background-space', ), ); // ------------------------------------------------------------------------- // Public API // ------------------------------------------------------------------------- public static function run( int $user_id ): array { $log = array(); $ad_ids = self::seed_ads( $log, $user_id ); $space_ids = self::seed_ad_spaces( $log, $user_id ); $campaign_ids = self::seed_campaigns( $log, $user_id ); self::seed_campaign_relations( $log, $campaign_ids, $ad_ids, $space_ids ); self::seed_global_ads( $log, $space_ids, $user_id ); return $log; } public static function get_status(): array { $ads = DB::get( TableName::ADFUZ_ADS() )->where( 'id', 'IN', self::seeded_ids( 'ad' ) ?: array( 0 ) )->count(); $spaces = DB::get( TableName::ADFUZ_AD_SPACES() )->where( 'id', 'IN', self::seeded_ids( 'space' ) ?: array( 0 ) )->count(); $campaigns = DB::get( TableName::ADFUZ_CAMPAIGNS() )->where( 'id', 'IN', self::seeded_ids( 'campaign' ) ?: array( 0 ) )->count(); $global_ads_seeded = ! empty( self::get_seeder_ids()['global_ads'] ); return array( 'ads' => $ads, 'ad_spaces' => $spaces, 'campaigns' => $campaigns, 'campaign_ads' => self::count_campaign_relations( 'ads' ), 'campaign_spaces' => self::count_campaign_relations( 'spaces' ), 'global_ads' => $global_ads_seeded ? 1 : 0, ); } public static function delete_all(): array { $log = array(); // 1. Get seeded IDs from meta tables $campaign_ids = DB::get( TableName::ADFUZ_CAMPAIGNS_META() ) ->where( 'meta_key', self::SEED_META_KEY ) ->where( 'meta_value', self::SEED_META_VALUE ) ->select( 'campaign_id' ) ->get(); $campaign_ids = array_column( $campaign_ids, 'campaign_id' ); $ad_space_ids = DB::get( TableName::ADFUZ_AD_SPACES_META() ) ->where( 'meta_key', self::SEED_META_KEY ) ->where( 'meta_value', self::SEED_META_VALUE ) ->select( 'ad_space_id' ) ->get(); $ad_space_ids = array_column( $ad_space_ids, 'ad_space_id' ); $ad_ids = DB::get( TableName::ADFUZ_ADS_META() ) ->where( 'meta_key', self::SEED_META_KEY ) ->where( 'meta_value', self::SEED_META_VALUE ) ->select( 'ad_id' ) ->get(); $ad_ids = array_column( $ad_ids, 'ad_id' ); // 2. Delete pivot tables via QueryBuilder if ( ! empty( $campaign_ids ) ) { DB::get( TableName::ADFUZ_CAMPAIGN_ADS() ) ->whereIn( 'campaign_id', $campaign_ids ) ->delete(); DB::get( TableName::ADFUZ_CAMPAIGN_AD_SPACES() ) ->whereIn( 'campaign_id', $campaign_ids ) ->delete(); } // 3. Delete META rows via QueryBuilder if ( ! empty( $campaign_ids ) ) { DB::get( TableName::ADFUZ_CAMPAIGNS_META() ) ->whereIn( 'campaign_id', $campaign_ids ) ->delete(); $log[] = array( 'type' => 'success', 'message' => 'Deleted campaign meta rows.', ); } if ( ! empty( $ad_space_ids ) ) { DB::get( TableName::ADFUZ_AD_SPACES_META() ) ->whereIn( 'ad_space_id', $ad_space_ids ) ->delete(); $log[] = array( 'type' => 'success', 'message' => 'Deleted ad space meta rows.', ); } if ( ! empty( $ad_ids ) ) { DB::get( TableName::ADFUZ_ADS_META() ) ->whereIn( 'ad_id', $ad_ids ) ->delete(); $log[] = array( 'type' => 'success', 'message' => 'Deleted ad meta rows.', ); } // 4. Delete MAIN table rows via QueryBuilder if ( ! empty( $campaign_ids ) ) { DB::get( TableName::ADFUZ_CAMPAIGNS() ) ->whereIn( 'id', $campaign_ids ) ->delete(); $log[] = array( 'type' => 'success', 'message' => 'Deleted ' . count( $campaign_ids ) . ' sample campaign(s).', ); } if ( ! empty( $ad_space_ids ) ) { DB::get( TableName::ADFUZ_AD_SPACES() ) ->whereIn( 'id', $ad_space_ids ) ->delete(); $log[] = array( 'type' => 'success', 'message' => 'Deleted ' . count( $ad_space_ids ) . ' sample ad space(s).', ); } if ( ! empty( $ad_ids ) ) { DB::get( TableName::ADFUZ_ADS() ) ->whereIn( 'id', $ad_ids ) ->delete(); $log[] = array( 'type' => 'success', 'message' => 'Deleted ' . count( $ad_ids ) . ' sample ad(s).', ); } if ( empty( $campaign_ids ) && empty( $ad_space_ids ) && empty( $ad_ids ) ) { $log[] = array( 'type' => 'error', 'message' => 'No sample data found to delete.', ); } // 5. Delete global ads options $seeder_ids = self::get_seeder_ids(); if ( ! empty( $seeder_ids['global_ads'] ) && is_array( $seeder_ids['global_ads'] ) ) { foreach ( $seeder_ids['global_ads'] as $option_name ) { delete_option( $option_name ); $log[] = array( 'type' => 'success', 'message' => "Deleted global ads option: {$option_name}", ); } unset( $seeder_ids['global_ads'] ); self::save_seeder_ids( $seeder_ids ); } return $log; } public static function remove_duplicates(): array { $log = array(); $removed = 0; $dup_ad_slugs = DB::get( TableName::ADFUZ_ADS() ) ->select( 'slug' ) ->groupBy( 'slug' ) ->having( 'COUNT(*)', '>', 1 ) ->get(); foreach ( $dup_ad_slugs as $slug ) { $ids = DB::get( TableName::ADFUZ_ADS() ) ->where( 'slug', $slug ) ->orderBy( 'id', 'ASC' ) ->select( 'id' ) ->get(); $ids = array_column( $ids, 'id' ); array_shift( $ids ); foreach ( $ids as $id ) { DB::get( TableName::ADFUZ_ADS_META() )->where( 'ad_id', $id )->delete(); DB::get( TableName::ADFUZ_ADS() )->hardDelete( $id ); ++$removed; } } $dup_ca = DB::get( TableName::ADFUZ_CAMPAIGN_ADS() ) ->select( 'campaign_id', 'ad_id' ) ->groupBy( 'campaign_id', 'ad_id' ) ->having( 'COUNT(*)', '>', 1 ) ->get(); foreach ( $dup_ca as $row ) { $ids = DB::get( TableName::ADFUZ_CAMPAIGN_ADS() ) ->where( 'campaign_id', $row->campaign_id ) ->where( 'ad_id', $row->ad_id ) ->orderBy( 'id', 'ASC' ) ->select( 'id' ) ->get(); $ids = array_column( $ids, 'id' ); array_shift( $ids ); foreach ( $ids as $id ) { DB::get( TableName::ADFUZ_CAMPAIGN_ADS() )->hardDelete( $id ); ++$removed; } } $dup_cs = DB::get( TableName::ADFUZ_CAMPAIGN_AD_SPACES() ) ->select( 'campaign_id', 'ad_space_id' ) ->groupBy( 'campaign_id', 'ad_space_id' ) ->having( 'COUNT(*)', '>', 1 ) ->get(); foreach ( $dup_cs as $row ) { $ids = DB::get( TableName::ADFUZ_CAMPAIGN_AD_SPACES() ) ->where( 'campaign_id', $row->campaign_id ) ->where( 'ad_space_id', $row->ad_space_id ) ->orderBy( 'id', 'ASC' ) ->select( 'id' ) ->get(); $ids = array_column( $ids, 'id' ); array_shift( $ids ); foreach ( $ids as $id ) { DB::get( TableName::ADFUZ_CAMPAIGN_AD_SPACES() )->hardDelete( $id ); ++$removed; } } $log[] = array( 'type' => 'success', 'message' => "Removed {$removed} duplicate record(s).", ); return $log; } // ------------------------------------------------------------------------- // Seed helpers // ------------------------------------------------------------------------- private static function seed_ads( array &$log, int $user_id ): array { $ids = array(); $seeder_ids = self::get_seeder_ids(); $existing = $seeder_ids['ad'] ?? array(); $video_json = self::SEEDER_DIR . '/video/video.json'; $yt_links = array(); $vm_links = array(); if ( file_exists( $video_json ) ) { $vdata = json_decode( file_get_contents( $video_json ), true ); $yt_links = array_values( $vdata['youtube'] ?? array() ); $vm_links = array_values( $vdata['vimeo'] ?? array() ); } $ads_data = array( array( 'ad_type' => 'image', 'title' => '160x600 - 1', 'size' => '160x600', 'slug' => 'demo-160x600-1', 'description' => 'Description 160x600 1', 'target_url' => 'https://adfuz.com', 'cta' => 'Discover', 'media_file' => self::SEEDER_DIR . '/image/160x600-1.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '160x600 - 2', 'size' => '160x600', 'slug' => 'demo-160x600-2', 'description' => 'Description 160x600 2', 'target_url' => 'https://adfuz.com', 'cta' => 'Taste it', 'media_file' => self::SEEDER_DIR . '/image/160x600-2.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '160x600 - 3', 'size' => '160x600', 'slug' => 'demo-160x600-3', 'description' => 'Description 160x600 3', 'target_url' => 'https://adfuz.com', 'cta' => 'Feel it', 'media_file' => self::SEEDER_DIR . '/image/160x600-3.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '300x250 - 1', 'size' => '300x250', 'slug' => 'demo-300x250-1', 'description' => 'Description 300x250 1', 'target_url' => 'https://adfuz.com', 'cta' => 'Taste it', 'media_file' => self::SEEDER_DIR . '/image/300x250-1.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '300x250 - 2', 'size' => '300x250', 'slug' => 'demo-300x250-2', 'description' => 'Description 300x250 2', 'target_url' => 'https://adfuz.com', 'cta' => 'Taste it', 'media_file' => self::SEEDER_DIR . '/image/300x250-2.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '300x250 - 3', 'size' => '300x250', 'slug' => 'demo-300x250-3', 'description' => 'Description 300x250 3', 'target_url' => 'https://adfuz.com', 'cta' => 'Buy Now', 'media_file' => self::SEEDER_DIR . '/image/300x250-3.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '300x300 - 1', 'size' => '300x300', 'slug' => 'demo-300x300-1', 'description' => 'Description 300x300 1', 'target_url' => 'https://adfuz.com', 'cta' => 'Buy Now', 'media_file' => self::SEEDER_DIR . '/image/300x300-1.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '300x300 - 2', 'size' => '300x300', 'slug' => 'demo-300x300-2', 'description' => 'Description 300x300 2', 'target_url' => 'https://adfuz.com', 'cta' => 'Feed it', 'media_file' => self::SEEDER_DIR . '/image/300x300-2.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '300x300 - 3', 'size' => '300x300', 'slug' => 'demo-300x300-3', 'description' => 'Description 300x300 3', 'target_url' => 'https://adfuz.com', 'cta' => 'Feed it', 'media_file' => self::SEEDER_DIR . '/image/300x300-3.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '300x600 - 1', 'size' => '300x600', 'slug' => 'demo-300x600-1', 'description' => 'Description 300x600 1', 'target_url' => 'https://adfuz.com', 'cta' => 'Feel it', 'media_file' => self::SEEDER_DIR . '/image/300x600-1.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '300x600 - 2', 'size' => '300x600', 'slug' => 'demo-300x600-2', 'description' => 'Description 300x600 2', 'target_url' => 'https://adfuz.com', 'cta' => 'See More', 'media_file' => self::SEEDER_DIR . '/image/300x600-2.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '300x600 - 3', 'size' => '300x600', 'slug' => 'demo-300x600-3', 'description' => 'Description 300x600 3', 'target_url' => 'https://adfuz.com', 'cta' => 'Plant it', 'media_file' => self::SEEDER_DIR . '/image/300x600-3.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '728x90 - 1', 'size' => '728x90', 'slug' => 'demo-728x90-1', 'description' => 'Description 728x90 1', 'target_url' => 'https://adfuz.com', 'cta' => 'Taste it', 'media_file' => self::SEEDER_DIR . '/image/728x90-1.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '728x90 - 2', 'size' => '728x90', 'slug' => 'demo-728x90-2', 'description' => 'Description 728x90 2', 'target_url' => 'https://adfuz.com', 'cta' => 'Feel it', 'media_file' => self::SEEDER_DIR . '/image/728x90-2.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '728x90 - 3', 'size' => '728x90', 'slug' => 'demo-728x90-3', 'description' => 'Description 728x90 3', 'target_url' => 'https://adfuz.com', 'cta' => 'Feel it', 'media_file' => self::SEEDER_DIR . '/image/728x90-3.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '970x250 - 1', 'size' => '970x250', 'slug' => 'demo-970x250-1', 'description' => 'Description 970x250 1', 'target_url' => 'https://adfuz.com', 'cta' => 'Feel it', 'media_file' => self::SEEDER_DIR . '/image/970x250-1.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '970x250 - 2', 'size' => '970x250', 'slug' => 'demo-970x250-2', 'description' => 'Description 970x250 2', 'target_url' => 'https://adfuz.com', 'cta' => 'Feel it', 'media_file' => self::SEEDER_DIR . '/image/970x250-2.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => '970x250 - 3', 'size' => '970x250', 'slug' => 'demo-970x250-3', 'description' => 'Description 970x250 3', 'target_url' => 'https://adfuz.com', 'cta' => 'Feel it', 'media_file' => self::SEEDER_DIR . '/image/970x250-3.jpg', 'status' => 'active', ), // Regular Video Ads array( 'ad_type' => 'video', 'title' => 'Red Bull – Gives You Wings', 'size' => '600x600', 'slug' => 'sample-redbull-gives-you-wings', 'description' => 'Energy drink for extraordinary people.', 'target_url' => 'https://adfuz.com', 'cta' => 'Watch Now', 'media_url' => $yt_links[1] ?? '', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'GoPro – Be a Hero', 'size' => '970x250', 'slug' => 'sample-gopro-be-a-hero', 'description' => 'Capture every adventure.', 'target_url' => 'https://adfuz.com', 'cta' => 'Explore', 'media_url' => $yt_links[0] ?? '', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Disney+ Global Popups', 'size' => 'global', 'format' => 'popup', 'slug' => 'global-popup-disney', 'description' => 'Stream Disney, Marvel, Pixar & more.', 'target_url' => 'https://adfuz.com', 'cta' => 'Start Free Trial', 'media_file' => self::SEEDER_DIR . '/video/4.mp4', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Puma Exit Offers', 'size' => 'global', 'format' => 'exit_popup', 'slug' => 'global-exit-puma', 'description' => 'Speed never looked this good.', 'target_url' => 'https://adfuz.com', 'cta' => 'Shop Now', 'media_file' => self::SEEDER_DIR . '/video/5.mp4', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Google Workspace Background', 'size' => 'global', 'format' => 'background', 'slug' => 'global-back-google', 'description' => 'Collaborate in real time from anywhere.', 'target_url' => 'https://adfuz.com', 'cta' => 'Get Started', 'media_file' => self::SEEDER_DIR . '/video/5.mp4', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'HubSpot Lead Capture Popup', 'size' => 'global', 'format' => 'popup', 'slug' => 'global-popup-hubspot', 'description' => 'The all-in-one CRM platform.', 'target_url' => 'https://adfuz.com', 'cta' => 'Try Free', 'media_file' => self::SEEDER_DIR . '/video/3.mp4', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Shopify Retail Expansion Exit', 'size' => 'global', 'format' => 'exit_popup', 'slug' => 'global-exit-shopify', 'description' => 'The platform to start and grow your business.', 'target_url' => 'https://adfuz.com', 'cta' => 'Start Free Trial', 'media_file' => self::SEEDER_DIR . '/video/4.mp4', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Canva Background Layouts', 'size' => 'global', 'format' => 'background', 'slug' => 'global-back-canva', 'description' => 'Create beautiful visuals in minutes.', 'target_url' => 'https://adfuz.com', 'cta' => 'Design Now', 'media_file' => self::SEEDER_DIR . '/video/2.mp4', 'status' => 'active', ), ); foreach ( $ads_data as $ad ) { $existing_ad = DB::get( TableName::ADFUZ_ADS() )->where( 'slug', $ad['slug'] )->first(); if ( $existing_ad ) { $ids[] = $existing_ad['id']; continue; } $attachment_id = null; $media_url = $ad['media_url'] ?? null; if ( ! empty( $ad['media_file'] ) && file_exists( $ad['media_file'] ) ) { $result = self::import_file_to_media( $ad['media_file'] ); $attachment_id = $result['attachment_id'] ?? null; $media_url = $result['url'] ?? null; } $insert = array( 'ad_type' => $ad['ad_type'], 'title' => $ad['title'], 'slug' => $ad['slug'], 'description' => $ad['description'] ?? null, 'target_url' => $ad['target_url'] ?? null, 'media_url' => $media_url, 'media_id' => $attachment_id, 'script_html' => $ad['script_html'] ?? null, 'cta' => $ad['cta'] ?? null, 'cta_type' => 'link', 'cta_position' => 'left', 'link_to' => 'link', 'status' => $ad['status'], 'user_id' => $user_id, ); $ad_id = DB::get( TableName::ADFUZ_ADS() )->insert( $insert ); if ( $ad_id ) { DB::get( TableName::ADFUZ_ADS_META() )->insert( array( 'ad_id' => $ad_id, 'meta_key' => self::SEED_META_KEY, 'meta_value' => self::SEED_META_VALUE, ) ); if ( isset( $ad['format'] ) ) { DB::get( TableName::ADFUZ_ADS_META() )->insert( array( 'ad_id' => $ad_id, 'meta_key' => '_global_format', 'meta_value' => $ad['format'], ) ); } if ( $attachment_id ) { DB::get( TableName::ADFUZ_ADS_META() )->insert( array( 'ad_id' => $ad_id, 'meta_key' => '_attachment_id', 'meta_value' => $attachment_id, ) ); } $ids[] = $ad_id; $log[] = array( 'type' => 'success', 'message' => "Ad created: {$ad['title']}", ); } else { $log[] = array( 'type' => 'error', 'message' => "Failed to create ad: {$ad['title']}", ); } } $seeder_ids['ad'] = array_unique( array_merge( $existing, $ids ) ); self::save_seeder_ids( $seeder_ids ); return $ids; } private static function seed_ad_spaces( array &$log, int $user_id ): array { $ids = array(); $seeder_ids = self::get_seeder_ids(); $existing = $seeder_ids['space'] ?? array(); $spaces = array( array( 'title' => 'Wide Skyscraper', 'slug' => 'wide-skyscraper', 'ad_format' => 'banner', 'ad_sequence' => 'seq', 'width' => 160, 'height' => 600, 'status' => 'active', ), array( 'title' => 'Medium Rectangle', 'slug' => 'medium-rectangle', 'ad_format' => 'banner', 'ad_sequence' => 'random', 'width' => 300, 'height' => 250, 'status' => 'active', ), array( 'title' => 'Square', 'slug' => 'square', 'ad_format' => 'banner', 'ad_sequence' => 'seq', 'width' => 300, 'height' => 300, 'status' => 'active', ), array( 'title' => 'Half Page', 'slug' => 'half-page', 'ad_format' => 'banner', 'ad_sequence' => 'seq', 'width' => 300, 'height' => 600, 'status' => 'active', ), array( 'title' => 'Leaderboard', 'slug' => 'leaderboard', 'ad_format' => 'slider', 'ad_sequence' => 'random', 'width' => 728, 'height' => 90, 'status' => 'active', ), array( 'title' => 'Billboard', 'slug' => 'billboard', 'ad_format' => 'banner', 'ad_sequence' => 'random', 'width' => 970, 'height' => 250, 'status' => 'active', ), ); // Loop over extensible global array map configurations to append custom spaces foreach ( self::GLOBAL_TYPES as $format => $config ) { $spaces[] = array( 'title' => $config['title'] . ' Space', 'slug' => $config['slug'], 'ad_format' => 'global', 'ad_sequence' => 'random', 'width' => 0, 'height' => 0, 'status' => 'active', 'is_global' => $format, ); } foreach ( $spaces as $space ) { $existing_space = DB::get( TableName::ADFUZ_AD_SPACES() )->where( 'slug', $space['slug'] )->first(); if ( $existing_space ) { $ids[] = $existing_space['id']; continue; } $space_id = DB::get( TableName::ADFUZ_AD_SPACES() )->insert( array( 'title' => $space['title'], 'slug' => $space['slug'], 'ad_format' => $space['ad_format'], 'ad_sequence' => $space['ad_sequence'], 'width' => $space['width'], 'height' => $space['height'], 'availability' => 1, 'status' => $space['status'], 'user_id' => $user_id, ) ); if ( $space_id ) { DB::get( TableName::ADFUZ_AD_SPACES_META() )->insert( array( 'ad_space_id' => $space_id, 'meta_key' => self::SEED_META_KEY, 'meta_value' => self::SEED_META_VALUE, ) ); if ( isset( $space['is_global'] ) ) { DB::get( TableName::ADFUZ_AD_SPACES_META() )->insert( array( 'ad_space_id' => $space_id, 'meta_key' => '_global_format', 'meta_value' => $space['is_global'], ) ); } $ids[] = $space_id; $log[] = array( 'type' => 'success', 'message' => "Ad Space created: {$space['title']}", ); } else { $log[] = array( 'type' => 'error', 'message' => "Failed to create ad space: {$space['title']}", ); } } $seeder_ids['space'] = array_unique( array_merge( $existing, $ids ) ); self::save_seeder_ids( $seeder_ids ); return $ids; } private static function seed_campaigns( array &$log, int $user_id ): array { $ids = array(); $seeder_ids = self::get_seeder_ids(); $existing = $seeder_ids['campaign'] ?? array(); $now = current_time( 'timestamp' ); $campaigns = array( array( 'name' => 'Campaign 160x600', 'slug' => 'campaign-160x600', 'pricing_model' => 'per_impression', 'price' => 2.50, 'currency' => 'USD', 'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-90 days', $now ) ), 'end_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+15 days', $now ) ), 'expiry_impressions' => 100000, 'status' => 'active', ), array( 'name' => 'Campaign 300x250', 'slug' => 'campaign-300x250', 'pricing_model' => 'flat', 'price' => 500.00, 'currency' => 'USD', 'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-10 days', $now ) ), 'end_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+20 days', $now ) ), 'status' => 'active', ), array( 'name' => 'Campaign 300x300', 'slug' => 'campaign-300x300', 'pricing_model' => 'per_click', 'price' => 0.75, 'currency' => 'USD', 'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-5 days', $now ) ), 'end_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+60 days', $now ) ), 'expiry_clicks' => 5000, 'status' => 'active', ), array( 'name' => 'Campaign 300x600', 'slug' => 'campaign-300x600', 'pricing_model' => 'mixed', 'price' => 1.20, 'currency' => 'USD', 'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-30 days', $now ) ), 'end_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+75 days', $now ) ), 'status' => 'paused', ), array( 'name' => 'Campaign 728x90', 'slug' => 'campaign-728x90', 'pricing_model' => 'per_impression', 'price' => 5.00, 'currency' => 'USD', 'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-7 days', $now ) ), 'end_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+14 days', $now ) ), 'status' => 'active', ), array( 'name' => 'Campaign 970x250', 'slug' => 'campaign-970x250', 'pricing_model' => 'per_impression', 'price' => 5.00, 'currency' => 'USD', 'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-7 days', $now ) ), 'end_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+14 days', $now ) ), 'status' => 'active', ), ); foreach ( self::GLOBAL_TYPES as $format => $config ) { $campaigns[] = array( 'name' => 'Campaign ' . $config['title'], 'slug' => 'campaign-global-' . $format, 'pricing_model' => 'flat', 'price' => 350.00, 'currency' => 'USD', 'start_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-15 days', $now ) ), 'end_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+45 days', $now ) ), 'status' => 'active', 'is_global' => $format, ); } foreach ( $campaigns as $camp ) { $existing_camp = DB::get( TableName::ADFUZ_CAMPAIGNS() )->where( 'slug', $camp['slug'] )->first(); if ( $existing_camp ) { $ids[] = $existing_camp['id']; continue; } $camp_id = DB::get( TableName::ADFUZ_CAMPAIGNS() )->insert( array( 'name' => $camp['name'], 'slug' => $camp['slug'], 'pricing_model' => $camp['pricing_model'], 'price' => $camp['price'], 'currency' => $camp['currency'], 'start_date' => $camp['start_date'] ?? null, 'end_date' => $camp['end_date'] ?? null, 'expiry_impressions' => $camp['expiry_impressions'] ?? null, 'expiry_clicks' => $camp['expiry_clicks'] ?? null, 'status' => $camp['status'], 'user_id' => $user_id, ) ); if ( $camp_id ) { DB::get( TableName::ADFUZ_CAMPAIGNS_META() )->insert( array( 'campaign_id' => $camp_id, 'meta_key' => self::SEED_META_KEY, 'meta_value' => self::SEED_META_VALUE, ) ); if ( isset( $camp['is_global'] ) ) { DB::get( TableName::ADFUZ_CAMPAIGNS_META() )->insert( array( 'campaign_id' => $camp_id, 'meta_key' => '_global_format', 'meta_value' => $camp['is_global'], ) ); } $ids[] = $camp_id; $log[] = array( 'type' => 'success', 'message' => "Campaign created: {$camp['name']}", ); } else { $log[] = array( 'type' => 'error', 'message' => "Failed to create campaign: {$camp['name']}", ); } } $seeder_ids['campaign'] = array_unique( array_merge( $existing, $ids ) ); self::save_seeder_ids( $seeder_ids ); return $ids; } private static function seed_campaign_relations( array &$log, array $campaign_ids, array $ad_ids, array $space_ids ): void { if ( empty( $campaign_ids ) || empty( $ad_ids ) || empty( $space_ids ) ) { return; } // 1. Fetch relations data alongside their assigned custom meta definitions $spaces = DB::get( TableName::ADFUZ_AD_SPACES() )->whereIn( 'id', $space_ids )->select( 'id', 'width', 'height', 'slug' )->get(); $campaigns = DB::get( TableName::ADFUZ_CAMPAIGNS() )->whereIn( 'id', $campaign_ids )->select( 'id', 'slug', 'name' )->get(); $ads = DB::get( TableName::ADFUZ_ADS() )->whereIn( 'id', $ad_ids )->select( 'id', 'slug', 'ad_type', 'title' )->get(); $spaces_by_size = array(); $global_spaces = array(); foreach ( $spaces as $space ) { $global_meta = DB::get( TableName::ADFUZ_AD_SPACES_META() )->where( 'ad_space_id', $space['id'] )->where( 'meta_key', '_global_format' )->first(); if ( $global_meta ) { $global_spaces[ $global_meta['meta_value'] ][] = $space['id']; } else { $size_key = $space['width'] . 'x' . $space['height']; $spaces_by_size[ $size_key ][] = $space['id']; } } $campaigns_by_size = array(); $global_campaigns = array(); foreach ( $campaigns as $campaign ) { $global_meta = DB::get( TableName::ADFUZ_CAMPAIGNS_META() )->where( 'campaign_id', $campaign['id'] )->where( 'meta_key', '_global_format' )->first(); if ( $global_meta ) { $global_campaigns[ $global_meta['meta_value'] ][] = $campaign['id']; } elseif ( preg_match( '/-(\d+x\d+)$/', $campaign['slug'], $matches ) ) { $campaigns_by_size[ $matches[1] ][] = $campaign['id']; } } $ads_by_size = array(); $global_ads = array(); foreach ( $ads as $ad ) { $global_meta = DB::get( TableName::ADFUZ_ADS_META() )->where( 'ad_id', $ad['id'] )->where( 'meta_key', '_global_format' )->first(); if ( $global_meta ) { $global_ads[ $global_meta['meta_value'] ][] = $ad['id']; } elseif ( preg_match( '/-(\d+x\d+)(?:-|$)/', $ad['slug'], $matches ) ) { $ads_by_size[ $matches[1] ][] = $ad['id']; } } // 4. Map Normal Dimensional Relations foreach ( $spaces_by_size as $size_key => $space_ids_for_size ) { if ( isset( $campaigns_by_size[ $size_key ] ) ) { foreach ( $campaigns_by_size[ $size_key ] as $campaign_id ) { foreach ( $space_ids_for_size as $space_id ) { if ( ! DB::get( TableName::ADFUZ_CAMPAIGN_AD_SPACES() )->where( 'campaign_id', $campaign_id )->where( 'ad_space_id', $space_id )->exists() ) { DB::get( TableName::ADFUZ_CAMPAIGN_AD_SPACES() )->insert( array( 'campaign_id' => $campaign_id, 'ad_space_id' => $space_id, 'display_order' => 0, 'status' => 'active', ) ); } } } } } foreach ( $ads_by_size as $size_key => $ad_ids_for_size ) { if ( isset( $campaigns_by_size[ $size_key ] ) ) { foreach ( $campaigns_by_size[ $size_key ] as $campaign_id ) { $order = 0; foreach ( $ad_ids_for_size as $ad_id ) { if ( ! DB::get( TableName::ADFUZ_CAMPAIGN_ADS() )->where( 'campaign_id', $campaign_id )->where( 'ad_id', $ad_id )->exists() ) { DB::get( TableName::ADFUZ_CAMPAIGN_ADS() )->insert( array( 'campaign_id' => $campaign_id, 'ad_id' => $ad_id, 'display_order' => $order++, ) ); } } } } } // 5. Connect Extensible Global Format Framework foreach ( self::GLOBAL_TYPES as $format => $config ) { if ( isset( $global_campaigns[ $format ] ) && isset( $global_spaces[ $format ] ) ) { foreach ( $global_campaigns[ $format ] as $campaign_id ) { foreach ( $global_spaces[ $format ] as $space_id ) { if ( ! DB::get( TableName::ADFUZ_CAMPAIGN_AD_SPACES() )->where( 'campaign_id', $campaign_id )->where( 'ad_space_id', $space_id )->exists() ) { DB::get( TableName::ADFUZ_CAMPAIGN_AD_SPACES() )->insert( array( 'campaign_id' => $campaign_id, 'ad_space_id' => $space_id, 'display_order' => 0, 'status' => 'active', ) ); } } } } if ( isset( $global_campaigns[ $format ] ) && isset( $global_ads[ $format ] ) ) { foreach ( $global_campaigns[ $format ] as $campaign_id ) { $order = 0; foreach ( $global_ads[ $format ] as $ad_id ) { if ( ! DB::get( TableName::ADFUZ_CAMPAIGN_ADS() )->where( 'campaign_id', $campaign_id )->where( 'ad_id', $ad_id )->exists() ) { DB::get( TableName::ADFUZ_CAMPAIGN_ADS() )->insert( array( 'campaign_id' => $campaign_id, 'ad_id' => $ad_id, 'display_order' => $order++, ) ); } } } } } $log[] = array( 'type' => 'success', 'message' => 'Campaign → Ads and Ad Spaces linked flawlessly by dynamic types.', ); } // ------------------------------------------------------------------------- // Media importer // ------------------------------------------------------------------------- private static function import_file_to_media( string $file_path ): array { if ( ! function_exists( 'wp_insert_attachment' ) ) { require_once ABSPATH . 'wp-includes/post.php'; } if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { require_once ABSPATH . 'wp-admin/includes/image.php'; } if ( ! function_exists( 'wp_handle_sideload' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } if ( ! function_exists( 'media_handle_sideload' ) ) { require_once ABSPATH . 'wp-admin/includes/media.php'; } $upload_dir = wp_upload_dir(); $dest_dir = $upload_dir['basedir'] . '/adfuz-seeder/'; $dest_url = $upload_dir['baseurl'] . '/adfuz-seeder/'; if ( ! file_exists( $dest_dir ) ) { wp_mkdir_p( $dest_dir ); } $filename = basename( $file_path ); $dest = $dest_dir . $filename; if ( ! file_exists( $dest ) ) { copy( $file_path, $dest ); } $file_url = $dest_url . $filename; $mime_type = mime_content_type( $dest ); $attachment = array( 'guid' => $file_url, 'post_mime_type' => $mime_type, 'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ), 'post_content' => '', 'post_status' => 'inherit', ); $attach_id = wp_insert_attachment( $attachment, $dest ); if ( ! is_wp_error( $attach_id ) ) { $attach_data = wp_generate_attachment_metadata( $attach_id, $dest ); wp_update_attachment_metadata( $attach_id, $attach_data ); } else { $attach_id = null; } return array( 'attachment_id' => $attach_id, 'url' => $file_url, ); } // ------------------------------------------------------------------------- // Tracking helpers // ------------------------------------------------------------------------- private static function get_seeder_ids(): array { return get_option( 'adfuz_seeder_ids', array() ); } private static function save_seeder_ids( array $ids ): void { update_option( 'adfuz_seeder_ids', $ids, false ); } private static function seeded_ids( string $type ): array { $ids = self::get_seeder_ids(); return $ids[ $type ] ?? array(); } private static function count_campaign_relations( string $type ): int { global $wpdb; $campaign_ids = self::seeded_ids( 'campaign' ); if ( empty( $campaign_ids ) ) { return 0; } $campaign_ids = array_map( 'intval', $campaign_ids ); $placeholders = implode( ',', array_fill( 0, count( $campaign_ids ), '%d' ) ); $table_name = ( 'ads' === $type ) ? esc_sql( $wpdb->prefix . 'adfuz_campaign_ads' ) : esc_sql( $wpdb->prefix . 'adfuz_campaign_ad_spaces' ); return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM `{$table_name}` WHERE campaign_id IN ({$placeholders})", ...$campaign_ids ) ); } private static function seed_global_ads( array &$log, array $space_ids, int $user_id ): void { $now = current_time( 'mysql' ); // 1. Dynamic construction matching options with specific dedicated spaces foreach ( self::GLOBAL_TYPES as $format => $config ) { $space = DB::get( TableName::ADFUZ_AD_SPACES() ) ->whereIn( 'id', $space_ids ) ->where( 'slug', $config['slug'] ) ->first(); $ad_space_id = $space ? (int) $space['id'] : 0; // Construct uniform layout setup payload structures $settings = array( 'overlay_color' => '#000000', 'overlay_opacity' => 60, 'box_bg_color' => '#ffffff', 'box_padding' => 20, 'box_radius' => 12, 'box_width' => 600, 'box_max_width' => 90, 'title_color' => '#000000', 'desc_color' => '#666666', 'cta_color' => '#2271b1', 'cta_text_color' => '#ffffff', 'close_color' => '#666666', 'close_bg_color' => 'transparent', ); if ( 'popup' === $format ) { $settings['delay'] = 3; } $payload = array( 'status' => 'active', 'updated_at' => $now, 'rules' => array( array( 'title' => $config['title'] . ' Rule', 'priority' => 10, 'status' => 'active', 'scope' => 'all', 'post_ids' => array(), 'post_types' => array(), 'ad_space_id' => $ad_space_id, 'settings' => ( 'background' === $format ) ? array() : $settings, ), ), ); update_option( $config['option_name'], $payload, false ); } $seeder_ids = self::get_seeder_ids(); $seeder_ids['global_ads'] = self::SEED_GLOBAL_ADS_OPTIONS; self::save_seeder_ids( $seeder_ids ); $log[] = array( 'type' => 'success', 'message' => 'Global layout formats mapped to specialized unique adspaces.', ); } }