// =========================================================================
// 27. SHORTCODE: Team Captains /captains/
// =========================================================================
add_shortcode('otg_rangers_captains_list', 'otg_rangers_captains_list_shortcode');
function otg_rangers_captains_list_shortcode() {
global $wpdb;
// Helper function to format dates nicely or leave seasons as-is
$format_date_display = function($date_str) {
if (empty($date_str)) {
return 'Present';
}
if (preg_match('/^\d{4}-\d{2}$/', $date_str)) {
return $date_str;
}
$time = strtotime($date_str);
return $time ? date('F j, Y', $time) : $date_str;
};
// Helper function to convert table date strings (like Y-m-d or YYYY-MM) directly into comparable Y-m-d bounds
$parse_date_bound = function($date_str, $is_end = false) {
if (empty($date_str)) return null;
if (preg_match('/^(\d{4})-(\d{2})$/', $date_str, $m)) {
$y = intval($m[1]);
$sub = intval($m[2]);
$full_y = ($sub < 50) ? 2000 + $sub : 1900 + $sub;
if (!$is_end) {
return $y . '-09-01'; // Season start baseline
} else {
return $full_y . '-07-31'; // Season end baseline
}
}
$time = strtotime($date_str);
return $time ? date('Y-m-d', $time) : null;
};
$captains = $wpdb->get_results("SELECT * FROM otg_rangers_captains ORDER BY id ASC");
$output = '
';
$output .= '
New York Rangers Captains Record & History
';
$output .= '';
$output .= '
| No. |
Captain / Period |
Tenure Began |
Tenure Ended |
Record (W-L-T/OT) |
';
if (!$captains) {
$output .= '| No captain records found. |
';
} else {
// Pre-fetch all non-exhibition games once to process records reliably in memory without SQL boundary discrepancies
$all_games = $wpdb->get_results("SELECT game_date, rangers_score, opponent_score, game_outcome, overtime_status FROM otg_rangers_games WHERE game_type != 1");
// Helper closure to calculate W-L-T/OT from an array of games filtered by date range
$calc_record_from_games = function($games_list, $start_date_str, $end_date_str) use ($parse_date_bound) {
$s_time = $parse_date_bound($start_date_str, false) ? strtotime($parse_date_bound($start_date_str, false)) : null;
$e_time = !empty($end_date_str) ? strtotime($parse_date_bound($end_date_str, true)) : PHP_INT_MAX;
$wins = 0;
$losses = 0;
$ties_ot = 0;
if ($games_list) {
foreach ($games_list as $g) {
$g_time = strtotime($g->game_date);
if (!$g_time) continue;
// Inclusive boundary check
if ($s_time && $g_time < $s_time) continue;
if ($e_time && $g_time > $e_time) continue;
$outcome = strtoupper(trim($g->game_outcome ?? ''));
$ot_status = strtoupper(trim($g->overtime_status ?? 'REG'));
$r_score = intval($g->rangers_score);
$o_score = intval($g->opponent_score);
if ($outcome === 'W' || ($r_score > $o_score && $outcome !== 'L' && $outcome !== 'T' && $outcome !== 'OTL')) {
$wins++;
} elseif ($outcome === 'L' || $outcome === 'OTL' || $outcome === 'SOL' || $ot_status === 'OT' || $ot_status === 'SO') {
if ($ot_status === 'OT' || $ot_status === 'SO' || $outcome === 'OTL' || $outcome === 'SOL') {
$ties_ot++;
} else {
$losses++;
}
} elseif ($outcome === 'T') {
$ties_ot++;
} else {
if ($r_score > $o_score) {
$wins++;
} elseif ($r_score < $o_score) {
if ($ot_status !== 'REG' && !empty($ot_status)) {
$ties_ot++;
} else {
$losses++;
}
} else {
$ties_ot++;
}
}
}
}
return "{$wins}-{$losses}-{$ties_ot}";
};
$seen_captain_numbers = [];
$total_captains = count($captains);
for ($i = 0; $i < $total_captains; $i++) {
$curr = $captains[$i];
// 1. Captain Number logic
$cap_num = intval($curr->captain_number);
if (isset($seen_captain_numbers[$cap_num])) {
$display_num = '';
} else {
$display_num = $cap_num;
$seen_captain_numbers[$cap_num] = true;
}
// Player profile link
if (!empty($curr->player_id)) {
$player_link = '' . esc_html($curr->player_name) . '';
} else {
$player_link = '' . esc_html($curr->player_name) . '';
}
$began_display = $format_date_display($curr->date_started);
$ended_display = $format_date_display($curr->date_finished);
// Compute record for tenure
$tenure_end_val = (!empty($curr->date_finished)) ? $curr->date_finished : (($i === $total_captains - 1) ? date('Y-m-d') : null);
$record_str = $calc_record_from_games($all_games, $curr->date_started, $tenure_end_val);
$output .= '';
$output .= '| ' . ($display_num !== '' ? esc_html($display_num) : '') . ' | ';
$output .= '' . $player_link . ' | ';
$output .= '' . $began_display . ' | ';
$output .= '' . $ended_display . ' | ';
$output .= '' . esc_html($record_str) . ' | ';
$output .= '
';
// 2. Check for Vacancy Period between current captain and next captain
if (isset($captains[$i + 1])) {
$next = $captains[$i + 1];
$gap_start_raw = $curr->date_finished;
$gap_end_raw = $next->date_started;
$s_bound = $parse_date_bound($gap_start_raw, true);
$e_bound = $parse_date_bound($gap_end_raw, false);
if ($s_bound && $e_bound) {
// Find any game strictly between gap boundaries
$v_first_game = null;
$v_last_game = null;
if ($all_games) {
foreach ($all_games as $g) {
if ($g->game_date > $s_bound && $g->game_date < $e_bound) {
if (!$v_first_game || $g->game_date < $v_first_game) {
$v_first_game = $g->game_date;
}
if (!$v_last_game || $g->game_date > $v_last_game) {
$v_last_game = $g->game_date;
}
}
}
}
if ($v_first_game && $v_last_game) {
$v_start = date('F j, Y', strtotime($v_first_game));
$v_end = date('F j, Y', strtotime($v_last_game));
$vacancy_label = ($v_start === $v_end) ? 'No Captain: ' . $v_start : 'No Captain: ' . $v_start . ' – ' . $v_end;
$v_record_str = $calc_record_from_games($all_games, $v_first_game, $v_last_game);
$output .= '';
$output .= '| — | ';
$output .= '' . esc_html($vacancy_label) . ' | ';
$output .= '— | ';
$output .= '' . esc_html($v_record_str) . ' | ';
$output .= '
';
}
}
}
}
}
$output .= '
';
$output .= '
';
return $output;
}
mjolnir – Activity – OutsideTheGarden Boards – Page 3
Skip to content