Add array functions and clean shit up
This commit is contained in:
@@ -103,7 +103,7 @@ function sp_save_post( $post_id ) {
|
||||
foreach ( $sportspress as $key => $value ):
|
||||
delete_post_meta( $post_id, $key );
|
||||
if ( is_array( $value ) ):
|
||||
if ( sp_array_depth( $value ) >= 3 ):
|
||||
if ( sp_get_array_depth( $value ) >= 3 ):
|
||||
add_post_meta( $post_id, $key, $value, false );
|
||||
else:
|
||||
$values = new RecursiveIteratorIterator( new RecursiveArrayIterator( $value ) );
|
||||
|
||||
16
event.php
16
event.php
@@ -77,24 +77,14 @@ function sp_event_stats_meta( $post ) {
|
||||
$teams = array_pad( array_slice( (array)get_post_meta( $post->ID, 'sp_team', false ), 0, $limit ), $limit, 0 );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
|
||||
foreach ( $teams as $key => $value ):
|
||||
$players = sp_array_between( (array)get_post_meta( $post->ID, 'sp_player', false ), 0, $key );
|
||||
$data = sp_array_combine( $players, sp_array_value( $stats, $value, array() ) );
|
||||
?>
|
||||
<div>
|
||||
<p>
|
||||
<strong><?php echo $value ? get_the_title( $value ) : sprintf( __( 'Select %s' ), 'Team' ); ?></strong>
|
||||
</p>
|
||||
<?php
|
||||
$ids = sp_array_between( (array)get_post_meta( $post->ID, 'sp_player', false ), 0, $key );
|
||||
if ( array_key_exists( $value, $stats ) )
|
||||
$team_stats = (array)$stats[ $value ];
|
||||
$data = array();
|
||||
foreach ( $ids as $id ):
|
||||
if ( array_key_exists( $id, $team_stats ) )
|
||||
$data[ $id ] = $team_stats[ $id ];
|
||||
else
|
||||
$data[ $id ] = array();
|
||||
endforeach;
|
||||
sp_data_table( $data, $value, array( 'Player', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ), true, false );
|
||||
?>
|
||||
<?php sp_data_table( $data, $value, array( 'Player', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ), true, false ); ?>
|
||||
</div>
|
||||
<?php
|
||||
endforeach;
|
||||
|
||||
57
helpers.php
57
helpers.php
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
if ( ! function_exists( 'sp_array_depth' ) ) {
|
||||
function sp_array_depth( $array ) {
|
||||
if ( !function_exists( 'sp_get_array_depth' ) ) {
|
||||
function sp_get_array_depth( $array ) {
|
||||
$max_depth = 1;
|
||||
if ( is_array( $array ) ):
|
||||
foreach ( $array as $value ):
|
||||
if ( is_array( $value ) ):
|
||||
$depth = sp_array_depth( $value ) + 1;
|
||||
$depth = sp_get_array_depth( $value ) + 1;
|
||||
if ( $depth > $max_depth )
|
||||
$max_depth = $depth;
|
||||
endif;
|
||||
@@ -17,6 +17,44 @@ if ( ! function_exists( 'sp_array_depth' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
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 ( 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 ( array_key_exists( $key, $values ) )
|
||||
$output[ $key ] = $values[ $key ];
|
||||
else
|
||||
$output[ $key ] = array();
|
||||
endforeach;
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !function_exists( 'sp_get_cpt_labels' ) ) {
|
||||
function sp_get_cpt_labels( $name, $singular_name ) {
|
||||
$labels = array(
|
||||
@@ -121,19 +159,6 @@ if ( ! function_exists( 'sp_the_posts' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
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_post_checklist' ) ) {
|
||||
function sp_post_checklist( $post_id = null, $meta = 'post', $display = 'block', $filter = null, $index = null ) {
|
||||
if ( ! isset( $post_id ) )
|
||||
|
||||
24
player.php
24
player.php
@@ -33,17 +33,23 @@ function sp_player_team_meta( $post ) {
|
||||
}
|
||||
|
||||
function sp_player_stats_meta( $post ) {
|
||||
$ids = (array)get_post_meta( $post->ID, 'sp_team', false );
|
||||
$teams = (array)get_post_meta( $post->ID, 'sp_team', false );
|
||||
$leagues = (array)get_the_terms( $post->ID, 'sp_league' );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
|
||||
$stats = $stats[0];
|
||||
$data = array();
|
||||
foreach ( $ids as $id ):
|
||||
if ( is_array( $stats ) && array_key_exists( $id, $stats ) )
|
||||
$data[ $id ] = $stats[ $id ];
|
||||
else
|
||||
$data[ $id ] = array();
|
||||
foreach ( $leagues as $league ):
|
||||
if ( !$league ) continue;
|
||||
$data = sp_array_combine( $teams, sp_array_value( $stats, $league->term_id, array() ) );
|
||||
?>
|
||||
<div>
|
||||
<p>
|
||||
<strong><?php echo $league->name; ?></strong>
|
||||
</p>
|
||||
<?php
|
||||
sp_data_table( $data, $league->term_id, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ) );
|
||||
?>
|
||||
</div>
|
||||
<?php
|
||||
endforeach;
|
||||
sp_data_table( $data, 0, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ) );
|
||||
}
|
||||
|
||||
function sp_player_profile_meta( $post ) {
|
||||
|
||||
11
table.php
11
table.php
@@ -41,16 +41,9 @@ function sp_table_team_meta( $post ) {
|
||||
}
|
||||
|
||||
function sp_table_stats_meta( $post ) {
|
||||
$ids = (array)get_post_meta( $post->ID, 'sp_team', false );
|
||||
$teams = (array)get_post_meta( $post->ID, 'sp_team', false );
|
||||
$stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
|
||||
$stats = $stats[0];
|
||||
$data = array();
|
||||
foreach ( $ids as $id ):
|
||||
if ( is_array( $stats ) && array_key_exists( $id, $stats ) )
|
||||
$data[ $id ] = $stats[ $id ];
|
||||
else
|
||||
$data[ $id ] = array();
|
||||
endforeach;
|
||||
$data = sp_array_combine( $teams, sp_array_value( $stats, 0, array() ) );
|
||||
sp_data_table( $data, 0, array( 'Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), false );
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user