Last active
December 17, 2018 16:22
-
-
Save kadimi/c5600a3e4013ffcdd1f4af3b89689d82 to your computer and use it in GitHub Desktop.
Add a column in event lists showing players by position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example Usage. | |
*/ | |
sp_event_list_players_by_position( 'team-1', 'Team 1' ); | |
sp_event_list_players_by_position( 'team-2', 'Team 2' ); | |
sp_event_list_players_by_position( 'team-3', 'Team 3' ); | |
sp_event_list_players_by_position( 'team-4', 'Team 4' ); | |
/** | |
* Add a column in event lists showing players by position. | |
* @param string $position_slug Position slug, e.g. `goalkeeper`. | |
* @param string $label Label for table header, e.g. `Goalkeeper`. | |
* @param string $team `home` or `away`. | |
*/ | |
function sp_event_list_players_by_position( $position_slug, $label, $team = 'home' ) { | |
add_action( 'sportspress_event_list_head_row', function( $use_columns ) use ( $label ) { | |
echo '<th>' . $label . '</th>'; | |
} ); | |
add_action( 'sportspress_event_list_row', function( $event, $use_columns ) use ( $team, $position_slug ) { | |
/** | |
* Getting all team players | |
*/ | |
$players_in_event = get_post_meta ( $event->ID, 'sp_players', true ); | |
$players_in_team = false; | |
if ( is_array( $players_in_event ) ) { | |
if ( 'home' == $team ) { | |
$players_in_team = array_values( $players_in_event )[ 0 ]; | |
} else if ( 'away' == $team ) { | |
$players_in_team = array_values( $players_in_event )[ 1 ]; | |
} | |
unset( $players_in_team[ 0 ] ); | |
} | |
/** | |
* Prepare output. | |
*/ | |
$players_in_position = []; | |
if ( $players_in_team ) { | |
foreach ( $players_in_team as $player_id => $player ) { | |
if ( array_key_exists( 'position', $player ) ) { | |
$player_positions_terms = []; | |
foreach( $player['position'] as $player_position_id) { | |
$player_positions_terms[] = get_term( $player_position_id, 'sp_position' ); | |
} | |
$player_positions_terms_matching = array_filter( $player_positions_terms, function( $position_term ) use ($position_slug) { | |
return $position_term->slug === $position_slug; | |
} ); | |
if( $player_positions_terms_matching ) { | |
$players_in_position[] = get_the_title( $player_id ); | |
} | |
} | |
} | |
} | |
/** | |
* Output. | |
*/ | |
echo '<td class="data-custom-' . $position_slug . '" data-label="' . $label . '">'; | |
echo implode( $players_in_position, '<hr>'); | |
echo '</td>'; | |
}, 10, 2 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment