false, 'error' => 'WordPress not loaded']); exit; } global $wpdb; $timeframe = isset($_GET['timeframe']) ? $_GET['timeframe'] : 'lifetime'; try { if ($_GET['api'] === 'granters') { // Top Granters API - query wishes table directly $query = " SELECT u.ID as user_id, u.display_name as granter_name, COUNT(w.id) as wishes_granted, COALESCE(SUM(w.price_paid), 0) as total_amount_granted, COALESCE(AVG(w.price_paid), 0) as average_grant_amount, 1 as level, 'Wisher' as title, 'default.png' as avatar, ( COUNT(w.id) * 10 + COALESCE(SUM(w.price_paid), 0) * 1 ) as impact_score FROM {$wpdb->users} u JOIN {$wpdb->prefix}bmw_wishes w ON u.ID = w.granter_id WHERE w.status IN ('granted', 'completed') AND w.granter_id IS NOT NULL " . ($timeframe === 'this_month' ? "AND w.granted_at >= DATE_FORMAT(NOW(), '%Y-%m-01')" : "") . " GROUP BY u.ID, u.display_name HAVING wishes_granted > 0 ORDER BY impact_score DESC, wishes_granted DESC, total_amount_granted DESC LIMIT 50 "; $results = $wpdb->get_results($query, ARRAY_A); if ($wpdb->last_error) { throw new Exception("Database query failed: " . $wpdb->last_error); } $granters = []; foreach ($results as $row) { $granters[] = [ 'rank' => count($granters) + 1, 'user_id' => (int)$row['user_id'], 'granter_name' => $row['granter_name'], 'impact_score' => (int)$row['impact_score'], 'wishes_granted' => (int)$row['wishes_granted'], 'total_amount_granted' => (float)$row['total_amount_granted'], 'average_grant_amount' => round((float)$row['average_grant_amount'], 2), 'level' => (int)$row['level'], 'title' => $row['title'], 'avatar' => $row['avatar'] ?: 'default.png', 'is_top_granter' => count($granters) === 0 ]; } echo json_encode([ 'success' => true, 'timeframe' => $timeframe, 'total_granters' => count($granters), 'granters' => $granters, 'last_updated' => date('Y-m-d H:i:s') ]); } elseif ($_GET['api'] === 'coins') { // Coin Masters API $query = " SELECT u.ID as user_id, u.display_name as granter_name, COALESCE(SUM(ct.amount), 0) as total_coins_earned, COUNT(DISTINCT g.id) as total_wishes_granted, ROUND(COALESCE(SUM(ct.amount), 0) / GREATEST(COUNT(DISTINCT g.id), 1), 2) as average_earn, 1 as level, 'Wisher' as title, 'default.png' as avatar FROM {$wpdb->users} u JOIN {$wpdb->prefix}bmw_coin_transactions ct ON u.ID = ct.user_id LEFT JOIN {$wpdb->prefix}bmw_grants g ON u.ID = g.granter_id WHERE ct.transaction_type = 'earn' " . ($timeframe === 'this_month' ? "AND ct.created_at >= DATE_FORMAT(NOW(), '%Y-%m-01')" : "") . " GROUP BY u.ID, u.display_name HAVING total_coins_earned > 0 ORDER BY total_coins_earned DESC LIMIT 50 "; $results = $wpdb->get_results($query, ARRAY_A); if ($wpdb->last_error) { throw new Exception("Database query failed: " . $wpdb->last_error); } $coin_masters = []; foreach ($results as $row) { $coin_masters[] = [ 'rank' => count($coin_masters) + 1, 'user_id' => (int)$row['user_id'], 'granter_name' => $row['granter_name'], 'total_coins_earned' => (float)$row['total_coins_earned'], 'average_earn' => round((float)$row['average_earn'], 2), 'total_wishes_granted' => (int)$row['total_wishes_granted'], 'level' => (int)$row['level'], 'title' => $row['title'], 'avatar' => $row['avatar'] ?: 'default.png', 'is_top_granter' => count($coin_masters) === 0 ]; } // Get biggest collection $biggest_query = " SELECT u.display_name as granter_name, ct.description as transaction_description, ct.amount as coin_amount, ct.created_at as collection_date FROM {$wpdb->prefix}bmw_coin_transactions ct JOIN {$wpdb->users} u ON ct.user_id = u.ID WHERE ct.transaction_type = 'earn' " . ($timeframe === 'this_month' ? "AND ct.created_at >= DATE_FORMAT(NOW(), '%Y-%m-01')" : "") . " ORDER BY ct.amount DESC LIMIT 1 "; $biggest_result = $wpdb->get_row($biggest_query, ARRAY_A); echo json_encode([ 'success' => true, 'timeframe' => $timeframe, 'total_coin_masters' => count($coin_masters), 'coin_masters' => $coin_masters, 'biggest_collection' => $biggest_result, 'category_breakdown' => [], 'last_updated' => date('Y-m-d H:i:s') ]); } else { echo json_encode(['success' => false, 'error' => 'Unknown API endpoint']); } } catch (Exception $e) { echo json_encode([ 'success' => false, 'error' => $e->getMessage(), 'timeframe' => $timeframe ]); } exit; } ?> Leaderboard - BuyMyWishes

Settings

Customize your experience

Change Profile Picture

For more avatar options, visit your profile page where you can choose from a wider selection or purchase premium avatars.

Top Granters

Rank
Granter
Impact Score
Wishes Granted
Total Amount
Avg Grant
Level
Title

Loading leaderboard...