Skip to content

Instantly share code, notes, and snippets.

@kadimi
Last active February 6, 2019 08:01
Show Gist options
  • Save kadimi/8106103ced1b79e4aa503c1677e324e0 to your computer and use it in GitHub Desktop.
Save kadimi/8106103ced1b79e4aa503c1677e324e0 to your computer and use it in GitHub Desktop.
Display event team players in a two dimensions matrix based on positions
<?php
/**
* Example Usage.
*/
add_action( 'sportspress_after_event_template', function( $elements ) {
$elements['performance']['action'] = function() {
sp_event_players_2d_positions(
[ 'team-1', 'team-2', 'team-3', 'team-4' ],
[ 'lead', 'second', 'third', 'skipper' ],
'home',
'Players Matrix'
);
sportspress_output_event_performance();
};
return $elements ;
} );
/**
* Display current event team players in a two dimensions matrix based on positions
* @param array $x_positions_slugs X axis positions slugs.
* @param array $y_positions_slugs Y axis positions slugs.
* @param string $team Which team to use, use team slug, `home` or `away`.
* @param string $caption Table caption.
* @return void
*/
function sp_event_players_2d_positions( $x_positions_slugs, $y_positions_slugs, $team, $caption = 'Players' ) {
/**
* Use current event ID.
*/
if ( is_singular( 'sp_event' ) ) {
$id = get_the_ID();
} else {
return;
}
/**
* Getting all team players
*/
$players_in_event = get_post_meta ( $id, 'sp_players', true );
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 ];
} else {
$team_post = get_page_by_path( $team, OBJECT, 'sp_team' );
if ( ! $team_post ) {
return;
}
$players_in_team = $players_in_event[ $team_post->ID ];
}
unset( $players_in_team[ 0 ] );
/**
* Make sure $players_in_team is an array.
*/
if ( ! $players_in_team ) {
$players_in_team = [];
}
/**
* Prepare X and Y axes positions.
*/
$x_positions_ids = [];
foreach ( $x_positions_slugs as $slug ) {
$term = get_term_by( 'slug', $slug, 'sp_position' );
if( is_a( $term , 'WP_Term' ) ) {
$x_positions_ids[] = $term->term_id;
}
}
$y_positions_ids = [];
foreach ( $y_positions_slugs as $slug ) {
$term = get_term_by( 'slug', $slug, 'sp_position' );
if( is_a( $term , 'WP_Term' ) ) {
$y_positions_ids[] = $term->term_id;
}
}
/**
* Build list.
*/
$list_2d = [];
foreach ( $x_positions_ids as $x ) {
$list_2d[ $x ] = [
'title' => get_term( $x, 'sp_position' )->name,
'elements' => [],
];
foreach ( $y_positions_ids as $y ) {
$list_2d[ $x ][ 'elements' ][ $y ] = [
'title' => get_term( $y, 'sp_position' )->name,
'elements' => [],
];
$players_in_xy = array_filter( $players_in_team, function( $player ) use ( $x, $y ) {
return ! empty( $player[ 'position' ] ) && in_array( $x, $player[ 'position' ] ) && in_array( $y, $player[ 'position' ] );
} );
$list_2d[ $x ]['elements'][ $y ][ 'elements' ] = array_map( function( $id ) {
return get_the_title( $id );
}, array_keys( $players_in_xy ) );
}
}
/**
* Display list.
*/
if ( $caption ) {
echo '<h4 class="sp-table-caption">' . $caption . '</h4>';
}
echo '<table class="sp-data-table">';
echo '<thead>';
echo '<tr>';
echo '<th>' . __( 'Position', 'sportspress' ) . '</th>';
foreach ( $x_positions_ids as $x_position_id ) {
echo '<th>' . $list_2d[ $x_position_id ][ 'title' ] . '</th>';
}
echo '</tr>';
echo '</thead>';
echo '<tbody>';
$i = 0;
foreach ( $y_positions_ids as $y_position_id ) {
echo '<tr>';
echo '<th>' . $list_2d[ $x_position_id ][ 'elements' ][ array_values($y_positions_ids)[ $i++ ] ][ 'title' ] . '</th>';
foreach ( $x_positions_ids as $x_position_id ) {
echo '<td>' . implode( '<br>', $list_2d[ $x_position_id ][ 'elements' ][ $y_position_id ][ 'elements' ] ) . '</td>';
}
echo '</tr>';
}
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment