' . $value . '';
break;
case 'checkbox':
echo '';
break;
default:
echo '';
break;
endswitch;
}
}
if ( !function_exists( 'sp_get_eos_safe_slug' ) ) {
function sp_get_eos_safe_slug( $title, $post_id = 'var' ) {
// String to lowercase
$title = strtolower( $title );
// Replace all numbers with words
$title = sp_numbers_to_words( $title );
// Remove all other non-alphabet characters
$title = preg_replace( "/[^a-z]/", '', $title );
// Convert post ID to words if title is empty
if ( $title == '' ):
$title = sp_numbers_to_words( $post_id );
endif;
return $title;
}
}
if ( !function_exists( 'sp_solve' ) ) {
function sp_solve( $equation, $vars ) {
// Return direct value if streak
if ( str_replace( ' ', '', $equation ) == '$streak' )
return sp_array_value( $vars, 'streak', 0 );
// Clearance to begin calculating remains true if all equation variables are in vars
$clearance = true;
// Check if each variable part is in vars
$parts = explode( ' ', $equation );
foreach( $parts as $key => $value ):
if ( substr( $value, 0, 1 ) == '$' ):
if ( ! array_key_exists( preg_replace( "/[^a-z]/", '', $value ), $vars ) )
$clearance = false;
endif;
endforeach;
if ( $clearance ):
// Equation Operating System
$eos = new eqEOS();
// Solve using EOS
return round( $eos->solveIF( str_replace( ' ', '', $equation ), $vars ), 3 ); // TODO: add precision setting to each column with default set to 3
else:
return 0;
endif;
}
}
if ( !function_exists( 'sp_get_table' ) ) {
function sp_get_table( $post_id, $breakdown = false ) {
$div_id = sp_get_the_term_id( $post_id, 'sp_season', 0 );
$team_ids = (array)get_post_meta( $post_id, 'sp_team', false );
$table_stats = (array)get_post_meta( $post_id, 'sp_teams', true );
// Get labels from result variables
$result_labels = (array)sp_get_var_labels( 'sp_result' );
// Get labels from outcome variables
$outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
// Get all leagues populated with stats where available
$tempdata = sp_array_combine( $team_ids, $table_stats );
// Create entry for each team in totals
$totals = array();
$placeholders = array();
// Initialize streaks counter
$streaks = array();
foreach ( $team_ids as $team_id ):
if ( ! $team_id )
continue;
$streaks[ $team_id ] = array( 'name' => '', 'count' => 0 );
$totals[ $team_id ] = array( 'eventsplayed' => 0, 'streak' => 0 );
foreach ( $result_labels as $key => $value ):
$totals[ $team_id ][ $key . 'for' ] = 0;
$totals[ $team_id ][ $key . 'against' ] = 0;
endforeach;
foreach ( $outcome_labels as $key => $value ):
$totals[ $team_id ][ $key ] = 0;
endforeach;
// Get static stats
$static = get_post_meta( $team_id, 'sp_columns', true );
// Create placeholders entry for the team
$placeholders[ $team_id ] = array();
// Add static stats to placeholders
if ( array_key_exists( $div_id, $static ) ):
$placeholders[ $team_id ] = $static[ $div_id ];
endif;
endforeach;
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
)
)
);
$events = get_posts( $args );
// Event loop
foreach ( $events as $event ):
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
foreach ( $results as $team_id => $team_result ):
foreach ( $team_result as $key => $value ):
if ( $key == 'outcome' ):
if ( array_key_exists( $value, $totals[ $team_id ] ) ):
$totals[ $team_id ]['eventsplayed']++;
$totals[ $team_id ][ $value ]++;
endif;
if ( $value && $value != '-1' ):
if ( $streaks[ $team_id ]['name'] == $value ):
$streaks[ $team_id ]['count'] ++;
else:
$streaks[ $team_id ]['name'] = $value;
$streaks[ $team_id ]['count'] = 1;
endif;
endif;
else:
if ( array_key_exists( $key . 'for', $totals[ $team_id ] ) ):
$totals[ $team_id ][ $key . 'for' ] += $value;
endif;
endif;
endforeach;
endforeach;
endforeach;
foreach ( $streaks as $team_id => $streak ):
// Compile streaks counter and add to totals
$args=array(
'name' => $streak['name'],
'post_type' => 'sp_outcome',
'post_status' => 'publish',
'posts_per_page' => 1
);
$outcomes = get_posts( $args );
if ( $outcomes ):
$outcome = $outcomes[0];
$totals[ $team_id ]['streak'] = $outcome->post_title . $streak['count'];
endif;
endforeach;
$args = array(
'post_type' => 'sp_column',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$stats = get_posts( $args );
$columns = array();
$priorities = array();
foreach ( $stats as $stat ):
// Get post meta
$meta = get_post_meta( $stat->ID );
// Add equation to object
$stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 );
// Add column name to columns
$columns[ $stat->post_name ] = $stat->post_title;
// Add order to priorities if priority is set and does not exist in array already
$priority = sp_array_value( sp_array_value( $meta, 'sp_priority', array() ), 0, 0 );
if ( $priority && ! array_key_exists( $priority, $priorities ) ):
$priorities[ $priority ] = array(
'column' => $stat->post_name,
'order' => sp_array_value( sp_array_value( $meta, 'sp_order', array() ), 0, 'DESC' )
);
endif;
endforeach;
// Sort priorities in descending order
ksort( $priorities );
// Fill in empty placeholder values for each team
foreach ( $team_ids as $team_id ):
if ( ! $team_id )
continue;
foreach ( $stats as $stat ):
if ( sp_array_value( $placeholders[ $team_id ], $stat->post_name, '' ) == '' ):
if ( sizeof( $events ) > 0 ):
$placeholders[ $team_id ][ $stat->post_name ] = sp_solve( $stat->equation, sp_array_value( $totals, $team_id, array() ) );
else:
$placeholders[ $team_id ][ $stat->post_name ] = 0;
endif;
endif;
endforeach;
endforeach;
// Merge the data and placeholders arrays
$merged = array();
foreach( $placeholders as $team_id => $team_data ):
// Add team name to row
$merged[ $team_id ] = array( 'name' => get_the_title( $team_id ) );
foreach( $team_data as $key => $value ):
// Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $team_id, $tempdata ) && array_key_exists( $key, $tempdata[ $team_id ] ) && $tempdata[ $team_id ][ $key ] != '' ):
$merged[ $team_id ][ $key ] = $tempdata[ $team_id ][ $key ];
else:
$merged[ $team_id ][ $key ] = $value;
endif;
endforeach;
endforeach;
uasort( $merged, function( $a, $b ) use ( $priorities ) {
// Loop through priorities
foreach( $priorities as $priority ):
// Proceed if columns are not equal
if ( sp_array_value( $a, $priority['column'], 0 ) != sp_array_value( $b, $priority['column'], 0 ) ):
// Compare column values
$output = sp_array_value( $a, $priority['column'], 0 ) - sp_array_value( $b, $priority['column'], 0 );
// Flip value if descending order
if ( $priority['order'] == 'DESC' ) $output = 0 - $output;
return $output;
endif;
endforeach;
// Default sort by alphabetical
return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) );
});
// Rearrange data array to reflect statistics
$data = array();
foreach( $merged as $key => $value ):
$data[ $key ] = $tempdata[ $key ];
endforeach;
if ( $breakdown ):
return array( $columns, $data, $placeholders, $merged );
else:
array_unshift( $columns, __( 'Team', 'sportspress' ) );
$merged[0] = $columns;
return $merged;
endif;
}
}
if ( !function_exists( 'sp_get_list' ) ) {
function sp_get_list( $post_id, $breakdown = false ) {
$div_id = sp_get_the_term_id( $post_id, 'sp_season', 0 );
$team_id = get_post_meta( $post_id, 'sp_team', true );
$player_ids = (array)get_post_meta( $post_id, 'sp_player', false );
$stats = (array)get_post_meta( $post_id, 'sp_players', true );
// Equation Operating System
$eos = new eqEOS();
// Get labels from result variables
$columns = (array)sp_get_var_labels( 'sp_statistic' );
// Get all leagues populated with stats where available
$tempdata = sp_array_combine( $player_ids, $stats );
// Get equations from statistics variables
$equations = sp_get_var_equations( 'sp_statistic' );
// Create entry for each player in totals
$totals = array();
$placeholders = array();
foreach ( $player_ids as $player_id ):
if ( ! $player_id )
continue;
$totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0 );
foreach ( $columns as $key => $value ):
$totals[ $player_id ][ $key ] = 0;
endforeach;
// Get static statistics
$static = get_post_meta( $player_id, 'sp_statistics', true );
// Create placeholders entry for the player
$placeholders[ $player_id ] = array();
// Add static statistics to placeholders
if ( array_key_exists( $team_id, $static ) && array_key_exists( $div_id, $static[ $team_id ] ) ):
$placeholders[ $player_id ] = $static[ $team_id ][ $div_id ];
endif;
endforeach;
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
)
),
'meta_query' => array(
array(
'key' => 'sp_team',
'value' => $team_id,
)
)
);
$events = get_posts( $args );
// Event loop
foreach( $events as $event ):
$teams = (array)get_post_meta( $event->ID, 'sp_players', true );
if ( ! array_key_exists( $team_id, $teams ) )
continue;
$players = sp_array_value( $teams, $team_id, array() );
foreach ( $players as $player_id => $player_statistics ):
// Increment events played
$totals[ $player_id ]['eventsplayed']++;
foreach ( $player_statistics as $key => $value ):
if ( array_key_exists( $key, $totals[ $player_id ] ) ):
$totals[ $player_id ][ $key ] += $value;
endif;
endforeach;
endforeach;
endforeach;
$args = array(
'post_type' => 'sp_statistic',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'sp_format',
'value' => 'custom',
'compare' => '!=',
),
),
);
$statistics = get_posts( $args );
$columns = array();
$priorities = array();
foreach ( $statistics as $statistic ):
// Get post meta
$meta = get_post_meta( $statistic->ID );
// Add equation to object
$statistic->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 );
// Add column name to columns
$columns[ $statistic->post_name ] = $statistic->post_title;
// Add order to priorities if priority is set and does not exist in array already
$priority = sp_array_value( sp_array_value( $meta, 'sp_priority', array() ), 0, 0 );
if ( $priority && ! array_key_exists( $priority, $priorities ) ):
$priorities[ $priority ] = array(
'column' => $statistic->post_name,
'order' => sp_array_value( sp_array_value( $meta, 'sp_order', array() ), 0, 'DESC' )
);
endif;
endforeach;
// Sort priorities in descending order
ksort( $priorities );
// Fill in empty placeholder values for each player
foreach ( $player_ids as $player_id ):
if ( ! $player_id )
continue;
foreach ( $statistics as $statistic ):
if ( sp_array_value( $placeholders[ $player_id ], $statistic->post_name, '' ) == '' ):
if ( empty( $statistic->equation ) ):
// Reflect totals
$placeholders[ $player_id ][ $statistic->post_name ] = sp_array_value( sp_array_value( $totals, $player_id, array() ), $key, 0 );
else:
// Calculate value
if ( sizeof( $events ) > 0 ):
$placeholders[ $player_id ][ $statistic->post_name ] = sp_solve( $statistic->equation, sp_array_value( $totals, $player_id, array() ) );
else:
$placeholders[ $player_id ][ $statistic->post_name ] = 0;
endif;
endif;
endif;
endforeach;
endforeach;
// Merge the data and placeholders arrays
$merged = array();
foreach( $placeholders as $player_id => $player_data ):
// Add team name to row
$merged[ $player_id ] = array( 'name' => get_the_title( $player_id ) );
foreach( $player_data as $key => $value ):
// Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $player_id, $tempdata ) && array_key_exists( $key, $tempdata[ $player_id ] ) && $tempdata[ $player_id ][ $key ] != '' ):
$merged[ $player_id ][ $key ] = $tempdata[ $player_id ][ $key ];
else:
$merged[ $player_id ][ $key ] = $value;
endif;
endforeach;
endforeach;
uasort( $merged, function( $a, $b ) use ( $priorities ) {
// Loop through priorities
foreach( $priorities as $priority ):
// Proceed if columns are not equal
if ( sp_array_value( $a, $priority['column'], 0 ) != sp_array_value( $b, $priority['column'], 0 ) ):
// Compare column values
$output = sp_array_value( $a, $priority['column'], 0 ) - sp_array_value( $b, $priority['column'], 0 );
// Flip value if descending order
if ( $priority['order'] == 'DESC' ) $output = 0 - $output;
return $output;
endif;
endforeach;
// Default sort by alphabetical
return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) );
});
// Rearrange data array to reflect statistics
$data = array();
foreach( $merged as $key => $value ):
$data[ $key ] = $tempdata[ $key ];
endforeach;
if ( $breakdown ):
return array( $columns, $data, $placeholders, $merged );
else:
array_unshift( $columns, __( 'Player', 'sportspress' ) );
$merged[0] = $columns;
return $merged;
endif;
}
}
if ( !function_exists( 'sp_get_table_html' ) ) {
function sp_get_table_html( $id ) {
$data = sp_get_table( $id );
$output = '
' . '' . '
';
// The first row should be column labels
$labels = $data[0];
// Remove the first row to leave us with the actual data
unset( $data[0] );
foreach( $labels as $label ):
$output .= '
';
// The first row should be column labels
$labels = $data[0];
// Remove the first row to leave us with the actual data
unset( $data[0] );
foreach( $labels as $label ):
$output .= '