Error: Match ID is required.
'; } $match_id = sanitize_text_field($atts['match_id']); $match_data = fetch_and_store_match_info($match_id); if (isset($match_data['error'])) { return 'Error: ' . esc_html($match_data['error']) . '
'; } $match_info = $match_data['match_info']['response'] ?? []; $squad_data = $match_data['squad_data']['response'] ?? []; $advanced_info = $match_data['advanced_info']['response'] ?? []; // Extract team names and IDs $team_a_name = $squad_data['teama']['short_name'] ?? 'Unknown Team'; $team_b_name = $squad_data['teamb']['short_name'] ?? 'Unknown Team'; $team_a_id = $squad_data['teama']['team_id'] ?? null; $team_b_id = $squad_data['teamb']['team_id'] ?? null; $players = array_merge( $squad_data['teama']['squads'] ?? [], $squad_data['teamb']['squads'] ?? [] ); if (empty($players)) { return "No squad data found for the selected match ({$match_id}).
"; } // Prepare data for the table $table_data = []; foreach ($players as $player) { $player_id = $player['player_id'] ?? 0; $player_name = $player['name'] ?? 'Unknown Player'; $player_role = strtoupper($player['role'] ?? 'UNKNOWN'); $player_team_id = $player['team_id'] ?? null; // Determine the team name based on team_id $team_name = 'Unknown Team'; if ($player_team_id == $team_a_id) { $team_name = $team_a_name; } elseif ($player_team_id == $team_b_id) { $team_name = $team_b_name; } if (!$player_id) { continue; } // Fetch Player's Last 10 Match Stats $player_stats_url = API_CALL_BASE_URL . "v4/players/$player_id/advancestats?token=" . GENERATE_DRAFT_TOKEN; $player_response = wp_remote_get($player_stats_url); if (is_wp_error($player_response)) { continue; } $player_data = json_decode(wp_remote_retrieve_body($player_response), true); if (empty($player_data['response']['last10_matches'])) { $table_data[] = [ 'role' => $player_role, 'player' => $player_name, 'team' => $team_name, 'avg_fpts' => 'N/A', 'vs_oppt' => 'N/A', 'at_venue' => 'N/A', '1st_inn' => 'N/A', '2nd_inn' => 'N/A', ]; continue; } $batting_stats = $player_data['response']['last10_matches']['batting'] ?? []; $bowling_stats = $player_data['response']['last10_matches']['bowling'] ?? []; // Calculate Fantasy Points $total_points = 0; $matches_count = 0; foreach ($batting_stats as $match) { $runs = intval($match['runs'] ?? 0); $notout = intval($match['notout'] ?? 0); $duck = ($runs == 0 && $notout == 0) ? -2 : 0; $total_points += $runs + $duck; $matches_count++; } foreach ($bowling_stats as $match) { $wickets = intval($match['wickets'] ?? 0); $four_wickets_bonus = ($wickets >= 4) ? 4 : 0; $five_wickets_bonus = ($wickets >= 5) ? 8 : 0; $total_points += ($wickets * 25) + $four_wickets_bonus + $five_wickets_bonus; } $avg_points = $matches_count > 0 ? round($total_points / $matches_count, 2) : 'N/A'; $table_data[] = [ 'role' => $player_role, 'player' => $player_name, 'team' => $team_name, 'avg_fpts' => $avg_points, 'vs_oppt' => 'N/A', // Placeholder for Vs Opponent stats 'at_venue' => 'N/A', // Placeholder for At Venue stats '1st_inn' => 'N/A', // Placeholder for 1st Inn stats '2nd_inn' => 'N/A', // Placeholder for 2nd Inn stats ]; } // Generate HTML table $html = ""; foreach ($table_data as $row) { $html .= ""; foreach ($row as $cell) { $html .= ""; } $html .= ""; } $html .= "Role | Player | Team | Avg Fpts | Vs Oppt | At Venue | 1st Inn | 2nd Inn |
---|---|---|---|---|---|---|---|
" . esc_html($cell) . " |