From c319bf8c8f58d76817edf91b1098654f8e25cb4c Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Thu, 19 Jan 2017 16:31:08 +1100 Subject: [PATCH] Fix empty player lists when no columns --- includes/class-sp-player-list.php | 162 +++++++++++++++--------------- 1 file changed, 81 insertions(+), 81 deletions(-) diff --git a/includes/class-sp-player-list.php b/includes/class-sp-player-list.php index 85306d97..487b3c0e 100644 --- a/includes/class-sp-player-list.php +++ b/includes/class-sp-player-list.php @@ -477,12 +477,11 @@ class SP_Player_List extends SP_Custom_Post { $formats = array(); $data = array(); $merged = array(); + $column_order = array(); $ordered_columns = array(); if ( $stats ): - $column_order = array(); - foreach ( $stats as $stat ): // Get post meta @@ -512,105 +511,106 @@ class SP_Player_List extends SP_Custom_Post { endforeach; - foreach ( $column_order as $slug ): + endif; - if ( ! in_array( $slug, $this->columns ) ) continue; + foreach ( $column_order as $slug ): - $ordered_columns[] = $slug; + if ( ! in_array( $slug, $this->columns ) ) continue; - endforeach; + $ordered_columns[] = $slug; - $diff = array_diff( $this->columns, $ordered_columns ); - $this->columns = array_merge( $diff, $ordered_columns ); + endforeach; - // Fill in empty placeholder values for each player - foreach ( $player_ids as $player_id ): - if ( ! $player_id ) - continue; + $diff = array_diff( $this->columns, $ordered_columns ); + $this->columns = array_merge( $diff, $ordered_columns ); - $placeholders[ $player_id ] = array_merge( sp_array_value( $totals, $player_id, array() ), array_filter( sp_array_value( $placeholders, $player_id, array() ) ) ); + // Fill in empty placeholder values for each player + foreach ( $player_ids as $player_id ): + if ( ! $player_id ) + continue; - // Player adjustments - $player_adjustments = sp_array_value( $adjustments, $player_id, array() ); + $placeholders[ $player_id ] = array_merge( sp_array_value( $totals, $player_id, array() ), array_filter( sp_array_value( $placeholders, $player_id, array() ) ) ); - foreach ( $stats as $stat ): - if ( $stat->equation === null ): - $placeholder = sp_array_value( $player_adjustments, $stat->post_name, null ); - if ( $placeholder == null ): - $placeholder = '-'; - endif; - else: - // Solve - $placeholder = sp_solve( $stat->equation, $placeholders[ $player_id ], $stat->precision ); + // Player adjustments + $player_adjustments = sp_array_value( $adjustments, $player_id, array() ); - // Adjustment - $adjustment = sp_array_value( $player_adjustments, $stat->post_name, 0 ); - - // Apply adjustment - if ( $adjustment != 0 ): - $placeholder += $adjustment; - $placeholder = number_format( $placeholder, $stat->precision, '.', '' ); - endif; + foreach ( $stats as $stat ): + if ( $stat->equation === null ): + $placeholder = sp_array_value( $player_adjustments, $stat->post_name, null ); + if ( $placeholder == null ): + $placeholder = '-'; endif; + else: + // Solve + $placeholder = sp_solve( $stat->equation, $placeholders[ $player_id ], $stat->precision ); - if ( $placeholder !== '' && is_numeric( $placeholder ) ): - $placeholders[ $player_id ][ $stat->post_name ] = sp_array_value( $placeholders[ $player_id ], $stat->post_name, 0 ) + $placeholder; - else: - $placeholders[ $player_id ][ $stat->post_name ] = sp_array_value( $placeholders[ $player_id ], $stat->post_name, '-' ); + // Adjustment + $adjustment = sp_array_value( $player_adjustments, $stat->post_name, 0 ); + + // Apply adjustment + if ( $adjustment != 0 ): + $placeholder += $adjustment; + $placeholder = number_format( $placeholder, $stat->precision, '.', '' ); endif; - endforeach; - - endforeach; - - // Merge the data and placeholders arrays - foreach( $placeholders as $player_id => $player_data ): - - $player_data = array_merge( $column_order, $player_data ); - $placeholders[ $player_id ] = $player_data; - - // Add player number and name to row - $merged[ $player_id ] = array(); - if ( in_array( 'number', $this->columns ) ): - $player_data['number'] = get_post_meta( $player_id, 'sp_number', true ); - endif; - - $player_data['name'] = get_the_title( $player_id ); - - if ( in_array( 'team', $this->columns ) ): - $player_data['team'] = get_post_meta( $player_id, 'sp_team', true ); - endif; - - if ( in_array( 'position', $this->columns ) ): - $player_data['position'] = null; endif; - 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 ] != '' ): - $value = $tempdata[ $player_id ][ $key ]; - endif; - - $merged[ $player_id ][ $key ] = $value; - - endforeach; + if ( $placeholder !== '' && is_numeric( $placeholder ) ): + $placeholders[ $player_id ][ $stat->post_name ] = sp_array_value( $placeholders[ $player_id ], $stat->post_name, 0 ) + $placeholder; + else: + $placeholders[ $player_id ][ $stat->post_name ] = sp_array_value( $placeholders[ $player_id ], $stat->post_name, '-' ); + endif; endforeach; - if ( $orderby != 'number' || $order != 'ASC' ): - $this->priorities = array( - array( - 'key' => $orderby, - 'order' => $order, - ), - ); - uasort( $merged, array( $this, 'sort' ) ); + endforeach; + + // Merge the data and placeholders arrays + foreach( $placeholders as $player_id => $player_data ): + + $player_data = array_merge( $column_order, $player_data ); + $placeholders[ $player_id ] = $player_data; + + // Add player number and name to row + $merged[ $player_id ] = array(); + if ( in_array( 'number', $this->columns ) ): + $player_data['number'] = get_post_meta( $player_id, 'sp_number', true ); + endif; + + $player_data['name'] = get_the_title( $player_id ); + + if ( in_array( 'team', $this->columns ) ): + $player_data['team'] = get_post_meta( $player_id, 'sp_team', true ); + endif; + + if ( in_array( 'position', $this->columns ) ): + $player_data['position'] = null; endif; - // Rearrange data array to reflect values - foreach( $merged as $key => $value ): - $data[ $key ] = $tempdata[ $key ]; + 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 ] != '' ): + $value = $tempdata[ $player_id ][ $key ]; + endif; + + $merged[ $player_id ][ $key ] = $value; + endforeach; + endforeach; + + if ( $orderby != 'number' || $order != 'ASC' ): + $this->priorities = array( + array( + 'key' => $orderby, + 'order' => $order, + ), + ); + uasort( $merged, array( $this, 'sort' ) ); endif; + + // Rearrange data array to reflect values + foreach( $merged as $key => $value ): + $data[ $key ] = $tempdata[ $key ]; + endforeach; if ( $admin ):