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(); return array( 'ads' => $ads, 'ad_spaces' => $spaces, 'campaigns' => $campaigns, 'campaign_ads' => self::count_campaign_relations( 'ads' ), 'campaign_spaces' => self::count_campaign_relations( 'spaces' ), ); } 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 (these have `id`) 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 (these have `id`) 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.', ); } 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 ); // keep first 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(); $upload_dir = wp_upload_dir(); $seeder_ids = self::get_seeder_ids(); $existing = $seeder_ids['ad'] ?? array(); $script_json = self::SEEDER_DIR . '/script/script.json'; $scripts = array(); if ( file_exists( $script_json ) ) { $decoded = json_decode( file_get_contents( $script_json ), true ); $scripts = is_array( $decoded ) ? array_values( $decoded ) : 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' => 'Nike – Just Do It', 'slug' => 'sample-nike-just-do-it', 'description' => 'Performance sportswear for every athlete.', 'target_url' => 'https://www.nike.com', 'cta' => 'Shop Now', 'media_file' => self::SEEDER_DIR . '/image/1.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => 'Apple MacBook Pro', 'slug' => 'sample-apple-macbook-pro', 'description' => 'The most powerful Mac ever built.', 'target_url' => 'https://www.apple.com/macbook-pro', 'cta' => 'Learn More', 'media_file' => self::SEEDER_DIR . '/image/2.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => 'Coca-Cola – Taste the Feeling', 'slug' => 'sample-coca-cola-taste-feeling', 'description' => 'Share a Coke and share a smile.', 'target_url' => 'https://www.coca-cola.com', 'cta' => 'Discover', 'media_file' => self::SEEDER_DIR . '/image/3.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => 'Samsung Galaxy S25', 'slug' => 'sample-samsung-galaxy-s25', 'description' => 'Redefine what your phone can do.', 'target_url' => 'https://www.samsung.com', 'cta' => 'Buy Now', 'media_file' => self::SEEDER_DIR . '/image/4.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => 'Amazon Prime Day', 'slug' => 'sample-amazon-prime-day', 'description' => 'Exclusive deals only for Prime members.', 'target_url' => 'https://www.amazon.com', 'cta' => 'See Deals', 'media_file' => self::SEEDER_DIR . '/image/5.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => 'Spotify Premium', 'slug' => 'sample-spotify-premium', 'description' => 'Listen without limits.', 'target_url' => 'https://www.spotify.com', 'cta' => 'Try Free', 'media_file' => self::SEEDER_DIR . '/image/6.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => 'Airbnb – Live Anywhere', 'slug' => 'sample-airbnb-live-anywhere', 'description' => 'Book unique stays around the world.', 'target_url' => 'https://www.airbnb.com', 'cta' => 'Explore', 'media_file' => self::SEEDER_DIR . '/image/7.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => 'Tesla Model 3', 'slug' => 'sample-tesla-model-3', 'description' => 'Zero emissions. Infinite performance.', 'target_url' => 'https://www.tesla.com', 'cta' => 'Order Now', 'media_file' => self::SEEDER_DIR . '/image/8.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => 'Netflix – Watch Anywhere', 'slug' => 'sample-netflix-watch-anywhere', 'description' => 'Unlimited movies, TV shows and more.', 'target_url' => 'https://www.netflix.com', 'cta' => 'Start Watching', 'media_file' => self::SEEDER_DIR . '/image/9.jpg', 'status' => 'active', ), array( 'ad_type' => 'image', 'title' => 'Adidas – Impossible is Nothing', 'slug' => 'sample-adidas-impossible-is-nothing', 'description' => 'Sport-inspired streetwear.', 'target_url' => 'https://www.adidas.com', 'cta' => 'Shop', 'media_file' => self::SEEDER_DIR . '/image/10.jpg', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Red Bull – Gives You Wings', 'slug' => 'sample-redbull-gives-you-wings', 'description' => 'Energy drink for extraordinary people.', 'target_url' => 'https://www.redbull.com', 'cta' => 'Watch Now', 'media_file' => self::SEEDER_DIR . '/video/1.mp4', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'GoPro – Be a Hero', 'slug' => 'sample-gopro-be-a-hero', 'description' => 'Capture every adventure.', 'target_url' => 'https://www.gopro.com', 'cta' => 'Explore', 'media_file' => self::SEEDER_DIR . '/video/2.mp4', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'BMW – Sheer Driving Pleasure', 'slug' => 'sample-bmw-sheer-driving-pleasure', 'description' => 'The ultimate driving machine.', 'target_url' => 'https://www.bmw.com', 'cta' => 'Configure Yours', 'media_file' => self::SEEDER_DIR . '/video/3.mp4', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Disney+ – All the Magic', 'slug' => 'sample-disneyplus-all-the-magic', 'description' => 'Stream Disney, Marvel, Pixar & more.', 'target_url' => 'https://www.disneyplus.com', 'cta' => 'Start Free Trial', 'media_file' => self::SEEDER_DIR . '/video/4.mp4', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Puma – Forever Faster', 'slug' => 'sample-puma-forever-faster', 'description' => 'Speed never looked this good.', 'target_url' => 'https://www.puma.com', 'cta' => 'Shop Now', 'media_file' => self::SEEDER_DIR . '/video/5.mp4', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Google Workspace – Work Smarter', 'slug' => 'sample-google-workspace', 'description' => 'Collaborate in real time from anywhere.', 'target_url' => 'https://workspace.google.com', 'cta' => 'Get Started', 'media_url' => $yt_links[0] ?? '', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'HubSpot – Grow Better', 'slug' => 'sample-hubspot-grow-better', 'description' => 'The all-in-one CRM platform.', 'target_url' => 'https://www.hubspot.com', 'cta' => 'Try Free', 'media_url' => $yt_links[1] ?? '', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Shopify – Build Your Empire', 'slug' => 'sample-shopify-build-your-empire', 'description' => 'The platform to start and grow your business.', 'target_url' => 'https://www.shopify.com', 'cta' => 'Start Free Trial', 'media_url' => $vm_links[0] ?? '', 'status' => 'active', ), array( 'ad_type' => 'video', 'title' => 'Canva – Design Anything', 'slug' => 'sample-canva-design-anything', 'description' => 'Create beautiful visuals in minutes.', 'target_url' => 'https://www.canva.com', 'cta' => 'Design Now', 'media_url' => $vm_links[1] ?? '', '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 ) { // Mark as sample 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 ( $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']}", ); } } // Save tracked IDs $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' => 'Homepage Leaderboard', 'slug' => 'sample-homepage-leaderboard', 'ad_format' => 'banner', 'ad_sequence' => 'seq', 'width' => 728, 'height' => 90, 'status' => 'active', ), array( 'title' => 'Sidebar Rectangle', 'slug' => 'sample-sidebar-rectangle', 'ad_format' => 'banner', 'ad_sequence' => 'random', 'width' => 300, 'height' => 250, 'status' => 'active', ), array( 'title' => 'Mobile Banner', 'slug' => 'sample-mobile-banner', 'ad_format' => 'banner', 'ad_sequence' => 'seq', 'width' => 320, 'height' => 50, 'status' => 'active', ), array( 'title' => 'Pre-Roll Video Zone', 'slug' => 'sample-pre-roll-video', 'ad_format' => 'slider', 'ad_sequence' => 'seq', 'width' => 1280, 'height' => 720, 'status' => 'active', ), array( 'title' => 'Article Inline Block', 'slug' => 'sample-article-inline-block', 'ad_format' => 'slider', 'ad_sequence' => 'random', 'width' => 600, 'height' => 200, 'status' => 'active', ), ); 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, ) ); $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, array $ad_ids, array $space_ids, 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' => 'Summer Sale Campaign', 'slug' => 'sample-summer-sale-campaign', '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' => 'Brand Awareness - Live', 'slug' => 'sample-brand-awareness-live', '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' => 'Retargeting - Tech Audience', 'slug' => 'sample-retargeting-tech-audience', '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' => 'Holiday Promo Scheduled', 'slug' => 'sample-holiday-promo-scheduled', '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' => 'Video Pre-Roll Burst', 'slug' => 'sample-video-preroll-burst', '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 ( $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, ) ); $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; } // Each campaign gets ~4 ads and 1–2 ad spaces foreach ( $campaign_ids as $ci => $campaign_id ) { $assigned_ads = array_slice( $ad_ids, ( $ci * 4 ) % count( $ad_ids ), 4 ); $order = 0; foreach ( $assigned_ads as $ad_id ) { $exists = DB::get( TableName::ADFUZ_CAMPAIGN_ADS() ) ->where( 'campaign_id', $campaign_id ) ->where( 'ad_id', $ad_id ) ->exists(); if ( ! $exists ) { DB::get( TableName::ADFUZ_CAMPAIGN_ADS() )->insert( array( 'campaign_id' => $campaign_id, 'ad_id' => $ad_id, 'display_order' => $order++, ) ); } } $assigned_spaces = array_slice( $space_ids, $ci % count( $space_ids ), 2 ); $so = 0; foreach ( $assigned_spaces as $space_id ) { $exists = DB::get( TableName::ADFUZ_CAMPAIGN_AD_SPACES() ) ->where( 'campaign_id', $campaign_id ) ->where( 'ad_space_id', $space_id ) ->exists(); if ( ! $exists ) { DB::get( TableName::ADFUZ_CAMPAIGN_AD_SPACES() )->insert( array( 'campaign_id' => $campaign_id, 'ad_space_id' => $space_id, 'display_order' => $so++, 'status' => 'active', ) ); } } } $log[] = array( 'type' => 'success', 'message' => 'Campaign → Ads and Ad Spaces linked.', ); } // ------------------------------------------------------------------------- // 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; if ( 'ads' === $type ) { $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 = esc_sql( $wpdb->prefix . 'adfuz_campaign_ads' ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table_name sanitised via esc_sql(); $placeholders contains only %d literals, not user input return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM `{$table_name}` WHERE campaign_id IN ({$placeholders})", ...$campaign_ids ) ); } if ( 'spaces' === $type ) { $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 = esc_sql( $wpdb->prefix . 'adfuz_campaign_ad_spaces' ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table_name sanitised via esc_sql(); $placeholders contains only %d literals, not user input return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM `{$table_name}` WHERE campaign_id IN ({$placeholders})", ...$campaign_ids ) ); } return 0; } }