function otg_get_remote_json($url) {
$response = wp_remote_get($url, [
'timeout' => 15,
'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
]);
if (is_wp_error($response)) {
return false;
}
$body = wp_remote_retrieve_body($response);
return !empty($body) ? $body : false;
}
function otg_get_player_full_data($player_id) {
global $wpdb;
$player_id = intval($player_id);
if ($player_id <= 0) {
return ['name' => '', 'first_name' => '', 'last_name' => ''];
}
$players_table = 'otg_players';
$cached = $wpdb->get_row($wpdb->prepare(
"SELECT Name as name, FirstName as first_name, LastName as last_name FROM {$players_table} WHERE nhl_id = %d LIMIT 1",
$player_id
));
if ($cached && !empty($cached->name)) {
return [
'name' => $cached->name,
'first_name' => $cached->first_name ?? '',
'last_name' => $cached->last_name ?? ''
];
}
$api_url = "https://api-web.nhle.com/v1/player/" . $player_id . "/landing";
$raw = otg_get_remote_json($api_url);
if ($raw !== false) {
$info = json_decode($raw, true);
if (isset($info['firstName']['default']) && isset($info['lastName']['default'])) {
$fname = sanitize_text_field($info['firstName']['default']);
$lname = sanitize_text_field($info['lastName']['default']);
$full_name = trim($fname . ' ' . $lname);
$exists_in_table = $wpdb->get_var($wpdb->prepare("SELECT 1 FROM {$players_table} WHERE nhl_id = %d LIMIT 1", $player_id));
if ($exists_in_table) {
$wpdb->update(
$players_table,
['Name' => $full_name, 'FirstName' => $fname, 'LastName' => $lname],
['nhl_id' => $player_id]
);
}
return [
'name' => $full_name,
'first_name' => $fname,
'last_name' => $lname
];
}
}
return ['name' => '', 'first_name' => '', 'last_name' => ''];
}
function otg_import_player_matrix_v12( $game_id ) {
global $wpdb;
$player_box_table = 'otg_rangers_player_boxscores';
$clean_id = intval($game_id);
$api_url = "https://api-web.nhle.com/v1/gamecenter/" . $clean_id . "/boxscore";
echo " Fetching boxscore matrix for Game ID: {$clean_id}... ";
$raw_body = otg_get_remote_json($api_url);
if ($raw_body === FALSE) {
echo " Error: Failed to fetch boxscore API content for game {$clean_id}. ";
return false;
}
$data = json_decode( $raw_body, true );
if ( !isset($data['playerByGameStats']) ) {
echo " Error: 'playerByGameStats' node missing in boxscore response for game {$clean_id}. ";
return false;
}
foreach ( ['homeTeam', 'awayTeam'] as $t_key ) {
if ( !isset($data['playerByGameStats'][$t_key]) ) continue;
$team_abbr = sanitize_text_field($data[$t_key]['abbrev'] ?? 'N/A');
echo " -> Processing Team: {$team_abbr} ";
// Process Forwards and Defense
foreach ( ['forwards', 'defense'] as $pos_group ) {
if ( !isset($data['playerByGameStats'][$t_key][$pos_group]) ) continue;
foreach ( $data['playerByGameStats'][$t_key][$pos_group] as $p ) {
$p_id = intval($p['playerId']);
$player_info = otg_get_player_full_data($p_id);
$p_name = !empty($player_info['name']) ? $player_info['name'] : sanitize_text_field($p['name']['default'] ?? '');
$data_array = [
'game_id' => $clean_id,
'player_id' => $p_id,
'team_abbr' => $team_abbr,
'player_name' => $p_name,
'jersey_number' => intval($p['sweaterNumber']),
'position_code' => $p['position'],
'goals' => intval($p['goals'] ?? 0),
'assists' => intval($p['assists'] ?? 0),
'points' => intval($p['points'] ?? 0),
'plus_minus' => intval($p['plusMinus'] ?? 0),
'pim' => intval($p['pim'] ?? 0),
'hits' => intval($p['hits'] ?? 0),
'powerplay_goals' => intval($p['powerPlayGoals'] ?? 0),
'shots' => intval($p['sog'] ?? 0),
'faceoff_pct' => floatval($p['faceoffWinningPctg'] ?? 0),
'time_on_ice' => $p['toi'] ?? '00:00',
'blocked_shots' => intval($p['blockedShots'] ?? 0),
'shifts' => intval($p['shifts'] ?? 0),
'giveaways' => intval($p['giveaways'] ?? 0),
'takeaways' => intval($p['takeaways'] ?? 0)
];
$existing_box = $wpdb->get_var($wpdb->prepare(
"SELECT id FROM {$player_box_table} WHERE game_id = %d AND player_id = %d LIMIT 1",
$clean_id, $p_id
));
if ($existing_box) {
$wpdb->update($player_box_table, $data_array, ['id' => intval($existing_box)]);
} else {
$wpdb->insert($player_box_table, $data_array);
}
}
}
// Process Goalies
if ( !isset($data['playerByGameStats'][$t_key]['goalies']) ) continue;
foreach ( $data['playerByGameStats'][$t_key]['goalies'] as $g ) {
$g_id = intval($g['playerId']);
$goalie_info = otg_get_player_full_data($g_id);
$g_name = !empty($goalie_info['name']) ? $goalie_info['name'] : sanitize_text_field($g['name']['default'] ?? '');
$is_starter = isset($g['starter']) && $g['starter'] === true ? 1 : 0;
$data_array = [
'game_id' => $clean_id,
'player_id' => $g_id,
'team_abbr' => $team_abbr,
'player_name' => $g_name,
'jersey_number' => intval($g['sweaterNumber']),
'position_code' => 'G',
'starter' => $is_starter,
'time_on_ice' => $g['toi'] ?? '60:00',
'saves' => intval($g['saves'] ?? 0),
'shots_against' => $g['saveShotsAgainst'] ?? '0/0',
'save_pct' => floatval($g['savePctg'] ?? 0),
'decision' => $g['decision'] ?? '',
'goals_against' => intval($g['goalsAgainst'] ?? 0),
'ev_ga' => intval($g['evenStrengthGoalsAgainst'] ?? 0),
'pp_ga' => intval($g['powerPlayGoalsAgainst'] ?? 0),
'sh_ga' => intval($g['shorthandedGoalsAgainst'] ?? 0),
'ev_shots_against' => $g['evenStrengthShotsAgainst'] ?? '0/0',
'pp_shots_against' => $g['powerPlayShotsAgainst'] ?? '0/0',
'sh_shots_against' => $g['shorthandedShotsAgainst'] ?? '0/0'
];
$existing_box = $wpdb->get_var($wpdb->prepare(
"SELECT id FROM {$player_box_table} WHERE game_id = %d AND player_id = %d LIMIT 1",
$clean_id, $g_id
));
if ($existing_box) {
$wpdb->update($player_box_table, $data_array, ['id' => intval($existing_box)]);
} else {
$wpdb->insert($player_box_table, $data_array);
}
}
}
echo " -> Transitioning to Scoring Summary import for Game {$clean_id}... ";
otg_import_scoring_summary($clean_id);
return true;
}
function otg_import_scoring_summary($game_id) {
global $wpdb;
$api_url = "https://api-web.nhle.com/v1/gamecenter/" . intval($game_id) . "/landing";
$raw_landing = otg_get_remote_json($api_url);
if ($raw_landing === false) {
echo " -> Error: Failed to fetch landing summary for scoring sync. ";
return;
}
$data = json_decode($raw_landing, true);
if (isset($data['summary']['scoring'])) {
foreach ($data['summary']['scoring'] as $period) {
foreach ($period['goals'] as $goal) {
$scorer_id = isset($goal['playerId']) ? intval($goal['playerId']) : 0;
$scorer_info = otg_get_player_full_data($scorer_id);
$scorer_name = !empty($scorer_info['name']) ? $scorer_info['name'] : trim(($goal['firstName']['default'] ?? '') . ' ' . ($goal['lastName']['default'] ?? ''));
$a1 = $goal['assists'][0] ?? null;
$a1_id = $a1 && isset($a1['playerId']) ? intval($a1['playerId']) : 0;
$a1_info = $a1_id ? otg_get_player_full_data($a1_id) : ['name' => ''];
$a1_name = !empty($a1_info['name']) ? $a1_info['name'] : ($a1 ? trim(($a1['firstName']['default'] ?? '') . ' ' . ($a1['lastName']['default'] ?? '')) : null);
$a2 = $goal['assists'][1] ?? null;
$a2_id = $a2 && isset($a2['playerId']) ? intval($a2['playerId']) : 0;
$a2_info = $a2_id ? otg_get_player_full_data($a2_id) : ['name' => ''];
$a2_name = !empty($a2_info['name']) ? $a2_info['name'] : ($a2 ? trim(($a2['firstName']['default'] ?? '') . ' ' . ($a2['lastName']['default'] ?? '')) : null);
$period_num = intval($period['periodDescriptor']['number']);
$time_elapsed = $goal['timeInPeriod'];
$incoming_strength = $goal['strength'];
$data_array = [
'game_id' => intval($game_id),
'period' => $period_num,
'time_elapsed' => $time_elapsed,
'team_abbr' => $goal['teamAbbrev']['default'],
'scorer_id' => $scorer_id,
'scorer_name' => $scorer_name,
'scorer_gtd' => intval($goal['goalsToDate']),
'assist1_id' => $a1_id,
'assist1_name' => $a1_name,
'assist1_gtd' => $a1 ? intval($a1['assistsToDate']) : 0,
'assist2_id' => $a2_id,
'assist2_name' => $a2_name,
'assist2_gtd' => $a2 ? intval($a2['assistsToDate']) : 0,
'home_score' => intval($goal['homeScore']),
'away_score' => intval($goal['awayScore']),
'strength' => $incoming_strength
];
$existing_row = $wpdb->get_row($wpdb->prepare(
"SELECT id, strength FROM otg_rangers_scoring WHERE game_id = %d AND period = %d AND time_elapsed = %s AND scorer_name = %s",
intval($game_id), $period_num, $time_elapsed, $scorer_name
));
if ($existing_row) {
$current_strength = strtolower(trim($existing_row->strength ?? ''));
if (in_array($current_strength, ['ps', 'sp', '2p'], true)) {
unset($data_array['strength']);
}
$wpdb->update('otg_rangers_scoring', $data_array, ['id' => intval($existing_row->id)]);
} else {
$global_time_check = $wpdb->get_var($wpdb->prepare(
"SELECT 1 FROM otg_rangers_scoring WHERE game_id = %d AND period = %d AND time_elapsed = %s LIMIT 1",
intval($game_id), $period_num, $time_elapsed
));
if (!$global_time_check) {
$wpdb->insert('otg_rangers_scoring', $data_array);
} else {
$fallback_row = $wpdb->get_row($wpdb->prepare(
"SELECT id, strength FROM otg_rangers_scoring WHERE game_id = %d AND period = %d AND time_elapsed = %s LIMIT 1",
intval($game_id), $period_num, $time_elapsed
));
if ($fallback_row) {
$current_strength = strtolower(trim($fallback_row->strength ?? ''));
if (in_array($current_strength, ['ps', 'sp', '2p'], true)) {
unset($data_array['strength']);
}
$wpdb->update('otg_rangers_scoring', $data_array, ['id' => intval($fallback_row->id)]);
}
}
}
}
}
}
if (isset($data['summary']['penalties'])) otg_import_penalties($game_id, $data['summary']['penalties']);
if (isset($data['shootoutInUse']) && $data['shootoutInUse'] === true && isset($data['summary']['shootout'])) otg_import_shootout_data($game_id, $data['summary']['shootout']);
}
Chmelar catching some attention – Prospects – OutsideTheGarden Boards
Skip to content