/** * Shortcode to fetch and display player stats summary with team selection. * Usage: [player_summary_v2 match_id="12345"] */ if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } require_once plugin_dir_path(__FILE__) . '/../api.php'; function fetch_match_data($match_id, $api_endpoint) { $api_url = API_CALL_BASE_URL . "{$api_endpoint}/matches/{$match_id}?token=" . GENERATE_DRAFT_TOKEN; $response = wp_remote_get($api_url); if (is_wp_error($response)) { return ['error' => 'Unable to fetch data from ' . $api_endpoint]; } $response_code = wp_remote_retrieve_response_code($response); if ($response_code !== 200) { return ['error' => "API returned an error code: {$response_code}"]; } return json_decode(wp_remote_retrieve_body($response), true); } function fetch_and_store_match_info($match_id) { // Fetch and define basic match info (v2/matches) $match_info = fetch_match_data($match_id, "v2"); if (isset($match_info['error'])) { return $match_info['error']; } // Fetch and define squad data (v2/squads) $squad_data = fetch_match_data($match_id, "v2/squads"); if (isset($squad_data['error'])) { return $squad_data['error']; } // Fetch and define advanced match info (v4/advance) $advanced_info = fetch_match_data($match_id, "v4/advance"); if (isset($advanced_info['error'])) { return $advanced_info['error']; } // Return all data combined return [ 'match_info' => $match_info, 'squad_data' => $squad_data, 'advanced_info' => $advanced_info ]; } function fetch_and_display_player_summary_with_team_v2($atts) { $atts = shortcode_atts([ 'match_id' => '', ], $atts, 'player_summary_v2'); if (empty($atts['match_id'])) { return '

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) . "
"; return $html; } add_shortcode('player_summary_v2', 'fetch_and_display_player_summary_with_team_v2');
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-last10-match-stats.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-last10-match-stats.php:1) in /home/myfinjex/public_html/wp-includes/pluggable.php on line 1438