/** * Shortcode to display merged player stats for a team. * Usage: [player_stats_on_venue match_id="62447" team="teama"] */ function display_merged_player_stats_shortcode($atts) { // Shortcode attributes with default values $atts = shortcode_atts([ 'match_id' => '', 'team' => 'teama' // Default to teama ], $atts, 'player_stats_on_venue'); // Validate match ID if (empty($atts['match_id'])) { return '
Error: Match ID is required.
'; } $match_id = sanitize_text_field($atts['match_id']); $selected_team = sanitize_text_field($atts['team']); $csv_file_path = WP_CONTENT_DIR . "/uploads/match_stats_{$match_id}_{$selected_team}.csv"; // Check if CSV exists if (file_exists($csv_file_path)) { // Read CSV data $csv_data = array_map('str_getcsv', file($csv_file_path)); $header = array_shift($csv_data); // Remove header row $merged_stats = []; foreach ($csv_data as $row) { $merged_stats[] = array_combine($header, $row); } } else { // Fetch data from API $match_info_url = API_CALL_BASE_URL . "v4/matches/$match_id/advance?token=" . GENERATE_DRAFT_TOKEN; $match_response = wp_remote_get($match_info_url); if (is_wp_error($match_response)) { return 'Error: Unable to fetch match info.
'; } $match_response_code = wp_remote_retrieve_response_code($match_response); if ($match_response_code !== 200) { return 'Error: Unable to fetch match info. API returned an error code: ' . $match_response_code . '
'; } $match_data = json_decode(wp_remote_retrieve_body($match_response), true); // Extract teams and stats $teama = $match_data['response']['items']['info']['teama'] ?? []; $teamb = $match_data['response']['items']['info']['teamb'] ?? []; $team1 = []; $batting_stats = []; $bowling_stats = []; if ($selected_team === 'teama') { $team1 = $teama; $batting_stats = $match_data['response']['items']['player_batting_stats_on_venue']['team1'] ?? []; $bowling_stats = $match_data['response']['items']['player_bowling_stats_on_venue']['team1'] ?? []; } elseif ($selected_team === 'teamb') { $team1 = $teamb; $batting_stats = $match_data['response']['items']['player_batting_stats_on_venue']['team2'] ?? []; $bowling_stats = $match_data['response']['items']['player_bowling_stats_on_venue']['team2'] ?? []; } else { return 'Error: Invalid team selection. Use "teama" or "teamb".
'; } // Combine stats for CSV $merged_stats = []; foreach ($batting_stats as $batsman) { $player_name = $batsman['batsman_title']; $merged_stats[$player_name] = [ 'player' => $player_name, 'innings' => $batsman['innings'] ?? '', 'runs' => $batsman['runs'] ?? '', 'wickets' => '', 'bat_sr' => $batsman['strike'] ?? '', 'bat_avg' => $batsman['average'] ?? '', 'bowl_eco' => '' ]; } foreach ($bowling_stats as $bowler) { $player_name = $bowler['bowler_title']; if (!isset($merged_stats[$player_name])) { $merged_stats[$player_name] = [ 'player' => $player_name, 'innings' => $bowler['innings'] ?? '', 'runs' => '', 'wickets' => $bowler['wickets'] ?? '', 'bat_sr' => '', 'bat_avg' => '', 'bowl_eco' => $bowler['econ'] ?? '' ]; } else { $merged_stats[$player_name]['wickets'] = $bowler['wickets'] ?? ''; $merged_stats[$player_name]['bowl_eco'] = $bowler['econ'] ?? ''; } } // Write data to CSV $csv_header = ['player', 'innings', 'runs', 'wickets', 'bat_sr', 'bat_avg', 'bowl_eco']; $csv_handle = fopen($csv_file_path, 'w'); fputcsv($csv_handle, $csv_header); foreach ($merged_stats as $row) { fputcsv($csv_handle, $row); } fclose($csv_handle); } // Generate HTML table $html = "Player | Innings | Runs | Wickets | BAT SR | BAT Avg | BOWL ECO |
---|---|---|---|---|---|---|
{$stats['player']} | {$stats['innings']} | {$stats['runs']} | {$stats['wickets']} | {$stats['bat_sr']} | {$stats['bat_avg']} | {$stats['bowl_eco']} |