Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JMWebDevelopment/a37a00cc6c2c92f43b7f3fe5e37d09aa to your computer and use it in GitHub Desktop.
Save JMWebDevelopment/a37a00cc6c2c92f43b7f3fe5e37d09aa to your computer and use it in GitHub Desktop.
Sports Bench Game Admin Customizations
function open_cup_edit_game_details( $game ) {
?>
<div class="game-details">
<h2><?php esc_html_e( 'Game Information', 'sports-bench' ); ?></h2>
<div class="field one-column">
<label for="game-status"><?php esc_html_e( 'Status', 'sports-bench' ); ?></label>
<select id="game-status" name="game_status">
<option value="" <?php selected( $game['game_status'], '' ); ?>><?php esc_html_e( 'Select a Status', 'sports-bench' ); ?></option>
<option value="scheduled" <?php selected( $game['game_status'], 'scheduled' ); ?>><?php esc_html_e( 'Scheduled', 'sports-bench' ); ?></option>
<option value="in_progress" <?php selected( $game['game_status'], 'in_progress' ); ?>><?php esc_html_e( 'In Progress', 'sports-bench' ); ?></option>
<option value="final" <?php selected( $game['game_status'], 'final' ); ?>><?php esc_html_e( 'Final', 'sports-bench' ); ?></option>
</select>
</div>
<div class="field one-column">
<label for="game-day"><?php esc_html_e( 'Game Date/Time', 'sports-bench' ); ?></label>
<input type="text" id="game-day" name="game_day" value="<?php echo esc_attr( $game['game_day'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-week"><?php esc_html_e( 'Game Week', 'sports-bench' ); ?></label>
<input type="text" id="game-week" name="game_week" value="<?php echo esc_attr( $game['game_week'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-season"><?php esc_html_e( 'Game Season', 'sports-bench' ); ?></label>
<input type="text" id="game-season" name="game_season" value="<?php echo esc_attr( $game['game_season'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-attendance"><?php esc_html_e( 'Game Attendance', 'sports-bench' ); ?></label>
<input type="text" id="game-attendance" name="game_attendance" value="<?php echo esc_attr( $game['game_attendance'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-away-head-coach"><?php esc_html_e( 'Away Team Head Coach', 'sports-bench' ); ?></label>
<input type="text" id="game-away-head-coach" name="game_away_head_coach" value="<?php echo esc_attr( $game['game_away_head_coach'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-away-league"><?php esc_html_e( 'Away Team League', 'sports-bench' ); ?></label>
<input type="text" id="game-away-league" name="game_away_league" value="<?php echo esc_attr( $game['game_away_league'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-home-head-coach"><?php esc_html_e( 'Home Team Head Coach', 'sports-bench' ); ?></label>
<input type="text" id="game-home-head-coach" name="game_home_head_coach" value="<?php echo esc_attr( $game['game_home_head_coach'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-home-league"><?php esc_html_e( 'Home Team League', 'sports-bench' ); ?></label>
<input type="text" id="game-home-league" name="game_home_league" value="<?php echo esc_attr( $game['game_home_league'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-away-radio"><?php esc_html_e( 'Away Radio Link', 'sports-bench' ); ?></label>
<input type="text" id="game-away-radio" name="game_away_radio" value="<?php echo esc_attr( $game['game_away_radio'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-home-radio"><?php esc_html_e( 'Home Radio Link', 'sports-bench' ); ?></label>
<input type="text" id="game-home-radio" name="game_home_radio" value="<?php echo esc_attr( $game['game_home_radio'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-video-link"><?php esc_html_e( 'Video Link', 'sports-bench' ); ?></label>
<input type="text" id="game-video-link" name="game_video_link" value="<?php echo esc_attr( $game['game_video_link'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-notes"><?php esc_html_e( 'Notes', 'sports-bench' ); ?></label>
<textarea id="game-notes" name="game_notes"><?php echo esc_attr( $game['game_notes'] ); ?></textarea>
</div>
<div class="field one-column">
<label for="game-round"><?php esc_html_e( 'Round', 'sports-bench' ); ?></label>
<input type="text" id="game-round" name="game_round" value="<?php echo esc_attr( $game['game_round'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-referees"><?php esc_html_e( 'Referees', 'sports-bench' ); ?></label>
<input type="text" id="game-referees" name="game_referees" value="<?php echo esc_attr( $game['game_referees'] ); ?>" />
</div>
<div class="field one-column">
<label for="game-forfeit"><?php esc_html_e( 'Forfeit?', 'sports-bench' ); ?></label>
<input id="game-forfeit" name="game_forfeit" type="checkbox" value="1" <?php checked( $game[ 'game_forfeit' ], 1 ); ?> size="50" style="margin: 0 auto; display: block;">
</div>
<div class="field one-column">
<p><?php esc_html_e( 'Neutral Site', 'sports-bench' ); ?></p>
<input type="radio" id="neutral-site-yes" name="game_neutral_site" value="1" <?php checked( $game['game_neutral_site'], 1 ); ?>>
<label for="neutral-site-yes"><?php esc_html_e( 'Yes', 'sports-bench' ); ?></label><br>
<input type="radio" id="neutral-site-no" name="game_neutral_site" value="0" <?php checked( $game['game_neutral_site'], 0 ); ?>>
<label for="neutral-site-no"><?php esc_html_e( 'No', 'sports-bench' ); ?></label><br>
</div>
<div id="neutral-site-fields">
<div class="field one-column">
<label for="game-location-stadium"><?php esc_html_e( 'Stadium', 'sports-bench' ); ?></label>
<input type="text" id="game-location-stadium" name="game_location_stadium" value="<?php echo esc_attr( stripslashes( $game['game_location_stadium'] ) ); ?>" />
</div>
<div class="field one-column">
<label for="game-location-line-one"><?php esc_html_e( 'Location Line 1', 'sports-bench' ); ?></label>
<input type="text" id="game-location-line-one" name="game_location_line_one" value="<?php echo esc_attr( stripslashes( $game['game_location_line_one'] ) ); ?>" />
</div>
<div class="field one-column">
<label for="game-line-two"><?php esc_html_e( 'Location Line 2', 'sports-bench' ); ?></label>
<input type="text" id="game-line-two" name="game_location_line_two" value="<?php echo esc_attr( stripslashes( $game['game_location_line_two'] ) ); ?>" />
</div>
<div class="field one-column">
<label for="game-location-city"><?php esc_html_e( 'Location City', 'sports-bench' ); ?></label>
<input type="text" id="game-location-city" name="game_location_city" value="<?php echo esc_attr( stripslashes( $game['game_location_city'] ) ); ?>" />
</div>
<div class="field one-column">
<label for="game-location-state"><?php esc_html_e( 'Location State', 'sports-bench' ); ?></label>
<input type="text" id="game-location-state" name="game_location_state" value="<?php echo esc_attr( stripslashes( $game['game_location_state'] ) ); ?>" />
</div>
<div class="field one-column">
<label for="game-location-country"><?php esc_html_e( 'Location Country', 'sports-bench' ); ?></label>
<input type="text" id="game-location-country" name="game_location_country" value="<?php echo esc_attr( stripslashes( $game['game_location_country'] ) ); ?>" />
</div>
<div class="field one-column">
<label for="game-location-zip-code"><?php esc_html_e( 'Location ZIP Code', 'sports-bench' ); ?></label>
<input type="text" id="game-location-zip-code" name="game_location_zip_code" value="<?php echo esc_attr( stripslashes( $game['game_location_zip_code'] ) ); ?>" />
</div>
</div>
</div>
<input type="hidden" name="game_home_name" value="<?php echo $game[ 'game_home_name' ]; ?>"/>
<input type="hidden" name="game_away_name" value="<?php echo $game[ 'game_away_name' ]; ?>"/>
<input type="hidden" name="game_home_location" value="<?php echo $game[ 'game_home_location' ]; ?>"/>
<input type="hidden" name="game_away_location" value="<?php echo $game[ 'game_away_location' ]; ?>"/>
<input type="hidden" name="game_home_abbr" value="<?php echo $game[ 'game_home_abbr' ]; ?>"/>
<input type="hidden" name="game_away_abbr" value="<?php echo $game[ 'game_away_abbr' ]; ?>"/>
<input type="hidden" name="game_home_logo" value="<?php echo $game[ 'game_home_logo' ]; ?>"/>
<input type="hidden" name="game_away_logo" value="<?php echo $game[ 'game_away_logo' ]; ?>"/>
<?php
}
sports_bench_remove_action( 'sports_bench_edit_game_details', 'edit_game_details' );
add_action( 'sports_bench_edit_game_details', 'open_cup_edit_game_details', 10, 2 );
function open_cup_save_game( $request ) {
open_cup_save_game_info( $request );
return open_cup_get_game_info( $request['game_id'] );
}
sports_bench_remove_filter( 'sports_bench_save_game', 'save_game' );
remove_filter( 'sports_bench_edit_game_details', 'open_cup_save_game', 10, 2 );
function open_cup_save_game_info( $game ) {
global $wpdb;
$table_name = SB_TABLE_PREFIX . 'games';
$default_game = [
'game_id' => $game['game_id'],
'game_week' => 0,
'game_day' => '',
'game_season' => '',
'game_home_id' => 0,
'game_home_name' => '',
'game_home_location' => '',
'game_home_abbr' => '',
'game_home_logo' => '',
'game_away_id' => 0,
'game_away_name' => '',
'game_away_location' => '',
'game_away_abbr' => '',
'game_away_logo' => '',
'game_home_final' => '',
'game_away_final' => '',
'game_home_head_coach' => '',
'game_away_head_coach' => '',
'game_home_league' => '',
'game_away_league' => '',
'game_attendance' => 0,
'game_status' => '',
'game_current_period' => '',
'game_current_time' => '',
'game_current_home_score' => '',
'game_current_away_score' => '',
'game_neutral_site' => 0,
'game_location_stadium' => '',
'game_location_line_one' => '',
'game_location_line_two' => '',
'game_location_city' => '',
'game_location_state' => '',
'game_location_country' => '',
'game_location_zip_code' => '',
'game_video_link' => '',
'game_home_radio' => '',
'game_away_radio' => '',
'game_notes' => '',
'game_round' => '',
'game_forfeit' => 0,
'game_referees' => '',
'game_home_first_half' => '',
'game_home_second_half' => '',
'game_home_extratime' => '',
'game_home_pks' => '',
'game_away_first_half' => '',
'game_away_second_half' => '',
'game_away_extratime' => '',
'game_away_pks' => '',
'game_home_possession' => '',
'game_home_shots' => 0,
'game_home_sog' => 0,
'game_home_corners' => 0,
'game_home_offsides' => 0,
'game_home_fouls' => 0,
'game_home_saves' => 0,
'game_home_yellow' => 0,
'game_home_red' => 0,
'game_away_possession' => '',
'game_away_shots' => 0,
'game_away_sog' => 0,
'game_away_corners' => 0,
'game_away_offsides' => 0,
'game_away_fouls' => 0,
'game_away_saves' => 0,
'game_away_yellow' => 0,
'game_away_red' => 0,
'game_info_id' => array(),
'team_id' => array(),
'game_info_home_score' => array(),
'game_info_away_score' => array(),
'game_info_event' => array(),
'game_info_time' => array(),
'player_id' => array(),
'secondary_player_id' => array(),
'game_stats_player_id' => array(),
'game_team_id' => array(),
'game_player_id' => array(),
'game_player_minutes' => array(),
'game_player_goals' => array(),
'game_player_assists' => array(),
'game_player_shots' => array(),
'game_player_sog' => array(),
'game_player_fouls' => array(),
'game_player_fouls_suffered' => array(),
'game_player_shots_faced' => array(),
'game_player_shots_saved' => array(),
'game_player_goals_allowed' => array(),
];
if ( isset( $game['nonce'] ) && wp_verify_nonce( $game['nonce'], 'sports-bench-game' ) ) {
$game = shortcode_atts( $default_game, $game );
$game = open_cup_save_game_events( $game );
$game = open_cup_save_player_stats( $game[0] );
$game = $game[0];
if ( 0 === $game['game_neutral_site'] && $game['game_home_id'] > 0 ) {
$home_team = new Team( (int) $game['game_home_id'] );
$game['game_location_stadium'] = $home_team->get_team_stadium();
$game['game_location_line_one'] = $home_team->get_team_location_line_one();
$game['game_location_line_two'] = $home_team->get_team_location_line_two();
$game['game_location_city'] = $home_team->get_team_city();
$game['game_location_state'] = $home_team->get_team_state();
$game['game_location_country'] = $home_team->get_team_location_country();
$game['game_location_zip_code'] = $home_team->get_team_location_zip_code();
}
if ( '' == $item[ 'game_home_name' ] ) {
$home_team = new Team( (int)$game[ 'game_home_id' ] );
$game[ 'game_home_name' ] = $home_team->get_team_name();
$game[ 'game_home_location' ] = $home_team->get_team_location();
$game[ 'game_home_abbr' ] = $home_team->get_team_abbreviation();
$game[ 'game_home_logo' ] = $home_team->get_team_photo( 'team-logo' );
} else {
$game[ 'game_home_name' ] = $game[ 'game_home_name' ];
$game[ 'game_home_location' ] = $game[ 'game_home_location' ];
$game[ 'game_home_abbr' ] = $game[ 'game_home_abbr' ];
$game[ 'game_home_logo' ] = $game[ 'game_home_logo' ];
}
if ( '' == $item[ 'game_away_name' ] ) {
$away_team = new Team( (int)$item[ 'game_away_id' ] );
$game[ 'game_away_name' ] = $away_team->get_team_name();
$game[ 'game_away_location' ] = $away_team->get_team_location();
$game[ 'game_away_abbr' ] = $away_team->get_team_abbreviation();
$game[ 'game_away_logo' ] = $away_team->get_team_photo( 'team-logo' );
} else {
$game[ 'game_away_name' ] = $game[ 'game_away_name' ];
$game[ 'game_away_location' ] = $game[ 'game_away_location' ];
$game[ 'game_away_abbr' ] = $game[ 'game_away_abbr' ];
$game[ 'game_away_logo' ] = $game[ 'game_away_logo' ];
}
$game = [
'game_id' => intval( $game['game_id'] ),
'game_week' => intval( $game['game_week'] ),
'game_day' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_day'] ) ),
'game_season' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_season'] ) ),
'game_home_id' => intval( $game['game_home_id'] ),
'game_home_name' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_name'] ) ),
'game_home_location' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_location'] ) ),
'game_home_abbr' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_abbr'] ) ),
'game_home_logo' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_logo'] ) ),
'game_away_id' => intval( $game['game_away_id'] ),
'game_away_name' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_name'] ) ),
'game_away_location' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_location'] ) ),
'game_away_abbr' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_abbr'] ) ),
'game_away_logo' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_logo'] ) ),
'game_home_final' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_final'] ) ),
'game_away_final' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_final'] ) ),
'game_attendance' => intval( $game['game_attendance'] ),
'game_status' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_status'] ) ),
'game_current_period' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_current_period'] ) ),
'game_current_time' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_current_time'] ) ),
'game_current_home_score' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_current_home_score'] ) ),
'game_current_away_score' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_current_away_score'] ) ),
'game_neutral_site' => intval( $game['game_neutral_site'] ),
'game_location_stadium' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_location_stadium'] ) ),
'game_location_line_one' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_location_line_one'] ) ),
'game_location_line_two' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_location_line_two'] ) ),
'game_location_city' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_location_city'] ) ),
'game_location_state' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_location_state'] ) ),
'game_location_country' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_location_country'] ) ),
'game_location_zip_code' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_location_zip_code'] ) ),
'game_video_link' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_video_link'] ) ),
'game_home_radio' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_radio'] ) ),
'game_away_radio' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_radio'] ) ),
'game_notes' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_notes'] ) ),
'game_round' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_round'] ) ),
'game_forfeit' => intval( $game['game_forfeit'] ),
'game_referees' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_referees'] ) ),
'game_home_first_half' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_first_half'] ) ),
'game_home_second_half' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_second_half'] ) ),
'game_home_extratime' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_extratime'] ) ),
'game_home_pks' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_pks'] ) ),
'game_away_first_half' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_first_half'] ) ),
'game_away_second_half' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_second_half'] ) ),
'game_away_extratime' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_extratime'] ) ),
'game_away_pks' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_pks'] ) ),
'game_home_possession' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_home_possession'] ) ),
'game_home_shots' => intval( $game['game_home_shots'] ),
'game_home_sog' => intval( $game['game_home_sog'] ),
'game_home_corners' => intval( $game['game_home_corners'] ),
'game_home_offsides' => intval( $game['game_home_offsides'] ),
'game_home_fouls' => intval( $game['game_home_fouls'] ),
'game_home_saves' => intval( $game['game_home_saves'] ),
'game_home_yellow' => intval( $game['game_home_yellow'] ),
'game_home_red' => intval( $game['game_home_red'] ),
'game_away_possession' => wp_filter_nohtml_kses( sanitize_text_field( $game['game_away_possession'] ) ),
'game_away_shots' => intval( $game['game_away_shots'] ),
'game_away_sog' => intval( $game['game_away_sog'] ),
'game_away_corners' => intval( $game['game_away_corners'] ),
'game_away_offsides' => intval( $game['game_away_offsides'] ),
'game_away_fouls' => intval( $game['game_away_fouls'] ),
'game_away_saves' => intval( $game['game_away_saves'] ),
'game_away_yellow' => intval( $game['game_away_yellow'] ),
'game_away_red' => intval( $game['game_away_red'] ),
];
$the_id = $game['game_id'];
$slug_test = $wpdb->get_results( "SELECT * FROM $table_name WHERE game_id = $the_id" );
if ( [] === $slug_test ) {
$result = $wpdb->insert( $table_name, $game );
} else {
$result = $wpdb->update( $table_name, $game, array( 'game_id' => $game['game_id'] ) );
}
}
}
function open_cup_save_game_events( $game ) {
global $wpdb;
$game_info_ids = $game['game_info_id'];
unset( $game['game_info_id'] );
$team_ids = $game['team_id'];
unset( $game['team_id'] );
$game_info_home_scores = $game['game_info_home_score'];
unset( $game['game_info_home_score'] );
$game_info_away_scores = $game['game_info_away_score'];
unset( $game['game_info_away_score'] );
$game_info_events = $game['game_info_event'];
unset( $game['game_info_event'] );
$game_info_times = $game['game_info_time'];
unset( $game['game_info_time'] );
$player_ids = $game['player_id'];
unset( $game['player_id'] );
$game_info_assists = $game['secondary_player_id'];
unset( $game['secondary_player_id'] );
$len = count( $team_ids );
$events = [];
for ( $i = 0; $i < $len; $i++ ) {
if ( isset( $player_ids[ $i ] ) ) {
if ( isset( $game_info_ids[ $i ] ) ) {
$gi_id = $game_info_ids[ $i ];
} else {
$gi_id = '';
}
if ( $game_info_assists[ $i ] == null ) {
$assists = '';
} else {
$assists = $game_info_assists[ $i ];
}
$event = array(
'game_info_id' => intval( $gi_id ),
'game_id' => intval( $game['game_id'] ),
'team_id' => intval( $team_ids[ $i ] ),
'game_info_home_score' => intval( $game_info_home_scores[ $i ] ),
'game_info_away_score' => intval( $game_info_away_scores[ $i ] ),
'game_info_event' => wp_filter_nohtml_kses( sanitize_text_field( $game_info_events[ $i ] ) ),
'game_info_time' => wp_filter_nohtml_kses( sanitize_text_field( $game_info_times[ $i ] ) ),
'player_id' => intval( $player_ids[ $i ] ),
'game_player_name' => '',
'secondary_player_id' => intval( $assists ),
);
array_push( $events, $event );
}
}
//* Get the game events already in the database to compare the new ones to
$game_info_table = SB_TABLE_PREFIX . 'game_info';
$game_id = $game['game_id'];
$quer = "SELECT * FROM $game_info_table WHERE game_id = $game_id;";
$game_events = $wpdb->get_results( $quer );
$info_ids = [];
foreach ( $game_events as $event ) {
array_push( $info_ids, $event->game_info_id );
}
foreach ( $events as $event ) {
if ( '' !== $event['team_id'] ) {
if ( in_array( $event['game_info_id'], $info_ids ) ) {
//* If the event id is already in the database, update it
$wpdb->update( $wpdb->prefix . 'sb_game_info', $event, array( 'game_info_id' => $event['game_info_id'] ) );
} else {
//* If the event is new, add it to the database
$wpdb->insert( $game_info_table, $event );
$event['game_info_id'] = $wpdb->insert_id;
}
}
}
$event_ids = [];
foreach ( $events as $event ) {
array_push( $event_ids, $event['game_info_id'] );
}
//* If an event is in the database but not the $games array, delete it from the database
foreach ( $info_ids as $info_id ) {
if ( ! in_array( $info_id, $event_ids ) ) {
$wpdb->query( "DELETE FROM $game_info_table WHERE game_info_id = $info_id" );
}
}
if ( '' === $game['game_home_extratime'] ) {
$game['game_home_extratime'] = null;
}
if ( '' === $game['game_home_pks'] ) {
$game['game_home_pks'] = null;
}
if ( '' === $game['game_away_extratime'] ) {
$game['game_away_extratime'] = null;
}
if ( '' === $game['game_away_pks'] ) {
$game['game_away_pks'] = null;
}
return array( $game, $events );
}
function open_cup_save_player_stats( $game ) {
global $wpdb;
//* Pull the player stats out of the $game array
$game_stats_player_ids = $game['game_stats_player_id'];
unset( $game['game_stats_player_id'] );
$team_ids = $game['game_team_id'];
unset( $game['game_team_id'] );
$player_ids = $game['game_player_id'];
unset( $game['game_player_id'] );
$game_player_minutes = $game['game_player_minutes'];
unset( $game['game_player_minutes'] );
$game_player_goals = $game['game_player_goals'];
unset( $game['game_player_goals'] );
$game_player_assists = $game['game_player_assists'];
unset( $game['game_player_assists'] );
$game_player_shots = $game['game_player_shots'];
unset( $game['game_player_shots'] );
$game_player_sog = $game['game_player_sog'];
unset( $game['game_player_sog'] );
$game_player_fouls = $game['game_player_fouls'];
unset( $game['game_player_fouls'] );
$game_player_fouls_suffered = $game['game_player_fouls_suffered'];
unset( $game['game_player_fouls_suffered'] );
$game_player_shots_faced = $game['game_player_shots_faced'];
unset( $game['game_player_shots_faced'] );
$game_player_shots_saved = $game['game_player_shots_saved'];
unset( $game['game_player_shots_saved'] );
$game_player_goals_allowed = $game['game_player_goals_allowed'];
unset( $game['game_player_goals_allowed'] );
//* Loop through each of the player stats and add it to the array of stats to be added or updated
if ( is_array( $team_ids ) ) {
$len = count( $team_ids );
} else {
$len = 0;
}
$stats = [];
for ( $i = 0; $i < $len; $i++ ) {
if ( isset( $game_stats_player_ids[ $i ] ) ) {
$gs_id = $game_stats_player_ids[ $i ];
} else {
$gs_id = '';
}
if ( $game_player_assists[ $i ] == null ) {
$assists = '';
} else {
$assists = $game_player_assists[ $i ];
}
if ( '' !== $player_ids[ $i ] ) {
$stat = array(
'game_stats_player_id' => intval( $gs_id ),
'game_id' => intval( $game['game_id'] ),
'game_team_id' => intval( $team_ids[ $i ] ),
'game_player_id' => intval( $player_ids[ $i ] ),
'game_player_minutes' => intval( $game_player_minutes[ $i ] ),
'game_player_goals' => intval( $game_player_goals[ $i ] ),
'game_player_assists' => intval( $assists ),
'game_player_shots' => intval( $game_player_shots[ $i ] ),
'game_player_sog' => intval( $game_player_sog[ $i ] ),
'game_player_fouls' => intval( $game_player_fouls[ $i ] ),
'game_player_fouls_suffered' => intval( $game_player_fouls_suffered[ $i ] ),
'game_player_shots_faced' => intval( $game_player_shots_faced[ $i ] ),
'game_player_shots_saved' => intval( $game_player_shots_saved[ $i ] ),
'game_player_goals_allowed' => intval( $game_player_goals_allowed[ $i ] ),
);
array_push( $stats, $stat );
}
}
//* Grab the player stats for the game already in the database to compare the new ones to
$game_info_table = SB_TABLE_PREFIX . 'game_stats';
$game_id = $game['game_id'];
$quer = "SELECT * FROM $game_info_table WHERE game_id = $game_id;";
$game_stats = $wpdb->get_results( $quer );
$stats_ids = [];
foreach ( $game_stats as $stat ) {
array_push( $stats_ids, $stat->game_stats_player_id );
}
foreach ( $stats as $stat ) {
if ( in_array( $stat['game_stats_player_id'], $stats_ids ) ) {
//* If the player's stats for the game are already in the database, update the stats
$wpdb->update( SB_TABLE_PREFIX . 'game_stats', $stat, array( 'game_stats_player_id' => $stat['game_stats_player_id'] ) );
} else {
//* If the player's stats for the game aren't already in the database, add the stats
$wpdb->insert( $game_info_table, $stat );
$stat['game_stats_player_id'] = $wpdb->insert_id;
}
}
$stat_ids = [];
foreach ( $stats as $stat ) {
array_push( $stat_ids, $stat['game_stats_player_id'] );
}
//* Check to see if player stats are in the database, but not the player stats array
foreach ( $stats_ids as $stat_id ) {
if ( ! in_array( $stat_id, $stat_ids ) ) {
//* If the database stats aren't in the player stats array, delete them
$wpdb->query( "DELETE FROM $game_info_table WHERE game_stats_player_id = $stat_id" );
}
}
return array( $game, $stats );
}
function open_cup_get_game_info( $game_id ) {
global $wpdb;
$game_table = SB_TABLE_PREFIX . 'games';
$game = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $game_table WHERE game_id = %d", $game_id ), ARRAY_A );
$events = open_cup_get_game_events( $game_id );
$stats = open_cup_get_game_player_stats( $game_id );
return [ $game, $events, $stats ];
}
function open_cup_get_game_events( $game_id ) {
global $wpdb;
$game_info_table = SB_TABLE_PREFIX . 'game_info';
$quer = "SELECT * FROM $game_info_table WHERE game_id = $game_id;";
$game_events = $wpdb->get_results( $quer );
$events = [];
foreach ( $game_events as $event ) {
if ( $event->secondary_player_id == null ) {
$assists = '';
} else {
$assists = $event->secondary_player_id;
}
$event_array = array(
'game_info_id' => $event->game_info_id,
'game_id' => $event->game_id,
'team_id' => $event->team_id,
'game_info_home_score' => $event->game_info_home_score,
'game_info_away_score' => $event->game_info_away_score,
'game_info_event' => $event->game_info_event,
'game_info_time' => $event->game_info_time,
'player_id' => $event->player_id,
'game_player_name' => $event->game_player_name,
'secondary_player_id' => $assists,
);
array_push( $events, $event_array );
}
return $events;
}
function open_cup_get_game_player_stats( $game_id ) {
global $wpdb;
$game_info_table = SB_TABLE_PREFIX . 'game_stats';
$quer = "SELECT * FROM $game_info_table WHERE game_id = $game_id;";
$game_stats = Database::get_results( $quer );
$stats = [];
if ( null !== $game_stats ) {
foreach ( $game_stats as $stat ) {
$stat_array = array(
'game_stats_player_id' => $stat->game_stats_player_id,
'game_id' => $stat->game_id,
'game_team_id' => $stat->game_team_id,
'game_player_id' => $stat->game_player_id,
'game_player_minutes' => $stat->game_player_minutes,
'game_player_goals' => $stat->game_player_goals,
'game_player_assists' => $stat->game_player_assists,
'game_player_shots' => $stat->game_player_shots,
'game_player_sog' => $stat->game_player_sog,
'game_player_fouls' => $stat->game_player_fouls,
'game_player_fouls_suffered' => $stat->game_player_fouls_suffered,
'game_player_shots_faced' => $stat->game_player_shots_faced,
'game_player_shots_saved' => $stat->game_player_shots_saved,
'game_player_goals_allowed' => $stat->game_player_goals_allowed,
);
array_push( $stats, $stat_array );
}
}
return $stats;
}
function open_cup_new_game_details() {
global $wp_filter;
?>
<div class="game-details">
<h2><?php esc_html_e( 'Game Information', 'sports-bench' ); ?></h2>
<div class="field one-column">
<label for="game-status"><?php esc_html_e( 'Status', 'sports-bench' ); ?></label>
<select id="game-status" name="game_status">
<option value=""><?php esc_html_e( 'Select a Status', 'sports-bench' ); ?></option>
<option value="scheduled"><?php esc_html_e( 'Scheduled', 'sports-bench' ); ?></option>
<option value="in_progress"><?php esc_html_e( 'In Progress', 'sports-bench' ); ?></option>
<option value="final"><?php esc_html_e( 'Final', 'sports-bench' ); ?></option>
</select>
</div>
<div class="field one-column">
<label for="game-day"><?php esc_html_e( 'Game Date/Time', 'sports-bench' ); ?></label>
<input type="text" id="game-day" name="game_day" />
</div>
<div class="field one-column">
<label for="game-week"><?php esc_html_e( 'Game Week', 'sports-bench' ); ?></label>
<input type="text" id="game-week" name="game_week" />
</div>
<div class="field one-column">
<label for="game-season"><?php esc_html_e( 'Game Season', 'sports-bench' ); ?></label>
<input type="text" id="game-season" name="game_season" />
</div>
<div class="field one-column">
<label for="game-attendance"><?php esc_html_e( 'Game Attendance', 'sports-bench' ); ?></label>
<input type="text" id="game-attendance" name="game_attendance" />
</div>
<div class="field one-column">
<label for="game-away-head-coach"><?php esc_html_e( 'Away Team Head Coach', 'sports-bench' ); ?></label>
<input type="text" id="game-away-head-coach" name="game_away_head_coach" />
</div>
<div class="field one-column">
<label for="game-away-league"><?php esc_html_e( 'Away Team League', 'sports-bench' ); ?></label>
<input type="text" id="game-away-league" name="game_away_league" />
</div>
<div class="field one-column">
<label for="game-home-head-coach"><?php esc_html_e( 'Home Team Head Coach', 'sports-bench' ); ?></label>
<input type="text" id="game-home-head-coach" name="game_home_head_coach" />
</div>
<div class="field one-column">
<label for="game-home-league"><?php esc_html_e( 'Home Team League', 'sports-bench' ); ?></label>
<input type="text" id="game-home-league" name="game_home_league" />
</div>
<div class="field one-column">
<label for="game-away-radio"><?php esc_html_e( 'Away Radio Link', 'sports-bench' ); ?></label>
<input type="text" id="game-away-radio" name="game_away_radio" />
</div>
<div class="field one-column">
<label for="game-home-radio"><?php esc_html_e( 'Home Radio Link', 'sports-bench' ); ?></label>
<input type="text" id="game-home-radio" name="game_home_radio" />
</div>
<div class="field one-column">
<label for="game-video-link"><?php esc_html_e( 'Video Link', 'sports-bench' ); ?></label>
<input type="text" id="game-video-link" name="game_video_link" />
</div>
<div class="field one-column">
<label for="game-notes"><?php esc_html_e( 'Notes', 'sports-bench' ); ?></label>
<textarea id="game-notes" name="game_notes"></textarea>
</div>
<div class="field one-column">
<label for="game-round"><?php esc_html_e( 'Round', 'sports-bench' ); ?></label>
<input type="text" id="game-round" name="game_round" />
</div>
<div class="field one-column">
<label for="game-referees"><?php esc_html_e( 'Referees', 'sports-bench' ); ?></label>
<input type="text" id="game-referees" name="game_referees" />
</div>
<div class="field one-column">
<label for="game-forfeit"><?php esc_html_e( 'Forfeit?', 'sports-bench' ); ?></label>
<input id="game-forfeit" name="game_forfeit" type="checkbox" value="1" size="50" style="margin: 0 auto; display: block;">
</div>
<div class="field one-column">
<p><?php esc_html_e( 'Neutral Site', 'sports-bench' ); ?></p>
<input type="radio" id="neutral-site-yes" name="game_neutral_site" value="1">
<label for="neutral-site-yes"><?php esc_html_e( 'Yes', 'sports-bench' ); ?></label><br>
<input type="radio" id="neutral-site-no" name="game_neutral_site" value="0" checked="checked">
<label for="neutral-site-no"><?php esc_html_e( 'No', 'sports-bench' ); ?></label><br>
</div>
<div id="neutral-site-fields">
<div class="field one-column">
<label for="game-location-stadium"><?php esc_html_e( 'Stadium', 'sports-bench' ); ?></label>
<input type="text" id="game-location-stadium" name="game_location_stadium" />
</div>
<div class="field one-column">
<label for="game-location-line-one"><?php esc_html_e( 'Location Line 1', 'sports-bench' ); ?></label>
<input type="text" id="game-location-line-one" name="game_location_line_one" />
</div>
<div class="field one-column">
<label for="game-line-two"><?php esc_html_e( 'Location Line 2', 'sports-bench' ); ?></label>
<input type="text" id="game-line-two" name="game_location_line_two" />
</div>
<div class="field one-column">
<label for="game-location-city"><?php esc_html_e( 'Location City', 'sports-bench' ); ?></label>
<input type="text" id="game-location-city" name="game_location_city" />
</div>
<div class="field one-column">
<label for="game-location-state"><?php esc_html_e( 'Location State', 'sports-bench' ); ?></label>
<input type="text" id="game-location-state" name="game_location_state" />
</div>
<div class="field one-column">
<label for="game-location-country"><?php esc_html_e( 'Location Country', 'sports-bench' ); ?></label>
<input type="text" id="game-location-country" name="game_location_country" />
</div>
<div class="field one-column">
<label for="game-location-zip-code"><?php esc_html_e( 'Location ZIP Code', 'sports-bench' ); ?></label>
<input type="text" id="game-location-zip-code" name="game_location_zip_code" />
</div>
</div>
</div>
<?php
}
sports_bench_remove_action( 'sports_bench_new_game_details', 'new_game_details', 10 );
add_action( 'sports_bench_new_game_details', 'open_cup_new_game_details', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment