From 284b0f1a5f65590490a7b56e3af4a953d32088db Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Sat, 26 Apr 2014 17:52:43 +1000 Subject: [PATCH] Move post type specific functions into classes --- includes/admin/class-sp-admin-menus.php | 2 +- includes/admin/class-sp-admin-post-types.php | 2 +- .../class-sp-meta-box-calendar-data.php | 134 +- .../class-sp-meta-box-column-details.php | 11 +- .../meta-boxes/class-sp-meta-box-config.php | 156 ++ .../class-sp-meta-box-event-performance.php | 115 +- .../class-sp-meta-box-event-results.php | 66 +- .../class-sp-meta-box-metric-details.php | 12 +- .../class-sp-meta-box-outcome-details.php | 12 +- .../class-sp-meta-box-performance-details.php | 13 +- .../class-sp-meta-box-player-performance.php | 89 +- .../class-sp-meta-box-player-statistics.php | 83 +- .../class-sp-meta-box-result-details.php | 14 +- .../class-sp-meta-box-statistic-details.php | 10 +- .../class-sp-meta-box-table-data.php | 2 +- .../class-sp-meta-box-team-columns.php | 56 +- includes/class-sp-calendar.php | 139 ++ includes/class-sp-data-formatter.php | 883 -------- includes/class-sp-event.php | 303 +++ ...sp-table.php => class-sp-league-table.php} | 14 +- includes/class-sp-player-list.php | 397 ++++ includes/class-sp-player.php | 324 +++ includes/class-sp-team.php | 278 +++ includes/sp-conditional-functions.php | 18 + includes/sp-core-functions.php | 1972 ----------------- includes/sp-deprecated-functions.php | 27 +- includes/sp-template-hooks.php | 6 +- templates/event-calendar.php | 3 +- templates/event-list.php | 4 +- templates/league-table.php | 2 +- templates/player-details.php | 10 +- templates/player-gallery.php | 13 +- templates/player-list.php | 13 +- templates/player-roster.php | 107 - templates/player-statistics.php | 3 +- templates/team-columns.php | 3 +- 36 files changed, 2234 insertions(+), 3062 deletions(-) create mode 100644 includes/admin/post-types/meta-boxes/class-sp-meta-box-config.php create mode 100644 includes/class-sp-calendar.php delete mode 100644 includes/class-sp-data-formatter.php create mode 100644 includes/class-sp-event.php rename includes/{class-sp-table.php => class-sp-league-table.php} (97%) create mode 100644 includes/class-sp-player-list.php create mode 100644 includes/class-sp-player.php create mode 100644 includes/class-sp-team.php delete mode 100644 templates/player-roster.php diff --git a/includes/admin/class-sp-admin-menus.php b/includes/admin/class-sp-admin-menus.php index c3509fc3..41e5b93d 100644 --- a/includes/admin/class-sp-admin-menus.php +++ b/includes/admin/class-sp-admin-menus.php @@ -68,7 +68,7 @@ class SP_Admin_Menus { */ public function menu_highlight() { global $typenow, $submenu; - if ( sp_is_config_type( $typenow ) ) + if ( is_sp_config_type( $typenow ) ) $this->highlight_admin_menu(); elseif ( $typenow == 'sp_calendar' ) $this->highlight_admin_menu( 'edit.php?post_type=sp_event', 'edit.php?post_type=sp_calendar' ); diff --git a/includes/admin/class-sp-admin-post-types.php b/includes/admin/class-sp-admin-post-types.php index d977e1f4..0ea92baa 100644 --- a/includes/admin/class-sp-admin-post-types.php +++ b/includes/admin/class-sp-admin-post-types.php @@ -54,7 +54,7 @@ class SP_Admin_Post_Types { public function post_updated_messages( $messages ) { global $typenow, $post; - if ( sp_is_config_type( $typenow ) ): + if ( is_sp_config_type( $typenow ) ): $obj = get_post_type_object( $typenow ); for ( $i = 0; $i <= 10; $i++ ): diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-data.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-data.php index 300bfa01..0f807470 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-data.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-calendar-data.php @@ -19,8 +19,10 @@ class SP_Meta_Box_Calendar_Data { * Output the metabox */ public static function output( $post ) { - list( $data, $usecolumns ) = sp_get_calendar_data( $post->ID, true ); - sp_edit_calendar_table( $data, $usecolumns ); + $calendar = new SP_Calendar( $post ); + $data = $calendar->data(); + $usecolumns = $calendar->columns; + self::table( $data, $usecolumns ); } /** @@ -29,4 +31,132 @@ class SP_Meta_Box_Calendar_Data { public static function save( $post_id, $post ) { update_post_meta( $post_id, 'sp_columns', sp_array_value( $_POST, 'sp_columns', array() ) ); } + + /** + * Admin edit table + */ + public static function table( $data = array(), $usecolumns = null ) { + if ( is_array( $usecolumns ) ) + $usecolumns = array_filter( $usecolumns ); + ?> +
+ + + + + + + + + + + + + 0 ): + $main_result = get_option( 'sportspress_primary_result', null ); + $i = 0; + foreach ( $data as $event ): + $teams = get_post_meta( $event->ID, 'sp_team' ); + $results = get_post_meta( $event->ID, 'sp_results', true ); + $video = get_post_meta( $event->ID, 'sp_video', true ); + ?> + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
post_title; ?> + ID ) . '">' . $team_result . ' '; + endif; + + echo $name . '
'; + endif; + endforeach; + ?> +
ID, 'sp_venue' ); ?> + + +
+ ID ) ): ?> +
+ + post_content == null ): + _e( 'None', 'sportspress' ); + elseif ( $event->post_status == 'publish' ): + _e( 'Recap', 'sportspress' ); + else: + _e( 'Preview', 'sportspress' ); + endif; + ?> +
+
+ +
+
+ ID, $piece, array( 'team_event', 'outcome', 'result' ) ); + self::select( $post->ID, $piece, array( 'team_event', 'outcome', 'result' ) ); endforeach; ?>

@@ -75,7 +78,7 @@ class SP_Meta_Box_Column_Details { * Save meta box data */ public static function save( $post_id, $post ) { - sp_delete_duplicate_post( $_POST ); + self::delete_duplicate( $_POST ); update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) ); update_post_meta( $post_id, 'sp_precision', (int) sp_array_value( $_POST, 'sp_precision', 1 ) ); update_post_meta( $post_id, 'sp_priority', sp_array_value( $_POST, 'sp_priority', '0' ) ); diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-config.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-config.php new file mode 100644 index 00000000..b4d29415 --- /dev/null +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-config.php @@ -0,0 +1,156 @@ + __( 'Attended', 'sportspress' ), '$eventsplayed' => __( 'Played', 'sportspress' ) ); + break; + case 'team_event': + $options[ __( 'Events', 'sportspress' ) ] = array( '$eventsplayed' => __( 'Played', 'sportspress' ) ); + break; + case 'result': + $options[ __( 'Results', 'sportspress' ) ] = self::optgroup( $postid, 'sp_result', array( 'for' => '→', 'against' => '←' ), null, false ); + break; + case 'outcome': + $options[ __( 'Outcomes', 'sportspress' ) ] = self::optgroup( $postid, 'sp_outcome', array() ); + $options[ __( 'Outcomes', 'sportspress' ) ]['$streak'] = __( 'Streak', 'sportspress' ); + $options[ __( 'Outcomes', 'sportspress' ) ]['$last5'] = __( 'Last 5', 'sportspress' ); + $options[ __( 'Outcomes', 'sportspress' ) ]['$last10'] = __( 'Last 10', 'sportspress' ); + break; + case 'performance': + $options[ __( 'Performance', 'sportspress' ) ] = self::optgroup( $postid, 'sp_performance' ); + break; + case 'metric': + $options[ __( 'Metric', 'sportspress' ) ] = self::optgroup( $postid, 'sp_metric' ); + break; + endswitch; + endforeach; + + // Create array of operators + $operators = array( '+' => '+', '-' => '−', '*' => '×', '/' => '÷', '(' => '(', ')' => ')' ); + + // Add operators to options + $options[ __( 'Operators', 'sportspress' ) ] = $operators; + + // Create array of constants + $max = 10; + $constants = array(); + for ( $i = 1; $i <= $max; $i ++ ): + $constants[$i] = $i; + endfor; + + // Add 100 to constants + $constants[100] = 100; + + // Add constants to options + $options[ __( 'Constants', 'sportspress' ) ] = (array) $constants; + + ?> + + $type, + 'numberposts' => -1, + 'posts_per_page' => -1, + 'orderby' => 'menu_order', + 'order' => 'ASC', + 'exclude' => $postid + ); + $vars = get_posts( $args ); + + // Add extra vars to the array + if ( isset( $defaults ) && is_array( $defaults ) ): + foreach ( $defaults as $key => $value ): + $arr[ $key ] = $value; + endforeach; + endif; + + // Add vars to the array + if ( isset( $variations ) && is_array( $variations ) ): + foreach ( $vars as $var ): + if ( $totals ) $arr[ '$' . $var->post_name ] = $var->post_title; + foreach ( $variations as $key => $value ): + $arr[ '$' . $var->post_name . $key ] = $var->post_title . ' ' . $value; + endforeach; + endforeach; + else: + foreach ( $vars as $var ): + $arr[ '$' . $var->post_name ] = $var->post_title; + endforeach; + endif; + + return (array) $arr; + } + + public static function delete_duplicate( &$post ) { + global $wpdb; + + $key = isset( $post['sp_key'] ) ? $post['sp_key'] : null; + if ( ! $key ) $key = $post['post_title']; + $id = sp_array_value( $post, 'post_ID', 'var' ); + $title = sp_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; + } + +} \ No newline at end of file diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-performance.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-performance.php index b2549949..83bc1819 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-performance.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-performance.php @@ -5,7 +5,7 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Meta Boxes - * @version 0.7 + * @version 0.8 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -35,7 +35,7 @@ class SP_Meta_Box_Event_Performance { ?>

- +
+
+ + + + + + + + + + + + + $player_performance ): + if ( !$player_id ) continue; + $number = get_post_meta( $player_id, 'sp_number', true ); + ?> + + + + $label ): + $value = sp_array_value( $player_performance, $column, '' ); + ?> + + + + + + + + + $label ): + $player_id = 0; + $player_performance = sp_array_value( $data, 0, array() ); + $value = sp_array_value( $player_performance, $column, '' ); + ?> + + + + + +
#
+ + + + +
  
+
+ __( 'Starting Lineup', 'sportspress' ), + 'sub' => __( 'Substitute', 'sportspress' ), + ); + + $output = ''; + + return $output; + + } + + /** + * Substitute selector + */ + public static function sub_select( $team_id, $player_id, $value, $data = array() ) { + + if ( ! $team_id || ! $player_id ) + return '—'; + + $output = ''; + + return $output; + + } } \ No newline at end of file diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php index c38e7b21..8c1201c4 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-event-results.php @@ -5,7 +5,7 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Meta Boxes - * @version 0.7 + * @version 0.8 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -31,7 +31,7 @@ class SP_Meta_Box_Event_Results { ?>
- +
+
+ + + + + $label ): ?> + + + + + + + $team_results ): + if ( !$team_id ) continue; + ?> + + + $label ): + $value = sp_array_value( $team_results, $column, '' ); + ?> + + + + + + +
+ + + 'sp_outcome', + 'name' => 'sp_results[' . $team_id . '][outcome][]', + 'option_none_value' => '', + 'sort_order' => 'ASC', + 'sort_column' => 'menu_order', + 'selected' => $values, + 'class' => 'sp-outcome', + 'property' => 'multiple', + 'chosen' => true, + ); + sp_dropdown_pages( $args ); + ?> +
+
+ ID, 'sp_calculate', true ); ?>

@@ -29,11 +31,4 @@ class SP_Meta_Box_Performance_Details {

ID, $league->term_id, true ); + $player = new SP_Player( $post ); - sp_edit_player_performance_table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, ! current_user_can( 'edit_sp_teams' ) ); + list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( $league->term_id, true ); + + self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, ! current_user_can( 'edit_sp_teams' ) ); endforeach; else: @@ -51,4 +53,85 @@ class SP_Meta_Box_Player_Performance { if ( current_user_can( 'edit_sp_teams' ) ) update_post_meta( $post_id, 'sp_performance', sp_array_value( $_POST, 'sp_performance', array() ) ); } + + /** + * Admin edit table + */ + public static function table( $id = null, $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $leagues = array(), $readonly = true ) { + if ( ! $id ) + $id = get_the_ID(); + + $teams = array_filter( get_post_meta( $id, 'sp_team', false ) ); + ?> +
+ + + + + + + + + + + + $div_stats ): + if ( !$div_id || $div_id == 'performance' ) continue; + $div = get_term( $div_id, 'sp_season' ); + ?> + + + + $label ): + ?> + + + + + +
+ name; ?> + + 'sp_team', + 'name' => 'sp_leagues[' . $league_id . '][' . $div_id . ']', + 'show_option_none' => __( '— None —', 'sportspress' ), + 'sort_order' => 'ASC', + 'sort_column' => 'menu_order', + 'selected' => $value, + 'values' => 'ID', + 'include' => $teams, + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'sp_league', + 'terms' => $league_id, + 'field' => 'id', + ), + array( + 'taxonomy' => 'sp_season', + 'terms' => $div_id, + 'field' => 'id', + ), + ), + ); + if ( ! sp_dropdown_pages( $args ) ): + _e( 'No results found.', 'sportspress' ); + endif; + ?> + '; + ?>
+
+ ID, $league->term_id, true ); + $player = new SP_Player( $post ); + list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( $league->term_id, true ); - sp_edit_player_statistics_table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, ! current_user_can( 'edit_sp_teams' ) ); + self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, ! current_user_can( 'edit_sp_teams' ) ); endforeach; else: @@ -51,4 +52,82 @@ class SP_Meta_Box_Player_Statistics { if ( current_user_can( 'edit_sp_teams' ) ) update_post_meta( $post_id, 'sp_statistics', sp_array_value( $_POST, 'sp_statistics', array() ) ); } + + /** + * Admin edit table + */ + public static function table( $id = null, $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $leagues = array(), $readonly = true ) { + $teams = array_filter( get_post_meta( $id, 'sp_team', false ) ); + ?> +
+ + + + + + + + + + + + $div_stats ): + if ( !$div_id || $div_id == 'statistics' ) continue; + $div = get_term( $div_id, 'sp_season' ); + ?> + + + + $label ): + ?> + + + + + +
+ name; ?> + + 'sp_team', + 'name' => 'sp_leagues[' . $league_id . '][' . $div_id . ']', + 'show_option_none' => __( '— None —', 'sportspress' ), + 'sort_order' => 'ASC', + 'sort_column' => 'menu_order', + 'selected' => $value, + 'values' => 'ID', + 'include' => $teams, + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'sp_league', + 'terms' => $league_id, + 'field' => 'id', + ), + array( + 'taxonomy' => 'sp_season', + 'terms' => $div_id, + 'field' => 'id', + ), + ), + ); + if ( ! sp_dropdown_pages( $args ) ): + _e( 'No results found.', 'sportspress' ); + endif; + ?> + '; + ?>
+
+ ID, $piece, array( 'player_event', 'outcome', 'performance', 'metric' ) ); + self::select( $post->ID, $piece, array( 'player_event', 'outcome', 'performance', 'metric' ) ); endforeach; ?>

@@ -50,8 +53,9 @@ class SP_Meta_Box_Statistic_Details { * Save meta box data */ public static function save( $post_id, $post ) { - sp_delete_duplicate_post( $_POST ); + self::delete_duplicate( $_POST ); update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) ); update_post_meta( $post_id, 'sp_precision', (int) sp_array_value( $_POST, 'sp_precision', 1 ) ); } + } \ No newline at end of file diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-data.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-data.php index 17b1350d..218bc85d 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-data.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-table-data.php @@ -19,7 +19,7 @@ class SP_Meta_Box_Table_Data { * Output the metabox */ public static function output( $post ) { - $table = new SP_Table( $post ); + $table = new SP_League_Table( $post ); list( $columns, $usecolumns, $data, $placeholders, $merged ) = $table->data( true ); $adjustments = $table->adjustments; self::table( $columns, $usecolumns, $data, $placeholders, $adjustments ); diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-team-columns.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-team-columns.php index ecfef55c..117bd50e 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-team-columns.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-team-columns.php @@ -34,9 +34,11 @@ class SP_Meta_Box_Team_Columns { ID, $league_id, true ); + $team = new SP_Team( $post ); - sp_edit_team_columns_table( $league_id, $columns, $data, $placeholders, $merged, $leagues_seasons, ! current_user_can( 'edit_sp_tables' ) ); + list( $columns, $data, $placeholders, $merged, $leagues_seasons ) = $team->data( $league_id, true ); + + self::table( $league_id, $columns, $data, $placeholders, $merged, $leagues_seasons, ! current_user_can( 'edit_sp_tables' ) ); endforeach; else: @@ -53,4 +55,54 @@ class SP_Meta_Box_Team_Columns { if ( current_user_can( 'edit_sp_tables' ) ) update_post_meta( $post_id, 'sp_columns', sp_array_value( $_POST, 'sp_columns', array() ) ); } + + /** + * Admin edit table + */ + public static function table( $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $seasons = array(), $readonly = true ) { + ?> +
+ + + + + + + + + + + + $div_stats ): + if ( !$div_id ) continue; + $div = get_term( $div_id, 'sp_season' ); + ?> + + + + $label ): + $value = sp_array_value( sp_array_value( $data, $div_id, array() ), $column, 0 ); + ?> + + + + + +
+ > + + + '; + ?>
+
+ ID = absint( $post->ID ); + $this->post = $post; + else: + $this->ID = absint( $post ); + $this->post = get_post( $this->ID ); + endif; + } + + /** + * __get function. + * + * @access public + * @param mixed $key + * @return bool + */ + public function __get( $key ) { + if ( ! isset( $key ) ): + return $this->post; + else: + $value = get_post_meta( $this->ID, 'sp_' . $key, true ); + endif; + + return $value; + } + + /** + * Returns formatted data + * + * @access public + * @return array + */ + public function data() { + global $pagenow; + + $args = array( + 'post_type' => 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'orderby' => 'post_date', + 'order' => 'ASC', + 'post_status' => 'any', + 'tax_query' => array( + 'relation' => 'AND' + ), + ); + + if ( $pagenow != 'post-new.php' ): + if ( $this->ID ): + $leagues = get_the_terms( $this->ID, 'sp_league' ); + $seasons = get_the_terms( $this->ID, 'sp_season' ); + $venues = get_the_terms( $this->ID, 'sp_venue' ); + $team = get_post_meta( $this->ID, 'sp_team', true ); + + if ( $leagues ): + $league_ids = array(); + foreach( $leagues as $league ): + $league_ids[] = $league->term_id; + endforeach; + $args['tax_query'][] = array( + 'taxonomy' => 'sp_league', + 'field' => 'id', + 'terms' => $league_ids + ); + endif; + + if ( $seasons ): + $season_ids = array(); + foreach( $seasons as $season ): + $season_ids[] = $season->term_id; + endforeach; + $args['tax_query'][] = array( + 'taxonomy' => 'sp_season', + 'field' => 'id', + 'terms' => $season_ids + ); + endif; + + if ( $venues ): + $venue_ids = array(); + foreach( $venues as $venue ): + $venue_ids[] = $venue->term_id; + endforeach; + $args['tax_query'][] = array( + 'taxonomy' => 'sp_venue', + 'field' => 'id', + 'terms' => $venue_ids + ); + endif; + + if ( $team ): + $args['meta_query'] = array( + array( + 'key' => 'sp_team', + 'value' => $team, + ), + ); + endif; + + endif; + + $events = get_posts( $args ); + + else: + $events = array(); + endif; + + return $events; + + } + +} diff --git a/includes/class-sp-data-formatter.php b/includes/class-sp-data-formatter.php deleted file mode 100644 index d223cc8c..00000000 --- a/includes/class-sp-data-formatter.php +++ /dev/null @@ -1,883 +0,0 @@ -term_id; - $season_names[ $season->term_id ] = $season->name; - endif; - endforeach; - - $data = array(); - - // Get all seasons populated with data where available - $data = sp_array_combine( $div_ids, sp_array_value( $columns, $league_id, array() ) ); - - // Get equations from column variables - $equations = sp_get_var_equations( 'sp_column' ); - - // Initialize placeholders array - $placeholders = array(); - - foreach ( $div_ids as $div_id ): - - $totals = array( 'eventsplayed' => 0, 'streak' => 0, 'last5' => null, 'last10' => null ); - - foreach ( $result_labels as $key => $value ): - $totals[ $key . 'for' ] = 0; - $totals[ $key . 'against' ] = 0; - endforeach; - - foreach ( $outcome_labels as $key => $value ): - $totals[ $key ] = 0; - endforeach; - - // Initialize streaks counter - $streak = array( 'name' => '', 'count' => 0, 'fire' => 1 ); - - // Initialize last counters - $last5 = array(); - $last10 = array(); - - // Add outcome types to last counters - foreach( $outcome_labels as $key => $value ): - $last5[ $key ] = 0; - $last10[ $key ] = 0; - endforeach; - - // Get all events involving the team in current season - $args = array( - 'post_type' => 'sp_event', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'order' => 'ASC', - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'sp_team', - 'value' => $post_id - ), - array( - 'key' => 'sp_format', - 'value' => 'league' - ) - ), - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'sp_league', - 'field' => 'id', - 'terms' => $league_id - ), - array( - 'taxonomy' => 'sp_season', - 'field' => 'id', - 'terms' => $div_id - ), - ) - ); - $events = get_posts( $args ); - - foreach( $events as $event ): - $results = (array)get_post_meta( $event->ID, 'sp_results', true ); - foreach ( $results as $team_id => $team_result ): - foreach ( $team_result as $key => $value ): - if ( $team_id == $post_id ): - if ( $key == 'outcome' ): - - // Convert to array - if ( ! is_array( $value ) ): - $value = array( $value ); - endif; - - foreach( $value as $outcome ): - - // Increment events played and outcome count - if ( array_key_exists( $outcome, $totals ) ): - $totals['eventsplayed']++; - $totals[ $outcome ]++; - endif; - - if ( $outcome && $outcome != '-1' ): - - // Add to streak counter - if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $outcome ) ): - $streak['name'] = $outcome; - $streak['count'] ++; - else: - $streak['fire'] = 0; - endif; - - // Add to last 5 counter if sum is less than 5 - if ( array_key_exists( $outcome, $last5 ) && array_sum( $last5 ) < 5 ): - $last5[ $outcome ] ++; - endif; - - // Add to last 10 counter if sum is less than 10 - if ( array_key_exists( $outcome, $last10 ) && array_sum( $last10 ) < 10 ): - $last10[ $outcome ] ++; - endif; - - endif; - - endforeach; - - else: - if ( array_key_exists( $key . 'for', $totals ) ): - $totals[ $key . 'for' ] += $value; - endif; - endif; - else: - if ( $key != 'outcome' ): - if ( array_key_exists( $key . 'against', $totals ) ): - $totals[ $key . 'against' ] += $value; - endif; - endif; - endif; - endforeach; - endforeach; - endforeach; - - // Compile streaks counter and add to totals - $args=array( - 'name' => $streak['name'], - 'post_type' => 'sp_outcome', - 'post_status' => 'publish', - 'posts_per_page' => 1 - ); - $outcomes = get_posts( $args ); - - if ( $outcomes ): - $outcome = reset( $outcomes ); - $totals['streak'] = $outcome->post_title . $streak['count']; - endif; - - // Add last counters to totals - $totals['last5'] = $last5; - $totals['last10'] = $last10; - - // Generate array of placeholder values for each league - $placeholders[ $div_id ] = array(); - foreach ( $equations as $key => $value ): - $placeholders[ $div_id ][ $key ] = sp_solve( $value['equation'], $totals, $value['precision'] ); - endforeach; - - endforeach; - - // Get columns from column variables - $columns = sp_get_var_labels( 'sp_column' ); - - // Merge the data and placeholders arrays - $merged = array(); - - foreach( $placeholders as $season_id => $season_data ): - - if ( ! sp_array_value( $leagues_seasons, $season_id, 0 ) ) - continue; - - $season_name = sp_array_value( $season_names, $season_id, ' ' ); - - // Add season name to row - $merged[ $season_id ] = array( - 'name' => $season_name - ); - - foreach( $season_data as $key => $value ): - - // Use static data if key exists and value is not empty, else use placeholder - if ( array_key_exists( $season_id, $data ) && array_key_exists( $key, $data[ $season_id ] ) && $data[ $season_id ][ $key ] != '' ): - $merged[ $season_id ][ $key ] = $data[ $season_id ][ $key ]; - else: - $merged[ $season_id ][ $key ] = $value; - endif; - - endforeach; - - endforeach; - - if ( $admin ): - return array( $columns, $data, $placeholders, $merged, $leagues_seasons ); - else: - $labels = array_merge( array( 'name' => SP()->text->string('Season') ), $columns ); - $merged[0] = $labels; - return $merged; - endif; - - } - - /** - * Formats data for league tables - */ - public function league_table( $post_id, $admin = false ) { - $league_id = sp_get_the_term_id( $post_id, 'sp_league', 0 ); - $div_id = sp_get_the_term_id( $post_id, 'sp_season', 0 ); - $team_ids = (array)get_post_meta( $post_id, 'sp_team', false ); - $table_stats = (array)get_post_meta( $post_id, 'sp_teams', true ); - $usecolumns = get_post_meta( $post_id, 'sp_columns', true ); - $adjustments = get_post_meta( $post_id, 'sp_adjustments', true ); - - // Get labels from result variables - $result_labels = (array)sp_get_var_labels( 'sp_result' ); - - // Get labels from outcome variables - $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); - - // Get all leagues populated with stats where available - $tempdata = sp_array_combine( $team_ids, $table_stats ); - - // Create entry for each team in totals - $totals = array(); - $placeholders = array(); - - // Initialize streaks counter - $streaks = array(); - - // Initialize last counters - $last5s = array(); - $last10s = array(); - - foreach ( $team_ids as $team_id ): - if ( ! $team_id ) - continue; - - // Initialize team streaks counter - $streaks[ $team_id ] = array( 'name' => '', 'count' => 0, 'fire' => 1 ); - - // Initialize team last counters - $last5s[ $team_id ] = array(); - $last10s[ $team_id ] = array(); - - // Add outcome types to team last counters - foreach( $outcome_labels as $key => $value ): - $last5s[ $team_id ][ $key ] = 0; - $last10s[ $team_id ][ $key ] = 0; - endforeach; - - // Initialize team totals - $totals[ $team_id ] = array( 'eventsplayed' => 0, 'streak' => 0 ); - - foreach ( $result_labels as $key => $value ): - $totals[ $team_id ][ $key . 'for' ] = 0; - $totals[ $team_id ][ $key . 'against' ] = 0; - endforeach; - - foreach ( $outcome_labels as $key => $value ): - $totals[ $team_id ][ $key ] = 0; - endforeach; - - // Get static stats - $static = get_post_meta( $team_id, 'sp_columns', true ); - - // Add static stats to placeholders - $placeholders[ $team_id ] = sp_array_value( $static, $div_id, array() ); - - endforeach; - - $args = array( - 'post_type' => 'sp_event', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'order' => 'ASC', - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'sp_league', - 'field' => 'id', - 'terms' => $league_id - ), - array( - 'taxonomy' => 'sp_season', - 'field' => 'id', - 'terms' => $div_id - ) - ) - ); - $events = get_posts( $args ); - - // Event loop - foreach ( $events as $event ): - - $results = (array)get_post_meta( $event->ID, 'sp_results', true ); - - foreach ( $results as $team_id => $team_result ): - - if ( ! in_array( $team_id, $team_ids ) ) - continue; - - foreach ( $team_result as $key => $value ): - - if ( $key == 'outcome' ): - - if ( ! is_array( $value ) ): - $value = array( $value ); - endif; - - foreach ( $value as $outcome ): - - // Increment events played and outcome count - if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $outcome, $totals[ $team_id ] ) ): - $totals[ $team_id ]['eventsplayed']++; - $totals[ $team_id ][ $outcome ]++; - endif; - - if ( $outcome && $outcome != '-1' ): - - // Add to streak counter - if ( $streaks[ $team_id ]['fire'] && ( $streaks[ $team_id ]['name'] == '' || $streaks[ $team_id ]['name'] == $outcome ) ): - $streaks[ $team_id ]['name'] = $outcome; - $streaks[ $team_id ]['count'] ++; - else: - $streaks[ $team_id ]['fire'] = 0; - endif; - - // Add to last 5 counter if sum is less than 5 - if ( array_key_exists( $team_id, $last5s ) && array_key_exists( $outcome, $last5s[ $team_id ] ) && array_sum( $last5s[ $team_id ] ) < 5 ): - $last5s[ $team_id ][ $outcome ] ++; - endif; - - // Add to last 10 counter if sum is less than 10 - if ( array_key_exists( $team_id, $last10s ) && array_key_exists( $outcome, $last10s[ $team_id ] ) && array_sum( $last10s[ $team_id ] ) < 10 ): - $last10s[ $team_id ][ $outcome ] ++; - endif; - - endif; - - endforeach; - - 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' ] += sp_array_value( $other_result, $key, 0 ); - endif; - endforeach; - endif; - endif; - - endforeach; - - endforeach; - - endforeach; - - foreach ( $streaks as $team_id => $streak ): - // Compile streaks counter and add to totals - if ( $streak['name'] ): - $args = array( - 'name' => $streak['name'], - 'post_type' => 'sp_outcome', - 'post_status' => 'publish', - 'posts_per_page' => 1 - ); - $outcomes = get_posts( $args ); - - if ( $outcomes ): - $outcome = reset( $outcomes ); - $totals[ $team_id ]['streak'] = $outcome->post_title . $streak['count']; - else: - $totals[ $team_id ]['streak'] = null; - endif; - else: - $totals[ $team_id ]['streak'] = null; - endif; - endforeach; - - foreach ( $last5s as $team_id => $last5 ): - // Add last 5 to totals - $totals[ $team_id ]['last5'] = $last5; - endforeach; - - foreach ( $last10s as $team_id => $last10 ): - // Add last 10 to totals - $totals[ $team_id ]['last10'] = $last10; - endforeach; - - $args = array( - 'post_type' => 'sp_column', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'orderby' => 'menu_order', - 'order' => 'ASC' - ); - $stats = get_posts( $args ); - - $columns = array(); - $this->table_priorities = array(); - - foreach ( $stats as $stat ): - - // Get post meta - $meta = get_post_meta( $stat->ID ); - - // Add equation to object - $stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 ); - $stat->precision = sp_array_value( sp_array_value( $meta, 'sp_precision', array() ), 0, 0 ); - - // Add column name to columns - $columns[ $stat->post_name ] = $stat->post_title; - - // Add order to priorities if priority is set and does not exist in array already - $priority = sp_array_value( sp_array_value( $meta, 'sp_priority', array() ), 0, 0 ); - if ( $priority && ! array_key_exists( $priority, $this->table_priorities ) ): - $this->table_priorities[ $priority ] = array( - 'column' => $stat->post_name, - 'order' => sp_array_value( sp_array_value( $meta, 'sp_order', array() ), 0, 'DESC' ) - ); - endif; - - endforeach; - - // Sort priorities in descending order - ksort( $this->table_priorities ); - - // Fill in empty placeholder values for each team - foreach ( $team_ids as $team_id ): - if ( ! $team_id ) - continue; - - foreach ( $stats as $stat ): - if ( sp_array_value( $placeholders[ $team_id ], $stat->post_name, '' ) == '' ): - - // Solve - $placeholder = sp_solve( $stat->equation, sp_array_value( $totals, $team_id, array() ), $stat->precision ); - - // Adjustments - $placeholder += sp_array_value( sp_array_value( $adjustments, $team_id, array() ), $stat->post_name, 0 ); - - $placeholders[ $team_id ][ $stat->post_name ] = $placeholder; - endif; - endforeach; - endforeach; - - // Merge the data and placeholders arrays - $merged = array(); - - foreach( $placeholders as $team_id => $team_data ): - - // Add team name to row - $merged[ $team_id ] = array(); - - $team_data['name'] = get_the_title( $team_id ); - - foreach( $team_data as $key => $value ): - - // Use static data if key exists and value is not empty, else use placeholder - if ( array_key_exists( $team_id, $tempdata ) && array_key_exists( $key, $tempdata[ $team_id ] ) && $tempdata[ $team_id ][ $key ] != '' ): - $merged[ $team_id ][ $key ] = $tempdata[ $team_id ][ $key ]; - else: - $merged[ $team_id ][ $key ] = $value; - endif; - - endforeach; - endforeach; - - uasort( $merged, array( $this, 'sort_table' ) ); - - // Rearrange data array to reflect values - $data = array(); - foreach( $merged as $key => $value ): - $data[ $key ] = $tempdata[ $key ]; - endforeach; - - if ( $admin ): - return array( $columns, $usecolumns, $data, $placeholders, $merged ); - else: - if ( ! is_array( $usecolumns ) ) - $usecolumns = array(); - foreach ( $columns as $key => $label ): - if ( ! in_array( $key, $usecolumns ) ): - unset( $columns[ $key ] ); - endif; - endforeach; - $labels = array_merge( array( 'name' => SP()->text->string('Team') ), $columns ); - $merged[0] = $labels; - return $merged; - endif; - } - - /** - * Formats data for player lists - */ - public static function player_list( $post_id, $admin = false ) { - $league_id = sp_get_the_term_id( $post_id, 'sp_league', 0 ); - $div_id = sp_get_the_term_id( $post_id, 'sp_season', 0 ); - $player_ids = (array)get_post_meta( $post_id, 'sp_player', false ); - $list_stats = (array)get_post_meta( $post_id, 'sp_players', true ); - $usecolumns = get_post_meta( $post_id, 'sp_columns', true ); - $adjustments = get_post_meta( $post_id, 'sp_adjustments', true ); - $orderby = get_post_meta( $post_id, 'sp_orderby', true ); - $order = get_post_meta( $post_id, 'sp_order', true ); - - // Get labels from performance variables - $performance_labels = (array)sp_get_var_labels( 'sp_performance' ); - - // Get labels from outcome variables - $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); - - // Get all leagues populated with stats where available - $tempdata = sp_array_combine( $player_ids, $list_stats ); - - // Create entry for each player in totals - $totals = array(); - $placeholders = array(); - - // Initialize streaks counter - $streaks = array(); - - // Initialize last counters - $last5s = array(); - $last10s = array(); - - foreach ( $player_ids as $player_id ): - if ( ! $player_id ) - continue; - - // Initialize player streaks counter - $streaks[ $player_id ] = array( 'name' => '', 'count' => 0, 'fire' => 1 ); - - // Initialize player last counters - $last5s[ $player_id ] = array(); - $last10s[ $player_id ] = array(); - - // Add outcome types to player last counters - foreach( $outcome_labels as $key => $value ): - $last5s[ $player_id ][ $key ] = 0; - $last10s[ $player_id ][ $key ] = 0; - endforeach; - - // Initialize player totals - $totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0, 'streak' => 0 ); - - foreach ( $performance_labels as $key => $value ): - $totals[ $player_id ][ $key ] = 0; - $totals[ $player_id ][ $key ] = 0; - endforeach; - - foreach ( $outcome_labels as $key => $value ): - $totals[ $player_id ][ $key ] = 0; - endforeach; - - // Get static stats - $static = get_post_meta( $player_id, 'sp_statistics', true ); - - // Add static stats to placeholders - $placeholders[ $player_id ] = sp_array_value( sp_array_value( $static, $league_id, array() ), $div_id, array() ); - - endforeach; - - $args = array( - 'post_type' => 'sp_event', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'order' => 'ASC', - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'sp_league', - 'field' => 'id', - 'terms' => $league_id - ), - array( - 'taxonomy' => 'sp_season', - 'field' => 'id', - 'terms' => $div_id - ) - ) - ); - $events = get_posts( $args ); - - // Event loop - foreach ( $events as $event ): - $results = (array)get_post_meta( $event->ID, 'sp_results', true ); - $team_performance = (array)get_post_meta( $event->ID, 'sp_players', true ); - - // Add all team performance - foreach ( $team_performance as $team_id => $players ): - foreach( $players as $player_id => $player_performance ): - if ( array_key_exists( $player_id, $totals ) && is_array( $totals[ $player_id ] ) ): - - $player_performance = sp_array_value( $players, $player_id, array() ); - - foreach ( $player_performance as $key => $value ): - if ( array_key_exists( $key, $totals[ $player_id ] ) ): - $totals[ $player_id ][ $key ] += $value; - endif; - endforeach; - - $team_results = sp_array_value( $results, $team_id, array() ); - - // Find the outcome - if ( array_key_exists( 'outcome', $team_results ) ): - - $value = $team_results['outcome']; - - // Convert to array - if ( ! is_array( $value ) ): - $value = array( $value ); - endif; - - foreach ( $value as $outcome ): - - if ( $outcome && $outcome != '-1' ): - - // Increment events attended and outcome count - if ( array_key_exists( $outcome, $totals[ $player_id ] ) ): - $totals[ $player_id ]['eventsattended']++; - $totals[ $player_id ][ $outcome ]++; - - // Increment events played if active in event - if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): - $totals[ $player_id ]['eventsplayed']++; - endif; - endif; - - // Add to streak counter - if ( $streaks[ $player_id ]['fire'] && ( $streaks[ $player_id ]['name'] == '' || $streaks[ $player_id ]['name'] == $outcome ) ): - $streaks[ $player_id ]['name'] = $outcome; - $streaks[ $player_id ]['count'] ++; - else: - $streaks[ $player_id ]['fire'] = 0; - endif; - - // Add to last 5 counter if sum is less than 5 - if ( array_key_exists( $player_id, $last5s ) && array_key_exists( $outcome, $last5s[ $player_id ] ) && array_sum( $last5s[ $player_id ] ) < 5 ): - $last5s[ $player_id ][ $outcome ] ++; - endif; - - // Add to last 10 counter if sum is less than 10 - if ( array_key_exists( $player_id, $last10s ) && array_key_exists( $outcome, $last10s[ $player_id ] ) && array_sum( $last10s[ $player_id ] ) < 10 ): - $last10s[ $player_id ][ $outcome ] ++; - endif; - - endif; - - endforeach; - - endif; - - endif; - - endforeach; - endforeach; - - endforeach; - - foreach ( $streaks as $player_id => $streak ): - // Compile streaks counter and add to totals - if ( $streak['name'] ): - $args = array( - 'name' => $streak['name'], - 'post_type' => 'sp_outcome', - 'post_status' => 'publish', - 'posts_per_page' => 1 - ); - $outcomes = get_posts( $args ); - - if ( $outcomes ): - $outcome = reset( $outcomes ); - $totals[ $player_id ]['streak'] = $outcome->post_title . $streak['count']; - else: - $totals[ $player_id ]['streak'] = null; - endif; - else: - $totals[ $player_id ]['streak'] = null; - endif; - endforeach; - - foreach ( $last5s as $player_id => $last5 ): - // Add last 5 to totals - $totals[ $player_id ]['last5'] = $last5; - endforeach; - - foreach ( $last10s as $player_id => $last10 ): - // Add last 10 to totals - $totals[ $player_id ]['last10'] = $last10; - endforeach; - - $args = array( - 'post_type' => 'sp_statistic', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'orderby' => 'menu_order', - 'order' => 'ASC' - ); - $stats = get_posts( $args ); - - $columns = array(); - - foreach ( $stats as $stat ): - - // Get post meta - $meta = get_post_meta( $stat->ID ); - - // Add equation to object - $stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 ); - $stat->precision = sp_array_value( sp_array_value( $meta, 'sp_precision', array() ), 0, 0 ); - - // Add column name to columns - $columns[ $stat->post_name ] = $stat->post_title; - - endforeach; - - // Fill in empty placeholder values for each player - foreach ( $player_ids as $player_id ): - if ( ! $player_id ) - continue; - - foreach ( $stats as $stat ): - if ( sp_array_value( $placeholders[ $player_id ], $stat->post_name, '' ) == '' ): - - // Solve - $placeholder = sp_solve( $stat->equation, sp_array_value( $totals, $player_id, array() ), $stat->precision ); - - // Adjustments - $placeholder += sp_array_value( sp_array_value( $adjustments, $player_id, array() ), $stat->post_name, 0 ); - - $placeholders[ $player_id ][ $stat->post_name ] = $placeholder; - endif; - endforeach; - endforeach; - - // Merge the data and placeholders arrays - $merged = array(); - - foreach( $placeholders as $player_id => $player_data ): - - // Add player name to row - $merged[ $player_id ] = array(); - - $player_data['name'] = get_the_title( $player_id ); - - 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 ] != '' ): - $merged[ $player_id ][ $key ] = $tempdata[ $player_id ][ $key ]; - else: - $merged[ $player_id ][ $key ] = $value; - endif; - - endforeach; - endforeach; - - if ( $orderby != 'number' || $order != 'ASC' ): - global $sportspress_statistic_priorities; - $sportspress_statistic_priorities = array( - array( - 'key' => $orderby, - 'order' => $order, - ), - ); - uasort( $merged, array( $this, 'sort_list' ) ); - endif; - - // Rearrange data array to reflect values - $data = array(); - foreach( $merged as $key => $value ): - $data[ $key ] = $tempdata[ $key ]; - endforeach; - - if ( $admin ): - return array( $columns, $usecolumns, $data, $placeholders, $merged ); - else: - if ( ! is_array( $usecolumns ) ) - $usecolumns = array(); - foreach ( $columns as $key => $label ): - if ( ! in_array( $key, $usecolumns ) ): - unset( $columns[ $key ] ); - endif; - endforeach; - $labels = array_merge( array( 'name' => SP()->text->string('Player') ), $columns ); - $merged[0] = $labels; - return $merged; - endif; - } - - private function sort_table( $a, $b ) { - - // Loop through priorities - foreach( $this->table_priorities as $priority ): - - // Proceed if columns are not equal - if ( sp_array_value( $a, $priority['column'], 0 ) != sp_array_value( $b, $priority['column'], 0 ) ): - - // Compare column values - $output = sp_array_value( $a, $priority['column'], 0 ) - sp_array_value( $b, $priority['column'], 0 ); - - // Flip value if descending order - if ( $priority['order'] == 'DESC' ) $output = 0 - $output; - - return ( $output > 0 ); - - endif; - - endforeach; - - // Default sort by alphabetical - return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) ); - } - - private function sort_list ( $a, $b ) { - - // Loop through priorities - if ( is_array( $this->list_priorities ) ) : foreach( $this->list_priorities as $priority ): - - // Proceed if columns are not equal - if ( sp_array_value( $a, $priority['key'], 0 ) != sp_array_value( $b, $priority['key'], 0 ) ): - - if ( $priority['key'] == 'name' ): - - $output = strcmp( sp_array_value( $a, 'name', null ), sp_array_value( $b, 'name', null ) ); - - else: - - // Compare performance values - $output = sp_array_value( $a, $priority['key'], 0 ) - sp_array_value( $b, $priority['key'], 0 ); - - endif; - - // Flip value if descending order - if ( $priority['order'] == 'DESC' ) $output = 0 - $output; - - return ( $output > 0 ); - - endif; - - endforeach; endif; - - // Default sort by number - return sp_array_value( $a, 'number', 0 ) - sp_array_value( $b, 'number', 0 ); - } -} diff --git a/includes/class-sp-event.php b/includes/class-sp-event.php new file mode 100644 index 00000000..5f3444f8 --- /dev/null +++ b/includes/class-sp-event.php @@ -0,0 +1,303 @@ +ID = absint( $post->ID ); + $this->post = $post; + else: + $this->ID = absint( $post ); + $this->post = get_post( $this->ID ); + endif; + } + + /** + * __get function. + * + * @access public + * @param mixed $key + * @return bool + */ + public function __get( $key ) { + if ( ! isset( $key ) ): + return $this->post; + else: + $value = get_post_meta( $this->ID, 'sp_' . $key, true ); + endif; + + return $value; + } + + /** + * Returns formatted data + * + * @access public + * @param int $league_id + * @param bool $admin + * @return array + */ + public function data( $league_id, $admin = false ) { + + $seasons = (array)get_the_terms( $this->ID, 'sp_season' ); + $stats = (array)get_post_meta( $this->ID, 'sp_statistics', true ); + $leagues = sp_array_value( (array)get_post_meta( $this->ID, 'sp_leagues', true ), $league_id, array() ); + + // Get labels from performance variables + $performance_labels = (array)sp_get_var_labels( 'sp_performance' ); + + // Get labels from outcome variables + $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); + + // Generate array of all season ids and season names + $div_ids = array(); + $season_names = array(); + foreach ( $seasons as $season ): + if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ): + $div_ids[] = $season->term_id; + $season_names[ $season->term_id ] = $season->name; + endif; + endforeach; + + $data = array(); + + // Get all seasons populated with data where available + $data = sp_array_combine( $div_ids, sp_array_value( $stats, $league_id, array() ) ); + + // Get equations from statistic variables + $equations = sp_get_var_equations( 'sp_statistic' ); + + // Initialize placeholders array + $placeholders = array(); + + foreach ( $div_ids as $div_id ): + + $totals = array( 'eventsattended' => 0, 'eventsplayed' => 0, 'streak' => 0, 'last5' => null, 'last10' => null ); + + foreach ( $performance_labels as $key => $value ): + $totals[ $key ] = 0; + endforeach; + + foreach ( $outcome_labels as $key => $value ): + $totals[ $key ] = 0; + endforeach; + + // Initialize streaks counter + $streak = array( 'name' => '', 'count' => 0, 'fire' => 1 ); + + // Initialize last counters + $last5 = array(); + $last10 = array(); + + // Add outcome types to last counters + foreach( $outcome_labels as $key => $value ): + $last5[ $key ] = 0; + $last10[ $key ] = 0; + endforeach; + + // Get all events involving the team in current season + $args = array( + 'post_type' => 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'order' => 'ASC', + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'sp_event', + 'value' => $this->ID + ), + array( + 'key' => 'sp_format', + 'value' => 'league' + ) + ), + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'sp_league', + 'field' => 'id', + 'terms' => $league_id + ), + array( + 'taxonomy' => 'sp_season', + 'field' => 'id', + 'terms' => $div_id + ), + ) + ); + $events = get_posts( $args ); + + // Event loop + foreach( $events as $event ): + $results = (array)get_post_meta( $event->ID, 'sp_results', true ); + $team_performance = (array)get_post_meta( $event->ID, 'sp_events', true ); + + // Add all team performance + foreach ( $team_performance as $team_id => $events ): + if ( array_key_exists( $this->ID, $events ) ): + + $event_performance = sp_array_value( $events, $this->ID, array() ); + + foreach ( $event_performance as $key => $value ): + if ( array_key_exists( $key, $totals ) ): + $totals[ $key ] += $value; + endif; + endforeach; + + $team_results = sp_array_value( $results, $team_id, array() ); + + // Find the outcome + if ( array_key_exists( 'outcome', $team_results ) ): + + $value = $team_results['outcome']; + + // Convert to array + if ( ! is_array( $value ) ): + $value = array( $value ); + endif; + + foreach( $value as $outcome ): + + if ( $outcome && $outcome != '-1' ): + + // Increment events attended and outcome count + if ( array_key_exists( $outcome, $totals ) ): + $totals['eventsattended']++; + $totals[ $outcome ]++; + + // Increment events played if active in event + if ( sp_array_value( $event_performance, 'status' ) != 'sub' || sp_array_value( $event_performance, 'sub', 0 ) ): + $totals['eventsplayed']++; + endif; + endif; + + // Add to streak counter + if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $outcome ) ): + $streak['name'] = $outcome; + $streak['count'] ++; + else: + $streak['fire'] = 0; + endif; + + // Add to last 5 counter if sum is less than 5 + if ( array_key_exists( $outcome, $last5 ) && array_sum( $last5 ) < 5 ): + $last5[ $outcome ] ++; + endif; + + // Add to last 10 counter if sum is less than 10 + if ( array_key_exists( $outcome, $last10 ) && array_sum( $last10 ) < 10 ): + $last10[ $outcome ] ++; + endif; + + endif; + + endforeach; + + endif; + endif; + endforeach; + endforeach; + + // Compile streaks counter and add to totals + $args = array( + 'name' => $streak['name'], + 'post_type' => 'sp_outcome', + 'post_status' => 'publish', + 'posts_per_page' => 1 + ); + $outcomes = get_posts( $args ); + + if ( $outcomes ): + $outcome = reset( $outcomes ); + $totals['streak'] = $outcome->post_title . $streak['count']; + endif; + + // Add last counters to totals + $totals['last5'] = $last5; + $totals['last10'] = $last10; + + // Generate array of placeholder values for each league + $placeholders[ $div_id ] = array(); + foreach ( $equations as $key => $value ): + $placeholders[ $div_id ][ $key ] = sp_solve( $value['equation'], $totals, $value['precision'] ); + endforeach; + + endforeach; + + // Get stats from statistic variables + $stats = sp_get_var_labels( 'sp_statistic' ); + + // Merge the data and placeholders arrays + $merged = array(); + + foreach( $placeholders as $season_id => $season_data ): + + if ( ! sp_array_value( $leagues, $season_id, 0 ) ) + continue; + + $season_name = sp_array_value( $season_names, $season_id, ' ' ); + + $team_id = sp_array_value( $leagues, $season_id, array() ); + + if ( ! $team_id || $team_id == '-1' ) + continue; + + $team_name = get_the_title( $team_id ); + + if ( get_option( 'sportspress_event_link_teams', 'no' ) == 'yes' ? true : false ): + $team_permalink = get_permalink( $team_id ); + $team_name = '' . $team_name . ''; + endif; + + // Add season name to row + $merged[ $season_id ] = array( + 'name' => $season_name, + 'team' => $team_name + ); + + foreach( $season_data as $key => $value ): + + // Use static data if key exists and value is not empty, else use placeholder + if ( array_key_exists( $season_id, $data ) && array_key_exists( $key, $data[ $season_id ] ) && $data[ $season_id ][ $key ] != '' ): + $merged[ $season_id ][ $key ] = $data[ $season_id ][ $key ]; + else: + $merged[ $season_id ][ $key ] = $value; + endif; + + endforeach; + + endforeach; + + if ( $admin ): + return array( $stats, $data, $placeholders, $merged, $leagues ); + else: + $labels = array_merge( array( 'name' => SP()->text->string('Season'), 'team' => SP()->text->string('Team') ), $stats ); + $merged[0] = $labels; + return $merged; + endif; + } + +} diff --git a/includes/class-sp-table.php b/includes/class-sp-league-table.php similarity index 97% rename from includes/class-sp-table.php rename to includes/class-sp-league-table.php index 5ac6a3e2..4fb7c9a7 100644 --- a/includes/class-sp-table.php +++ b/includes/class-sp-league-table.php @@ -2,15 +2,15 @@ /** * League Table Class * - * The SportsPress table class handles individual league table data. + * The SportsPress league table class handles individual league table data. * - * @class SP_Table + * @class SP_League_Table * @version 0.8 - * @package SportsPress/Abstracts + * @package SportsPress/Classes * @category Class * @author ThemeBoy */ -class SP_Table { +class SP_League_Table { /** @var int The league table (post) ID. */ public $ID; @@ -19,7 +19,7 @@ class SP_Table { public $post; /** @var array The sort priorities array. */ - private $priorities; + public $priorities; /** * __construct function. @@ -28,7 +28,7 @@ class SP_Table { * @param mixed $post */ public function __construct( $post ) { - if ( $post instanceof WP_Post || $post instanceof SP_Table ): + if ( $post instanceof WP_Post || $post instanceof SP_League_Table ): $this->ID = absint( $post->ID ); $this->post = $post; else: @@ -356,7 +356,7 @@ class SP_Table { * @param array $b * @return int */ - private function sort( $a, $b ) { + public function sort( $a, $b ) { // Loop through priorities foreach( $this->priorities as $priority ): diff --git a/includes/class-sp-player-list.php b/includes/class-sp-player-list.php new file mode 100644 index 00000000..137b0e21 --- /dev/null +++ b/includes/class-sp-player-list.php @@ -0,0 +1,397 @@ +ID = absint( $post->ID ); + $this->post = $post; + else: + $this->ID = absint( $post ); + $this->post = get_post( $this->ID ); + endif; + } + + /** + * __get function. + * + * @access public + * @param mixed $key + * @return bool + */ + public function __get( $key ) { + if ( ! isset( $key ) ): + return $this->post; + else: + $value = get_post_meta( $this->ID, 'sp_' . $key, true ); + endif; + + return $value; + } + + /** + * Returns formatted data + * + * @access public + * @param bool $admin + * @return array + */ + public function data( $admin = false ) { + $league_id = sp_get_the_term_id( $this->ID, 'sp_league', 0 ); + $div_id = sp_get_the_term_id( $this->ID, 'sp_season', 0 ); + $player_ids = (array)get_post_meta( $this->ID, 'sp_player', false ); + $list_stats = (array)get_post_meta( $this->ID, 'sp_players', true ); + $usecolumns = get_post_meta( $this->ID, 'sp_columns', true ); + $adjustments = get_post_meta( $this->ID, 'sp_adjustments', true ); + $orderby = get_post_meta( $this->ID, 'sp_orderby', true ); + $order = get_post_meta( $this->ID, 'sp_order', true ); + + // Get labels from performance variables + $performance_labels = (array)sp_get_var_labels( 'sp_performance' ); + + // Get labels from outcome variables + $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); + + // Get all leagues populated with stats where available + $tempdata = sp_array_combine( $player_ids, $list_stats ); + + // Create entry for each player in totals + $totals = array(); + $placeholders = array(); + + // Initialize streaks counter + $streaks = array(); + + // Initialize last counters + $last5s = array(); + $last10s = array(); + + foreach ( $player_ids as $player_id ): + if ( ! $player_id ) + continue; + + // Initialize player streaks counter + $streaks[ $player_id ] = array( 'name' => '', 'count' => 0, 'fire' => 1 ); + + // Initialize player last counters + $last5s[ $player_id ] = array(); + $last10s[ $player_id ] = array(); + + // Add outcome types to player last counters + foreach( $outcome_labels as $key => $value ): + $last5s[ $player_id ][ $key ] = 0; + $last10s[ $player_id ][ $key ] = 0; + endforeach; + + // Initialize player totals + $totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0, 'streak' => 0 ); + + foreach ( $performance_labels as $key => $value ): + $totals[ $player_id ][ $key ] = 0; + $totals[ $player_id ][ $key ] = 0; + endforeach; + + foreach ( $outcome_labels as $key => $value ): + $totals[ $player_id ][ $key ] = 0; + endforeach; + + // Get static stats + $static = get_post_meta( $player_id, 'sp_statistics', true ); + + // Add static stats to placeholders + $placeholders[ $player_id ] = sp_array_value( sp_array_value( $static, $league_id, array() ), $div_id, array() ); + + endforeach; + + $args = array( + 'post_type' => 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'order' => 'ASC', + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'sp_league', + 'field' => 'id', + 'terms' => $league_id + ), + array( + 'taxonomy' => 'sp_season', + 'field' => 'id', + 'terms' => $div_id + ) + ) + ); + $events = get_posts( $args ); + + // Event loop + foreach ( $events as $event ): + $results = (array)get_post_meta( $event->ID, 'sp_results', true ); + $team_performance = (array)get_post_meta( $event->ID, 'sp_players', true ); + + // Add all team performance + foreach ( $team_performance as $team_id => $players ): + foreach( $players as $player_id => $player_performance ): + if ( array_key_exists( $player_id, $totals ) && is_array( $totals[ $player_id ] ) ): + + $player_performance = sp_array_value( $players, $player_id, array() ); + + foreach ( $player_performance as $key => $value ): + if ( array_key_exists( $key, $totals[ $player_id ] ) ): + $totals[ $player_id ][ $key ] += $value; + endif; + endforeach; + + $team_results = sp_array_value( $results, $team_id, array() ); + + // Find the outcome + if ( array_key_exists( 'outcome', $team_results ) ): + + $value = $team_results['outcome']; + + // Convert to array + if ( ! is_array( $value ) ): + $value = array( $value ); + endif; + + foreach ( $value as $outcome ): + + if ( $outcome && $outcome != '-1' ): + + // Increment events attended and outcome count + if ( array_key_exists( $outcome, $totals[ $player_id ] ) ): + $totals[ $player_id ]['eventsattended']++; + $totals[ $player_id ][ $outcome ]++; + + // Increment events played if active in event + if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): + $totals[ $player_id ]['eventsplayed']++; + endif; + endif; + + // Add to streak counter + if ( $streaks[ $player_id ]['fire'] && ( $streaks[ $player_id ]['name'] == '' || $streaks[ $player_id ]['name'] == $outcome ) ): + $streaks[ $player_id ]['name'] = $outcome; + $streaks[ $player_id ]['count'] ++; + else: + $streaks[ $player_id ]['fire'] = 0; + endif; + + // Add to last 5 counter if sum is less than 5 + if ( array_key_exists( $player_id, $last5s ) && array_key_exists( $outcome, $last5s[ $player_id ] ) && array_sum( $last5s[ $player_id ] ) < 5 ): + $last5s[ $player_id ][ $outcome ] ++; + endif; + + // Add to last 10 counter if sum is less than 10 + if ( array_key_exists( $player_id, $last10s ) && array_key_exists( $outcome, $last10s[ $player_id ] ) && array_sum( $last10s[ $player_id ] ) < 10 ): + $last10s[ $player_id ][ $outcome ] ++; + endif; + + endif; + + endforeach; + + endif; + + endif; + + endforeach; + endforeach; + + endforeach; + + foreach ( $streaks as $player_id => $streak ): + // Compile streaks counter and add to totals + if ( $streak['name'] ): + $args = array( + 'name' => $streak['name'], + 'post_type' => 'sp_outcome', + 'post_status' => 'publish', + 'posts_per_page' => 1 + ); + $outcomes = get_posts( $args ); + + if ( $outcomes ): + $outcome = reset( $outcomes ); + $totals[ $player_id ]['streak'] = $outcome->post_title . $streak['count']; + else: + $totals[ $player_id ]['streak'] = null; + endif; + else: + $totals[ $player_id ]['streak'] = null; + endif; + endforeach; + + foreach ( $last5s as $player_id => $last5 ): + // Add last 5 to totals + $totals[ $player_id ]['last5'] = $last5; + endforeach; + + foreach ( $last10s as $player_id => $last10 ): + // Add last 10 to totals + $totals[ $player_id ]['last10'] = $last10; + endforeach; + + $args = array( + 'post_type' => 'sp_statistic', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'orderby' => 'menu_order', + 'order' => 'ASC' + ); + $stats = get_posts( $args ); + + $columns = array(); + + foreach ( $stats as $stat ): + + // Get post meta + $meta = get_post_meta( $stat->ID ); + + // Add equation to object + $stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 ); + $stat->precision = sp_array_value( sp_array_value( $meta, 'sp_precision', array() ), 0, 0 ); + + // Add column name to columns + $columns[ $stat->post_name ] = $stat->post_title; + + endforeach; + + // Fill in empty placeholder values for each player + foreach ( $player_ids as $player_id ): + if ( ! $player_id ) + continue; + + foreach ( $stats as $stat ): + if ( sp_array_value( $placeholders[ $player_id ], $stat->post_name, '' ) == '' ): + + // Solve + $placeholder = sp_solve( $stat->equation, sp_array_value( $totals, $player_id, array() ), $stat->precision ); + + // Adjustments + $placeholder += sp_array_value( sp_array_value( $adjustments, $player_id, array() ), $stat->post_name, 0 ); + + $placeholders[ $player_id ][ $stat->post_name ] = $placeholder; + endif; + endforeach; + endforeach; + + // Merge the data and placeholders arrays + $merged = array(); + + foreach( $placeholders as $player_id => $player_data ): + + // Add player name to row + $merged[ $player_id ] = array(); + + $player_data['name'] = get_the_title( $player_id ); + + 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 ] != '' ): + $merged[ $player_id ][ $key ] = $tempdata[ $player_id ][ $key ]; + else: + $merged[ $player_id ][ $key ] = $value; + endif; + + 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 + $data = array(); + foreach( $merged as $key => $value ): + $data[ $key ] = $tempdata[ $key ]; + endforeach; + + if ( $admin ): + return array( $columns, $usecolumns, $data, $placeholders, $merged ); + else: + if ( ! is_array( $usecolumns ) ) + $usecolumns = array(); + foreach ( $columns as $key => $label ): + if ( ! in_array( $key, $usecolumns ) ): + unset( $columns[ $key ] ); + endif; + endforeach; + $labels = array_merge( array( 'name' => SP()->text->string('Player') ), $columns ); + $merged[0] = $labels; + return $merged; + endif; + } + + /** + * Sort the table by priorities. + * + * @param array $a + * @param array $b + * @return int + */ + public function sort( $a, $b ) { + + // Loop through priorities + if ( is_array( $this->priorities ) ) : foreach( $this->priorities as $priority ): + + // Proceed if columns are not equal + if ( sp_array_value( $a, $priority['key'], 0 ) != sp_array_value( $b, $priority['key'], 0 ) ): + + if ( $priority['key'] == 'name' ): + + $output = strcmp( sp_array_value( $a, 'name', null ), sp_array_value( $b, 'name', null ) ); + + else: + + // Compare performance values + $output = sp_array_value( $a, $priority['key'], 0 ) - sp_array_value( $b, $priority['key'], 0 ); + + endif; + + // Flip value if descending order + if ( $priority['order'] == 'DESC' ) $output = 0 - $output; + + return ( $output > 0 ); + + endif; + + endforeach; endif; + + // Default sort by number + return sp_array_value( $a, 'number', 0 ) - sp_array_value( $b, 'number', 0 ); + } + +} diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php new file mode 100644 index 00000000..e0dd666b --- /dev/null +++ b/includes/class-sp-player.php @@ -0,0 +1,324 @@ +ID = absint( $post->ID ); + $this->post = $post; + else: + $this->ID = absint( $post ); + $this->post = get_post( $this->ID ); + endif; + } + + /** + * __get function. + * + * @access public + * @param mixed $key + * @return bool + */ + public function __get( $key ) { + if ( ! isset( $key ) ): + return $this->post; + elseif ( 'past_teams' == $key ): + return get_post_meta( $this->ID, 'sp_past_team', false ); + elseif ( 'metrics' == $key ): + $metrics = (array)get_post_meta( $this->ID, 'sp_metrics', true ); + + // Get labels from metric variables + $metric_labels = (array)sp_get_var_labels( 'sp_metric' ); + + $data = array(); + + foreach( $metric_labels as $key => $value ): + + $metric = sp_array_value( $metrics, $key, null ); + if ( $metric == null ) + continue; + + $data[ $value ] = sp_array_value( $metrics, $key, ' ' ); + + endforeach; + + return $data; + else: + $value = get_post_meta( $this->ID, 'sp_' . $key, true ); + endif; + + return $value; + } + + /** + * Returns formatted data + * + * @access public + * @param int $league_id + * @param bool $admin + * @return array + */ + public function data( $league_id, $admin = false ) { + + $seasons = (array)get_the_terms( $this->ID, 'sp_season' ); + $stats = (array)get_post_meta( $this->ID, 'sp_statistics', true ); + $leagues = sp_array_value( (array)get_post_meta( $this->ID, 'sp_leagues', true ), $league_id, array() ); + + // Get labels from performance variables + $performance_labels = (array)sp_get_var_labels( 'sp_performance' ); + + // Get labels from outcome variables + $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); + + // Generate array of all season ids and season names + $div_ids = array(); + $season_names = array(); + foreach ( $seasons as $season ): + if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ): + $div_ids[] = $season->term_id; + $season_names[ $season->term_id ] = $season->name; + endif; + endforeach; + + $data = array(); + + // Get all seasons populated with data where available + $data = sp_array_combine( $div_ids, sp_array_value( $stats, $league_id, array() ) ); + + // Get equations from statistic variables + $equations = sp_get_var_equations( 'sp_statistic' ); + + // Initialize placeholders array + $placeholders = array(); + + foreach ( $div_ids as $div_id ): + + $totals = array( 'eventsattended' => 0, 'eventsplayed' => 0, 'streak' => 0, 'last5' => null, 'last10' => null ); + + foreach ( $performance_labels as $key => $value ): + $totals[ $key ] = 0; + endforeach; + + foreach ( $outcome_labels as $key => $value ): + $totals[ $key ] = 0; + endforeach; + + // Initialize streaks counter + $streak = array( 'name' => '', 'count' => 0, 'fire' => 1 ); + + // Initialize last counters + $last5 = array(); + $last10 = array(); + + // Add outcome types to last counters + foreach( $outcome_labels as $key => $value ): + $last5[ $key ] = 0; + $last10[ $key ] = 0; + endforeach; + + // Get all events involving the team in current season + $args = array( + 'post_type' => 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'order' => 'ASC', + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'sp_player', + 'value' => $this->ID + ), + array( + 'key' => 'sp_format', + 'value' => 'league' + ) + ), + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'sp_league', + 'field' => 'id', + 'terms' => $league_id + ), + array( + 'taxonomy' => 'sp_season', + 'field' => 'id', + 'terms' => $div_id + ), + ) + ); + $events = get_posts( $args ); + + // Event loop + foreach( $events as $event ): + $results = (array)get_post_meta( $event->ID, 'sp_results', true ); + $team_performance = (array)get_post_meta( $event->ID, 'sp_players', true ); + + // Add all team performance + foreach ( $team_performance as $team_id => $players ): + if ( array_key_exists( $this->ID, $players ) ): + + $player_performance = sp_array_value( $players, $this->ID, array() ); + + foreach ( $player_performance as $key => $value ): + if ( array_key_exists( $key, $totals ) ): + $totals[ $key ] += $value; + endif; + endforeach; + + $team_results = sp_array_value( $results, $team_id, array() ); + + // Find the outcome + if ( array_key_exists( 'outcome', $team_results ) ): + + $value = $team_results['outcome']; + + // Convert to array + if ( ! is_array( $value ) ): + $value = array( $value ); + endif; + + foreach( $value as $outcome ): + + if ( $outcome && $outcome != '-1' ): + + // Increment events attended and outcome count + if ( array_key_exists( $outcome, $totals ) ): + $totals['eventsattended']++; + $totals[ $outcome ]++; + + // Increment events played if active in event + if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): + $totals['eventsplayed']++; + endif; + endif; + + // Add to streak counter + if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $outcome ) ): + $streak['name'] = $outcome; + $streak['count'] ++; + else: + $streak['fire'] = 0; + endif; + + // Add to last 5 counter if sum is less than 5 + if ( array_key_exists( $outcome, $last5 ) && array_sum( $last5 ) < 5 ): + $last5[ $outcome ] ++; + endif; + + // Add to last 10 counter if sum is less than 10 + if ( array_key_exists( $outcome, $last10 ) && array_sum( $last10 ) < 10 ): + $last10[ $outcome ] ++; + endif; + + endif; + + endforeach; + + endif; + endif; + endforeach; + endforeach; + + // Compile streaks counter and add to totals + $args = array( + 'name' => $streak['name'], + 'post_type' => 'sp_outcome', + 'post_status' => 'publish', + 'posts_per_page' => 1 + ); + $outcomes = get_posts( $args ); + + if ( $outcomes ): + $outcome = reset( $outcomes ); + $totals['streak'] = $outcome->post_title . $streak['count']; + endif; + + // Add last counters to totals + $totals['last5'] = $last5; + $totals['last10'] = $last10; + + // Generate array of placeholder values for each league + $placeholders[ $div_id ] = array(); + foreach ( $equations as $key => $value ): + $placeholders[ $div_id ][ $key ] = sp_solve( $value['equation'], $totals, $value['precision'] ); + endforeach; + + endforeach; + + // Get stats from statistic variables + $stats = sp_get_var_labels( 'sp_statistic' ); + + // Merge the data and placeholders arrays + $merged = array(); + + foreach( $placeholders as $season_id => $season_data ): + + if ( ! sp_array_value( $leagues, $season_id, 0 ) ) + continue; + + $season_name = sp_array_value( $season_names, $season_id, ' ' ); + + $team_id = sp_array_value( $leagues, $season_id, array() ); + + if ( ! $team_id || $team_id == '-1' ) + continue; + + $team_name = get_the_title( $team_id ); + + if ( get_option( 'sportspress_player_link_teams', 'no' ) == 'yes' ? true : false ): + $team_permalink = get_permalink( $team_id ); + $team_name = '' . $team_name . ''; + endif; + + // Add season name to row + $merged[ $season_id ] = array( + 'name' => $season_name, + 'team' => $team_name + ); + + foreach( $season_data as $key => $value ): + + // Use static data if key exists and value is not empty, else use placeholder + if ( array_key_exists( $season_id, $data ) && array_key_exists( $key, $data[ $season_id ] ) && $data[ $season_id ][ $key ] != '' ): + $merged[ $season_id ][ $key ] = $data[ $season_id ][ $key ]; + else: + $merged[ $season_id ][ $key ] = $value; + endif; + + endforeach; + + endforeach; + + if ( $admin ): + return array( $stats, $data, $placeholders, $merged, $leagues ); + else: + $labels = array_merge( array( 'name' => SP()->text->string('Season'), 'team' => SP()->text->string('Team') ), $stats ); + $merged[0] = $labels; + return $merged; + endif; + } + +} diff --git a/includes/class-sp-team.php b/includes/class-sp-team.php new file mode 100644 index 00000000..46415737 --- /dev/null +++ b/includes/class-sp-team.php @@ -0,0 +1,278 @@ +ID = absint( $post->ID ); + $this->post = $post; + else: + $this->ID = absint( $post ); + $this->post = get_post( $this->ID ); + endif; + } + + /** + * __get function. + * + * @access public + * @param mixed $key + * @return bool + */ + public function __get( $key ) { + if ( ! isset( $key ) ): + return $this->post; + else: + $value = get_post_meta( $this->ID, 'sp_' . $key, true ); + endif; + + return $value; + } + + /** + * Returns formatted data + * + * @access public + * @param bool $admin + * @return array + */ + public function data( $league_id, $admin = false ) { + $seasons = (array)get_the_terms( $this->ID, 'sp_season' ); + $columns = (array)get_post_meta( $this->ID, 'sp_columns', true ); + $leagues_seasons = sp_array_value( (array)get_post_meta( $this->ID, 'sp_leagues', true ), $league_id, array() ); + + // Get labels from result variables + $result_labels = (array)sp_get_var_labels( 'sp_result' ); + + // Get labels from outcome variables + $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); + + // Generate array of all season ids and season names + $div_ids = array(); + $season_names = array(); + foreach ( $seasons as $season ): + if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ): + $div_ids[] = $season->term_id; + $season_names[ $season->term_id ] = $season->name; + endif; + endforeach; + + $data = array(); + + // Get all seasons populated with data where available + $data = sp_array_combine( $div_ids, sp_array_value( $columns, $league_id, array() ) ); + + // Get equations from column variables + $equations = sp_get_var_equations( 'sp_column' ); + + // Initialize placeholders array + $placeholders = array(); + + foreach ( $div_ids as $div_id ): + + $totals = array( 'eventsplayed' => 0, 'streak' => 0, 'last5' => null, 'last10' => null ); + + foreach ( $result_labels as $key => $value ): + $totals[ $key . 'for' ] = 0; + $totals[ $key . 'against' ] = 0; + endforeach; + + foreach ( $outcome_labels as $key => $value ): + $totals[ $key ] = 0; + endforeach; + + // Initialize streaks counter + $streak = array( 'name' => '', 'count' => 0, 'fire' => 1 ); + + // Initialize last counters + $last5 = array(); + $last10 = array(); + + // Add outcome types to last counters + foreach( $outcome_labels as $key => $value ): + $last5[ $key ] = 0; + $last10[ $key ] = 0; + endforeach; + + // Get all events involving the team in current season + $args = array( + 'post_type' => 'sp_event', + 'numberposts' => -1, + 'posts_per_page' => -1, + 'order' => 'ASC', + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'sp_team', + 'value' => $this->ID + ), + array( + 'key' => 'sp_format', + 'value' => 'league' + ) + ), + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'sp_league', + 'field' => 'id', + 'terms' => $league_id + ), + array( + 'taxonomy' => 'sp_season', + 'field' => 'id', + 'terms' => $div_id + ), + ) + ); + $events = get_posts( $args ); + + foreach( $events as $event ): + $results = (array)get_post_meta( $event->ID, 'sp_results', true ); + foreach ( $results as $team_id => $team_result ): + foreach ( $team_result as $key => $value ): + if ( $team_id == $this->ID ): + if ( $key == 'outcome' ): + + // Convert to array + if ( ! is_array( $value ) ): + $value = array( $value ); + endif; + + foreach( $value as $outcome ): + + // Increment events played and outcome count + if ( array_key_exists( $outcome, $totals ) ): + $totals['eventsplayed']++; + $totals[ $outcome ]++; + endif; + + if ( $outcome && $outcome != '-1' ): + + // Add to streak counter + if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $outcome ) ): + $streak['name'] = $outcome; + $streak['count'] ++; + else: + $streak['fire'] = 0; + endif; + + // Add to last 5 counter if sum is less than 5 + if ( array_key_exists( $outcome, $last5 ) && array_sum( $last5 ) < 5 ): + $last5[ $outcome ] ++; + endif; + + // Add to last 10 counter if sum is less than 10 + if ( array_key_exists( $outcome, $last10 ) && array_sum( $last10 ) < 10 ): + $last10[ $outcome ] ++; + endif; + + endif; + + endforeach; + + else: + if ( array_key_exists( $key . 'for', $totals ) ): + $totals[ $key . 'for' ] += $value; + endif; + endif; + else: + if ( $key != 'outcome' ): + if ( array_key_exists( $key . 'against', $totals ) ): + $totals[ $key . 'against' ] += $value; + endif; + endif; + endif; + endforeach; + endforeach; + endforeach; + + // Compile streaks counter and add to totals + $args=array( + 'name' => $streak['name'], + 'post_type' => 'sp_outcome', + 'post_status' => 'publish', + 'posts_per_page' => 1 + ); + $outcomes = get_posts( $args ); + + if ( $outcomes ): + $outcome = reset( $outcomes ); + $totals['streak'] = $outcome->post_title . $streak['count']; + endif; + + // Add last counters to totals + $totals['last5'] = $last5; + $totals['last10'] = $last10; + + // Generate array of placeholder values for each league + $placeholders[ $div_id ] = array(); + foreach ( $equations as $key => $value ): + $placeholders[ $div_id ][ $key ] = sp_solve( $value['equation'], $totals, $value['precision'] ); + endforeach; + + endforeach; + + // Get columns from column variables + $columns = sp_get_var_labels( 'sp_column' ); + + // Merge the data and placeholders arrays + $merged = array(); + + foreach( $placeholders as $season_id => $season_data ): + + if ( ! sp_array_value( $leagues_seasons, $season_id, 0 ) ) + continue; + + $season_name = sp_array_value( $season_names, $season_id, ' ' ); + + // Add season name to row + $merged[ $season_id ] = array( + 'name' => $season_name + ); + + foreach( $season_data as $key => $value ): + + // Use static data if key exists and value is not empty, else use placeholder + if ( array_key_exists( $season_id, $data ) && array_key_exists( $key, $data[ $season_id ] ) && $data[ $season_id ][ $key ] != '' ): + $merged[ $season_id ][ $key ] = $data[ $season_id ][ $key ]; + else: + $merged[ $season_id ][ $key ] = $value; + endif; + + endforeach; + + endforeach; + + if ( $admin ): + return array( $columns, $data, $placeholders, $merged, $leagues_seasons ); + else: + $labels = array_merge( array( 'name' => SP()->text->string('Season') ), $columns ); + $merged[0] = $labels; + return $merged; + endif; + } + +} diff --git a/includes/sp-conditional-functions.php b/includes/sp-conditional-functions.php index be00cd3b..3ddc8573 100644 --- a/includes/sp-conditional-functions.php +++ b/includes/sp-conditional-functions.php @@ -22,6 +22,24 @@ function is_sportspress() { return apply_filters( 'is_sportspress', ( is_singular( array( 'sp_event', 'sp_calendar', 'sp_team', 'sp_table', 'sp_player', 'sp_list', 'sp_staff' ) ) ) ? true : false ); } +/** + * is_sp_config_type - Returns true if post is SportsPress config type + * + * @access public + * @return bool + */ +if ( ! function_exists( 'is_sp_config_type' ) ) { + function is_sp_config_type( $typenow = null ) { + if ( $typenow == null ) global $typenow; + + $post_types = array( 'sp_result', 'sp_outcome', 'sp_column', 'sp_performance', 'sp_metric', 'sp_statistic' ); + + if ( in_array( $typenow, $post_types ) ) + return true; + return false; + } +} + if ( ! function_exists( 'is_ajax' ) ) { /** diff --git a/includes/sp-core-functions.php b/includes/sp-core-functions.php index 6f2dc6fd..2dd931ad 100644 --- a/includes/sp-core-functions.php +++ b/includes/sp-core-functions.php @@ -662,138 +662,6 @@ if ( !function_exists( 'sp_post_checklist' ) ) { } } -if ( !function_exists( 'sp_calculate_selector' ) ) { - function sp_calculate_selector( $postid, $selected = null ) { - $options = array( - 'total' => __( 'Total', 'sportspress' ), - 'average' => __( 'Average', 'sportspress' ), - ); - ?> - - $type, - 'numberposts' => -1, - 'posts_per_page' => -1, - 'orderby' => 'menu_order', - 'order' => 'ASC', - 'exclude' => $postid - ); - $vars = get_posts( $args ); - - // Add extra vars to the array - if ( isset( $defaults ) && is_array( $defaults ) ): - foreach ( $defaults as $key => $value ): - $arr[ $key ] = $value; - endforeach; - endif; - - // Add vars to the array - if ( isset( $variations ) && is_array( $variations ) ): - foreach ( $vars as $var ): - if ( $totals ) $arr[ '$' . $var->post_name ] = $var->post_title; - foreach ( $variations as $key => $value ): - $arr[ '$' . $var->post_name . $key ] = $var->post_title . ' ' . $value; - endforeach; - endforeach; - else: - foreach ( $vars as $var ): - $arr[ '$' . $var->post_name ] = $var->post_title; - endforeach; - endif; - - return (array) $arr; - } -} - -if ( !function_exists( 'sp_equation_selector' ) ) { - function sp_equation_selector( $postid, $selected = null, $groups = array() ) { - - if ( ! isset( $postid ) ) - return; - - // Initialize options array - $options = array(); - - // Add groups to options - foreach ( $groups as $group ): - switch ( $group ): - case 'player_event': - $options[ __( 'Events', 'sportspress' ) ] = array( '$eventsattended' => __( 'Attended', 'sportspress' ), '$eventsplayed' => __( 'Played', 'sportspress' ) ); - break; - case 'team_event': - $options[ __( 'Events', 'sportspress' ) ] = array( '$eventsplayed' => __( 'Played', 'sportspress' ) ); - break; - case 'result': - $options[ __( 'Results', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_result', array( 'for' => '→', 'against' => '←' ), null, false ); - break; - case 'outcome': - $options[ __( 'Outcomes', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_outcome', array() ); - $options[ __( 'Outcomes', 'sportspress' ) ]['$streak'] = __( 'Streak', 'sportspress' ); - $options[ __( 'Outcomes', 'sportspress' ) ]['$last5'] = __( 'Last 5', 'sportspress' ); - $options[ __( 'Outcomes', 'sportspress' ) ]['$last10'] = __( 'Last 10', 'sportspress' ); - break; - case 'performance': - $options[ __( 'Performance', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_performance' ); - break; - case 'metric': - $options[ __( 'Metric', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_metric' ); - break; - endswitch; - endforeach; - - // Create array of operators - $operators = array( '+' => '+', '-' => '−', '*' => '×', '/' => '÷', '(' => '(', ')' => ')' ); - - // Add operators to options - $options[ __( 'Operators', 'sportspress' ) ] = $operators; - - // Create array of constants - $max = 10; - $constants = array(); - for ( $i = 1; $i <= $max; $i ++ ): - $constants[$i] = $i; - endfor; - - // Add 100 to constants - $constants[100] = 100; - - // Add constants to options - $options[ __( 'Constants', 'sportspress' ) ] = (array) $constants; - - ?> - - -
- - - - - - - - - - - - - 0 ): - $main_result = get_option( 'sportspress_primary_result', null ); - $i = 0; - foreach ( $data as $event ): - $teams = get_post_meta( $event->ID, 'sp_team' ); - $results = get_post_meta( $event->ID, 'sp_results', true ); - $video = get_post_meta( $event->ID, 'sp_video', true ); - ?> - - - - - - - - - - - - - - -
- - - - - - - - - - - -
post_title; ?> - ID ) . '">' . $team_result . ' '; - endif; - - echo $name . '
'; - endif; - endforeach; - ?> -
ID, 'sp_venue' ); ?> - - -
- ID ) ): ?> -
- - post_content == null ): - _e( 'None', 'sportspress' ); - elseif ( $event->post_status == 'publish' ): - _e( 'Recap', 'sportspress' ); - else: - _e( 'Preview', 'sportspress' ); - endif; - ?> -
-
- -
-
- -
- - - - - - - - - - - - $div_stats ): - if ( !$div_id ) continue; - $div = get_term( $div_id, 'sp_season' ); - ?> - - - - $label ): - $value = sp_array_value( sp_array_value( $data, $div_id, array() ), $column, 0 ); - ?> - - - - - -
- > - - - '; - ?>
-
- -
- - - - - - - - - - - - $div_stats ): - if ( !$div_id || $div_id == 'performance' ) continue; - $div = get_term( $div_id, 'sp_season' ); - ?> - - - - $label ): - ?> - - - - - -
- name; ?> - - 'sp_team', - 'name' => 'sp_leagues[' . $league_id . '][' . $div_id . ']', - 'show_option_none' => __( '— None —', 'sportspress' ), - 'sort_order' => 'ASC', - 'sort_column' => 'menu_order', - 'selected' => $value, - 'values' => 'ID', - 'include' => $teams, - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'sp_league', - 'terms' => $league_id, - 'field' => 'id', - ), - array( - 'taxonomy' => 'sp_season', - 'terms' => $div_id, - 'field' => 'id', - ), - ), - ); - if ( ! sp_dropdown_pages( $args ) ): - _e( 'No results found.', 'sportspress' ); - endif; - ?> - '; - ?>
-
- -
- - - - - - - - - - - - $div_stats ): - if ( !$div_id || $div_id == 'statistics' ) continue; - $div = get_term( $div_id, 'sp_season' ); - ?> - - - - $label ): - ?> - - - - - -
- name; ?> - - 'sp_team', - 'name' => 'sp_leagues[' . $league_id . '][' . $div_id . ']', - 'show_option_none' => __( '— None —', 'sportspress' ), - 'sort_order' => 'ASC', - 'sort_column' => 'menu_order', - 'selected' => $value, - 'values' => 'ID', - 'include' => $teams, - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'sp_league', - 'terms' => $league_id, - 'field' => 'id', - ), - array( - 'taxonomy' => 'sp_season', - 'terms' => $div_id, - 'field' => 'id', - ), - ), - ); - if ( ! sp_dropdown_pages( $args ) ): - _e( 'No results found.', 'sportspress' ); - endif; - ?> - '; - ?>
-
- -
- - - - - $label ): ?> - - - - - - - $team_results ): - if ( !$team_id ) continue; - ?> - - - $label ): - $value = sp_array_value( $team_results, $column, '' ); - ?> - - - - - - -
- - - 'sp_outcome', - 'name' => 'sp_results[' . $team_id . '][outcome][]', - 'option_none_value' => '', - 'sort_order' => 'ASC', - 'sort_column' => 'menu_order', - 'selected' => $values, - 'class' => 'sp-outcome', - 'property' => 'multiple', - 'chosen' => true, - ); - sp_dropdown_pages( $args ); - ?> -
-
- __( 'Starting Lineup', 'sportspress' ), - 'sub' => __( 'Substitute', 'sportspress' ), - ); - - $output = ''; - - return $output; - - } -} - -if ( !function_exists( 'sp_event_player_sub_selector' ) ) { - function sp_event_player_sub_selector( $team_id, $player_id, $value, $data = array() ) { - - if ( ! $team_id || ! $player_id ) - return '—'; - - $output = ''; - - return $output; - - } -} - -if ( !function_exists( 'sp_edit_event_players_table' ) ) { - function sp_edit_event_players_table( $columns = array(), $data = array(), $team_id ) { - ?> -
- - - - - - - - - - - - - $player_performance ): - if ( !$player_id ) continue; - $number = get_post_meta( $player_id, 'sp_number', true ); - ?> - - - - $label ): - $value = sp_array_value( $player_performance, $column, '' ); - ?> - - - - - - - - - $label ): - $player_id = 0; - $player_performance = sp_array_value( $data, 0, array() ); - $value = sp_array_value( $player_performance, $column, '' ); - ?> - - - - - -
#
- - - - -
  
-
- 'sp_event', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'orderby' => 'post_date', - 'order' => 'ASC', - 'post_status' => 'any', - 'tax_query' => array( - 'relation' => 'AND' - ), - ); - - if ( $pagenow != 'post-new.php' ): - if ( $post_id ): - $leagues = get_the_terms( $post_id, 'sp_league' ); - $seasons = get_the_terms( $post_id, 'sp_season' ); - $venues = get_the_terms( $post_id, 'sp_venue' ); - $team = get_post_meta( $post_id, 'sp_team', true ); - $usecolumns = get_post_meta( $post_id, 'sp_columns', true ); - - if ( $leagues ): - $league_ids = array(); - foreach( $leagues as $league ): - $league_ids[] = $league->term_id; - endforeach; - $args['tax_query'][] = array( - 'taxonomy' => 'sp_league', - 'field' => 'id', - 'terms' => $league_ids - ); - endif; - - if ( $seasons ): - $season_ids = array(); - foreach( $seasons as $season ): - $season_ids[] = $season->term_id; - endforeach; - $args['tax_query'][] = array( - 'taxonomy' => 'sp_season', - 'field' => 'id', - 'terms' => $season_ids - ); - endif; - - if ( $venues ): - $venue_ids = array(); - foreach( $venues as $venue ): - $venue_ids[] = $venue->term_id; - endforeach; - $args['tax_query'][] = array( - 'taxonomy' => 'sp_venue', - 'field' => 'id', - 'terms' => $venue_ids - ); - endif; - - if ( $team ): - $args['meta_query'] = array( - array( - 'key' => 'sp_team', - 'value' => $team, - ), - ); - endif; - - else: - $usecolumns = null; - endif; - - $events = get_posts( $args ); - - else: - $usecolumns = null; - $events = array(); - endif; - - if ( $admin ): - return array( $events, $usecolumns ); - else: - return $events; - endif; - - } -} - -if ( !function_exists( 'sp_get_team_columns_data' ) ) { - function sp_get_team_columns_data( $post_id, $league_id, $admin = false ) { - $formatter = new SP_Data_Formatter; - return $formatter->team_columns( $post_id, $league_id, $admin ); - } -} - if ( !function_exists( 'sp_sort_table_teams' ) ) { function sp_sort_table_teams ( $a, $b ) { @@ -1618,1212 +883,6 @@ if ( !function_exists( 'sp_sort_table_teams' ) ) { } } -if ( !function_exists( 'sp_get_player_alist_data' ) ) { - function sp_get_player_alist_data( $post_id, $admin = false ) { - $league_id = sp_get_the_term_id( $post_id, 'sp_league', 0 ); - $div_id = sp_get_the_term_id( $post_id, 'sp_season', 0 ); - $team_id = get_post_meta( $post_id, 'sp_team', true ); - $player_ids = (array)get_post_meta( $post_id, 'sp_player', false ); - $stats = (array)get_post_meta( $post_id, 'sp_players', true ); - $orderby = get_post_meta( $post_id, 'sp_orderby', true ); - $order = get_post_meta( $post_id, 'sp_order', true ); - $usecolumns = get_post_meta( $post_id, 'sp_columns', true ); - - // Get labels from result variables - $columns = (array)sp_get_var_labels( array( 'sp_metric', 'sp_statistic' ) ); - - // Get all leagues populated with stats where available - $tempdata = sp_array_combine( $player_ids, $stats ); - - // Create entry for each player in totals - $totals = array(); - $placeholders = array(); - - foreach ( $player_ids as $player_id ): - if ( ! $player_id ) - continue; - - $totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0 ); - - foreach ( $columns as $key => $value ): - $totals[ $player_id ][ $key ] = 0; - endforeach; - - // Get static statistics - $static = get_post_meta( $player_id, 'sp_statistics', true ); - - // Create placeholders entry for the player - $placeholders[ $player_id ] = array( 'eventsplayed' => 0 ); - - // Add static statistics to placeholders - if ( is_array( $static ) && array_key_exists( $league_id, $static ) && array_key_exists( $div_id, $static[ $league_id ] ) ): - $placeholders[ $player_id ] = array_merge( $placeholders[ $player_id ], $static[ $league_id ][ $div_id ] ); - endif; - endforeach; - - $args = array( - 'post_type' => 'sp_event', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'sp_league', - 'field' => 'id', - 'terms' => $league_id - ), - array( - 'taxonomy' => 'sp_season', - 'field' => 'id', - 'terms' => $div_id - ), - ), - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'sp_team', - 'compare' => 'EXISTS', - ), - ), - ); - - if ( $team_id ): - $args['meta_query'] = array( - array( - 'key' => 'sp_team', - 'value' => $team_id, - ), - ); - endif; - - $events = get_posts( $args ); - - // Event loop - foreach( $events as $event ): - - $teams = (array)get_post_meta( $event->ID, 'sp_players', true ); - - if ( $team_id ): - - if ( ! array_key_exists( $team_id, $teams ) ) - continue; - - $players = sp_array_value( $teams, $team_id, array() ); - - foreach ( $players as $player_id => $player_statistics ): - - if ( ! $player_id || ! in_array( $player_id, $player_ids ) ) - continue; - - // Increment events played - if ( sp_array_value( $player_statistics, 'status' ) != 'sub' || sp_array_value( $player_statistics, 'sub', 0 ) ): - $totals[ $player_id ]['eventsplayed']++; - endif; - - foreach ( $player_statistics as $key => $value ): - - if ( array_key_exists( $key, $totals[ $player_id ] ) ): - $totals[ $player_id ][ $key ] += $value; - endif; - - endforeach; - - endforeach; - - else: - - foreach ( $teams as $players ): - - foreach ( $players as $player_id => $player_statistics ): - - if ( ! $player_id || ! in_array( $player_id, $player_ids ) ) - continue; - - // Increment events played - if ( sp_array_value( $player_statistics, 'status' ) != 'sub' || sp_array_value( $player_statistics, 'sub', 0 ) ): - $totals[ $player_id ]['eventsplayed']++; - endif; - - foreach ( $player_statistics as $key => $value ): - - if ( array_key_exists( $key, $totals[ $player_id ] ) ): - $totals[ $player_id ][ $key ] += $value; - endif; - - endforeach; - - endforeach; - - endforeach; - - endif; - - endforeach; - - $args = array( - 'post_type' => array( 'sp_metric', 'sp_statistic' ), - 'numberposts' => -1, - 'posts_per_page' => -1, - 'orderby' => 'menu_order', - 'order' => 'ASC', - ); - $statistics = get_posts( $args ); - - foreach ( $statistics as $statistic ): - - // Get post meta - $meta = get_post_meta( $statistic->ID ); - - // Add equation to object - $statistic->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 ); - - // Add column name to columns - $columns[ $statistic->post_name ] = $statistic->post_title; - - endforeach; - - // Fill in empty placeholder values for each player - foreach ( $player_ids as $player_id ): - - if ( ! $player_id ) - continue; - - foreach ( $statistics as $statistic ): - if ( sp_array_value( $placeholders[ $player_id ], $statistic->post_name, '' ) == '' ): - - $placeholders[ $player_id ][ $statistic->post_name ] = sp_array_value( sp_array_value( $totals, $player_id, array() ), $statistic->post_name, 0 ); - - endif; - endforeach; - endforeach; - - // Merge the data and placeholders arrays - $merged = array(); - - foreach( $placeholders as $player_id => $player_data ): - - // Add player name to row - $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 ): - - // 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 ] != '' ): - $merged[ $player_id ][ $key ] = $tempdata[ $player_id ][ $key ]; - else: - $merged[ $player_id ][ $key ] = $value; - endif; - - endforeach; - endforeach; - - if ( $orderby != 'number' || $order != 'ASC' ): - global $sportspress_statistic_priorities; - $sportspress_statistic_priorities = array( - array( - 'key' => $orderby, - 'order' => $order, - ), - ); - uasort( $merged, 'sp_sort_list_players' ); - endif; - - // Rearrange data array to reflect statistics - $data = array(); - foreach( $merged as $key => $value ): - $data[ $key ] = $tempdata[ $key ]; - endforeach; - - if ( $admin ): - return array( $columns, $usecolumns, $data, $placeholders, $merged ); - else: - if ( ! is_array( $usecolumns ) ) - $usecolumns = array(); - foreach ( $columns as $key => $label ): - if ( ! in_array( $key, $usecolumns ) ): - unset( $columns[ $key ] ); - endif; - endforeach; - $labels = array_merge( array( 'name' => SP()->text->string('Player') ), $columns ); - $merged[0] = $labels; - return $merged; - endif; - } -} - -if ( !function_exists( 'sp_get_player_roster_data' ) ) { - function sp_get_player_roster_data( $post_id, $admin = false ) { - $league_id = sp_get_the_term_id( $post_id, 'sp_league', 0 ); - $div_id = sp_get_the_term_id( $post_id, 'sp_season', 0 ); - $team_id = get_post_meta( $post_id, 'sp_team', true ); - $player_ids = (array)get_post_meta( $post_id, 'sp_player', false ); - $stats = (array)get_post_meta( $post_id, 'sp_players', true ); - $orderby = get_post_meta( $post_id, 'sp_orderby', true ); - $order = get_post_meta( $post_id, 'sp_order', true ); - - // Get labels from result variables - $columns = (array)sp_get_var_labels( 'sp_performance' ); - - // Get all leagues populated with stats where available - $tempdata = sp_array_combine( $player_ids, $stats ); - - // Create entry for each player in totals - $totals = array(); - $placeholders = array(); - - foreach ( $player_ids as $player_id ): - if ( ! $player_id ) - continue; - - $positions = get_the_terms( $player_id, 'sp_position' ); - $position_ids = array(); - foreach ( $positions as $position ): - $position_ids[] = $position->term_id; - endforeach; - - $totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0 ); - - foreach ( $columns as $key => $value ): - $totals[ $player_id ][ $key ] = 0; - endforeach; - - // Get static performance - $static = get_post_meta( $player_id, 'sp_performance', true ); - - // Create placeholders entry for the player - $placeholders[ $player_id ] = array( 'eventsplayed' => 0, 'positions' => $position_ids ); - - // Add static performance to placeholders - if ( is_array( $static ) && array_key_exists( $league_id, $static ) && array_key_exists( $div_id, $static[ $league_id ] ) ): - $placeholders[ $player_id ] = array_merge( $placeholders[ $player_id ], $static[ $league_id ][ $div_id ] ); - endif; - endforeach; - - $args = array( - 'post_type' => 'sp_event', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'sp_league', - 'field' => 'id', - 'terms' => $league_id - ), - array( - 'taxonomy' => 'sp_season', - 'field' => 'id', - 'terms' => $div_id - ), - ), - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'sp_team', - 'compare' => 'EXISTS', - ), - ), - ); - - if ( $team_id ): - $args['meta_query'] = array( - array( - 'key' => 'sp_team', - 'value' => $team_id, - ), - ); - endif; - - $events = get_posts( $args ); - - // Event loop - foreach( $events as $event ): - - $teams = (array)get_post_meta( $event->ID, 'sp_players', true ); - - if ( $team_id ): - - if ( ! array_key_exists( $team_id, $teams ) ) - continue; - - $players = sp_array_value( $teams, $team_id, array() ); - - foreach ( $players as $player_id => $player_performance ): - - if ( ! $player_id || ! in_array( $player_id, $player_ids ) ) - continue; - - // Increment events played - if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): - $totals[ $player_id ]['eventsplayed']++; - endif; - - foreach ( $player_performance as $key => $value ): - - if ( array_key_exists( $key, $totals[ $player_id ] ) ): - $totals[ $player_id ][ $key ] += $value; - endif; - - endforeach; - - endforeach; - - else: - - foreach ( $teams as $players ): - - foreach ( $players as $player_id => $player_performance ): - - if ( ! $player_id || ! in_array( $player_id, $player_ids ) ) - continue; - - // Increment events played - if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): - $totals[ $player_id ]['eventsplayed']++; - endif; - - foreach ( $player_performance as $key => $value ): - - if ( array_key_exists( $key, $totals[ $player_id ] ) ): - $totals[ $player_id ][ $key ] += $value; - endif; - - endforeach; - - endforeach; - - endforeach; - - endif; - - endforeach; - - $args = array( - 'post_type' => 'sp_performance', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'orderby' => 'menu_order', - 'order' => 'ASC', - ); - $performances = get_posts( $args ); - - $columns = array( 'eventsplayed' => SP()->text->string('Played') ); - - foreach ( $performances as $performance ): - - // Get post meta - $meta = get_post_meta( $performance->ID ); - - // Add equation to object - $performance->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 ); - - // Add column name to columns - $columns[ $performance->post_name ] = $performance->post_title; - - endforeach; - - // 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 performance for placeholder calculations - $epstat = new stdClass(); - $epstat->post_name = 'eventsplayed'; - array_unshift( $performances, $epstat ); - - foreach ( $performances as $performance ): - if ( sp_array_value( $placeholders[ $player_id ], $performance->post_name, '' ) == '' ): - - if ( $performance->post_name == 'eventsplayed' ): - $calculate = 'total'; - else: - $calculate = get_post_meta( $performance->ID, 'sp_calculate', true ); - endif; - - if ( $calculate && $calculate == 'average' ): - - // Reflect average - $eventsplayed = (int)sp_array_value( $totals[ $player_id ], 'eventsplayed', 0 ); - if ( ! $eventsplayed ): - $placeholders[ $player_id ][ $performance->post_name ] = 0; - else: - $placeholders[ $player_id ][ $performance->post_name ] = sp_array_value( sp_array_value( $totals, $player_id, array() ), $performance->post_name, 0 ) / $eventsplayed; - endif; - - else: - - // Reflect total - $placeholders[ $player_id ][ $performance->post_name ] = sp_array_value( sp_array_value( $totals, $player_id, array() ), $performance->post_name, 0 ); - - endif; - - endif; - endforeach; - endforeach; - - // Merge the data and placeholders arrays - $merged = array(); - - foreach( $placeholders as $player_id => $player_data ): - - // Add player name to row - $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 ): - - // 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 ] != '' ): - $merged[ $player_id ][ $key ] = $tempdata[ $player_id ][ $key ]; - else: - $merged[ $player_id ][ $key ] = $value; - endif; - - endforeach; - endforeach; - - if ( $orderby != 'number' || $order != 'ASC' ): - global $sportspress_performance_priorities; - $sportspress_performance_priorities = array( - array( - 'key' => $orderby, - 'order' => $order, - ), - ); - uasort( $merged, 'sp_sort_list_players' ); - endif; - - // Rearrange data array to reflect performance - $data = array(); - foreach( $merged as $key => $value ): - $data[ $key ] = $tempdata[ $key ]; - endforeach; - - if ( $admin ): - return array( $columns, $data, $placeholders, $merged ); - else: - $labels = array_merge( array( 'name' => SP()->text->string('Player') ), $columns ); - $merged[0] = $labels; - return $merged; - endif; - } -} - -if ( !function_exists( 'sp_sort_list_players' ) ) { - function sp_sort_list_players ( $a, $b ) { - - global $sportspress_statistic_priorities; - - // Loop through priorities - if ( is_array( $sportspress_statistic_priorities ) ) : foreach( $sportspress_statistic_priorities as $priority ): - - // Proceed if columns are not equal - if ( sp_array_value( $a, $priority['key'], 0 ) != sp_array_value( $b, $priority['key'], 0 ) ): - - if ( $priority['key'] == 'name' ): - - $output = strcmp( sp_array_value( $a, 'name', null ), sp_array_value( $b, 'name', null ) ); - - else: - - // Compare performance values - $output = sp_array_value( $a, $priority['key'], 0 ) - sp_array_value( $b, $priority['key'], 0 ); - - endif; - - // Flip value if descending order - if ( $priority['order'] == 'DESC' ) $output = 0 - $output; - - return ( $output > 0 ); - - endif; - - endforeach; endif; - - // Default sort by number - return sp_array_value( $a, 'number', 0 ) - sp_array_value( $b, 'number', 0 ); - } -} - -if ( !function_exists( 'sp_get_player_metrics_data' ) ) { - function sp_get_player_metrics_data( $post_id ) { - - $metrics = (array)get_post_meta( $post_id, 'sp_metrics', true ); - - // Get labels from metric variables - $metric_labels = (array)sp_get_var_labels( 'sp_metric' ); - - $data = array(); - - foreach( $metric_labels as $key => $value ): - - $metric = sp_array_value( $metrics, $key, null ); - if ( $metric == null ) - continue; - - $data[ $value ] = sp_array_value( $metrics, $key, ' ' ); - - endforeach; - - return $data; - - } -} - -if ( !function_exists( 'sp_get_player_performance_data' ) ) { - function sp_get_player_performance_data( $post_id, $league_id, $admin = false ) { - - $seasons = (array)get_the_terms( $post_id, 'sp_season' ); - $stats = (array)get_post_meta( $post_id, 'sp_performance', true ); - $seasons_teams = sp_array_value( (array)get_post_meta( $post_id, 'sp_leagues', true ), $league_id, array() ); - - $args = array( - 'post_type' => 'sp_performance', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'orderby' => 'menu_order', - 'order' => 'ASC', - ); - - $performances = get_posts( $args ); - - $performance_labels = array(); - $equations = array( 'eventsplayed' => 'total' ); - foreach ( $performances as $performance ): - $performance_labels[ $performance->post_name ] = $performance->post_title; - $equations[ $performance->post_name ] = get_post_meta( $performance->ID, 'sp_calculate', true ); - endforeach; - $columns = array_merge( array( 'eventsplayed' => SP()->text->string('Played') ), $performance_labels ); - - // Generate array of all season ids and season names - $div_ids = array(); - $season_names = array(); - foreach ( $seasons as $season ): - if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ): - $div_ids[] = $season->term_id; - $season_names[ $season->term_id ] = $season->name; - endif; - endforeach; - - $tempdata = array(); - - // Get all seasons populated with stats where available - $tempdata = sp_array_combine( $div_ids, sp_array_value( $stats, $league_id, array() ) ); - - foreach ( $div_ids as $div_id ): - - $team_id = sp_array_value( $seasons_teams, $div_id, '-1' ); - - $totals = array( 'eventsattended' => 0, 'eventsplayed' => 0 ); - - foreach ( $performance_labels as $key => $value ): - $totals[ $key ] = 0; - endforeach; - - $args = array( - 'post_type' => 'sp_event', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'meta_query' => array( - array( - 'key' => 'sp_player', - 'value' => $post_id - ) - ), - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'sp_league', - 'field' => 'id', - 'terms' => $league_id - ), - array( - 'taxonomy' => 'sp_season', - 'field' => 'id', - 'terms' => $div_id - ) - ) - ); - $events = get_posts( $args ); - - foreach( $events as $event ): - $totals['eventsattended']++; - $team_performance = (array)get_post_meta( $event->ID, 'sp_players', true ); - - // Add all team performance - foreach ( $team_performance as $players ): - if ( array_key_exists( $post_id, $players ) ): - $player_performance = sp_array_value( $players, $post_id, array() ); - if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): - $totals['eventsplayed']++; - endif; - foreach ( $player_performance as $key => $value ): - if ( array_key_exists( $key, $totals ) ): - $totals[ $key ] += $value; - endif; - endforeach; - endif; - endforeach; - endforeach; - - // Generate array of placeholder values for each league - $placeholders[ $div_id ] = array(); - foreach ( $equations as $key => $value ): - - if ( $value == 'average' ): - - // Reflect average - $eventsplayed = (int)sp_array_value( $totals, 'eventsplayed', 0 ); - if ( ! $eventsplayed ): - $placeholders[ $div_id ][ $key ] = 0; - else: - $placeholders[ $div_id ][ $key ] = sp_array_value( $totals, $key, 0 ) / $eventsplayed; - endif; - - else: - - // Reflect total - $placeholders[ $div_id ][ $key ] = sp_array_value( $totals, $key, 0 ); - - endif; - - endforeach; - - endforeach; - - // Merge the data and placeholders arrays - $merged = array(); - - foreach( $placeholders as $season_id => $season_data ): - - $team_id = sp_array_value( $seasons_teams, $season_id, array() ); - - if ( ! $team_id || $team_id == '-1' ) - continue; - - $team_name = get_the_title( $team_id ); - $team_permalink = get_permalink( $team_id ); - - $season_name = sp_array_value( $season_names, $season_id, ' ' ); - - // Add season name to row - $merged[ $season_id ] = array( - 'name' => $season_name, - 'team' => '' . $team_name . '' - ); - - foreach( $season_data as $key => $value ): - - // Use static data if key exists and value is not empty, else use placeholder - if ( array_key_exists( $season_id, $tempdata ) && array_key_exists( $key, $tempdata[ $season_id ] ) && $tempdata[ $season_id ][ $key ] != '' ): - $merged[ $season_id ][ $key ] = $tempdata[ $season_id ][ $key ]; - else: - $merged[ $season_id ][ $key ] = $value; - endif; - - endforeach; - - endforeach; - - if ( $admin ): - return array( $columns, $tempdata, $placeholders, $merged, $seasons_teams ); - else: - $labels = array_merge( array( 'name' => SP()->text->string('Season'), 'team' => SP()->text->string('Team'), 'eventsplayed' => SP()->text->string('Played') ), $columns ); - $merged[0] = $labels; - return $merged; - endif; - - } -} - -if ( !function_exists( 'sp_get_player_statistics_data' ) ) { - function sp_get_player_statistics_data( $post_id, $league_id, $admin = false ) { - - $seasons = (array)get_the_terms( $post_id, 'sp_season' ); - $stats = (array)get_post_meta( $post_id, 'sp_statistics', true ); - $leagues = sp_array_value( (array)get_post_meta( $post_id, 'sp_leagues', true ), $league_id, array() ); - - // Get labels from performance variables - $performance_labels = (array)sp_get_var_labels( 'sp_performance' ); - - // Get labels from outcome variables - $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); - - // Generate array of all season ids and season names - $div_ids = array(); - $season_names = array(); - foreach ( $seasons as $season ): - if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ): - $div_ids[] = $season->term_id; - $season_names[ $season->term_id ] = $season->name; - endif; - endforeach; - - $data = array(); - - // Get all seasons populated with data where available - $data = sp_array_combine( $div_ids, sp_array_value( $stats, $league_id, array() ) ); - - // Get equations from statistic variables - $equations = sp_get_var_equations( 'sp_statistic' ); - - // Initialize placeholders array - $placeholders = array(); - - foreach ( $div_ids as $div_id ): - - $totals = array( 'eventsattended' => 0, 'eventsplayed' => 0, 'streak' => 0, 'last5' => null, 'last10' => null ); - - foreach ( $performance_labels as $key => $value ): - $totals[ $key ] = 0; - endforeach; - - foreach ( $outcome_labels as $key => $value ): - $totals[ $key ] = 0; - endforeach; - - // Initialize streaks counter - $streak = array( 'name' => '', 'count' => 0, 'fire' => 1 ); - - // Initialize last counters - $last5 = array(); - $last10 = array(); - - // Add outcome types to last counters - foreach( $outcome_labels as $key => $value ): - $last5[ $key ] = 0; - $last10[ $key ] = 0; - endforeach; - - // Get all events involving the team in current season - $args = array( - 'post_type' => 'sp_event', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'order' => 'ASC', - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'sp_player', - 'value' => $post_id - ), - array( - 'key' => 'sp_format', - 'value' => 'league' - ) - ), - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'sp_league', - 'field' => 'id', - 'terms' => $league_id - ), - array( - 'taxonomy' => 'sp_season', - 'field' => 'id', - 'terms' => $div_id - ), - ) - ); - $events = get_posts( $args ); - - // Event loop - foreach( $events as $event ): - $results = (array)get_post_meta( $event->ID, 'sp_results', true ); - $team_performance = (array)get_post_meta( $event->ID, 'sp_players', true ); - - // Add all team performance - foreach ( $team_performance as $team_id => $players ): - if ( array_key_exists( $post_id, $players ) ): - - $player_performance = sp_array_value( $players, $post_id, array() ); - - foreach ( $player_performance as $key => $value ): - if ( array_key_exists( $key, $totals ) ): - $totals[ $key ] += $value; - endif; - endforeach; - - $team_results = sp_array_value( $results, $team_id, array() ); - - // Find the outcome - if ( array_key_exists( 'outcome', $team_results ) ): - - $value = $team_results['outcome']; - - // Convert to array - if ( ! is_array( $value ) ): - $value = array( $value ); - endif; - - foreach( $value as $outcome ): - - if ( $outcome && $outcome != '-1' ): - - // Increment events attended and outcome count - if ( array_key_exists( $outcome, $totals ) ): - $totals['eventsattended']++; - $totals[ $outcome ]++; - - // Increment events played if active in event - if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): - $totals['eventsplayed']++; - endif; - endif; - - // Add to streak counter - if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $outcome ) ): - $streak['name'] = $outcome; - $streak['count'] ++; - else: - $streak['fire'] = 0; - endif; - - // Add to last 5 counter if sum is less than 5 - if ( array_key_exists( $outcome, $last5 ) && array_sum( $last5 ) < 5 ): - $last5[ $outcome ] ++; - endif; - - // Add to last 10 counter if sum is less than 10 - if ( array_key_exists( $outcome, $last10 ) && array_sum( $last10 ) < 10 ): - $last10[ $outcome ] ++; - endif; - - endif; - - endforeach; - - endif; - endif; - endforeach; - endforeach; - - // Compile streaks counter and add to totals - $args = array( - 'name' => $streak['name'], - 'post_type' => 'sp_outcome', - 'post_status' => 'publish', - 'posts_per_page' => 1 - ); - $outcomes = get_posts( $args ); - - if ( $outcomes ): - $outcome = reset( $outcomes ); - $totals['streak'] = $outcome->post_title . $streak['count']; - endif; - - // Add last counters to totals - $totals['last5'] = $last5; - $totals['last10'] = $last10; - - // Generate array of placeholder values for each league - $placeholders[ $div_id ] = array(); - foreach ( $equations as $key => $value ): - $placeholders[ $div_id ][ $key ] = sp_solve( $value['equation'], $totals, $value['precision'] ); - endforeach; - - endforeach; - - // Get stats from statistic variables - $stats = sp_get_var_labels( 'sp_statistic' ); - - // Merge the data and placeholders arrays - $merged = array(); - - foreach( $placeholders as $season_id => $season_data ): - - if ( ! sp_array_value( $leagues, $season_id, 0 ) ) - continue; - - $season_name = sp_array_value( $season_names, $season_id, ' ' ); - - $team_id = sp_array_value( $leagues, $season_id, array() ); - - if ( ! $team_id || $team_id == '-1' ) - continue; - - $team_name = get_the_title( $team_id ); - - if ( get_option( 'sportspress_player_link_teams', 'no' ) == 'yes' ? true : false ): - $team_permalink = get_permalink( $team_id ); - $team_name = '' . $team_name . ''; - endif; - - // Add season name to row - $merged[ $season_id ] = array( - 'name' => $season_name, - 'team' => $team_name - ); - - foreach( $season_data as $key => $value ): - - // Use static data if key exists and value is not empty, else use placeholder - if ( array_key_exists( $season_id, $data ) && array_key_exists( $key, $data[ $season_id ] ) && $data[ $season_id ][ $key ] != '' ): - $merged[ $season_id ][ $key ] = $data[ $season_id ][ $key ]; - else: - $merged[ $season_id ][ $key ] = $value; - endif; - - endforeach; - - endforeach; - - if ( $admin ): - return array( $stats, $data, $placeholders, $merged, $leagues ); - else: - $labels = array_merge( array( 'name' => SP()->text->string('Season'), 'team' => SP()->text->string('Team') ), $stats ); - $merged[0] = $labels; - return $merged; - endif; - } -} - -/* -if ( !function_exists( 'sp_get_player_statistics_data' ) ) { - function sp_get_player_statistics_data( $post_id, $league_id, $admin = false ) { - - $seasons = (array)get_the_terms( $post_id, 'sp_season' ); - $stats = (array)get_post_meta( $post_id, 'sp_statistics', true ); - $leagues = sp_array_value( (array)get_post_meta( $post_id, 'sp_leagues', true ), $league_id, array() ); - - // Get labels from performance variables - $performance_labels = (array)sp_get_var_labels( 'sp_performance' ); - - // Get labels from outcome variables - $outcome_labels = (array)sp_get_var_labels( 'sp_outcome' ); - - // Generate array of all season ids and season names - $div_ids = array(); - $season_names = array(); - foreach ( $seasons as $season ): - if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ): - $div_ids[] = $season->term_id; - $season_names[ $season->term_id ] = $season->name; - endif; - endforeach; - - $data = array(); - - // Get all seasons populated with data where available - $data = sp_array_combine( $div_ids, sp_array_value( $stats, $league_id, array() ) ); - - // Get equations from statistic variables - $equations = sp_get_var_equations( 'sp_statistic' ); - - // Initialize placeholders array - $placeholders = array(); - - foreach ( $div_ids as $div_id ): - - $totals = array( 'eventsattended' => 0, 'eventsplayed' => 0, 'streak' => 0, 'last5' => null, 'last10' => null ); - - foreach ( $performance_labels as $key => $value ): - $totals[ $key ] = 0; - endforeach; - - foreach ( $outcome_labels as $key => $value ): - $totals[ $key ] = 0; - endforeach; - - // Initialize streaks counter - $streak = array( 'name' => '', 'count' => 0, 'fire' => 1 ); - - // Initialize last counters - $last5 = array(); - $last10 = array(); - - // Add outcome types to last counters - foreach( $outcome_labels as $key => $value ): - $last5[ $key ] = 0; - $last10[ $key ] = 0; - endforeach; - - // Get all events involving the team in current season - $args = array( - 'post_type' => 'sp_event', - 'numberposts' => -1, - 'posts_per_page' => -1, - 'order' => 'ASC', - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'sp_player', - 'value' => $post_id - ), - array( - 'key' => 'sp_format', - 'value' => 'league' - ) - ), - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'sp_league', - 'field' => 'id', - 'terms' => $league_id - ), - array( - 'taxonomy' => 'sp_season', - 'field' => 'id', - 'terms' => $div_id - ), - ) - ); - $events = get_posts( $args ); - - foreach( $events as $event ): - $totals['eventsattended']++; - $results = (array)get_post_meta( $event->ID, 'sp_results', true ); - $team_performance = (array)get_post_meta( $event->ID, 'sp_players', true ); - - // Add all team performance - foreach ( $team_performance as $team_id => $players ): - if ( array_key_exists( $post_id, $players ) ): - - $team_results = sp_array_value( $results, $team_id, array() ); - - // Find the outcome - if ( array_key_exists( 'outcome', $team_results ) ): - - $value = $team_results['outcome']; - - // Convert to array - if ( ! is_array( $value ) ): - $value = array( $value ); - endif; - - foreach( $value as $outcome ): - - // Increment events played and outcome count - if ( array_key_exists( $outcome, $totals ) ): - $totals[ $outcome ]++; - endif; - - if ( $outcome && $outcome != '-1' ): - - // Add to streak counter - if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $outcome ) ): - $streak['name'] = $outcome; - $streak['count'] ++; - else: - $streak['fire'] = 0; - endif; - - // Add to last 5 counter if sum is less than 5 - if ( array_key_exists( $outcome, $last5 ) && array_sum( $last5 ) < 5 ): - $last5[ $outcome ] ++; - endif; - - // Add to last 10 counter if sum is less than 10 - if ( array_key_exists( $outcome, $last10 ) && array_sum( $last10 ) < 10 ): - $last10[ $outcome ] ++; - endif; - - endif; - - endforeach; - - endif; - - $player_performance = sp_array_value( $players, $post_id, array() ); - - // Increment events played if active in event - if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ): - $totals['eventsplayed']++; - endif; - - foreach ( $player_performance as $key => $value ): - if ( array_key_exists( $key, $totals ) ): - $totals[ $key ] += $value; - endif; - endforeach; - endif; - endforeach; - endforeach; - - // Compile streaks counter and add to totals - $args = array( - 'name' => $streak['name'], - 'post_type' => 'sp_outcome', - 'post_status' => 'publish', - 'posts_per_page' => 1 - ); - $outcomes = get_posts( $args ); - - if ( $outcomes ): - $outcome = reset( $outcomes ); - $totals['streak'] = $outcome->post_title . $streak['count']; - endif; - - // Add last counters to totals - $totals['last5'] = $last5; - $totals['last10'] = $last10; - - // Generate array of placeholder values for each league - $placeholders[ $div_id ] = array(); - foreach ( $equations as $key => $value ): - $placeholders[ $div_id ][ $key ] = sp_solve( $value['equation'], $totals, $value['precision'] ); - endforeach; - - endforeach; - - // Get stats from statistic variables - $stats = sp_get_var_labels( 'sp_statistic' ); - - // Merge the data and placeholders arrays - $merged = array(); - - foreach( $placeholders as $season_id => $season_data ): - - if ( ! sp_array_value( $leagues, $season_id, 0 ) ) - continue; - - $season_name = sp_array_value( $season_names, $season_id, ' ' ); - - $team_id = sp_array_value( $leagues, $season_id, array() ); - - if ( ! $team_id || $team_id == '-1' ) - continue; - - $team_name = get_the_title( $team_id ); - - if ( get_option( 'sportspress_player_link_teams', 'no' ) == 'yes' ? true : false ): - $team_permalink = get_permalink( $team_id ); - $team_name = '' . $team_name . ''; - endif; - - // Add season name to row - $merged[ $season_id ] = array( - 'name' => $season_name, - 'team' => $team_name - ); - - foreach( $season_data as $key => $value ): - - // Use static data if key exists and value is not empty, else use placeholder - if ( array_key_exists( $season_id, $data ) && array_key_exists( $key, $data[ $season_id ] ) && $data[ $season_id ][ $key ] != '' ): - $merged[ $season_id ][ $key ] = $data[ $season_id ][ $key ]; - else: - $merged[ $season_id ][ $key ] = $value; - endif; - - endforeach; - - endforeach; - - if ( $admin ): - return array( $stats, $data, $placeholders, $merged, $leagues ); - else: - $labels = array_merge( array( 'name' => SP()->text->string('Season'), 'team' => SP()->text->string('Team') ), $stats ); - $merged[0] = $labels; - return $merged; - endif; - } -} -*/ - if ( !function_exists( 'sp_get_next_event' ) ) { function sp_get_next_event( $args = array() ) { $options = array( @@ -2839,27 +898,6 @@ if ( !function_exists( 'sp_get_next_event' ) ) { } } -if ( !function_exists( 'sp_delete_duplicate_post' ) ) { - function sp_delete_duplicate_post( &$post ) { - global $wpdb; - - $key = isset( $post['sp_key'] ) ? $post['sp_key'] : null; - if ( ! $key ) $key = $post['post_title']; - $id = sp_array_value( $post, 'post_ID', 'var' ); - $title = sp_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; - } -} - /** * Get an array of sport options and settings. * @return array @@ -5152,13 +3190,3 @@ function sp_get_text_options() { asort( $strings ); return $strings; } - -function sp_is_config_type( $typenow = null ) { - if ( $typenow == null ) global $typenow; - - $post_types = array( 'sp_result', 'sp_outcome', 'sp_column', 'sp_performance', 'sp_metric', 'sp_statistic' ); - - if ( in_array( $typenow, $post_types ) ) - return true; - return false; -} diff --git a/includes/sp-deprecated-functions.php b/includes/sp-deprecated-functions.php index 2449582e..f071503d 100644 --- a/includes/sp-deprecated-functions.php +++ b/includes/sp-deprecated-functions.php @@ -155,9 +155,6 @@ function sportspress_sort_list_players ( $a, $b ) { function sportspress_get_player_metrics_data( $post_id ) { return sp_get_player_metrics_data( $post_id ); } -function sportspress_get_player_performance_data( $post_id, $league_id, $admin = false ) { - return sp_get_player_performance_data( $post_id, $league_id, $admin ); -} function sportspress_get_next_event( $args = array() ) { return sp_get_next_event( $args ); } @@ -168,16 +165,30 @@ function sportspress_highlight_admin_menu( $p = 'options-general.php', $s = 'spo return sp_highlight_admin_menu( $p, $s ); } -if ( !function_exists( 'sp_get_player_list_data' ) ) { - function sp_get_player_list_data( $post_id, $admin = false ) { - $formatter = new SP_Data_Formatter; - return $formatter->player_list( $post_id, $admin ); +if ( !function_exists( 'sp_get_team_columns_data' ) ) { + function sp_get_team_columns_data( $post_id, $league_id, $admin = false ) { + $team = new SP_Team( $post_id ); + return $team->data( $league_id, $admin ); } } if ( !function_exists( 'sp_get_league_table_data' ) ) { function sp_get_league_table_data( $post_id, $admin = false ) { - $table = new SP_Table( $post_id ); + $table = new SP_League_Table( $post_id ); return $table->data( $admin ); } } + +if ( !function_exists( 'sp_get_player_statistics_data' ) ) { + function sp_get_player_statistics_data( $post_id, $league_id, $admin = false ) { + $list = new SP_Player_List( $post_id ); + return $list->data( $league_id, $admin ); + } +} + +if ( !function_exists( 'sp_get_player_list_data' ) ) { + function sp_get_player_list_data( $post_id, $admin = false ) { + $list = new SP_Player_List( $post_id ); + return $list->data( $admin ); + } +} diff --git a/includes/sp-template-hooks.php b/includes/sp-template-hooks.php index 3a204f09..9aff1072 100644 --- a/includes/sp-template-hooks.php +++ b/includes/sp-template-hooks.php @@ -101,7 +101,7 @@ function sportspress_gettext( $translated_text, $untranslated_text, $domain ) { global $typenow; if ( is_admin() ): - if ( sp_is_config_type( $typenow ) ): + if ( is_sp_config_type( $typenow ) ): switch ( $untranslated_text ): case 'Slug': $translated_text = ( in_array( $typenow, array( 'sp_column', 'sp_statistic' ) ) ) ? __( 'Key', 'sportspress' ) : __( 'Variable', 'sportspress' ); @@ -146,7 +146,7 @@ function sportspress_pre_get_posts( $query ) { endif; $post_type = $query->query['post_type']; - if ( sp_is_config_type( $post_type ) ): + if ( is_sp_config_type( $post_type ) ): $query->set( 'orderby', 'menu_order' ); $query->set( 'order', 'ASC' ); elseif ( $post_type == 'sp_event' ): @@ -178,7 +178,7 @@ function sportspress_sanitize_title( $title ) { return $title; - elseif ( isset( $_POST ) && array_key_exists( 'post_type', $_POST ) && sp_is_config_type( $_POST['post_type'] ) ): + elseif ( isset( $_POST ) && array_key_exists( 'post_type', $_POST ) && is_sp_config_type( $_POST['post_type'] ) ): $key = isset( $_POST['sp_key'] ) ? $_POST['sp_key'] : null; diff --git a/templates/event-calendar.php b/templates/event-calendar.php index f63f8532..605f7728 100644 --- a/templates/event-calendar.php +++ b/templates/event-calendar.php @@ -25,7 +25,8 @@ $defaults = array( extract( $defaults, EXTR_SKIP ); if ( isset( $id ) ): - $events = sp_get_calendar_data( $id ); + $calendar = new SP_Calendar( $id ); + $events = $calendar->data(); $event_ids = array(); foreach ( $events as $event ): $event_ids[] = $event->ID; diff --git a/templates/event-list.php b/templates/event-list.php index 07f5fe12..25e0b4e6 100644 --- a/templates/event-list.php +++ b/templates/event-list.php @@ -27,7 +27,9 @@ extract( $defaults, EXTR_SKIP ); data(); + $usecolumns = $calendar->columns; if ( isset( $columns ) ) $usecolumns = $columns; diff --git a/templates/league-table.php b/templates/league-table.php index 10b6a59f..0c65c996 100644 --- a/templates/league-table.php +++ b/templates/league-table.php @@ -27,7 +27,7 @@ extract( $defaults, EXTR_SKIP ); $output = '
' . '' . '' . ''; -$table = new SP_Table( $id ); +$table = new SP_League_Table( $id ); $data = $table->data(); diff --git a/templates/player-details.php b/templates/player-details.php index 43b463e7..eb02dc64 100644 --- a/templates/player-details.php +++ b/templates/player-details.php @@ -20,10 +20,12 @@ extract( $defaults, EXTR_SKIP ); $countries = SP()->countries->countries; -$nationality = get_post_meta( $id, 'sp_nationality', true ); -$current_team = get_post_meta( $id, 'sp_current_team', true ); -$past_teams = get_post_meta( $id, 'sp_past_team', false ); -$metrics = sp_get_player_metrics_data( $id ); +$player = new SP_Player( $id ); + +$nationality = $player->nationality; +$current_team = $player->current_team; +$past_teams = $player->past_teams; +$metrics = $player->metrics; $common = array(); if ( $nationality ): diff --git a/templates/player-gallery.php b/templates/player-gallery.php index 12d59bb8..d45554f2 100644 --- a/templates/player-gallery.php +++ b/templates/player-gallery.php @@ -43,7 +43,8 @@ $float = is_rtl() ? 'right' : 'left'; $selector = 'sp-player-gallery-' . $id; -$data = sp_get_player_list_data( $id ); +$list = new SP_Player_List( $id ); +$data = $list->data(); // The first row should be column labels $labels = $data[0]; @@ -51,18 +52,14 @@ $labels = $data[0]; // Remove the first row to leave us with the actual data unset( $data[0] ); -if ( $orderby == 'default' ): - $orderby = get_post_meta( $id, 'sp_orderby', true ); - $order = get_post_meta( $id, 'sp_order', true ); -else: - global $sportspress_performance_priorities; - $sportspress_performance_priorities = array( +if ( $orderby != 'default' ): + $list->priorities = array( array( 'key' => $orderby, 'order' => $order, ), ); - uasort( $data, 'sp_sort_list_players' ); + uasort( $data, array( $list, 'sort' ) ); endif; $gallery_style = $gallery_div = ''; diff --git a/templates/player-list.php b/templates/player-list.php index fb7cb14f..754e8108 100644 --- a/templates/player-list.php +++ b/templates/player-list.php @@ -28,7 +28,8 @@ extract( $defaults, EXTR_SKIP ); $output = '
' . '
' . '' . ''; -$data = sp_get_player_list_data( $id ); +$list = new SP_Player_List( $id ); +$data = $list->data(); // The first row should be column labels $labels = $data[0]; @@ -36,18 +37,14 @@ $labels = $data[0]; // Remove the first row to leave us with the actual data unset( $data[0] ); -if ( $orderby == 'default' ): - $orderby = get_post_meta( $id, 'sp_orderby', true ); - $order = get_post_meta( $id, 'sp_order', true ); -else: - global $sportspress_performance_priorities; - $sportspress_performance_priorities = array( +if ( $orderby != 'default' ): + $list->priorities = array( array( 'key' => $orderby, 'order' => $order, ), ); - uasort( $data, 'sp_sort_list_players' ); + uasort( $data, array( $list, 'sort' ) ); endif; if ( in_array( $orderby, array( 'number', 'name' ) ) ): diff --git a/templates/player-roster.php b/templates/player-roster.php deleted file mode 100644 index d02f83c5..00000000 --- a/templates/player-roster.php +++ /dev/null @@ -1,107 +0,0 @@ - null, - 'orderby' => 'default', - 'order' => 'ASC', -); - -$r = wp_parse_args( $args, $defaults ); - -$output = ''; - -$data = sp_get_player_roster_data( $id ); - -// The first row should be column labels -$labels = $data[0]; - -// Remove the first row to leave us with the actual data -unset( $data[0] ); - -$performance = sp_array_value( $r, 'performance', null ); - -if ( $r['orderby'] == 'default' ): - $r['orderby'] = get_post_meta( $id, 'sp_orderby', true ); - $r['order'] = get_post_meta( $id, 'sp_order', true ); -else: - global $sportspress_performance_priorities; - $sportspress_performance_priorities = array( - array( - 'key' => $r['orderby'], - 'order' => $r['order'], - ), - ); - uasort( $data, 'sp_sort_list_players' ); -endif; - -$positions = get_terms ( 'sp_position' ); - -foreach ( $positions as $position ): - $rows = ''; - $i = 0; - - foreach ( $data as $player_id => $row ): - - if ( ! in_array( $position->term_id, $row['positions']) ) - continue; - - $rows .= ''; - - // Rank or number - if ( isset( $r['orderby'] ) && $r['orderby'] != 'number' ): - $rows .= ''; - else: - $number = get_post_meta( $player_id, 'sp_number', true ); - $rows .= ''; - endif; - - // Name as link - $permalink = get_post_permalink( $player_id ); - $name = sp_array_value( $row, 'name', sp_array_value( $row, 'name', ' ' ) ); - $rows .= ''; - - foreach( $labels as $key => $value ): - if ( $key == 'name' ) - continue; - if ( ! is_array( $performance ) || in_array( $key, $performance ) ) - $rows .= ''; - endforeach; - - $rows .= ''; - - $i++; - - endforeach; - - if ( ! empty( $rows ) ): - $output .= '

' . $position->name . '

'; - $output .= '
' . - '
' . ( $i + 1 ) . '' . ( $number ? $number : ' ' ) . '' . '' . $name . '' . sp_array_value( $row, $key, '—' ) . '
' . '' . ''; - if ( in_array( $r['orderby'], array( 'number', 'name' ) ) ): - $output .= ''; - else: - $output .= ''; - endif; - - foreach( $labels as $key => $label ): - if ( ! is_array( $performance ) || $key == 'name' || in_array( $key, $performance ) ) - $output .= ''; - endforeach; - $output .= '' . '' . '' . $rows . '' . '
#' . SP()->text->string('Rank') . ''. $label . '
' . '
'; - endif; - -endforeach; - -echo apply_filters( 'sportspress_player_roster', $output ); diff --git a/templates/player-statistics.php b/templates/player-statistics.php index ac883103..eecc905e 100644 --- a/templates/player-statistics.php +++ b/templates/player-statistics.php @@ -19,7 +19,8 @@ if ( is_array( $leagues ) ): foreach ( $leagues as $league ): $responsive = get_option( 'sportspress_enable_responsive_tables', 'yes' ) == 'yes' ? true : false; - $data = sp_get_player_statistics_data( $id, $league->term_id ); + $player = new SP_Player( $id ); + $data = $player->data( $league->term_id ); // The first row should be column labels $labels = $data[0]; diff --git a/templates/team-columns.php b/templates/team-columns.php index b9a0fa77..d9793ccc 100644 --- a/templates/team-columns.php +++ b/templates/team-columns.php @@ -22,7 +22,8 @@ $output = ''; // Loop through data for each league foreach ( $leagues as $league ): - $data = sp_get_team_columns_data( $id, $league->term_id ); + $team = new SP_Team( $id ); + $data = $team->data( $league->term_id ); if ( sizeof( $data ) <= 1 ) continue;