/** * Shortcode to fetch and display the last 10 match stats for all players in a match. * Usage: [fetch_match_player_stats match_id="49689"] */ function fetch_match_player_stats_shortcode($atts) { $atts = shortcode_atts([ 'match_id' => '', ], $atts, 'fetch_match_player_stats'); if (empty($atts['match_id'])) { return '
Error: Match ID is required.
'; } $match_id = sanitize_text_field($atts['match_id']); // Step 1: Fetch Match Details $match_info_url = "https://rest.entitysport.com/v2/matches/$match_id/info?token=" . GENERATE_DRAFT_TOKEN; $match_info_response = wp_remote_get($match_info_url); if (is_wp_error($match_info_response)) { return 'Error: Unable to fetch match info.
'; } $match_info_code = wp_remote_retrieve_response_code($match_info_response); if ($match_info_code !== 200) { return 'Error: Unable to fetch match info. API returned an error code: ' . $match_info_code . '
'; } $match_info_data = json_decode(wp_remote_retrieve_body($match_info_response), true); $teama_name = $match_info_data['response']['teama']['name'] ?? 'Unknown Team A'; $teamb_name = $match_info_data['response']['teamb']['name'] ?? 'Unknown Team B'; $match_format = strtolower($match_info_data['response']['format_str'] ?? 'unknown'); // Step 2: Fetch Squad Details $squad_url = "https://rest.entitysport.com/v2/matches/$match_id/squads?token=" . GENERATE_DRAFT_TOKEN; $squad_response = wp_remote_get($squad_url); if (is_wp_error($squad_response)) { return 'Error: Unable to fetch squad info.
'; } $squad_response_code = wp_remote_retrieve_response_code($squad_response); if ($squad_response_code !== 200) { return 'Error: Unable to fetch squad info. API returned an error code: ' . $squad_response_code . '
'; } $squad_data = json_decode(wp_remote_retrieve_body($squad_response), true); $teama_squad = $squad_data['response']['teama']['squads'] ?? []; $teamb_squad = $squad_data['response']['teamb']['squads'] ?? []; $all_players = array_merge($teama_squad, $teamb_squad); if (empty($all_players)) { return 'Error: No players found in the squads.
'; } // Step 3: Fetch Last 10 Match Stats for Each Player $html = "Player Name | Team | Matches | Runs | Wickets | Batting Average | Bowling Economy |
---|---|---|---|---|---|---|
$player_name | $team_name | $total_matches | $total_runs | $total_wickets | $batting_avg | $bowling_econ |