diff --git a/admin/hooks/manage-posts-columns.php b/admin/hooks/manage-posts-columns.php index aec3038f..73e99158 100644 --- a/admin/hooks/manage-posts-columns.php +++ b/admin/hooks/manage-posts-columns.php @@ -75,6 +75,9 @@ function sportspress_manage_posts_custom_column( $column, $post_id ) { case 'sp_precision': echo sportspress_get_post_precision( $post_id ); break; + case 'sp_calculate': + echo sportspress_get_post_calculate( $post_id ); + break; case 'sp_player': echo sportspress_the_posts( $post_id, 'sp_player' ); break; diff --git a/admin/hooks/sanitize-title.php b/admin/hooks/sanitize-title.php index 2d779592..de2f641e 100644 --- a/admin/hooks/sanitize-title.php +++ b/admin/hooks/sanitize-title.php @@ -11,7 +11,9 @@ function sportspress_sanitize_title( $title ) { if ( ! $key ) $key = $_POST['post_title']; - $title = sportspress_get_eos_safe_slug( $key, sportspress_array_value( $_POST, 'ID', 'var' ) ); + $id = sportspress_array_value( $_POST, 'post_ID', 'var' ); + + $title = sportspress_get_eos_safe_slug( $key, $id ); elseif ( isset( $_POST ) && array_key_exists( 'post_type', $_POST ) && $_POST['post_type'] == 'sp_event' ): diff --git a/admin/hooks/save-post.php b/admin/hooks/save-post.php index 3aa859c7..24e46eca 100644 --- a/admin/hooks/save-post.php +++ b/admin/hooks/save-post.php @@ -47,7 +47,24 @@ function sportspress_save_post( $post_id ) { break; + case ( 'sp_result' ): + + // Delete posts with duplicate key + sportspress_delete_duplicate_post( $_POST ); + + break; + + case ( 'sp_outcome' ): + + // Delete posts with duplicate key + sportspress_delete_duplicate_post( $_POST ); + + break; + case ( 'sp_column' ): + + // Delete posts with duplicate key + sportspress_delete_duplicate_post( $_POST ); // Update equation as string update_post_meta( $post_id, 'sp_equation', implode( ' ', sportspress_array_value( $_POST, 'sp_equation', array() ) ) ); @@ -65,8 +82,11 @@ function sportspress_save_post( $post_id ) { case ( 'sp_statistic' ): - // Update equation as string - update_post_meta( $post_id, 'sp_equation', implode( ' ', sportspress_array_value( $_POST, 'sp_equation', array() ) ) ); + // Delete posts with duplicate key + sportspress_delete_duplicate_post( $_POST ); + + // Update calculation method as string + update_post_meta( $post_id, 'sp_calculate', sportspress_array_value( $_POST, 'sp_calculate', 'DESC' ) ); break; diff --git a/admin/post-types/column.php b/admin/post-types/column.php index 5fb87da0..58ff84c2 100644 --- a/admin/post-types/column.php +++ b/admin/post-types/column.php @@ -54,7 +54,7 @@ function sportspress_column_details_meta( $post ) {

ID, $piece, array( 'team_event', 'result', 'outcome' ) ); + sportspress_equation_selector( $post->ID, $piece, array( 'team_event', 'result', 'outcome' ) ); endforeach; ?>

diff --git a/admin/post-types/statistic.php b/admin/post-types/statistic.php index 3c1d0b25..a45f951a 100644 --- a/admin/post-types/statistic.php +++ b/admin/post-types/statistic.php @@ -25,7 +25,7 @@ function sportspress_statistic_edit_columns() { 'cb' => '', 'title' => __( 'Label', 'sportspress' ), 'sp_positions' => __( 'Positions', 'sportspress' ), - 'sp_equation' => __( 'Equation', 'sportspress' ), + 'sp_calculate' => __( 'Calculate', 'sportspress' ), ); return $columns; } @@ -36,15 +36,11 @@ function sportspress_statistic_meta_init() { } function sportspress_statistic_equation_meta( $post ) { - $equation = explode( ' ', get_post_meta( $post->ID, 'sp_equation', true ) ); + $calculate = get_post_meta( $post->ID, 'sp_calculate', true ); ?> -

-

- ID, $piece, array( 'player_event' ) ); - endforeach; - ?> +

+

+ ID, $calculate ); ?>

- + > post_title; ?> ID, 'sp_position' ) ? the_terms( $row->ID, 'sp_position' ) : sprintf( __( 'All %s', 'sportspress' ), __( 'positions', 'sportspress' ) ); ?> - ID ); ?> + ID ); ?> diff --git a/functions.php b/functions.php index 169a3f24..8c0b8812 100644 --- a/functions.php +++ b/functions.php @@ -95,8 +95,8 @@ if ( !function_exists( 'sportspress_get_term_labels' ) ) { if ( !function_exists( 'sportspress_get_the_term_id' ) ) { function sportspress_get_the_term_id( $post_id, $taxonomy, $index ) { $terms = get_the_terms( $post_id, $taxonomy ); - if ( is_array( $terms ) && array_key_exists( $index, $terms ) ): - $term = $terms[0]; + if ( is_array( $terms ) && sizeof( $terms ) > 0 ): + $term = reset( $terms ); if ( is_object( $term ) && property_exists( $term, 'term_id' ) ) return $term->term_id; else @@ -142,6 +142,21 @@ if ( !function_exists( 'sportspress_get_post_precision' ) ) { } } +if ( !function_exists( 'sportspress_get_post_calculate' ) ) { + function sportspress_get_post_calculate( $post_id ) { + $calculate = get_post_meta ( $post_id, 'sp_calculate', true ); + if ( $calculate ): + return str_replace( + array( 'sum', 'average' ), + array( __( 'Sum', 'sportspress' ), __( 'Average', 'sportspress' ) ), + $calculate + ); + else: + return __( 'Sum', 'sportspress' ); + endif; + } +} + if ( !function_exists( 'sportspress_get_post_equation' ) ) { function sportspress_get_post_equation( $post_id ) { $equation = get_post_meta ( $post_id, 'sp_equation', true ); @@ -310,8 +325,15 @@ if ( !function_exists( 'sportspress_post_checklist' ) ) { $meta, 'number' => 0 ) ); - if ( empty( $posts ) ) - $posts = get_posts( array( 'post_type' => $meta, 'numberposts' => -1, 'post_per_page' => -1 ) ); + if ( empty( $posts ) ): + $query = array( 'post_type' => $meta, 'numberposts' => -1, 'post_per_page' => -1 ); + if ( $meta == 'sp_player' ): + $query['meta_key'] = 'sp_number'; + $query['orderby'] = 'meta_value_num'; + $query['order'] = 'ASC'; + endif; + $posts = get_posts( $query ); + endif; foreach ( $posts as $post ): $parents = get_post_ancestors( $post ); if ( $filter ): @@ -352,6 +374,22 @@ if ( !function_exists( 'sportspress_post_checklist' ) ) { } } +if ( !function_exists( 'sportspress_calculate_selector' ) ) { + function sportspress_calculate_selector( $postid, $selected = null ) { + $options = array( + 'sum' => __( 'Sum', 'sportspress' ), + 'average' => __( 'Average', 'sportspress' ), + ); + ?> + + $post_type, + 'numberposts' => -1, + 'posts_per_page' => -1, + 'orderby' => 'menu_order', + 'order' => 'ASC' + ); + + $vars = get_posts( $args ); + + $output = array(); + foreach ( $vars as $var ): + $calculate = get_post_meta( $var->ID, 'sp_calculate', true ); + $output[ $var->post_name ] = $calculate; + endforeach; + + return $output; + } +} + if ( !function_exists( 'sportspress_edit_calendar_table' ) ) { function sportspress_edit_calendar_table( $data = array() ) { if ( empty( $data ) ): @@ -599,6 +659,7 @@ if ( !function_exists( 'sportspress_edit_player_list_table' ) ) { + @@ -611,8 +672,10 @@ if ( !function_exists( 'sportspress_edit_player_list_table' ) ) { foreach ( $data as $player_id => $player_stats ): if ( !$player_id ) continue; $div = get_term( $player_id, 'sp_season' ); + $number = get_post_meta( $player_id, 'sp_number', true ); ?> + @@ -739,7 +802,7 @@ if ( !function_exists( 'sportspress_edit_player_statistics_table' ) ) { $label ): ?> @@ -1616,7 +1679,7 @@ if ( !function_exists( 'sportspress_get_league_table_data' ) ) { foreach( $merged as $key => $value ): $data[ $key ] = $tempdata[ $key ]; endforeach; - + if ( $breakdown ): return array( $columns, $data, $placeholders, $merged ); else: @@ -1641,9 +1704,6 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) { // Get all leagues populated with stats where available $tempdata = sportspress_array_combine( $player_ids, $stats ); - // Get equations from statistics variables - $equations = sportspress_get_var_equations( 'sp_statistic' ); - // Create entry for each player in totals $totals = array(); $placeholders = array(); @@ -1662,11 +1722,11 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) { $static = get_post_meta( $player_id, 'sp_statistics', true ); // Create placeholders entry for the player - $placeholders[ $player_id ] = array(); + $placeholders[ $player_id ] = array( 'eventsplayed' => 0 ); // Add static statistics to placeholders if ( array_key_exists( $league_id, $static ) && array_key_exists( $div_id, $static[ $league_id ] ) ): - $placeholders[ $player_id ] = $static[ $league_id ][ $div_id ]; + $placeholders[ $player_id ] = array_merge( $placeholders[ $player_id ], $static[ $league_id ][ $div_id ] ); endif; endforeach; @@ -1735,8 +1795,7 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) { ); $statistics = get_posts( $args ); - $columns = array(); - $priorities = array(); + $columns = array( 'eventsplayed' => __( 'Played', 'sportspress' ) ); foreach ( $statistics as $statistic ): @@ -1749,31 +1808,44 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) { // Add column name to columns $columns[ $statistic->post_name ] = $statistic->post_title; - // Add order to priorities if priority is set and does not exist in array already - $priority = sportspress_array_value( sportspress_array_value( $meta, 'sp_priority', array() ), 0, 0 ); - if ( $priority && ! array_key_exists( $priority, $priorities ) ): - $priorities[ $priority ] = array( - 'column' => $statistic->post_name, - 'order' => sportspress_array_value( sportspress_array_value( $meta, 'sp_order', array() ), 0, 'DESC' ) - ); - endif; - endforeach; - // Sort priorities in descending order - ksort( $priorities ); - // Fill in empty placeholder values for each player foreach ( $player_ids as $player_id ): if ( ! $player_id ) continue; + // Add events played as an object to statistics for placeholder calculations + $epstat = new stdClass(); + $epstat->post_name = 'eventsplayed'; + array_unshift( $statistics, $epstat ); + foreach ( $statistics as $statistic ): if ( sportspress_array_value( $placeholders[ $player_id ], $statistic->post_name, '' ) == '' ): - // Reflect totals - $placeholders[ $player_id ][ $statistic->post_name ] = sportspress_array_value( sportspress_array_value( $totals, $player_id, array() ), $statistic->post_name, 0 ); + if ( $statistic->post_name == 'eventsplayed' ): + $calculate = 'sum'; + else: + $calculate = get_post_meta( $statistic->ID, 'sp_calculate', true ); + endif; + + if ( $calculate && $calculate == 'average' ): + + // Reflect average + $eventsplayed = (int)sportspress_array_value( $totals[ $player_id ], 'eventsplayed', 0 ); + if ( ! $eventsplayed ): + $placeholders[ $player_id ][ $statistic->post_name ] = 0; + else: + $placeholders[ $player_id ][ $statistic->post_name ] = sportspress_array_value( sportspress_array_value( $totals, $player_id, array() ), $statistic->post_name, 0 ) / $eventsplayed; + endif; + + else: + + // Reflect total + $placeholders[ $player_id ][ $statistic->post_name ] = sportspress_array_value( sportspress_array_value( $totals, $player_id, array() ), $statistic->post_name, 0 ); + + endif; endif; endforeach; @@ -1785,7 +1857,7 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) { foreach( $placeholders as $player_id => $player_data ): // Add player name to row - $merged[ $player_id ] = array( 'name' => get_the_title( $player_id ) ); + $merged[ $player_id ] = array( 'number' => get_post_meta( $player_id, 'sp_number', true ), 'name' => get_the_title( $player_id ), 'eventsplayed' => 0 ); foreach( $player_data as $key => $value ): @@ -1799,30 +1871,6 @@ if ( !function_exists( 'sportspress_get_player_list_data' ) ) { endforeach; endforeach; - uasort( $merged, function( $a, $b ) use ( $priorities ) { - - // Loop through priorities - foreach( $priorities as $priority ): - - // Proceed if columns are not equal - if ( sportspress_array_value( $a, $priority['column'], 0 ) != sportspress_array_value( $b, $priority['column'], 0 ) ): - - // Compare column values - $output = sportspress_array_value( $a, $priority['column'], 0 ) - sportspress_array_value( $b, $priority['column'], 0 ); - - // Flip value if descending order - if ( $priority['order'] == 'DESC' ) $output = 0 - $output; - - return $output; - - endif; - - endforeach; - - // Default sort by alphabetical - return strcmp( sportspress_array_value( $a, 'name', '' ), sportspress_array_value( $b, 'name', '' ) ); - }); - // Rearrange data array to reflect statistics $data = array(); foreach( $merged as $key => $value ): @@ -1886,7 +1934,9 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) { $tempdata = sportspress_array_combine( $div_ids, sportspress_array_value( $stats, $league_id, array() ) ); // Get equations from statistics variables - $equations = sportspress_get_var_equations( 'sp_statistic' ); + $equations = sportspress_get_var_calculates( 'sp_statistic' ); + + $equations['eventsplayed'] = 'sum'; foreach ( $div_ids as $div_id ): @@ -1950,19 +2000,20 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) { $placeholders[ $div_id ] = array(); foreach ( $equations as $key => $value ): - if ( empty( $value ) ): + if ( $value == 'average' ): - // Reflect totals - $placeholders[ $div_id ][ $key ] = sportspress_array_value( $totals, $key, 0 ); + // Reflect average + $eventsplayed = (int)sportspress_array_value( $totals, 'eventsplayed', 0 ); + if ( ! $eventsplayed ): + $placeholders[ $div_id ][ $key ] = 0; + else: + $placeholders[ $div_id ][ $key ] = sportspress_array_value( $totals, $key, 0 ) / $eventsplayed; + endif; else: - // Calculate value - if ( sizeof( $events ) > 0 ): - $placeholders[ $div_id ][ $key ] = sportspress_solve( $value, $totals ); - else: - $placeholders[ $div_id ][ $key ] = 0; - endif; + // Reflect total + $placeholders[ $div_id ][ $key ] = sportspress_array_value( $totals, $key, 0 ); endif; @@ -1971,7 +2022,10 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) { endforeach; // Get columns from statistics variables - $columns = sportspress_get_var_labels( 'sp_statistic' ); + $columns = array_merge( + array( 'eventsplayed' => __( 'Played', 'sportspress' ) ), + sportspress_get_var_labels( 'sp_statistic' ) + ); // Merge the data and placeholders arrays $merged = array(); @@ -2010,7 +2064,7 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) { if ( $admin ): return array( $columns, $tempdata, $placeholders, $merged, $seasons_teams ); else: - $labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ), 'team' => __( 'Team', 'sportspress' ) ), $columns ); + $labels = array_merge( array( 'name' => __( 'Season', 'sportspress' ), 'team' => __( 'Team', 'sportspress' ), 'eventsplayed' => __( 'Played', 'sportspress' ) ), $columns ); $merged[0] = $labels; return $merged; endif; @@ -2018,6 +2072,27 @@ if ( !function_exists( 'sportspress_get_player_statistics_data' ) ) { } } +if ( !function_exists( 'sportspress_delete_duplicate_post' ) ) { + function sportspress_delete_duplicate_post( &$post ) { + global $wpdb; + + $key = isset( $post['sp_key'] ) ? $post['sp_key'] : null; + if ( ! $key ) $key = $post['post_title']; + $id = sportspress_array_value( $post, 'post_ID', 'var' ); + $title = sportspress_get_eos_safe_slug( $key, $id ); + + $check_sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; + $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $title, $post['post_type'], $id ) ); + + if ( $post_name_check ): + wp_delete_post( $post_name_check, true ); + $post['post_status'] = 'draft'; + endif; + + return $post_name_check; + } +} + if ( !function_exists( 'sportspress_highlight_admin_menu' ) ) { function sportspress_highlight_admin_menu() { global $parent_file, $submenu_file;
#
'; ?>