From f2641222ad046c80c23abe676a04d9aa13f4b764 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Mon, 20 Jan 2014 14:49:20 +1100 Subject: [PATCH] Allow statistic and column editing for admin only --- admin-functions.php | 41 +++++++++++++++++++---------- admin/hooks/save-post.php | 10 ++++++- admin/hooks/the-content.php | 2 +- admin/post-types/player.php | 4 +-- admin/post-types/team.php | 10 +++---- functions.php | 52 ++++++++++++++++++------------------- 6 files changed, 71 insertions(+), 48 deletions(-) diff --git a/admin-functions.php b/admin-functions.php index 057ab9ae..c24613fe 100644 --- a/admin-functions.php +++ b/admin-functions.php @@ -628,7 +628,7 @@ if ( !function_exists( 'sportspress_edit_player_list_table' ) ) { } if ( !function_exists( 'sportspress_edit_team_columns_table' ) ) { - function sportspress_edit_team_columns_table( $league_id, $columns = array(), $data = array(), $seasons = array() ) { + function sportspress_edit_team_columns_table( $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $seasons = array(), $readonly = true ) { ?>
@@ -658,7 +658,11 @@ if ( !function_exists( 'sportspress_edit_team_columns_table' ) ) { $label ): $value = sportspress_array_value( sportspress_array_value( $data, $div_id, array() ), $column, 0 ); ?> - +
'; + ?>
@@ -690,7 +694,7 @@ if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) { $div_stats ): - if ( !$div_id ) continue; + if ( !$div_id || $div_id == 'statistics' ) continue; $div = get_term( $div_id, 'sp_season' ); ?> @@ -702,7 +706,7 @@ if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) { $value = sportspress_array_value( $seasons_teams, $div_id, '-1' ); $args = array( 'post_type' => 'sp_team', - 'name' => 'sp_seasons_teams[' . $league_id . '][' . $div_id . ']', + 'name' => 'sp_leagues[' . $league_id . '][' . $div_id . ']', 'show_option_none' => __( '-- Select --', 'sportspress' ), 'sort_order' => 'ASC', 'sort_column' => 'menu_order', @@ -726,9 +730,12 @@ if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) { ?> $label ): - $value = sportspress_array_value( sportspress_array_value( $data, $div_id, array() ), $column, 0 ); ?> - + + $label ): $player_id = 0; @@ -1139,8 +1147,10 @@ if ( !function_exists( 'sportspress_get_team_columns_data' ) ) { endif; endforeach; - // Get all leagues populated with columns where available - $data = $div_ids; + $data = array(); + + // Get all seasons populated with data where available + $data = sportspress_array_combine( $div_ids, sportspress_array_value( $columns, $league_id, array() ) ); // Get equations from column variables $equations = sportspress_get_var_equations( 'sp_column' ); @@ -1307,7 +1317,7 @@ if ( !function_exists( 'sportspress_get_team_columns_data' ) ) { endforeach; if ( $admin ): - return array( $columns, $placeholders, $leagues_seasons ); + return array( $columns, $data, $placeholders, $merged, $leagues_seasons ); else: $labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ) ), $columns ); $merged[0] = $labels; @@ -1440,6 +1450,11 @@ if ( !function_exists( 'sportspress_get_league_table_data' ) ) { else: if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $key . 'for', $totals[ $team_id ] ) ): $totals[ $team_id ][ $key . 'for' ] += $value; + foreach( $results as $other_team_id => $other_result ): + if ( $other_team_id != $team_id && array_key_exists( $key . 'against', $totals[ $team_id ] ) ): + $totals[ $team_id ][ $key . 'against' ] += sportspress_array_value( $other_result, $key, 0 ); + endif; + endforeach; endif; endif; @@ -1794,7 +1809,7 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) { endforeach; if ( $admin ): - return array( $columns, $placeholders ); + return array( $columns, $tempdata, $placeholders, $merged ); else: $labels = array_merge( array( 'name' => __( 'Player', 'sportspress' ) ), $columns ); $merged[0] = $labels; @@ -1829,7 +1844,7 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) { $seasons = (array)get_the_terms( $post_id, 'sp_season' ); $stats = (array)get_post_meta( $post_id, 'sp_statistics', true ); - $seasons_teams = sportspress_array_value( (array)get_post_meta( $post_id, 'sp_seasons_teams', true ), $league_id, array() ); + $seasons_teams = sportspress_array_value( (array)get_post_meta( $post_id, 'sp_leagues', true ), $league_id, array() ); // Get labels from statistic variables $statistic_labels = (array)sportspress_get_var_labels( 'sp_statistic' ); @@ -1971,7 +1986,7 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) { endforeach; if ( $admin ): - return array( $columns, $placeholders, $seasons_teams ); + return array( $columns, $tempdata, $placeholders, $merged, $seasons_teams ); else: $labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ), 'team' => __( 'Team', 'sportspress' ) ), $columns ); $merged[0] = $labels; diff --git a/admin/hooks/save-post.php b/admin/hooks/save-post.php index a33f6ad5..91947ceb 100644 --- a/admin/hooks/save-post.php +++ b/admin/hooks/save-post.php @@ -10,6 +10,10 @@ function sportspress_save_post( $post_id ) { // Update leagues seasons to show update_post_meta( $post_id, 'sp_leagues_seasons', sportspress_array_value( $_POST, 'sp_leagues_seasons', array() ) ); + // Update player statistics array + if ( current_user_can( 'edit_sp_tables' ) ) + update_post_meta( $post_id, 'sp_columns', sportspress_array_value( $_POST, 'sp_columns', array() ) ); + break; case ( 'sp_event' ): @@ -88,7 +92,7 @@ function sportspress_save_post( $post_id ) { case ( 'sp_player' ): // Update seasons teams to show - update_post_meta( $post_id, 'sp_seasons_teams', sportspress_array_value( $_POST, 'sp_seasons_teams', array() ) ); + update_post_meta( $post_id, 'sp_leagues', sportspress_array_value( $_POST, 'sp_leagues', array() ) ); // Update team array sportspress_update_post_meta_recursive( $post_id, 'sp_team', sportspress_array_value( $_POST, 'sp_team', array() ) ); @@ -102,6 +106,10 @@ function sportspress_save_post( $post_id ) { // Update player metrics array update_post_meta( $post_id, 'sp_metrics', sportspress_array_value( $_POST, 'sp_metrics', array() ) ); + // Update player statistics array + if ( current_user_can( 'edit_sp_teams' ) ) + update_post_meta( $post_id, 'sp_statistics', sportspress_array_value( $_POST, 'sp_statistics', array() ) ); + break; case ( 'sp_staff' ): diff --git a/admin/hooks/the-content.php b/admin/hooks/the-content.php index 5153868a..f54d0c9f 100644 --- a/admin/hooks/the-content.php +++ b/admin/hooks/the-content.php @@ -37,7 +37,7 @@ function sportspress_the_content( $content ) { global $post; - sportspress_league_table( $post->ID ); + $table = sportspress_league_table( $post->ID ); $content = $table . $content; diff --git a/admin/post-types/player.php b/admin/post-types/player.php index 426805aa..157ebc98 100644 --- a/admin/post-types/player.php +++ b/admin/post-types/player.php @@ -109,9 +109,9 @@ function sportspress_player_stats_meta( $post ) { ID, $league->term_id, true ); + list( $columns, $data, $placeholders, $merged, $seasons_teams ) = sportspress_get_player_statistics_data( $post->ID, $league->term_id, true ); - sportspress_edit_player_statistics_table( $league->term_id, $columns, $data, $seasons_teams ); + sportspress_edit_player_statistics_table( $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, ! current_user_can( 'edit_sp_teams' ) ); endforeach; } diff --git a/admin/post-types/team.php b/admin/post-types/team.php index 05d452c0..89905423 100644 --- a/admin/post-types/team.php +++ b/admin/post-types/team.php @@ -20,15 +20,15 @@ function sportspress_team_post_init() { add_action( 'init', 'sportspress_team_post_init' ); function sportspress_team_meta_init( $post ) { - $leagues = (array)get_the_terms( $post->ID, 'sp_league' ); - $seasons = (array)get_the_terms( $post->ID, 'sp_season' ); + $leagues = get_the_terms( $post->ID, 'sp_league' ); + $seasons = get_the_terms( $post->ID, 'sp_season' ); remove_meta_box( 'submitdiv', 'sp_team', 'side' ); add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sp_team', 'side', 'high' ); remove_meta_box( 'postimagediv', 'sp_team', 'side' ); add_meta_box( 'postimagediv', __( 'Logo', 'sportspress' ), 'post_thumbnail_meta_box', 'sp_team', 'side', 'high' ); - if ( $leagues && $leagues != array(0) && $seasons && $seasons != array(0) ): + if ( $leagues && $seasons ): add_meta_box( 'sp_columnssdiv', __( 'Table Columns', 'sportspress' ), 'sportspress_team_columns_meta', 'sp_team', 'normal', 'high' ); endif; } @@ -61,9 +61,9 @@ function sportspress_team_columns_meta( $post ) { ID, $league_id, true ); + list( $columns, $data, $placeholders, $merged, $leagues_seasons ) = sportspress_get_team_columns_data( $post->ID, $league_id, true ); - sportspress_edit_team_columns_table( $league_id, $columns, $data, $leagues_seasons ); + sportspress_edit_team_columns_table( $league_id, $columns, $data, $placeholders, $merged, $leagues_seasons, ! current_user_can( 'edit_sp_tables' ) ); endforeach; diff --git a/functions.php b/functions.php index 424c3a4c..6bde7963 100644 --- a/functions.php +++ b/functions.php @@ -71,7 +71,7 @@ if ( !function_exists( 'sportspress_event_results' ) ) { else: $value = '—'; endif; - $table_rows .= ''; + $table_rows .= ''; endforeach; $table_rows .= ''; @@ -86,9 +86,9 @@ if ( !function_exists( 'sportspress_event_results' ) ) { $output .= '

' . __( 'Results', 'sportspress' ) . '

'; $output .= '
'; + ?>
  ' . $value . '' . $value . '
'; - $output .= ''; + $output .= ''; foreach( $result_labels as $key => $label ): - $output .= ''; + $output .= ''; endforeach; $output .= '' . '' . ''; $output .= $table_rows; @@ -122,11 +122,11 @@ if ( !function_exists( 'sportspress_event_players' ) ) { $output .= '
' . __( 'Team', 'sportspress' ) . '' . __( 'Team', 'sportspress' ) . '' . $label . '' . $label . '
' . '' . ''; - $output .= ''; - $output .= ''; + $output .= ''; + $output .= ''; foreach( $statistic_labels as $key => $label ): - $output .= ''; + $output .= ''; endforeach; $output .= '' . '' . ''; @@ -143,12 +143,12 @@ if ( !function_exists( 'sportspress_event_players' ) ) { $number = get_post_meta( $player_id, 'sp_number', true ); // Player number - $output .= ''; + $output .= ''; // Name as link $permalink = get_post_permalink( $player_id ); $name = get_the_title( $player_id ); - $output .= ''; + $output .= ''; foreach( $statistic_labels as $key => $label ): if ( $key == 'name' ) @@ -158,7 +158,7 @@ if ( !function_exists( 'sportspress_event_players' ) ) { else: $value = 0; endif; - $output .= ''; + $output .= ''; endforeach; $output .= ''; @@ -176,8 +176,8 @@ if ( !function_exists( 'sportspress_event_players' ) ) { $number = get_post_meta( $player_id, 'sp_number', true ); // Player number - $output .= ''; - $output .= ''; + $output .= ''; + $output .= ''; $row = $data[0]; @@ -185,7 +185,7 @@ if ( !function_exists( 'sportspress_event_players' ) ) { if ( $key == 'name' ): continue; endif; - $output .= ''; + $output .= ''; endforeach; $output .= ''; @@ -257,9 +257,9 @@ if ( !function_exists( 'sportspress_league_table' ) ) { // Remove the first row to leave us with the actual data unset( $data[0] ); - $output .= ''; + $output .= ''; foreach( $labels as $key => $label ): - $output .= ''; + $output .= ''; endforeach; $output .= '' . '' . ''; @@ -271,18 +271,18 @@ if ( !function_exists( 'sportspress_league_table' ) ) { $output .= ''; // Position as number - $output .= ''; + $output .= ''; // Thumbnail and name as link $permalink = get_post_permalink( $team_id ); $thumbnail = get_the_post_thumbnail( $team_id, 'thumbnail', array( 'class' => 'logo' ) ); $name = sportspress_array_value( $row, 'name', sportspress_array_value( $row, 'name', ' ' ) ); - $output .= ''; + $output .= ''; foreach( $labels as $key => $value ): if ( $key == 'name' ) continue; - $output .= ''; + $output .= ''; endforeach; $output .= ''; @@ -326,7 +326,7 @@ if ( !function_exists( 'sportspress_team_columns' ) ) { $output .= '
#' . __( 'Player', 'sportspress' ) . '#' . __( 'Player', 'sportspress' ) . '' . $label . '' . $label . '
' . $number . '' . $number . '' . '' . $name . '' . '' . $name . '' . $value . '' . $value . '
 ' . __( 'Total', 'sportspress' ) . ' ' . __( 'Total', 'sportspress' ) . '' . sportspress_array_value( $row, $key, '—' ) . '' . sportspress_array_value( $row, $key, '—' ) . '
##' . $label . '' . $label . '
' . $i . '' . $i . '' . ( $thumbnail ? $thumbnail . ' ' : '' ) . '' . $name . '' . ( $thumbnail ? $thumbnail . ' ' : '' ) . '' . $name . '' . sportspress_array_value( $row, $key, '—' ) . '' . sportspress_array_value( $row, $key, '—' ) . '
' . '' . ''; foreach( $labels as $key => $label ): - $output .= ''; + $output .= ''; endforeach; $output .= '' . '' . ''; @@ -338,7 +338,7 @@ if ( !function_exists( 'sportspress_team_columns' ) ) { $output .= ''; foreach( $labels as $key => $value ): - $output .= ''; + $output .= ''; endforeach; $output .= ''; @@ -370,9 +370,9 @@ if ( !function_exists( 'sportspress_player_list' ) ) { // Remove the first row to leave us with the actual data unset( $data[0] ); - $output .= ''; + $output .= ''; foreach( $labels as $key => $label ): - $output .= ''; + $output .= ''; endforeach; $output .= '' . '' . ''; @@ -385,17 +385,17 @@ if ( !function_exists( 'sportspress_player_list' ) ) { // Player number $number = get_post_meta( $player_id, 'sp_number', true ); - $output .= ''; + $output .= ''; // Name as link $permalink = get_post_permalink( $player_id ); $name = sportspress_array_value( $row, 'name', sportspress_array_value( $row, 'name', ' ' ) ); - $output .= ''; + $output .= ''; foreach( $labels as $key => $value ): if ( $key == 'name' ) continue; - $output .= ''; + $output .= ''; endforeach; $output .= ''; @@ -473,7 +473,7 @@ if ( !function_exists( 'sportspress_player_statistics' ) ) { $output .= '
' . $label . '' . $label . '
' . sportspress_array_value( $row, $key, '—' ) . '' . sportspress_array_value( $row, $key, '—' ) . '
##' . $label . '' . $label . '
' . ( $number ? $number : ' ' ) . '' . ( $number ? $number : ' ' ) . '' . '' . $name . '' . '' . $name . '' . sportspress_array_value( $row, $key, '—' ) . '' . sportspress_array_value( $row, $key, '—' ) . '
' . '' . ''; foreach( $labels as $key => $label ): - $output .= ''; + $output .= ''; endforeach; $output .= '' . '' . ''; @@ -485,7 +485,7 @@ if ( !function_exists( 'sportspress_player_statistics' ) ) { $output .= ''; foreach( $labels as $key => $value ): - $output .= ''; + $output .= ''; endforeach; $output .= '';
' . $label . '' . $label . '
' . sportspress_array_value( $row, $key, '—' ) . '' . sportspress_array_value( $row, $key, '—' ) . '