$max_depth )
$max_depth = $depth;
endif;
endforeach;
return $max_depth;
else:
return 0;
endif;
}
}
if ( !function_exists( 'sp_array_between' ) ) {
function sp_array_between ( $array = array(), $delimiter = 0, $index = 0 ) {
$keys = array_keys( $array, $delimiter );
if ( array_key_exists( $index, $keys ) ):
$offset = $keys[ $index ];
$end = sizeof( $array );
if ( array_key_exists( $index + 1, $keys ) )
$end = $keys[ $index + 1 ];
$length = $end - $offset;
$array = array_slice( $array, $offset, $length );
endif;
return $array;
}
}
if ( !function_exists( 'sp_array_value' ) ) {
function sp_array_value( $arr = array(), $key = 0, $default = null ) {
if ( is_array( $arr ) && array_key_exists( $key, $arr ) )
$subset = $arr[ $key ];
else
$subset = $default;
return $subset;
}
}
if ( !function_exists( 'sp_array_combine' ) ) {
function sp_array_combine( $keys = array(), $values = array() ) {
$output = array();
foreach ( $keys as $key ):
if ( is_array( $values ) && array_key_exists( $key, $values ) )
$output[ $key ] = $values[ $key ];
else
$output[ $key ] = array();
endforeach;
return $output;
}
}
if ( !function_exists( 'sp_numbers_to_words' ) ) {
function sp_numbers_to_words( $str ) {
$output = str_replace( array( '1st', '2nd', '3rd', '5th', '8th', '9th', '10', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ), array( 'first', 'second', 'third', 'fifth', 'eight', 'ninth', 'ten', 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine' ), $str );
return $output;
}
}
if ( !function_exists( 'sp_cpt_labels' ) ) {
function sp_cpt_labels( $name, $singular_name, $lowercase_name = null, $is_submenu = false ) {
if ( !$lowercase_name ) $lowercase_name = $name;
$labels = array(
'name' => $name,
'singular_name' => $singular_name,
'all_items' => $name,
'add_new' => sprintf( __( 'Add %s', 'sportspress' ), $singular_name ),
'add_new_item' => sprintf( __( 'Add New %s', 'sportspress' ), $singular_name ),
'edit_item' => sprintf( __( 'Edit %s', 'sportspress' ), $singular_name ),
'new_item' => sprintf( __( 'New %s', 'sportspress' ), $singular_name ),
'view_item' => sprintf( __( 'View %s', 'sportspress' ), $singular_name ),
'search_items' => sprintf( __( 'Search %s', 'sportspress' ), $name ),
'not_found' => sprintf( __( 'No %s found', 'sportspress' ), $lowercase_name ),
'not_found_in_trash' => sprintf( __( 'No %s found in trash', 'sportspress' ), $lowercase_name ),
'parent_item_colon' => sprintf( __( 'Parent %s', 'sportspress' ), $singular_name ) . ':'
);
return $labels;
}
}
if ( !function_exists( 'sp_tax_labels' ) ) {
function sp_tax_labels( $name, $singular_name, $lowercase_name = null ) {
if ( !$lowercase_name ) $lowercase_name = $name;
$labels = array(
'name' => $name,
'singular_name' => $singular_name,
'all_items' => sprintf( __( 'All %s', 'sportspress' ), $name ),
'edit_item' => sprintf( __( 'Edit %s', 'sportspress' ), $singular_name ),
'view_item' => sprintf( __( 'View %s', 'sportspress' ), $singular_name ),
'update_item' => sprintf( __( 'Update %s', 'sportspress' ), $singular_name ),
'add_new_item' => sprintf( __( 'Add New %s', 'sportspress' ), $singular_name ),
'new_item_name' => sprintf( __( 'New %s Name', 'sportspress' ), $singular_name ),
'parent_item' => sprintf( __( 'Parent %s', 'sportspress' ), $singular_name ),
'parent_item_colon' => sprintf( __( 'Parent %s', 'sportspress' ), $singular_name ) . ':',
'search_items' => sprintf( __( 'Search %s', 'sportspress' ), $name ),
'not_found' => sprintf( __( 'No %s found', 'sportspress' ), $lowercase_name )
);
return $labels;
}
}
if ( !function_exists( 'sp_get_the_term_id' ) ) {
function sp_get_the_term_id( $post_id, $taxonomy, $index ) {
$terms = get_the_terms( $post_id, $taxonomy );
if ( is_array( $terms ) && array_key_exists( $index, $terms ) ):
$term = $terms[0];
if ( is_object( $term ) && property_exists( $term, 'term_id' ) )
return $term->term_id;
else
return 0;
else:
return 0;
endif;
}
}
if ( !function_exists( 'sp_dropdown_taxonomies' ) ) {
function sp_dropdown_taxonomies( $args = array() ) {
$defaults = array(
'show_option_all' => false,
'show_option_none' => false,
'taxonomy' => null,
'name' => null,
'selected' => null
);
$args = array_merge( $defaults, $args );
$terms = get_terms( $args['taxonomy'] );
$name = ( $args['name'] ) ? $args['name'] : $args['taxonomy'];
if ( $terms ) {
printf( '' );
}
}
}
if ( !function_exists( 'sp_the_posts' ) ) {
function sp_the_posts( $post_id = null, $meta = 'post', $before = '', $sep = ', ', $after = '', $delimiter = ' - ' ) {
if ( ! isset( $post_id ) )
global $post_id;
$ids = get_post_meta( $post_id, $meta, false );
if ( ( $key = array_search( 0, $ids ) ) !== false )
unset( $ids[ $key ] );
$i = 0;
$count = count( $ids );
if ( isset( $ids ) && $ids && is_array( $ids ) && !empty( $ids ) ):
foreach ( $ids as $id ):
if ( !$id ) continue;
if ( !empty( $before ) ):
if ( is_array( $before ) && array_key_exists( $i, $before ) )
echo $before[ $i ] . ' - ';
else
echo $before;
endif;
$parents = get_post_ancestors( $id );
$parents = array_combine( array_keys( $parents ), array_reverse( array_values( $parents ) ) );
foreach ( $parents as $parent ):
if ( !in_array( $parent, $ids ) )
edit_post_link( get_the_title( $parent ), '', '', $parent );
echo $delimiter;
endforeach;
$title = get_the_title( $id );
if ( empty( $title ) )
$title = __( '(no title)', 'sportspress' );
edit_post_link( $title, '', '', $id );
if ( !empty( $after ) ):
if ( is_array( $after ) ):
if ( array_key_exists( $i, $after ) && $after[ $i ] != '' ):
echo ' - ' . $after[ $i ];
endif;
else:
echo $after;
endif;
endif;
if ( ++$i !== $count )
echo $sep;
endforeach;
endif;
}
}
if ( !function_exists( 'sp_the_plain_terms' ) ) {
function sp_the_plain_terms( $id, $taxonomy ) {
$terms = get_the_terms( $id, $taxonomy );
$arr = array();
foreach( $terms as $term ):
$arr[] = $term->name;
endforeach;
echo implode( ', ', $arr );
}
}
if ( !function_exists( 'sp_post_checklist' ) ) {
function sp_post_checklist( $post_id = null, $meta = 'post', $display = 'block', $filter = null, $index = null ) {
if ( ! isset( $post_id ) )
global $post_id;
?>
', sizeof( $parents ) ); ?>
$type,
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'exclude' => $postid
);
$vars = get_posts( $args );
// Add extra vars to the array
if ( isset( $defaults ) && is_array( $defaults ) ):
foreach ( $defaults as $key => $value ):
$arr[ $key ] = $value;
endforeach;
endif;
// Add vars to the array
if ( isset( $variations ) && is_array( $variations ) ):
foreach ( $vars as $var ):
if ( $plain ) $arr[ $var->post_name ] = $var->post_title;
foreach ( $variations as $key => $value ):
$arr[ $var->post_name . '_' . $key ] = $var->post_title . ' ' . $value;
endforeach;
endforeach;
else:
foreach ( $vars as $var ):
$arr[ $var->post_name ] = $var->post_title;
endforeach;
endif;
return (array) $arr;
}
}
if ( !function_exists( 'sp_get_equation_selector' ) ) {
function sp_get_equation_selector( $postid, $selected = null, $groups = array() ) {
if ( ! isset( $postid ) )
return;
// Initialize options array
$options = array();
// Add groups to options
foreach ( $groups as $group ):
switch ( $group ):
case 'event':
$options[ __( 'Events', 'sportspress' ) ] = array( 'events_scheduled' => __( 'Scheduled', 'sportspress' ), 'events_attended' => __( 'Attended', 'sportspress' ), 'events_played' => __( 'Played', 'sportspress' ) );
break;
case 'result':
$options[ __( 'Results', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_result', array( 'for' => '→', 'against' => '←' ), null, false );
break;
case 'outcome':
$options[ __( 'Outcomes', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_outcome', array( 'max' => '↑', 'min' => '↓' ) );
break;
case 'stat':
$options[ __( 'Statistics', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_stat' );
break;
case 'metric':
$options[ __( 'Metrics', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_metric' );
break;
endswitch;
endforeach;
// Create array of operators
$operators = array( '+' => '+', '-' => '−', '*' => '×', '/' => '÷', '==' => '=', '!=' => '≠', '<' => '<', '<=' => '≤', '>' => '>', '>=' => '≥', '(' => '(', ')' => ')' );
// Add operators to options
$options[ __( 'Operators', 'sportspress' ) ] = $operators;
// Create array of constants
$max = 10;
$constants = array();
for ( $i = 1; $i <= $max; $i ++ ):
$constants[$i] = $i;
endfor;
// Add constants to options
$options[ __( 'Constants', 'sportspress' ) ] = (array) $constants;
?>
-1
),
(array)$args
);
$posts = (array)get_posts( $args );
// Equation Operating System
$eos = new eqEOS();
$vars = array();
$stats_settings = get_option( 'sportspress_stats' );
// Get dynamic stats
switch ($post_type):
case 'sp_team':
// Get stats settings columns
$columns = sp_get_eos_rows( get_option( 'sp_event_stats_columns' ) );
// Setup variables
$results = array();
foreach ( $columns as $key => $value ):
$column = explode( ':', $value );
$var_name = preg_replace("/[^A-Za-z0-9 ]/", '', sp_array_value( $column, 1 ) );
$results[] = $var_name;
$vars[ $var_name ] = 0;
endforeach;
foreach( $posts as $post ):
// Add object properties needed for retreiving event stats
$post->sp_team = get_post_meta( $post->ID, 'sp_team', false );
$post->sp_team_index = array_search( $args['meta_query'][0]['value'], $post->sp_team );
$post->sp_result = get_post_meta( $post->ID, 'sp_result', false );
$post->sp_results = sp_array_value( sp_array_value( sp_array_value( get_post_meta( $post->ID, 'sp_results', false ), 0, array() ), 0, array() ), $args['meta_query'][0]['value'], array() );
// Add event stats to vars as sum
foreach( $results as $key => $value ):
if ( !array_key_exists( $value, $vars ) ) $vars[ $value ] = 0;
$vars[ $value ] += sp_array_value( $post->sp_results, $key, 0 );
endforeach;
endforeach;
// All events attended by the team
$vars['appearances'] = sizeof( $posts );
// Events with a aesult value greater than at least one competitor
$vars['greater'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) > 1 && max( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ] && min( $post->sp_result ) < $post->sp_result[ $post->sp_team_index ]; } ) );
// Events with a aesult value equal to all competitors
$vars['equal'] = sizeof( array_filter( $posts, function( $post ) { return max( $post->sp_result ) == min( $post->sp_result ); } ) );
// Events with a aesult value less than at least one competitor
$vars['less'] = sizeof( array_filter( $posts, function( $post ) { return array_count_values( $post->sp_result ) > 1 && min( $post->sp_result ) == $post->sp_result[ $post->sp_team_index ] && max( $post->sp_result ) > $post->sp_result[ $post->sp_team_index ]; } ) );
// Sum of the team's result values
$vars['for'] = 0; foreach( $posts as $post ): $vars['for'] += $post->sp_result[ $post->sp_team_index ]; endforeach;
// Sum of opposing result values
$vars['against'] = 0; foreach( $posts as $post ): $result = $post->sp_result; unset( $result[ $post->sp_team_index ] ); $vars['against'] += array_sum( $result ); endforeach;
// Get EOS array
$rows = sp_get_eos_rows( get_option( 'sp_team_stats_columns' ) );
break;
case 'sp_player':
// Get stats settings keys
$keys = sp_get_eos_keys( get_option( 'sp_player_stats_columns' ) );
// Add object properties needed for retreiving event stats
foreach( $posts as $post ):
$post->sp_player = get_post_meta( $post->ID, 'sp_team', false );
$post->sp_player_index = array_search( $args['meta_query'][0]['value'], $post->sp_player );
endforeach;
// Create array of event stats columns
$columns = sp_get_eos_rows( get_option( 'sp_event_stats_columns' ) );
foreach ( $columns as $key => $value ):
$row = explode( ':', $value );
$var_name = strtolower( preg_replace( '~[^\p{L}]++~u', '', end( $row ) ) );
$vars[ $var_name ] = 0;
$stats_keys[ $key ] = $var_name;
endforeach;
// Populate columns with player stats from events
foreach ( $posts as $post ):
$team_stats = get_post_meta( $post->ID, 'sp_stats', true );
foreach ( $team_stats as $team_id => $stat ):
if ( array_key_exists( 1, $args['meta_query'] ) && $team_id != sp_array_value( $args['meta_query'][1], 'value', 0 ) ) continue;
$player_id = sp_array_value( $args['meta_query'][0], 'value', 0 );
if ( !array_key_exists( $player_id, $stat ) ) continue;
foreach ( $stat[ $player_id ] as $key => $value ):
if ( !array_key_exists( $key, $stats_keys ) || !array_key_exists( $stats_keys[ $key ], $vars ) ) continue;
$vars[ $stats_keys[ $key ] ] += $value;
endforeach;
endforeach;
endforeach;
// Add appearances event count to vars
$vars['appearances'] = sizeof( $posts );
// Get EOS array
$rows = sp_get_eos_rows( get_option( 'sp_player_stats_columns' ) );
break;
default:
$rows = array();
break;
endswitch;
// Get dynamic stats
$dynamic = array();
foreach ( $rows as $key => $value ):
$row = explode( ':', $value );
$dynamic[ $key ] = $eos->solveIF( sp_array_value( $row, 1, '$appearances'), $vars );
endforeach;
if ( $static ):
// Get static stats
$static = (array)get_post_meta( $args['meta_query'][0]['value'], 'sp_stats', true );
$table = sp_array_value( $static, 0, array() );
if ( array_key_exists( 'tax_query', $args ) )
$row_id = $args['tax_query'][0]['terms'];
else
$row_id = 0;
$static = sp_array_value( $table, $row_id, array() );
// Combine static and dynamic stats
$output = array_filter( $static ) + $dynamic;
ksort( $output );
else:
$output = $dynamic;
endif;
return $output;
}
}
if ( !function_exists( 'sp_stats_table' ) ) {
function sp_stats_table( $stats = array(), $placeholders = array(), $index = 0, $columns = array(), $total = true, $rowtype = 'post', $slug = 'sp_stats' ) {
global $pagenow;
if ( !is_array( $stats ) )
$stats = array( __( 'Name', 'sportspress' ) );
?>
' . $value . '';
break;
case 'checkbox':
echo '';
break;
default:
echo '';
break;
endswitch;
}
}
?>