/** * Shortcode to fetch and display player stats summary with team selection (without CSV storage and CSS). * Usage: [player_summary_no_csv match_id="12345" team="teama"] */ if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } require_once plugin_dir_path(__FILE__) . '/../api.php'; function fetch_and_display_player_summary_no_csv($atts) { $atts = shortcode_atts([ 'match_id' => '', 'team' => 'teama', // Default to "teama" ], $atts, 'player_summary_no_csv'); if (empty($atts['match_id'])) { return '

Error: Match ID is required.

'; } $match_id = sanitize_text_field($atts['match_id']); $team_key = sanitize_text_field($atts['team']); // Validate team key if (!in_array($team_key, ['teama', 'teamb'])) { return '

Error: Invalid team key. Use "teama" or "teamb".

'; } // Fetch Match Info (to get team names) $match_info_url = API_CALL_BASE_URL . "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_data = json_decode(wp_remote_retrieve_body($match_info_response), true); // Extract team names $team_a_name = $match_info_data['response']['teama']['name'] ?? 'Unknown Team'; $team_b_name = $match_info_data['response']['teamb']['name'] ?? 'Unknown Team'; // Fetch Squad Data $squad_url = API_CALL_BASE_URL . "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 data.

'; } $squad_data = json_decode(wp_remote_retrieve_body($squad_response), true); $players = $squad_data['response'][$team_key]['squads'] ?? []; if (empty($players)) { return "

No squad data found for the selected team ({$team_key}).

"; } // Prepare data for the table $data = [ ['Player', 'Team', 'Avg Fpts', 'Runs', 'WK', 'RR', 'RW'] // Header row ]; foreach ($players as $player) { $player_id = $player['player_id'] ?? 0; $player_name = $player['name'] ?? 'Unknown Player'; $player_role = strtoupper($player['role'] ?? 'UNKNOWN'); // Assign team name based on team_id $team_name = $player['team_id'] == $match_info_data['response']['teama']['team_id'] ? $team_a_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'])) { 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) : 0; $total_runs = array_sum(array_column($batting_stats, 'runs')); $total_wickets = array_sum(array_column($bowling_stats, 'wickets')); $runs_per_match = implode(',', array_slice(array_column($batting_stats, 'runs'), 0, 5)); $wickets_per_match = implode(',', array_slice(array_column($bowling_stats, 'wickets'), 0, 5)); // Add row to data array $data[] = [ esc_html($player_name) . " ({$player_role})", esc_html($team_name), $avg_points, $total_runs, $total_wickets, $runs_per_match, $wickets_per_match ]; } // Generate and return HTML $html = generate_html_table_no_csv($data); return $html; } function generate_html_table_no_csv($data) { $html = ""; foreach ($data[0] as $header) { $html .= ""; } $html .= ""; for ($i = 1; $i < count($data); $i++) { $html .= ""; foreach ($data[$i] as $cell) { $html .= ""; } $html .= ""; } $html .= "
" . esc_html($header) . "
" . $cell . "
"; // Add description for the short names $html .= "

Table Description: WK: Total Wickets, RR: Recent Runs (Runs scored in recent matches), RW: Recent Wickets (Wickets taken in recent matches)

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