/**
* 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 = "Last 10 Match Stats for Match ID: $match_id (Format: $match_format)
";
$html .= "";
foreach ($all_players as $player) {
$player_id = $player['player_id'];
$player_name = $player['name'] ?? 'Unknown Player';
$team_name = $player['team_name'] ?? ($teama_name ?: $teamb_name);
// Fetch advanced stats for the player
$player_stats_url = "https://rest.entitysport.com/v4/players/$player_id/advancestats/?token=" . GENERATE_DRAFT_TOKEN;
$player_stats_response = wp_remote_get($player_stats_url);
if (is_wp_error($player_stats_response)) {
continue; // Skip player if stats can't be fetched
}
$player_stats_code = wp_remote_retrieve_response_code($player_stats_response);
if ($player_stats_code !== 200) {
continue; // Skip player if API returns an error
}
$player_stats_data = json_decode(wp_remote_retrieve_body($player_stats_response), true);
$last_10_matches = $player_stats_data['response']['last10_matches'][$match_format] ?? [];
// Calculate aggregate stats for last 10 matches
$total_matches = count($last_10_matches);
$total_runs = 0;
$total_wickets = 0;
$total_balls = 0;
$total_runs_conceded = 0;
foreach ($last_10_matches as $match) {
$total_runs += $match['batting']['runs'] ?? 0;
$total_wickets += $match['bowling']['wickets'] ?? 0;
$total_balls += $match['bowling']['balls'] ?? 0;
$total_runs_conceded += $match['bowling']['runs_conceded'] ?? 0;
}
$batting_avg = $total_matches > 0 ? round($total_runs / $total_matches, 2) : '-';
$bowling_econ = $total_balls > 0 ? round($total_runs_conceded / ($total_balls / 6), 2) : '-';
// Add to table
$html .= "";
}
$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 |
";
return $html;
}
// Register the shortcode
add_shortcode('fetch_match_player_stats', 'fetch_match_player_stats_shortcode');
Warning: Cannot modify header information - headers already sent by (output started at /home/myfinjex/public_html/wp-content/plugins/cricket-live-dream11/includes/venue stats/player-stats-with-team.php:1) in /home/myfinjex/public_html/wp-content/plugins/digits/includes/core/digits_session.php on line 121
Warning: Cannot modify header information - headers already sent by (output started at /home/myfinjex/public_html/wp-content/plugins/cricket-live-dream11/includes/venue stats/player-stats-with-team.php:1) in /home/myfinjex/public_html/wp-content/plugins/digits/includes/core/digits_session.php on line 121
Warning: Cannot modify header information - headers already sent by (output started at /home/myfinjex/public_html/wp-content/plugins/cricket-live-dream11/includes/venue stats/player-stats-with-team.php:1) in /home/myfinjex/public_html/wp-includes/pluggable.php on line 1435
Warning: Cannot modify header information - headers already sent by (output started at /home/myfinjex/public_html/wp-content/plugins/cricket-live-dream11/includes/venue stats/player-stats-with-team.php:1) in /home/myfinjex/public_html/wp-includes/pluggable.php on line 1438