Add array functions and clean shit up

This commit is contained in:
Takumi
2013-07-30 19:22:50 +10:00
parent f76bc15c56
commit e3f2e521cc
5 changed files with 69 additions and 55 deletions

View File

@@ -103,7 +103,7 @@ function sp_save_post( $post_id ) {
foreach ( $sportspress as $key => $value ): foreach ( $sportspress as $key => $value ):
delete_post_meta( $post_id, $key ); delete_post_meta( $post_id, $key );
if ( is_array( $value ) ): 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 ); add_post_meta( $post_id, $key, $value, false );
else: else:
$values = new RecursiveIteratorIterator( new RecursiveArrayIterator( $value ) ); $values = new RecursiveIteratorIterator( new RecursiveArrayIterator( $value ) );

View File

@@ -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 ); $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 ); $stats = (array)get_post_meta( $post->ID, 'sp_stats', true );
foreach ( $teams as $key => $value ): 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> <div>
<p> <p>
<strong><?php echo $value ? get_the_title( $value ) : sprintf( __( 'Select %s' ), 'Team' ); ?></strong> <strong><?php echo $value ? get_the_title( $value ) : sprintf( __( 'Select %s' ), 'Team' ); ?></strong>
</p> </p>
<?php <?php sp_data_table( $data, $value, array( 'Player', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ), true, false ); ?>
$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 );
?>
</div> </div>
<?php <?php
endforeach; endforeach;

View File

@@ -1,11 +1,11 @@
<?php <?php
if ( ! function_exists( 'sp_array_depth' ) ) { if ( !function_exists( 'sp_get_array_depth' ) ) {
function sp_array_depth( $array ) { function sp_get_array_depth( $array ) {
$max_depth = 1; $max_depth = 1;
if ( is_array( $array ) ): if ( is_array( $array ) ):
foreach ( $array as $value ): foreach ( $array as $value ):
if ( is_array( $value ) ): if ( is_array( $value ) ):
$depth = sp_array_depth( $value ) + 1; $depth = sp_get_array_depth( $value ) + 1;
if ( $depth > $max_depth ) if ( $depth > $max_depth )
$max_depth = $depth; $max_depth = $depth;
endif; endif;
@@ -17,7 +17,45 @@ if ( ! function_exists( 'sp_array_depth' ) ) {
} }
} }
if ( ! function_exists( 'sp_get_cpt_labels' ) ) { 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 ) { function sp_get_cpt_labels( $name, $singular_name ) {
$labels = array( $labels = array(
'name' => $name, 'name' => $name,
@@ -36,7 +74,7 @@ if ( ! function_exists( 'sp_get_cpt_labels' ) ) {
} }
} }
if ( ! function_exists( 'sp_get_tax_labels' ) ) { if ( !function_exists( 'sp_get_tax_labels' ) ) {
function sp_get_tax_labels( $name, $singular_name ) { function sp_get_tax_labels( $name, $singular_name ) {
$labels = array( $labels = array(
'name' => $name, 'name' => $name,
@@ -56,7 +94,7 @@ if ( ! function_exists( 'sp_get_tax_labels' ) ) {
} }
} }
if ( ! function_exists( 'sp_dropdown_taxonomies' ) ) { if ( !function_exists( 'sp_dropdown_taxonomies' ) ) {
function sp_dropdown_taxonomies( $args = array() ) { function sp_dropdown_taxonomies( $args = array() ) {
$defaults = array( $defaults = array(
'show_option_all' => false, 'show_option_all' => false,
@@ -84,7 +122,7 @@ if ( ! function_exists( 'sp_dropdown_taxonomies' ) ) {
} }
} }
if ( ! function_exists( 'sp_the_posts' ) ) { if ( !function_exists( 'sp_the_posts' ) ) {
function sp_the_posts( $post_id = null, $meta = 'post', $before = '', $sep = ', ', $after = '', $delimiter = ' - ' ) { function sp_the_posts( $post_id = null, $meta = 'post', $before = '', $sep = ', ', $after = '', $delimiter = ' - ' ) {
if ( ! isset( $post_id ) ) if ( ! isset( $post_id ) )
global $post_id; global $post_id;
@@ -121,20 +159,7 @@ if ( ! function_exists( 'sp_the_posts' ) ) {
} }
} }
function sp_array_between ( $array = array(), $delimiter = 0, $index = 0 ) { if ( !function_exists( 'sp_post_checklist' ) ) {
$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 ) { function sp_post_checklist( $post_id = null, $meta = 'post', $display = 'block', $filter = null, $index = null ) {
if ( ! isset( $post_id ) ) if ( ! isset( $post_id ) )
global $post_id; global $post_id;
@@ -176,7 +201,7 @@ if ( ! function_exists( 'sp_post_checklist' ) ) {
} }
} }
if ( ! function_exists( 'sp_data_table' ) ) { if ( !function_exists( 'sp_data_table' ) ) {
function sp_data_table( $data = array(), $index = 0, $columns = array( 'Name' ), $total = true, $auto = true ) { function sp_data_table( $data = array(), $index = 0, $columns = array( 'Name' ), $total = true, $auto = true ) {
if ( !is_array( $data ) ) if ( !is_array( $data ) )
$data = array(); $data = array();
@@ -250,7 +275,7 @@ if ( ! function_exists( 'sp_data_table' ) ) {
} }
} }
if ( ! function_exists( 'sp_post_adder' ) ) { if ( !function_exists( 'sp_post_adder' ) ) {
function sp_post_adder( $meta = 'post' ) { function sp_post_adder( $meta = 'post' ) {
$obj = get_post_type_object( $meta ); $obj = get_post_type_object( $meta );
?> ?>

View File

@@ -33,17 +33,23 @@ function sp_player_team_meta( $post ) {
} }
function sp_player_stats_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 = (array)get_post_meta( $post->ID, 'sp_stats', true );
$stats = $stats[0]; foreach ( $leagues as $league ):
$data = array(); if ( !$league ) continue;
foreach ( $ids as $id ): $data = sp_array_combine( $teams, sp_array_value( $stats, $league->term_id, array() ) );
if ( is_array( $stats ) && array_key_exists( $id, $stats ) ) ?>
$data[ $id ] = $stats[ $id ]; <div>
else <p>
$data[ $id ] = array(); <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; endforeach;
sp_data_table( $data, 0, array( 'Team', 'Played', 'Goals', 'Assists', 'Yellow Cards', 'Red Cards' ) );
} }
function sp_player_profile_meta( $post ) { function sp_player_profile_meta( $post ) {

View File

@@ -41,16 +41,9 @@ function sp_table_team_meta( $post ) {
} }
function sp_table_stats_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 = (array)get_post_meta( $post->ID, 'sp_stats', true );
$stats = $stats[0]; $data = sp_array_combine( $teams, sp_array_value( $stats, 0, array() ) );
$data = array();
foreach ( $ids as $id ):
if ( is_array( $stats ) && array_key_exists( $id, $stats ) )
$data[ $id ] = $stats[ $id ];
else
$data[ $id ] = array();
endforeach;
sp_data_table( $data, 0, array( 'Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), false ); sp_data_table( $data, 0, array( 'Team', 'P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts' ), false );
} }
?> ?>