get_var( $wpdb->prepare( "SELECT COALESCE(SUM(amount),0) FROM {$wpdb->prefix}vv_wallet_transactions WHERE user_id=%d AND type='credit'", $user_id ) ); $debit = (float) $wpdb->get_var( $wpdb->prepare( "SELECT COALESCE(SUM(amount),0) FROM {$wpdb->prefix}vv_wallet_transactions WHERE user_id=%d AND type='debit'", $user_id ) ); return round( $credit - $debit, 2 ); } public static function record_transaction( $user_id, $amount, $type, $description, $product_id = 0, $order_id = 0 ) { global $wpdb; return $wpdb->insert( "{$wpdb->prefix}vv_wallet_transactions", [ 'user_id' => $user_id, 'amount' => $amount, 'type' => $type, 'description' => $description, 'product_id' => $product_id, 'order_id' => $order_id, ] ); } public static function get_vendor_stats( $vendor_id ) { $cache_key = 'vv_vendor_stats_' . $vendor_id; $stats = wp_cache_get( $cache_key, 'vendorverse' ); if ( false !== $stats ) return $stats; global $wpdb; $stats = [ 'total_products' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}posts WHERE post_author=%d AND post_type='product' AND post_status='publish'", $vendor_id ) ), 'total_orders' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT order_id) FROM {$wpdb->prefix}vv_system_orders WHERE product_owner_user_id=%d", $vendor_id ) ), 'total_earnings' => (float) $wpdb->get_var( $wpdb->prepare( "SELECT COALESCE(SUM(amount),0) FROM {$wpdb->prefix}vv_wallet_transactions WHERE user_id=%d AND type='credit'", $vendor_id ) ), 'balance' => self::get_vendor_balance( $vendor_id ), 'avg_rating' => (float) $wpdb->get_var( $wpdb->prepare( "SELECT COALESCE(AVG(rating),0) FROM {$wpdb->prefix}vv_vendor_reviews WHERE vendor_id=%d", $vendor_id ) ), 'review_count' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}vv_vendor_reviews WHERE vendor_id=%d", $vendor_id ) ), 'follower_count' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}vv_followers WHERE vendor_id=%d", $vendor_id ) ), ]; wp_cache_set( $cache_key, $stats, 'vendorverse', HOUR_IN_SECONDS ); return $stats; } public static function flush_vendor_cache( $vendor_id ) { wp_cache_delete( 'vv_vendor_stats_' . $vendor_id, 'vendorverse' ); } public static function get_commission_rate( $vendor_id, $product_id = null ) { // Vendor-specific override $vendor_rate = get_user_meta( $vendor_id, 'vv_commission_rate', true ); if ( $vendor_rate !== '' && is_numeric( $vendor_rate ) ) { return (float) $vendor_rate; } // Product-specific override if ( $product_id ) { $product_rate = get_post_meta( $product_id, '_vv_commission_rate', true ); if ( $product_rate !== '' && is_numeric( $product_rate ) ) { return (float) $product_rate; } } // Tiered commission by lifetime sales $total_sales = (float) self::get_vendor_stats( $vendor_id )['total_orders']; if ( $total_sales >= 200 ) return (float) get_option( 'vv_commission_tier3', 5 ); if ( $total_sales >= 50 ) return (float) get_option( 'vv_commission_tier2', 8 ); return (float) get_option( 'vv_commission_rate', 10 ); } public static function currency( $amount ) { return wc_price( $amount ); } public static function sales_chart_data( $vendor_id, $days = 30 ) { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT DATE(t.created_at) AS day, SUM(t.amount) AS total FROM {$wpdb->prefix}vv_wallet_transactions t WHERE t.user_id = %d AND t.type = 'credit' AND t.created_at >= DATE_SUB(NOW(), INTERVAL %d DAY) GROUP BY DATE(t.created_at) ORDER BY day ASC", $vendor_id, $days ) ); $labels = $data = []; foreach ( $rows as $r ) { $labels[] = date( 'M j', strtotime( $r->day ) ); $data[] = (float) $r->total; } return [ 'labels' => $labels, 'data' => $data ]; } public static function allowed_file_types() { return [ 'jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'webp' => 'image/webp', 'pdf' => 'application/pdf', 'zip' => 'application/zip', 'mp3' => 'audio/mpeg', 'mp4' => 'video/mp4', ]; } public static function nonce_field( $action = 'vv_action' ) { wp_nonce_field( $action, 'vv_nonce' ); } public static function verify_nonce( $action = 'vv_action' ) { if ( empty( $_POST['vv_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['vv_nonce'] ) ), $action ) ) { wp_die( esc_html__( 'Security check failed.', 'vendorverse' ) ); } } }