diff --git a/actions.php b/actions.php index 72897e5d..52ab22ac 100644 --- a/actions.php +++ b/actions.php @@ -20,8 +20,8 @@ function sp_manage_posts_custom_column( $column, $post_id ) { echo get_the_terms ( $post_id, 'sp_position' ) ? the_terms( $post_id, 'sp_position' ) : '—'; break; case 'sp_team': - $score = get_post_meta( $post_id, 'sp_score', false ); - echo get_post_meta ( $post_id, 'sp_team' ) ? sp_the_posts( $post_id, 'sp_team', '', '
', $score, ( empty( $score ) ? ' — ' : ' ' ) ) : '—'; + $result = get_post_meta( $post_id, 'sp_result', false ); + echo get_post_meta ( $post_id, 'sp_team' ) ? sp_the_posts( $post_id, 'sp_team', '', '
', $result, ( empty( $result ) ? ' — ' : ' ' ) ) : '—'; break; case 'sp_event': echo get_post_meta ( $post_id, 'sp_event' ) ? sizeof( get_post_meta ( $post_id, 'sp_event' ) ) : '—'; @@ -99,24 +99,46 @@ function sp_save_post( $post_id ) { if ( !isset( $_POST['sportspress_nonce'] ) || ! wp_verify_nonce( $_POST['sportspress_nonce'], plugin_basename( __FILE__ ) ) ) return $post_id; switch ( $_POST['post_type'] ): case ( 'sp_team' ): - update_post_meta( $post_id, 'sp_stats', $_POST['sp_stats'] ); + update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); break; case ( 'sp_event' ): - update_post_meta( $post_id, 'sp_stats', $_POST['sp_stats'] ); - sp_update_post_meta_recursive( $post_id, 'sp_team', $_POST['sp_team'] ); - sp_update_post_meta_recursive( $post_id, 'sp_player', $_POST['sp_player'] ); + update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); + delete_post_meta( $post_id, 'sp_result'); + $stats = (array)sp_array_value( $_POST, 'sp_stats', array() ); + foreach ( $stats as $table ) { + + // Add values from each row where total is blank + $total = sp_array_value( sp_array_value( $table, 0, null ), 0, null ); + if ( $total == null ): + $values = array(); + foreach ( $table as $row ): + $value = sp_array_value( $row, 0, null ); + if ( is_numeric( $value ) ): + $values[] = $value; + endif; + endforeach; + if ( sizeof( $values ) ): + $total = array_sum( $values ); + endif; + endif; + + // Save first column of total row as result + add_post_meta( $post_id, 'sp_result', $total ); + } + sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); + sp_update_post_meta_recursive( $post_id, 'sp_player', sp_array_value( $_POST, 'sp_player', array() ) ); break; case ( 'sp_player' ): - update_post_meta( $post_id, 'sp_stats', $_POST['sp_stats'] ); - sp_update_post_meta_recursive( $post_id, 'sp_team', $_POST['sp_team'] ); + update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); + sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); break; case ( 'sp_staff' ): - sp_update_post_meta_recursive( $post_id, 'sp_team', $_POST['sp_team'] ); + sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); break; case ( 'sp_table' ): - update_post_meta( $post_id, 'sp_stats', $_POST['sp_stats'] ); - wp_set_post_terms( $post_id, $_POST['sp_league'], 'sp_league' ); - sp_update_post_meta_recursive( $post_id, 'sp_team', $_POST['sp_team'] ); + update_post_meta( $post_id, 'sp_stats', sp_array_value( $_POST, 'sp_stats', array() ) ); + wp_set_post_terms( $post_id, sp_array_value( $_POST, 'sp_league', 0 ), 'sp_league' ); + sp_update_post_meta_recursive( $post_id, 'sp_team', sp_array_value( $_POST, 'sp_team', array() ) ); break; endswitch; diff --git a/event.php b/event.php index 6effb381..4cde0689 100644 --- a/event.php +++ b/event.php @@ -76,6 +76,8 @@ function sp_event_stats_meta( $post ) { $limit = get_option( 'sp_event_team_count' ); $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 ); + $result = (array)get_post_meta( $post->ID, 'sp_result', false ); + print_r( $result ); // Teams foreach ( $teams as $key => $value ): diff --git a/helpers.php b/helpers.php index 00f4d1f7..d958c0ac 100644 --- a/helpers.php +++ b/helpers.php @@ -167,10 +167,13 @@ if ( !function_exists( 'sp_the_posts' ) ) { $title = __( '(no title)' ); edit_post_link( $title, '', '', $id ); if ( !empty( $after ) ): - if ( is_array( $after ) && array_key_exists( $i, $after ) ) - echo ' - ' . $after[ $i ]; - else + 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; @@ -233,29 +236,37 @@ if ( !function_exists( 'sp_post_checklist' ) ) { } if ( !function_exists( 'sp_get_stats_row' ) ) { - function sp_get_stats_row( $args ) { - extract( $args ); - $args = array( - 'post_type' => $post_type, - 'posts_per_page' => -1, - 'meta_query' => array( - array( - 'key' => $meta_key, - 'value' => $meta_value, - ) - ) + function sp_get_stats_row( $args = array() ) { + $args = array_merge( + array( + 'posts_per_page' => -1 + ), + (array)$args ); - if ( isset( $terms ) ): - $args['tax_query'] = array( - array( - 'taxonomy' => $taxonomy, - 'terms' => $terms - ) - ); - endif; $posts = (array)get_posts( $args ); - $row = array(); - $row[] = sizeof( $posts ); + foreach( $posts as $post ): + $post->sp_stats = get_post_meta( $post->ID, 'sp_stats', true ); + endforeach; + $row = array( + sizeof( $posts ), + sizeof( + array_filter( + $posts, + function( $var ) { + echo '
';
+				print_r( $var->sp_stats );
+				echo '
'; + return true; + } + ) + ), + 99, + 99, + 93, + 99, + 99, + 99 + ); return $row; } } @@ -268,7 +279,7 @@ if ( !function_exists( 'sp_get_stats' ) ) { $subset = sp_array_value( $set, $subset_id, array() ); // If empty columns exist in subset - if ( in_array( '', $subset ) ): + if ( empty( $subset ) || in_array( '', $subset ) ): // If subset is total row if ( $subset_id == 0 ): @@ -297,8 +308,9 @@ if ( !function_exists( 'sp_get_stats' ) ) { // Get fallback values switch ( $post_type ): + + // Teams: all events attended in the league case 'sp_team': - // Get all events attended in that league $args = array( 'post_type' => 'sp_event', 'meta_key' => 'sp_team', @@ -308,13 +320,11 @@ if ( !function_exists( 'sp_get_stats' ) ) { ); $fallback = sp_get_stats_row( $args ); break; + endswitch; - // Add values from fallback where missing - foreach( $subset as $key => $value ): - if ( $value != '' ) continue; - $subset[ $key ] = sp_array_value( $fallback, $key, 0 ); - endforeach; + // Merge fallback values with current subset + $subset = array_merge( $fallback, $subset ); endif; @@ -406,12 +416,20 @@ if ( !function_exists( 'sp_post_adder' ) ) { } } +if ( !function_exists( 'sp_update_post_meta' ) ) { + function sp_update_post_meta( $post_id, $meta_key, $meta_value, $default = null ) { + if ( !isset( $meta_value ) && isset( $default ) ) + $meta_value = $default; + add_post_meta( $post_id, $meta_key, $meta_value, true ); + } +} + if ( !function_exists( 'sp_update_post_meta_recursive' ) ) { - function sp_update_post_meta_recursive( $post_id, $name, $array ) { - delete_post_meta( $post_id, $name ); - $values = new RecursiveIteratorIterator( new RecursiveArrayIterator( $array ) ); + function sp_update_post_meta_recursive( $post_id, $meta_key, $meta_value ) { + delete_post_meta( $post_id, $meta_key ); + $values = new RecursiveIteratorIterator( new RecursiveArrayIterator( $meta_value ) ); foreach ( $values as $value ): - add_post_meta( $post_id, $name, $value, false ); + add_post_meta( $post_id, $meta_key, $value, false ); endforeach; } } diff --git a/team.php b/team.php index 7ccca5a8..90079af6 100644 --- a/team.php +++ b/team.php @@ -63,9 +63,7 @@ function sp_team_stats_meta( $post ) { $placeholders[ $league_id ] = sp_get_stats_row( $args ); endforeach; - ?> - \ No newline at end of file