get_charset_collate(); require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $wpdb->hide_errors(); // Suppress errors during table creation // ── Add-to-cart tracking ───────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_cart_report ( id bigint(20) NOT NULL AUTO_INCREMENT, product_id bigint(20) NOT NULL, user_id bigint(20) NOT NULL DEFAULT 0, ip_address varchar(45) NOT NULL DEFAULT '', created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY product_id (product_id), KEY user_id (user_id) ) $charset;" ); // ── System orders (vendor ↔ order mapping) ─────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_system_orders ( id bigint(20) NOT NULL AUTO_INCREMENT, product_id bigint(20) NOT NULL, order_id bigint(20) NOT NULL, product_owner_user_id bigint(20) NOT NULL, order_owner_user_id bigint(20) NOT NULL, ip_address varchar(45) NOT NULL DEFAULT '', created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY order_id (order_id), KEY product_owner_user_id (product_owner_user_id) ) $charset;" ); // ── Wallet transactions ────────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_wallet_transactions ( id bigint(20) NOT NULL AUTO_INCREMENT, user_id bigint(20) NOT NULL, product_id bigint(20) NOT NULL DEFAULT 0, order_id bigint(20) NOT NULL DEFAULT 0, amount decimal(10,2) NOT NULL, type varchar(10) NOT NULL COMMENT 'credit|debit', description text, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY user_id (user_id) ) $charset;" ); // ── Withdrawal requests ────────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_withdrawals ( id bigint(20) NOT NULL AUTO_INCREMENT, vendor_id bigint(20) NOT NULL, amount decimal(10,2) NOT NULL, method varchar(50) NOT NULL DEFAULT 'bank', account_details text, gateway_charge decimal(10,2) NOT NULL DEFAULT 0.00, status varchar(20) NOT NULL DEFAULT 'pending', admin_note text, transaction_ref varchar(100) DEFAULT NULL, processed_at datetime DEFAULT NULL, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY vendor_id (vendor_id), KEY status (status) ) $charset;" ); // ── Product approval workflow ──────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_product_approvals ( id bigint(20) NOT NULL AUTO_INCREMENT, product_id bigint(20) NOT NULL, vendor_id bigint(20) NOT NULL, status varchar(20) NOT NULL DEFAULT 'pending', admin_notes text, revision_requested text, submitted_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, reviewed_at datetime DEFAULT NULL, PRIMARY KEY (id), KEY product_id (product_id), KEY vendor_id (vendor_id), KEY status (status) ) $charset;" ); // ── Vendor reviews ─────────────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_vendor_reviews ( id bigint(20) NOT NULL AUTO_INCREMENT, vendor_id bigint(20) NOT NULL, customer_id bigint(20) NOT NULL, order_id bigint(20) NOT NULL, rating tinyint(1) NOT NULL DEFAULT 5, review text, vendor_response text, is_verified tinyint(1) NOT NULL DEFAULT 0, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY vendor_id (vendor_id), KEY customer_id (customer_id) ) $charset;" ); // ── Vendor messages (live chat) ────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_messages ( id bigint(20) NOT NULL AUTO_INCREMENT, conversation_id bigint(20) NOT NULL, sender_id bigint(20) NOT NULL, receiver_id bigint(20) NOT NULL, order_id bigint(20) NOT NULL DEFAULT 0, message text NOT NULL, attachment_url varchar(500) DEFAULT NULL, is_read tinyint(1) NOT NULL DEFAULT 0, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY conversation_id (conversation_id), KEY receiver_id (receiver_id) ) $charset;" ); // ── Vendor followers ───────────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_followers ( id bigint(20) NOT NULL AUTO_INCREMENT, vendor_id bigint(20) NOT NULL, customer_id bigint(20) NOT NULL, followed_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY unique_follow (vendor_id, customer_id) ) $charset;" ); // ── Vendor subscription plans ──────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_vendor_plans ( id bigint(20) NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL, price decimal(10,2) NOT NULL DEFAULT 0.00, duration_days int(11) NOT NULL DEFAULT 30, max_products int(11) NOT NULL DEFAULT 10, commission_rate decimal(5,2) NOT NULL DEFAULT 10.00, featured_listing tinyint(1) NOT NULL DEFAULT 0, priority_support tinyint(1) NOT NULL DEFAULT 0, is_active tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (id) ) $charset;" ); // ── Vendor subscriptions ───────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_vendor_subscriptions ( id bigint(20) NOT NULL AUTO_INCREMENT, vendor_id bigint(20) NOT NULL, plan_id bigint(20) NOT NULL, starts_at datetime NOT NULL, expires_at datetime NOT NULL, status varchar(20) NOT NULL DEFAULT 'active', PRIMARY KEY (id), KEY vendor_id (vendor_id) ) $charset;" ); // ── Fraud alerts ───────────────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_fraud_alerts ( id bigint(20) NOT NULL AUTO_INCREMENT, vendor_id bigint(20) DEFAULT NULL, customer_id bigint(20) DEFAULT NULL, order_id bigint(20) DEFAULT NULL, alert_type varchar(50) NOT NULL, severity varchar(20) NOT NULL DEFAULT 'low', details text, is_resolved tinyint(1) NOT NULL DEFAULT 0, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY alert_type (alert_type), KEY is_resolved (is_resolved) ) $charset;" ); // ── Webhooks ───────────────────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_webhooks ( id bigint(20) NOT NULL AUTO_INCREMENT, vendor_id bigint(20) NOT NULL, name varchar(100) NOT NULL, url varchar(500) NOT NULL, event_type varchar(50) NOT NULL, secret_key varchar(64) NOT NULL DEFAULT '', is_active tinyint(1) NOT NULL DEFAULT 1, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY vendor_id (vendor_id) ) $charset;" ); // ── Webhook logs ───────────────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_webhook_logs ( id bigint(20) NOT NULL AUTO_INCREMENT, webhook_id bigint(20) NOT NULL, event_type varchar(50) NOT NULL, payload longtext, response text, http_code int(11) DEFAULT NULL, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY webhook_id (webhook_id) ) $charset;" ); // ── Affiliates ─────────────────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_affiliates ( id bigint(20) NOT NULL AUTO_INCREMENT, referrer_vendor_id bigint(20) NOT NULL, referred_vendor_id bigint(20) NOT NULL, commission_rate decimal(5,2) NOT NULL DEFAULT 5.00, total_earned decimal(10,2) NOT NULL DEFAULT 0.00, status varchar(20) NOT NULL DEFAULT 'active', created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) ) $charset;" ); // ── Vendor inventory ───────────────────────────────────────────────── dbDelta( "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}vv_inventory ( id bigint(20) NOT NULL AUTO_INCREMENT, vendor_id bigint(20) NOT NULL, product_id bigint(20) NOT NULL, sku varchar(100) DEFAULT NULL, quantity int(11) NOT NULL DEFAULT 0, low_stock_threshold int(11) NOT NULL DEFAULT 5, last_updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY vendor_product (vendor_id, product_id) ) $charset;" ); // ── Composite indexes for performance ──────────────────────────────── $tables_indexes = [ "{$wpdb->prefix}vv_wallet_transactions" => [ "ADD INDEX IF NOT EXISTS idx_user_created (user_id, created_at)", "ADD INDEX IF NOT EXISTS idx_user_type (user_id, type)", ], "{$wpdb->prefix}vv_system_orders" => [ "ADD INDEX IF NOT EXISTS idx_vendor_order (product_owner_user_id, order_id)", "ADD INDEX IF NOT EXISTS idx_vendor_product (product_owner_user_id, product_id)", ], "{$wpdb->prefix}vv_cart_report" => [ "ADD INDEX IF NOT EXISTS idx_product_date (product_id, created_at)", ], "{$wpdb->prefix}vv_messages" => [ "ADD INDEX IF NOT EXISTS idx_conv_date (conversation_id, created_at)", ], ]; foreach ( $tables_indexes as $table => $indexes ) { foreach ( $indexes as $idx ) { $wpdb->query( "ALTER TABLE `$table` $idx" ); // Silently skip if exists } } $wpdb->show_errors(); update_option( 'vendorverse_db_version', VENDORVERSE_VERSION ); } private static function add_default_options() { $defaults = [ 'vv_commission_rate' => 10, 'vv_min_withdrawal' => 20, 'vv_auto_approve_products' => 'no', 'vv_product_show_in_menu' => 'yes', 'vv_vendor_store_slug' => 'vendor', 'vv_payout_schedule' => 'manual', 'vv_fraud_detection' => 'yes', 'vv_enable_chat' => 'yes', 'vv_enable_reviews' => 'yes', 'vv_enable_affiliate' => 'yes', 'vv_add_product_page_id' => 0, 'vv_list_product_page_id' => 0, 'vv_edit_product_page_id' => 0, 'vv_list_order_page_id' => 0, 'vv_edit_order_page_id' => 0, 'vv_vendor_dashboard_page_id'=> 0, ]; foreach ( $defaults as $key => $val ) { if ( get_option( $key ) === false ) { add_option( $key, $val ); } } // Insert default subscription plans — check table exists first global $wpdb; $table_exists = $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}vv_vendor_plans'" ); $count = $table_exists ? (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}vv_vendor_plans" ) : 0; if ( $count == 0 ) { $wpdb->insert( "{$wpdb->prefix}vv_vendor_plans", [ 'name' => 'Free', 'price' => 0, 'duration_days' => 365, 'max_products' => 5, 'commission_rate' => 15, 'featured_listing' => 0, 'priority_support' => 0, ] ); $wpdb->insert( "{$wpdb->prefix}vv_vendor_plans", [ 'name' => 'Pro', 'price' => 9.99, 'duration_days' => 30, 'max_products' => 50, 'commission_rate' => 8, 'featured_listing' => 1, 'priority_support' => 0, ] ); $wpdb->insert( "{$wpdb->prefix}vv_vendor_plans", [ 'name' => 'Enterprise', 'price' => 29.99, 'duration_days' => 30, 'max_products' => 999, 'commission_rate' => 5, 'featured_listing' => 1, 'priority_support' => 1, ] ); } } }