Esempio n. 1
0
 public function performance($admin = false)
 {
     $teams = get_post_meta($this->ID, 'sp_team', false);
     $performance = (array) get_post_meta($this->ID, 'sp_players', true);
     $labels = sp_get_var_labels('sp_performance');
     $columns = get_post_meta($this->ID, 'sp_columns', true);
     if (is_array($teams)) {
         foreach ($teams as $i => $team_id) {
             $players = sp_array_between((array) get_post_meta($this->ID, 'sp_player', false), 0, $i);
             $data = sp_array_combine($players, sp_array_value($performance, $team_id, array()));
             $totals = array();
             foreach ($labels as $key => $label) {
                 $totals[$key] = 0;
             }
             foreach ($data as $player_id => $player_performance) {
                 foreach ($labels as $key => $label) {
                     if (array_key_exists($key, $totals)) {
                         $totals[$key] += sp_array_value($player_performance, $key, 0);
                     }
                 }
                 if (!array_key_exists('number', $player_performance)) {
                     $performance[$team_id][$player_id]['number'] = get_post_meta($player_id, 'sp_number', true);
                 }
                 if (!array_key_exists('position', $player_performance) || $player_performance['position'] == null) {
                     $performance[$team_id][$player_id]['position'] = get_post_meta($player_id, 'sp_position', true);
                 }
             }
             foreach ($totals as $key => $value) {
                 $manual_total = sp_array_value(sp_array_value($performance, 0, array()), $key, null);
                 if ($manual_total != null) {
                     $totals[$key] = $manual_total;
                 }
             }
         }
     }
     if ($admin) {
         return array($labels, $columns, $performance, $teams);
     } else {
         // Add position to performance labels
         $labels = array_merge(array('position' => __('Position', 'prosports')), $labels);
         if (is_array($columns)) {
             foreach ($labels as $key => $label) {
                 if (!in_array($key, $columns)) {
                     unset($labels[$key]);
                 }
             }
         }
         // Move individual players to top level of array
         if (array_key_exists(0, $performance)) {
             foreach ($performance[0] as $player_id => $player_performance) {
                 $performance[$player_id] = array($player_id => $player_performance);
             }
         }
         $performance[0] = $labels;
         return $performance;
     }
 }
Esempio n. 2
0
 public function performance($admin = false)
 {
     $teams = get_post_meta($this->ID, 'sp_team', false);
     $performance = (array) get_post_meta($this->ID, 'sp_players', true);
     $labels = apply_filters('sportspress_event_performance_labels', sp_get_var_labels('sp_performance'), $this);
     $columns = get_post_meta($this->ID, 'sp_columns', true);
     if (is_array($teams)) {
         foreach ($teams as $i => $team_id) {
             $players = sp_array_between((array) get_post_meta($this->ID, 'sp_player', false), 0, $i);
             $data = sp_array_combine($players, sp_array_value($performance, $team_id, array()));
             $totals = array();
             foreach ($labels as $key => $label) {
                 $totals[$key] = 0;
             }
             foreach ($data as $player_id => $player_performance) {
                 foreach ($labels as $key => $label) {
                     if (array_key_exists($key, $totals)) {
                         $totals[$key] += sp_array_value($player_performance, $key, 0);
                     }
                 }
                 if (!array_key_exists('number', $player_performance)) {
                     $performance[$team_id][$player_id]['number'] = get_post_meta($player_id, 'sp_number', true);
                 }
                 if (!array_key_exists('position', $player_performance) || $player_performance['position'] == null) {
                     $performance[$team_id][$player_id]['position'] = sp_get_the_term_id($player_id, 'sp_position', null);
                 }
             }
             foreach ($totals as $key => $value) {
                 $manual_total = sp_array_value(sp_array_value($performance, 0, array()), $key, null);
                 if ($manual_total != null) {
                     $totals[$key] = $manual_total;
                 }
             }
         }
     }
     if ($admin) {
         return array($labels, $columns, $performance, $teams);
     } else {
         // Add position to performance labels
         if (taxonomy_exists('sp_position')) {
             $labels = array_merge(array('position' => __('Position', 'sportspress')), $labels);
         }
         if ('manual' == get_option('sportspress_event_performance_columns', 'auto') && is_array($columns)) {
             foreach ($labels as $key => $label) {
                 if (!in_array($key, $columns)) {
                     unset($labels[$key]);
                 }
             }
         }
         if ('no' == get_option('sportspress_event_show_position', 'yes')) {
             unset($labels['position']);
         }
         $performance[0] = $labels;
         return $performance;
     }
 }
Esempio n. 3
0
 public function results($admin = false)
 {
     $teams = (array) get_post_meta($this->ID, 'sp_team', false);
     $results = (array) get_post_meta($this->ID, 'sp_results', true);
     // Get columns from result variables
     $columns = sp_get_var_labels('sp_result');
     // Get result columns to display
     $usecolumns = get_post_meta($this->ID, 'sp_result_columns', true);
     // Get results for all teams
     $data = sp_array_combine($teams, $results, true);
     if ('yes' === get_option('sportspress_event_results_reverse_teams', 'no')) {
         $data = array_reverse($data, true);
     }
     if ($admin) {
         return array($columns, $usecolumns, $data);
     } else {
         // Add outcome to result columns
         $columns['outcome'] = __('Outcome', 'sportspress');
         if (is_array($usecolumns)) {
             if ('manual' == get_option('sportspress_event_result_columns', 'auto')) {
                 foreach ($columns as $key => $label) {
                     if (!in_array($key, $usecolumns)) {
                         unset($columns[$key]);
                     }
                 }
             } else {
                 $active_columns = array();
                 foreach ($data as $team_results) {
                     foreach ($team_results as $key => $result) {
                         if (is_string($result) && strlen($result)) {
                             $active_columns[$key] = $key;
                         }
                     }
                 }
                 $columns = array_intersect_key($columns, $active_columns);
             }
             if ('yes' == get_option('sportspress_event_show_outcome', 'no')) {
                 $columns['outcome'] = __('Outcome', 'sportspress');
             }
         }
         $data[0] = $columns;
         return $data;
     }
 }
 /**
  * Returns formatted data
  *
  * @access public
  * @param bool $admin
  * @return array
  */
 public function data($admin = false)
 {
     $league_ids = sp_get_the_term_ids($this->ID, 'sp_league');
     $season_ids = sp_get_the_term_ids($this->ID, 'sp_season');
     $position_ids = sp_get_the_term_ids($this->ID, 'sp_position');
     $team = get_post_meta($this->ID, 'sp_team', true);
     $list_stats = (array) get_post_meta($this->ID, 'sp_players', 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);
     $select = get_post_meta($this->ID, 'sp_select', 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 labels from result variables
     $result_labels = (array) sp_get_var_labels('sp_result');
     // Get players automatically if set to auto
     if ('auto' == $select) {
         $player_ids = array();
         $args = array('post_type' => 'sp_player', 'numberposts' => -1, 'posts_per_page' => -1, 'meta_key' => 'sp_number', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'tax_query' => array('relation' => 'AND'));
         if ($league_ids) {
             $args['tax_query'][] = array('taxonomy' => 'sp_league', 'field' => 'id', 'terms' => $league_ids);
         }
         if ($season_ids) {
             $args['tax_query'][] = array('taxonomy' => 'sp_season', 'field' => 'id', 'terms' => $season_ids);
         }
         if ($position_ids) {
             $args['tax_query'][] = array('taxonomy' => 'sp_position', 'field' => 'id', 'terms' => $position_ids);
         }
         if ($team && apply_filters('sportspress_has_teams', true)) {
             $args['meta_query'] = array(array('key' => 'sp_team', 'value' => $team));
         }
         $players = get_posts($args);
         if ($players && is_array($players)) {
             foreach ($players as $player) {
                 $player_ids[] = $player->ID;
             }
         }
     } else {
         $player_ids = (array) get_post_meta($this->ID, 'sp_player', false);
     }
     // 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 columns
     $columns = 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;
         }
         // Initialize player totals
         $totals[$player_id] = array('eventsattended' => 0, 'eventsplayed' => 0, 'eventsstarted' => 0, 'eventssubbed' => 0, 'eventminutes' => 0, 'streak' => 0);
         foreach ($performance_labels as $key => $value) {
             $totals[$player_id][$key] = 0;
         }
         foreach ($outcome_labels as $key => $value) {
             $totals[$player_id][$key] = 0;
         }
         foreach ($result_labels as $key => $value) {
             $totals[$player_id][$key . 'for'] = $totals[$player_id][$key . 'against'] = 0;
         }
         // Get metrics
         $metrics = (array) get_post_meta($player_id, 'sp_metrics', true);
         foreach ($metrics as $key => $value) {
             $adjustment = sp_array_value(sp_array_value($adjustments, $player_id, array()), $key, null);
             if ($adjustment != null) {
                 $metrics[$key] += $adjustment;
             }
         }
         // Get static stats
         $static = get_post_meta($player_id, 'sp_statistics', true);
         // Get league and season arrays for static stats
         $static_league_ids = empty($league_ids) ? array(0) : $league_ids;
         $static_season_ids = empty($season_ids) ? array(0) : $season_ids;
         // Add static stats to placeholders
         if ($static_league_ids && $static_season_ids) {
             foreach ($static_league_ids as $league_id) {
                 foreach ($static_season_ids as $season_id) {
                     $player_league_season_stats = sp_array_value(sp_array_value($static, $league_id, array()), $season_id, array());
                     if (is_array($player_league_season_stats)) {
                         foreach ($player_league_season_stats as $key => $value) {
                             $current_value = sp_array_value(sp_array_value($placeholders, $player_id, array()), $key, 0);
                             $placeholders[$player_id][$key] = $current_value + $value;
                         }
                     }
                 }
             }
         } else {
             $placeholders[$player_id] = sp_array_value(sp_array_value($static, 0, array()), 0, array());
         }
         // Add metrics to placeholders
         $placeholders[$player_id] = array_merge($metrics, sp_array_value($placeholders, $player_id, array()));
     }
     $args = array('post_type' => 'sp_event', 'numberposts' => -1, 'posts_per_page' => -1, 'order' => 'DESC', 'meta_query' => array(array('key' => 'sp_format', 'value' => apply_filters('sportspress_competitive_event_formats', array('league')), 'compare' => 'IN')), 'tax_query' => array('relation' => 'AND'));
     if ($league_ids) {
         $args['tax_query'][] = array('taxonomy' => 'sp_league', 'field' => 'id', 'terms' => $league_ids);
     }
     if ($season_ids) {
         $args['tax_query'][] = array('taxonomy' => 'sp_season', 'field' => 'id', 'terms' => $season_ids);
     }
     $args = apply_filters('sportspress_list_data_event_args', $args);
     $events = get_posts($args);
     // Event loop
     foreach ($events as $i => $event) {
         $results = (array) get_post_meta($event->ID, 'sp_results', true);
         $team_performance = get_post_meta($event->ID, 'sp_players', true);
         $minutes = get_post_meta($event->ID, 'sp_minutes', true);
         if ($minutes === '') {
             $minutes = get_option('sportspress_event_minutes', 90);
         }
         // Add all team performance
         if (is_array($team_performance)) {
             foreach ($team_performance as $team_id => $players) {
                 if (is_array($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 ('outcome' == $key) {
                                     // Increment events attended, played, and started
                                     $totals[$player_id]['eventsattended']++;
                                     $totals[$player_id]['eventsplayed']++;
                                     $totals[$player_id]['eventsstarted']++;
                                     $totals[$player_id]['eventminutes'] += $minutes;
                                     // Convert to array
                                     if (!is_array($value)) {
                                         $value = array($value);
                                     }
                                     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][$outcome]++;
                                             }
                                             // 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;
                                             }
                                             // 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]++;
                                             }
                                             // 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]++;
                                             }
                                         }
                                     }
                                 } elseif (array_key_exists($key, $totals[$player_id])) {
                                     $totals[$player_id][$key] += $value;
                                 }
                             }
                             $team_results = sp_array_value($results, $team_id, array());
                             // Loop through home team
                             foreach ($team_results as $result_slug => $team_result) {
                                 if ('outcome' == $result_slug) {
                                     // Increment events attended
                                     $totals[$player_id]['eventsattended']++;
                                     // Continue with incrementing values if active in event
                                     if (sp_array_value($player_performance, 'status') != 'sub' || sp_array_value($player_performance, 'sub', 0)) {
                                         $totals[$player_id]['eventsplayed']++;
                                         $totals[$player_id]['eventminutes'] += $minutes;
                                         if (sp_array_value($player_performance, 'status') == 'lineup') {
                                             $totals[$player_id]['eventsstarted']++;
                                         } elseif (sp_array_value($player_performance, 'status') == 'sub' && sp_array_value($player_performance, 'sub', 0)) {
                                             $totals[$player_id]['eventssubbed']++;
                                         }
                                         $value = $team_result;
                                         // Convert to array
                                         if (!is_array($value)) {
                                             $value = array($value);
                                         }
                                         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][$outcome]++;
                                                 }
                                                 // 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;
                                                 }
                                                 // 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]++;
                                                 }
                                                 // 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]++;
                                                 }
                                             }
                                         }
                                     }
                                 } else {
                                     // Add to total
                                     $value = sp_array_value($totals[$player_id], $result_slug . 'for', 0);
                                     $value += $team_result;
                                     $totals[$player_id][$result_slug . 'for'] = $value;
                                     // Add subset
                                     $totals[$player_id][$result_slug . 'for' . ($i + 1)] = $team_result;
                                 }
                             }
                             // Loop through away teams
                             if (sizeof($results)) {
                                 foreach ($results as $id => $team_results) {
                                     if ($team_id == $id) {
                                         continue;
                                     }
                                     unset($team_results['outcome']);
                                     foreach ($team_results as $result_slug => $team_result) {
                                         // Add to total
                                         $value = sp_array_value($totals[$player_id], $result_slug . 'against', 0);
                                         $value += $team_result;
                                         $totals[$player_id][$result_slug . 'against'] = $value;
                                         // Add subset
                                         $totals[$player_id][$result_slug . 'against' . ($i + 1)] = $team_result;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         $i++;
     }
     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);
                 $abbreviation = sp_get_abbreviation($outcome->ID);
                 if (empty($abbreviation)) {
                     $abbreviation = strtoupper(substr($outcome->post_title, 0, 1));
                 }
                 $totals[$player_id]['streak'] = $abbreviation . $streak['count'];
             } else {
                 $totals[$player_id]['streak'] = null;
             }
         } else {
             $totals[$player_id]['streak'] = null;
         }
     }
     foreach ($last5s as $player_id => $last5) {
         // Add last 5 to totals
         $totals[$player_id]['last5'] = $last5;
     }
     foreach ($last10s as $player_id => $last10) {
         // Add last 10 to totals
         $totals[$player_id]['last10'] = $last10;
     }
     $args = array('post_type' => array('sp_performance', 'sp_metric', 'sp_statistic'), 'numberposts' => -1, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC');
     $stats = get_posts($args);
     foreach ($stats as $stat) {
         // Get post meta
         $meta = get_post_meta($stat->ID);
         // Add equation to object
         if ($stat->post_type == 'sp_metric') {
             $stat->equation = null;
         } else {
             $stat->equation = sp_array_value(sp_array_value($meta, 'sp_equation', array()), 0, 0);
         }
         // Add precision to object
         $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;
     }
     // Fill in empty placeholder values for each player
     foreach ($player_ids as $player_id) {
         if (!$player_id) {
             continue;
         }
         $placeholders[$player_id] = array_merge(sp_array_value($totals, $player_id, array()), array_filter(sp_array_value($placeholders, $player_id, array())));
         foreach ($stats as $stat) {
             if (sp_array_value($placeholders[$player_id], $stat->post_name, '') == '') {
                 if ($stat->equation === null) {
                     $placeholder = sp_array_value(sp_array_value($adjustments, $player_id, array()), $stat->post_name, null);
                     if ($placeholder == null) {
                         $placeholder = '-';
                     }
                 } else {
                     // Solve
                     $placeholder = sp_solve($stat->equation, $placeholders[$player_id], $stat->precision);
                     // Adjustments
                     $adjustment = sp_array_value($adjustments, $player_id, array());
                     if ($adjustment != 0) {
                         $placeholder += sp_array_value($adjustment, $stat->post_name, 0);
                         $placeholder = number_format($placeholder, $stat->precision, '.', '');
                     }
                 }
                 $placeholders[$player_id][$stat->post_name] = $placeholder;
             }
         }
     }
     // Merge the data and placeholders arrays
     $merged = array();
     foreach ($placeholders as $player_id => $player_data) {
         // Add player number and name to row
         $merged[$player_id] = array();
         if (in_array('number', $this->columns)) {
             $player_data['number'] = get_post_meta($player_id, 'sp_number', true);
         }
         $player_data['name'] = get_the_title($player_id);
         if (in_array('team', $this->columns)) {
             $player_data['team'] = get_post_meta($player_id, 'sp_team', true);
         }
         if (in_array('position', $this->columns)) {
             $player_data['position'] = null;
         }
         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;
             }
         }
     }
     if ($orderby != 'number' || $order != 'ASC') {
         $this->priorities = array(array('key' => $orderby, 'order' => $order));
         uasort($merged, array($this, 'sort'));
     }
     // Rearrange data array to reflect values
     $data = array();
     foreach ($merged as $key => $value) {
         $data[$key] = $tempdata[$key];
     }
     if ($admin) {
         $labels = array();
         foreach ($this->columns as $key) {
             if ($key == 'number') {
                 $labels[$key] = '#';
             } elseif ($key == 'team' && apply_filters('sportspress_has_teams', true)) {
                 $labels[$key] = __('Team', 'sportspress');
             } elseif ($key == 'position') {
                 $labels[$key] = __('Position', 'sportspress');
             } elseif (array_key_exists($key, $columns)) {
                 $labels[$key] = $columns[$key];
             }
         }
         return array($labels, $data, $placeholders, $merged, $orderby);
     } else {
         if (!is_array($this->columns)) {
             $this->columns = array();
         }
         foreach ($columns as $key => $label) {
             if (!in_array($key, $this->columns)) {
                 unset($columns[$key]);
             }
         }
         $labels = array();
         if (in_array('number', $this->columns)) {
             $labels['number'] = '#';
         }
         $labels['name'] = __('Player', 'sportspress');
         if (in_array('team', $this->columns) && apply_filters('sportspress_has_teams', true)) {
             $labels['team'] = __('Team', 'sportspress');
         }
         if (in_array('position', $this->columns)) {
             $labels['position'] = __('Position', 'sportspress');
         }
         $merged[0] = array_merge($labels, $columns);
         return $merged;
     }
 }
function prosports_get_var_labels($post_type)
{
    return sp_get_var_labels($post_type);
}
 /**
  * Returns formatted data
  *
  * @access public
  * @param bool $admin
  * @return array
  */
 public function data($admin = false)
 {
     $league_ids = sp_get_the_term_ids($this->ID, 'sp_league');
     $season_ids = sp_get_the_term_ids($this->ID, 'sp_season');
     $table_stats = (array) get_post_meta($this->ID, 'sp_teams', true);
     $usecolumns = get_post_meta($this->ID, 'sp_columns', true);
     $adjustments = get_post_meta($this->ID, 'sp_adjustments', true);
     $select = get_post_meta($this->ID, 'sp_select', 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 teams automatically if set to auto
     if ('auto' == $select) {
         $team_ids = array();
         $args = array('post_type' => 'sp_team', 'numberposts' => -1, 'posts_per_page' => -1, 'order' => 'ASC', 'tax_query' => array('relation' => 'AND'));
         if ($league_ids) {
             $args['tax_query'][] = array('taxonomy' => 'sp_league', 'field' => 'id', 'terms' => $league_ids);
         }
         if ($season_ids) {
             $args['tax_query'][] = array('taxonomy' => 'sp_season', 'field' => 'id', 'terms' => $season_ids);
         }
         $teams = get_posts($args);
         if ($teams && is_array($teams)) {
             foreach ($teams as $team) {
                 $team_ids[] = $team->ID;
             }
         }
     } else {
         $team_ids = (array) get_post_meta($this->ID, 'sp_team', false);
     }
     // 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 incremental counter
     $this->pos = 0;
     $this->counter = 0;
     // Initialize team compare
     $this->compare = null;
     // Initialize streaks counter
     $streaks = array();
     // Initialize last counters
     $last5s = array();
     $last10s = array();
     // Initialize record counters
     $homerecords = array();
     $awayrecords = 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();
         // Initialize team record counters
         $homerecords[$team_id] = array();
         $awayrecords[$team_id] = array();
         // Add outcome types to team last and record counters
         foreach ($outcome_labels as $key => $value) {
             $last5s[$team_id][$key] = 0;
             $last10s[$team_id][$key] = 0;
             $homerecords[$team_id][$key] = 0;
             $awayrecords[$team_id][$key] = 0;
         }
         // Initialize team totals
         $totals[$team_id] = array('eventsplayed' => 0, 'eventsplayed_home' => 0, 'eventsplayed_away' => 0, 'eventsplayed_venue' => 0, 'eventminutes' => 0, 'eventminutes_home' => 0, 'eventminutes_away' => 0, 'eventminutes_venue' => 0, 'streak' => 0, 'streak_home' => 0, 'streak_away' => 0, 'streak_venue' => 0);
         foreach ($result_labels as $key => $value) {
             $totals[$team_id][$key . 'for'] = 0;
             $totals[$team_id][$key . 'for_home'] = 0;
             $totals[$team_id][$key . 'for_away'] = 0;
             $totals[$team_id][$key . 'for_venue'] = 0;
             $totals[$team_id][$key . 'against'] = 0;
             $totals[$team_id][$key . 'against_home'] = 0;
             $totals[$team_id][$key . 'against_away'] = 0;
             $totals[$team_id][$key . 'against_venue'] = 0;
         }
         foreach ($outcome_labels as $key => $value) {
             $totals[$team_id][$key] = 0;
             $totals[$team_id][$key . '_home'] = 0;
             $totals[$team_id][$key . '_away'] = 0;
             $totals[$team_id][$key . '_venue'] = 0;
         }
         // Get static stats
         $static = get_post_meta($team_id, 'sp_columns', true);
         if ('yes' == get_option('sportspress_team_column_editing', 'no') && $league_ids && $season_ids) {
             // Add static stats to placeholders
             foreach ($league_ids as $league_id) {
                 foreach ($season_ids as $season_id) {
                     $placeholders[$team_id] = (array) sp_array_value(sp_array_value($static, $league_id, array()), $season_id, array());
                 }
             }
         }
     }
     $args = array('post_type' => 'sp_event', 'post_status' => array('publish', 'future'), 'numberposts' => -1, 'posts_per_page' => -1, 'orderby' => 'post_date', 'order' => 'DESC', 'meta_query' => array(array('key' => 'sp_format', 'value' => apply_filters('sportspress_competitive_event_formats', array('league')), 'compare' => 'IN')), 'tax_query' => array('relation' => 'AND'));
     if ($league_ids) {
         $args['tax_query'][] = array('taxonomy' => 'sp_league', 'field' => 'id', 'terms' => $league_ids);
     }
     if ($season_ids) {
         $args['tax_query'][] = array('taxonomy' => 'sp_season', 'field' => 'id', 'terms' => $season_ids);
     }
     $args = apply_filters('sportspress_table_data_event_args', $args);
     $events = get_posts($args);
     $e = 0;
     // Event loop
     foreach ($events as $event) {
         $results = (array) get_post_meta($event->ID, 'sp_results', true);
         $minutes = get_post_meta($event->ID, 'sp_minutes', true);
         if ($minutes === '') {
             $minutes = get_option('sportspress_event_minutes', 90);
         }
         $i = 0;
         foreach ($results as $team_id => $team_result) {
             if (!in_array($team_id, $team_ids)) {
                 continue;
             }
             if ($team_result) {
                 foreach ($team_result as $key => $value) {
                     if ($key == 'outcome') {
                         if (!is_array($value)) {
                             $value = array($value);
                         }
                         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]['eventminutes'] += $minutes;
                                 $totals[$team_id][$outcome]++;
                                 // Add to home or away stats
                                 if (0 === $i) {
                                     $totals[$team_id]['eventsplayed_home']++;
                                     $totals[$team_id]['eventminutes_home'] += $minutes;
                                     $totals[$team_id][$outcome . '_home']++;
                                 } else {
                                     $totals[$team_id]['eventsplayed_away']++;
                                     $totals[$team_id]['eventminutes_away'] += $minutes;
                                     $totals[$team_id][$outcome . '_away']++;
                                 }
                                 // Add to venue stats
                                 if (sp_is_home_venue($team_id, $event->ID)) {
                                     $totals[$team_id]['eventsplayed_venue']++;
                                     $totals[$team_id]['eventminutes_venue'] += $minutes;
                                     $totals[$team_id][$outcome . '_venue']++;
                                 }
                             }
                             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;
                                 }
                                 // 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]++;
                                 }
                                 // 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]++;
                                 }
                                 // Add to home or away record
                                 if (0 === $i) {
                                     if (array_key_exists($team_id, $homerecords) && array_key_exists($outcome, $homerecords[$team_id])) {
                                         $homerecords[$team_id][$outcome]++;
                                     }
                                 } else {
                                     if (array_key_exists($team_id, $awayrecords) && array_key_exists($outcome, $awayrecords[$team_id])) {
                                         $awayrecords[$team_id][$outcome]++;
                                     }
                                 }
                             }
                         }
                     } 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;
                             $totals[$team_id][$key . 'for' . ($e + 1)] = $value;
                             // Add to home or away stats
                             if (0 === $i) {
                                 $totals[$team_id][$key . 'for_home'] += $value;
                             } else {
                                 $totals[$team_id][$key . 'for_away'] += $value;
                             }
                             // Add to venue stats
                             if (sp_is_home_venue($team_id, $event->ID)) {
                                 $totals[$team_id][$key . 'for_venue'] += $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);
                                     $totals[$team_id][$key . 'against' . ($e + 1)] = sp_array_value($other_result, $key, 0);
                                     // Add to home or away stats
                                     if (0 === $i) {
                                         $totals[$team_id][$key . 'against_home'] += sp_array_value($other_result, $key, 0);
                                     } else {
                                         $totals[$team_id][$key . 'against_away'] += sp_array_value($other_result, $key, 0);
                                     }
                                     // Add to venue stats
                                     if (sp_is_home_venue($team_id, $event->ID)) {
                                         $totals[$team_id][$key . 'against_venue'] += sp_array_value($other_result, $key, 0);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             $i++;
         }
         $e++;
     }
     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, 'orderby' => 'menu_order', 'order' => 'ASC');
             $outcomes = get_posts($args);
             if ($outcomes) {
                 $outcome = reset($outcomes);
                 $abbreviation = get_post_meta($outcome->ID, 'sp_abbreviation', true);
                 if (!$abbreviation) {
                     $abbreviation = substr($outcome->post_title, 0, 1);
                 }
                 $totals[$team_id]['streak'] = $abbreviation . $streak['count'];
             } else {
                 $totals[$team_id]['streak'] = null;
             }
         } else {
             $totals[$team_id]['streak'] = null;
         }
     }
     foreach ($last5s as $team_id => $last5) {
         // Add last 5 to totals
         $totals[$team_id]['last5'] = $last5;
     }
     foreach ($last10s as $team_id => $last10) {
         // Add last 10 to totals
         $totals[$team_id]['last10'] = $last10;
     }
     foreach ($homerecords as $team_id => $homerecord) {
         // Add home record to totals
         $totals[$team_id]['homerecord'] = $homerecord;
     }
     foreach ($awayrecords as $team_id => $awayrecord) {
         // Add away record to totals
         $totals[$team_id]['awayrecord'] = $awayrecord;
     }
     $args = array('post_type' => 'sp_column', 'numberposts' => -1, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC');
     $stats = get_posts($args);
     $columns = array();
     $this->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, null);
         $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->priorities)) {
             $this->priorities[$priority] = array('column' => $stat->post_name, 'order' => sp_array_value(sp_array_value($meta, 'sp_order', array()), 0, 'DESC'));
         }
     }
     // Sort priorities in descending order
     ksort($this->priorities);
     // Initialize games back column variable
     $gb_column = null;
     // 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(sp_array_value($placeholders, $team_id, array()), $stat->post_name, '') == '') {
                 if ($stat->equation == null) {
                     $placeholder = sp_array_value(sp_array_value($adjustments, $team_id, array()), $stat->post_name, null);
                     if ($placeholder == null) {
                         $placeholder = '-';
                     }
                 } else {
                     // Solve
                     $placeholder = sp_solve($stat->equation, sp_array_value($totals, $team_id, array()), $stat->precision);
                     if ('$gamesback' == $stat->equation) {
                         $gb_column = $stat->post_name;
                     }
                     if (!in_array($stat->equation, array('$gamesback', '$streak', '$last5', '$last10', '$homerecord', '$awayrecord'))) {
                         // Adjustments
                         $adjustment = sp_array_value($adjustments, $team_id, array());
                         if ($adjustment != 0) {
                             $placeholder += sp_array_value($adjustment, $stat->post_name, 0);
                             $placeholder = number_format($placeholder, $stat->precision, '.', '');
                         }
                     }
                 }
                 $placeholders[$team_id][$stat->post_name] = $placeholder;
             }
         }
     }
     // Find win and loss variables for games back
     $w = $l = null;
     if ($gb_column) {
         $args = array('post_type' => 'sp_outcome', 'numberposts' => 1, 'posts_per_page' => 1, 'meta_query' => array(array('key' => 'sp_condition', 'value' => '>')));
         $outcomes = get_posts($args);
         if ($outcomes) {
             $outcome = reset($outcomes);
             if (is_array($stats)) {
                 foreach ($stats as $stat) {
                     if ('$' . $outcome->post_name == $stat->equation) {
                         $w = $stat->post_name;
                     }
                 }
             }
         }
         // Calculate games back
         $args = array('post_type' => 'sp_outcome', 'numberposts' => 1, 'posts_per_page' => 1, 'meta_query' => array(array('key' => 'sp_condition', 'value' => '<')));
         $outcomes = get_posts($args);
         if ($outcomes) {
             $outcome = reset($outcomes);
             if (is_array($stats)) {
                 foreach ($stats as $stat) {
                     if ('$' . $outcome->post_name == $stat->equation) {
                         $l = $stat->post_name;
                     }
                 }
             }
         }
     }
     // 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;
             }
         }
     }
     uasort($merged, array($this, 'sort'));
     // Calculate position of teams for ties
     foreach ($merged as $team_id => $team_columns) {
         $merged[$team_id]['pos'] = $this->calculate_pos($team_columns);
     }
     // Rearrange data array to reflect values
     $data = array();
     foreach ($merged as $key => $value) {
         $data[$key] = $tempdata[$key];
     }
     if ($admin) {
         $this->add_gb($placeholders, $w, $l, $gb_column);
         return array($columns, $usecolumns, $data, $placeholders, $merged);
     } else {
         $this->add_gb($merged, $w, $l, $gb_column);
         if (!is_array($usecolumns)) {
             $usecolumns = array();
         }
         $labels = array_merge(array('pos' => __('Pos', 'sportspress'), 'name' => __('Team', 'sportspress')), $columns);
         $merged[0] = $labels;
         return $merged;
     }
 }
 /**
  * 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');
     $metrics = (array) get_post_meta($this->ID, 'sp_metrics', true);
     $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());
     $usecolumns = get_post_meta($this->ID, 'sp_columns', 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 labels from result variables
     $result_labels = (array) sp_get_var_labels('sp_result');
     // 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;
         }
     }
     $div_ids[] = 0;
     $season_names[0] = __('Total', 'sportspress');
     $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, 'eventsstarted' => 0, 'eventssubbed' => 0, 'eventminutes' => 0, 'streak' => 0, 'last5' => null, 'last10' => null);
         foreach ($performance_labels as $key => $value) {
             $totals[$key] = 0;
         }
         foreach ($outcome_labels as $key => $value) {
             $totals[$key] = 0;
         }
         foreach ($result_labels as $key => $value) {
             $totals[$key . 'for'] = $totals[$key . 'against'] = 0;
         }
         // 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;
         }
         // Get all events involving the team in current season
         $args = array('post_type' => 'sp_event', 'numberposts' => -1, 'posts_per_page' => -1, 'order' => 'DESC', 'meta_query' => array('relation' => 'AND', array('key' => 'sp_player', 'value' => $this->ID), array('key' => 'sp_format', 'value' => apply_filters('sportspress_competitive_event_formats', array('league')), 'compare' => 'IN')), 'tax_query' => array('relation' => 'AND'));
         if ($league_id) {
             $args['tax_query'][] = array('taxonomy' => 'sp_league', 'field' => 'id', 'terms' => $league_id);
         }
         if ($div_id) {
             $args['tax_query'][] = array('taxonomy' => 'sp_season', 'field' => 'id', 'terms' => $div_id);
         }
         $args = apply_filters('sportspress_player_data_event_args', $args);
         $events = get_posts($args);
         // Event loop
         foreach ($events as $i => $event) {
             $results = (array) get_post_meta($event->ID, 'sp_results', true);
             $team_performance = (array) get_post_meta($event->ID, 'sp_players', true);
             $minutes = get_post_meta($event->ID, 'sp_minutes', true);
             if ($minutes === '') {
                 $minutes = get_option('sportspress_event_minutes', 90);
             }
             // Add all team performance
             foreach ($team_performance as $team_id => $players) {
                 if (is_array($players) && array_key_exists($this->ID, $players)) {
                     $player_performance = sp_array_value($players, $this->ID, array());
                     foreach ($player_performance as $key => $value) {
                         if ('outcome' == $key) {
                             // Increment events attended, played, and started
                             $totals['eventsattended']++;
                             $totals['eventsplayed']++;
                             $totals['eventsstarted']++;
                             $totals['eventminutes'] += $minutes;
                             // Convert to array
                             if (!is_array($value)) {
                                 $value = array($value);
                             }
                             foreach ($value as $outcome) {
                                 if ($outcome && $outcome != '-1') {
                                     // Increment outcome count
                                     if (array_key_exists($outcome, $totals)) {
                                         $totals[$outcome]++;
                                     }
                                     // Add to streak counter
                                     if ($streak['fire'] && ($streak['name'] == '' || $streak['name'] == $outcome)) {
                                         $streak['name'] = $outcome;
                                         $streak['count']++;
                                     } else {
                                         $streak['fire'] = 0;
                                     }
                                     // Add to last 5 counter if sum is less than 5
                                     if (array_key_exists($outcome, $last5) && array_sum($last5) < 5) {
                                         $last5[$outcome]++;
                                     }
                                     // Add to last 10 counter if sum is less than 10
                                     if (array_key_exists($outcome, $last10) && array_sum($last10) < 10) {
                                         $last10[$outcome]++;
                                     }
                                 }
                             }
                         } elseif (array_key_exists($key, $totals)) {
                             $totals[$key] += $value;
                         }
                     }
                     $team_results = sp_array_value($results, $team_id, array());
                     unset($results[$team_id]);
                     // Loop through home team
                     foreach ($team_results as $result_slug => $team_result) {
                         if ('outcome' == $result_slug) {
                             // Increment events attended
                             $totals['eventsattended']++;
                             // Continue with incrementing values if active in event
                             if (sp_array_value($player_performance, 'status') != 'sub' || sp_array_value($player_performance, 'sub', 0)) {
                                 $totals['eventsplayed']++;
                                 $totals['eventminutes'] += $minutes;
                                 if (sp_array_value($player_performance, 'status') == 'lineup') {
                                     $totals['eventsstarted']++;
                                 } elseif (sp_array_value($player_performance, 'status') == 'sub' && sp_array_value($player_performance, 'sub', 0)) {
                                     $totals['eventssubbed']++;
                                 }
                                 $value = $team_result;
                                 // Convert to array
                                 if (!is_array($value)) {
                                     $value = array($value);
                                 }
                                 foreach ($value as $outcome) {
                                     if ($outcome && $outcome != '-1') {
                                         // Increment outcome count
                                         if (array_key_exists($outcome, $totals)) {
                                             $totals[$outcome]++;
                                         }
                                         // Add to streak counter
                                         if ($streak['fire'] && ($streak['name'] == '' || $streak['name'] == $outcome)) {
                                             $streak['name'] = $outcome;
                                             $streak['count']++;
                                         } else {
                                             $streak['fire'] = 0;
                                         }
                                         // Add to last 5 counter if sum is less than 5
                                         if (array_key_exists($outcome, $last5) && array_sum($last5) < 5) {
                                             $last5[$outcome]++;
                                         }
                                         // Add to last 10 counter if sum is less than 10
                                         if (array_key_exists($outcome, $last10) && array_sum($last10) < 10) {
                                             $last10[$outcome]++;
                                         }
                                     }
                                 }
                             }
                         } else {
                             // Add to total
                             $value = sp_array_value($totals, $result_slug . 'for', 0);
                             $value += $team_result;
                             $totals[$result_slug . 'for'] = $value;
                             // Add subset
                             $totals[$result_slug . 'for' . ($i + 1)] = $team_result;
                         }
                     }
                     // Loop through away teams
                     if (sizeof($results)) {
                         foreach ($results as $team_results) {
                             unset($team_results['outcome']);
                             foreach ($team_results as $result_slug => $team_result) {
                                 // Add to total
                                 $value = sp_array_value($totals, $result_slug . 'against', 0);
                                 $value += $team_result;
                                 $totals[$result_slug . 'against'] = $value;
                                 // Add subset
                                 $totals[$result_slug . 'against' . ($i + 1)] = $team_result;
                             }
                         }
                     }
                 }
             }
             $i++;
         }
         // 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);
             $abbreviation = sp_get_abbreviation($outcome->ID);
             if (empty($abbreviation)) {
                 $abbreviation = strtoupper(substr($outcome->post_title, 0, 1));
             }
             $totals['streak'] = $abbreviation . $streak['count'];
         }
         // Add last counters to totals
         $totals['last5'] = $last5;
         $totals['last10'] = $last10;
         // Add metrics to totals
         $totals = array_merge($metrics, $totals);
         // 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']);
         }
         foreach ($performance_labels as $key => $label) {
             $placeholders[$div_id][$key] = sp_array_value($totals, $key, 0);
         }
     }
     // 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 (-1 == sp_array_value($leagues, $season_id, 0)) {
             continue;
         }
         $season_name = sp_array_value($season_names, $season_id, '&nbsp;');
         $team_id = sp_array_value($leagues, $season_id, array());
         if (-1 == $team_id) {
             continue;
         }
         if ($team_id) {
             $team_name = get_the_title($team_id);
             if (get_option('sportspress_link_teams', 'no') == 'yes' ? true : false) {
                 $team_permalink = get_permalink($team_id);
                 $team_name = '<a href="' . $team_permalink . '">' . $team_name . '</a>';
             }
         } else {
             $team_name = __('Total', 'sportspress');
         }
         // 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;
             }
         }
     }
     $columns = array_merge($performance_labels, $stats);
     if ($admin) {
         $labels = array();
         if (is_array($usecolumns)) {
             foreach ($usecolumns as $key) {
                 if ($key == 'team') {
                     $labels[$key] = __('Team', 'sportspress');
                 } elseif (array_key_exists($key, $columns)) {
                     $labels[$key] = $columns[$key];
                 }
             }
         }
         return array($labels, $data, $placeholders, $merged, $leagues);
     } else {
         if (!is_array($this->columns)) {
             $this->columns = array();
         }
         foreach ($columns as $key => $label) {
             if (!in_array($key, $this->columns)) {
                 unset($columns[$key]);
             }
         }
         if (!is_array($usecolumns)) {
             $usecolumns = array();
         }
         foreach ($columns as $key => $label) {
             if (!in_array($key, $usecolumns)) {
                 unset($columns[$key]);
             }
         }
         $labels = array('name' => __('Season', 'sportspress'));
         if (in_array('team', $this->columns)) {
             $labels['team'] = __('Team', 'sportspress');
         }
         $merged[0] = array_merge($labels, $columns);
         return $merged;
     }
 }
        /**
         * import function.
         *
         * @access public
         * @param array $array
         * @param array $columns
         * @return void
         */
        function import($array = array(), $columns = array('post_title'))
        {
            $this->imported = $this->skipped = 0;
            if (!is_array($array) || !sizeof($array)) {
                $this->footer();
                die;
            }
            $rows = array_chunk($array, sizeof($columns));
            // Get event format, league, and season from post vars
            $event_format = empty($_POST['sp_format']) ? false : $_POST['sp_format'];
            $league = sp_array_value($_POST, 'sp_league', '-1') == '-1' ? false : $_POST['sp_league'];
            $season = sp_array_value($_POST, 'sp_season', '-1') == '-1' ? false : $_POST['sp_season'];
            $date_format = empty($_POST['sp_date_format']) ? 'yyyy/mm/dd' : $_POST['sp_date_format'];
            // Get labels from result and performance post types
            $result_labels = sp_get_var_labels('sp_result');
            $performance_labels = sp_get_var_labels('sp_performance');
            foreach ($rows as $row) {
                $row = array_filter($row);
                if (empty($row)) {
                    continue;
                }
                $meta = array();
                foreach ($columns as $index => $key) {
                    $meta[$key] = sp_array_value($row, $index);
                }
                // Slice array into event, team, and player
                $event = array_slice($row, 0, 3);
                $team = array_slice($row, 3, 3);
                $player = array_slice($row, 6);
                // Get event details
                $event = array(sp_array_value($meta, 'post_date'), sp_array_value($meta, 'post_time'), sp_array_value($meta, 'sp_venue'));
                unset($meta['post_date']);
                unset($meta['post_time']);
                unset($meta['sp_venue']);
                // Get team results
                $team = array(sp_array_value($meta, 'sp_team'), sp_array_value($meta, 'sp_results'), sp_array_value($meta, 'sp_outcome'));
                unset($meta['sp_team']);
                unset($meta['sp_results']);
                unset($meta['sp_outcome']);
                // Get player performance
                $player = array(sp_array_value($meta, 'sp_player'));
                unset($meta['sp_player']);
                foreach ($performance_labels as $key => $label) {
                    $player[] = sp_array_value($meta, $key, '');
                }
                // Add new event if date is given
                if (sizeof($event) > 0 && !empty($event[0])) {
                    // Add player performance to last event if available
                    if (isset($id) && isset($players) && sizeof($players) > 0) {
                        update_post_meta($id, 'sp_players', $players);
                    }
                    // List event columns
                    list($date, $time, $venue) = $event;
                    // Format date
                    $date = str_replace('/', '-', trim($date));
                    $date_array = explode('-', $date);
                    switch ($date_format) {
                        case 'dd/mm/yyyy':
                            $date = substr(str_pad(sp_array_value($date_array, 2, '0000'), 4, '0', STR_PAD_LEFT), 0, 4) . '-' . substr(str_pad(sp_array_value($date_array, 1, '00'), 2, '0', STR_PAD_LEFT), 0, 2) . '-' . substr(str_pad(sp_array_value($date_array, 0, '00'), 2, '0', STR_PAD_LEFT), 0, 2);
                            break;
                        case 'mm/dd/yyyy':
                            $date = substr(str_pad(sp_array_value($date_array, 2, '0000'), 4, '0', STR_PAD_LEFT), 0, 4) . '-' . substr(str_pad(sp_array_value($date_array, 0, '00'), 2, '0', STR_PAD_LEFT), 0, 2) . '-' . substr(str_pad(sp_array_value($date_array, 1, '00'), 2, '0', STR_PAD_LEFT), 0, 2);
                            break;
                        default:
                            $date = substr(str_pad(sp_array_value($date_array, 0, '0000'), 4, '0', STR_PAD_LEFT), 0, 4) . '-' . substr(str_pad(sp_array_value($date_array, 1, '00'), 2, '0', STR_PAD_LEFT), 0, 2) . '-' . substr(str_pad(sp_array_value($date_array, 2, '00'), 2, '0', STR_PAD_LEFT), 0, 2);
                    }
                    // Add time to date if given
                    if (!empty($time)) {
                        $date .= ' ' . trim($time);
                    }
                    // Define post type args
                    $args = array('post_type' => 'sp_event', 'post_status' => 'publish', 'post_date' => $date);
                    // Insert event
                    $id = wp_insert_post($args);
                    // Initialize performance array
                    $players = array();
                    // Flag as import
                    update_post_meta($id, '_sp_import', 1);
                    // Update event format
                    if ($event_format) {
                        update_post_meta($id, 'sp_format', $event_format);
                    }
                    // Update league
                    if ($league) {
                        wp_set_object_terms($id, $league, 'sp_league', false);
                    }
                    // Update season
                    if ($season) {
                        wp_set_object_terms($id, $season, 'sp_season', false);
                    }
                    // Update venue
                    wp_set_object_terms($id, $venue, 'sp_venue', false);
                    // Increment
                    $this->imported++;
                }
                // Add new team if team name is given
                if (sizeof($team) > 0 && !empty($team[0])) {
                    // List team columns
                    list($team_name, $result, $outcome) = $team;
                    // Find out if team exists
                    $team_object = get_page_by_title($team_name, OBJECT, 'sp_team');
                    // Get or insert team
                    if ($team_object) {
                        // Make sure team is published
                        if ($team_object->post_status != 'publish') {
                            wp_update_post(array('ID' => $team_object->ID, 'post_status' => 'publish'));
                        }
                        // Get team ID
                        $team_id = $team_object->ID;
                    } else {
                        // Insert team
                        $team_id = wp_insert_post(array('post_type' => 'sp_team', 'post_status' => 'publish', 'post_title' => $team_name));
                        // Flag as import
                        update_post_meta($team_id, '_sp_import', 1);
                    }
                    // Update league
                    if ($league) {
                        wp_set_object_terms($team_id, $league, 'sp_league', true);
                    }
                    // Update season
                    if ($season) {
                        wp_set_object_terms($team_id, $season, 'sp_season', true);
                    }
                    // Add to event if exists
                    if (isset($id)) {
                        // Add team to event
                        add_post_meta($id, 'sp_team', $team_id);
                        // Add empty player to event
                        add_post_meta($id, 'sp_player', 0);
                        // Explode results into array
                        $results = explode('|', $result);
                        // Create team results array from result keys
                        $team_results = array();
                        if (sizeof($result_labels) > 0) {
                            foreach ($result_labels as $key => $label) {
                                $team_results[$key] = trim(array_shift($results));
                            }
                            $team_results['outcome'] = array();
                        }
                        // Explode outcomes into array
                        $outcomes = explode('|', $outcome);
                        // Add outcome slugs to team outcomes array
                        foreach ($outcomes as $outcome) {
                            // Continue if outcome doesn't exist
                            if ($outcome == null) {
                                continue;
                            }
                            // Remove whitespace
                            $outcome = trim($outcome);
                            // Get or insert outcome
                            $outcome_object = get_page_by_title($outcome, OBJECT, 'sp_outcome');
                            if ($outcome_object) {
                                // Make sure outcome is published
                                if ($outcome_object->post_status != 'publish') {
                                    wp_update_post(array('ID' => $outcome_object->ID, 'post_status' => 'publish'));
                                }
                                // Get outcome slug
                                $outcome_slug = $outcome_object->post_name;
                            } else {
                                // Insert outcome
                                $outcome_id = wp_insert_post(array('post_type' => 'sp_outcome', 'post_status' => 'publish', 'post_title' => $outcome));
                                // Get outcome slug
                                $post_data = get_post($outcome_id, ARRAY_A);
                                $outcome_slug = $post_data['post_name'];
                                // Flag as import
                                update_post_meta($outcome_id, '_sp_import', 1);
                            }
                            // Add to team results array
                            $team_results['outcome'][] = $outcome_slug;
                        }
                        // Get existing results
                        $event_results = get_post_meta($id, 'sp_results', true);
                        // Create new array if results not exists
                        if (!$event_results) {
                            $event_results = array();
                        }
                        // Add team results to existing results
                        $event_results[$team_id] = $team_results;
                        // Update event results
                        update_post_meta($id, 'sp_results', $event_results);
                        // Get event name
                        $title = get_the_title($id);
                        // Add delimiter if event name is set
                        if ($title) {
                            $title .= ' ' . get_option('sportspress_event_teams_delimiter', 'vs') . ' ';
                        }
                        // Append team name to event name
                        $title .= $team_name;
                        // Update event with new name
                        $post = array('ID' => $id, 'post_title' => $title);
                        wp_update_post($post);
                    }
                }
                // Add new player if player name is given
                if (sizeof($player) > 0 && !empty($player[0])) {
                    // Get and unset player name leaving us with the performance
                    $player_name = $player[0];
                    unset($player[0]);
                    // Find out if player exists
                    $player_object = get_page_by_title($player_name, OBJECT, 'sp_player');
                    // Get or insert player
                    if ($player_object) {
                        // Make sure player is published
                        if ($player_object->post_status != 'publish') {
                            wp_update_post(array('ID' => $player_object->ID, 'post_status' => 'publish'));
                        }
                        // Get player ID
                        $player_id = $player_object->ID;
                        // Get player number
                        $player_number = get_post_meta($player_id, 'sp_number', true);
                    } else {
                        // Insert player
                        $player_id = wp_insert_post(array('post_type' => 'sp_player', 'post_status' => 'publish', 'post_title' => $player_name));
                        // Flag as import
                        update_post_meta($player_id, '_sp_import', 1);
                        // Update number
                        update_post_meta($player_id, 'sp_number', null);
                        // Get player number
                        $player_number = null;
                    }
                    // Update league
                    if ($league) {
                        wp_set_object_terms($player_id, $league, 'sp_league', true);
                    }
                    // Update season
                    if ($season) {
                        wp_set_object_terms($player_id, $season, 'sp_season', true);
                    }
                    // Add to event if exists
                    if (isset($id)) {
                        // Add player to event
                        add_post_meta($id, 'sp_player', $player_id);
                        // Add player performance to array if team is available
                        if (isset($team_id)) {
                            // Initialize performance array
                            $performance = array('number' => $player_number);
                            // Map keys to player performance
                            foreach ($performance_labels as $key => $label) {
                                $performance[$key] = array_shift($player);
                            }
                            $players[$team_id][$player_id] = $performance;
                            // Get player teams
                            $player_teams = get_post_meta($player_id, 'sp_team', false);
                            $current_team = get_post_meta($player_id, 'sp_current_team', true);
                            $past_teams = get_post_meta($player_id, 'sp_past_team', false);
                            // Add team if not exists in player
                            if (!in_array($team_id, $player_teams)) {
                                add_post_meta($player_id, 'sp_team', $team_id);
                            }
                            // Add as past team or set current team if not set
                            if (!$current_team) {
                                update_post_meta($player_id, 'sp_current_team', $team_id);
                            } elseif ($current_team != $team_id && !in_array($team_id, $past_teams)) {
                                add_post_meta($player_id, 'sp_past_team', $team_id);
                            }
                        }
                    }
                }
            }
            // Add player performance to last event if available
            if (isset($id) && isset($players) && sizeof($players) > 0) {
                update_post_meta($id, 'sp_players', $players);
            }
            // Show Result
            echo '<div class="updated settings-error below-h2"><p>
				' . sprintf(__('Import complete - imported <strong>%s</strong> events and skipped <strong>%s</strong>.', 'sportspress'), $this->imported, $this->skipped) . '
			</p></div>';
            $this->import_end();
        }
 /**
  * Returns formatted data
  *
  * @access public
  * @param bool $admin
  * @return array
  */
 public function columns($league_id)
 {
     $seasons = (array) get_the_terms($this->ID, 'sp_season');
     $columns = (array) get_post_meta($this->ID, 'sp_columns', 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');
     // 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;
         }
     }
     $div_ids[] = 0;
     $season_names[0] = __('Total', 'sportspress');
     $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, 'eventsplayed_home' => 0, 'eventsplayed_away' => 0, 'eventsplayed_venue' => 0, 'eventminutes' => 0, 'eventminutes_home' => 0, 'eventminutes_away' => 0, 'eventminutes_venue' => 0, 'streak' => 0, 'streak_home' => 0, 'streak_away' => 0, 'streak_venue' => 0, 'last5' => null, 'last10' => null, 'homerecord' => null, 'awayrecord' => null);
         foreach ($result_labels as $key => $value) {
             $totals[$key . 'for'] = 0;
             $totals[$key . 'for_home'] = 0;
             $totals[$key . 'for_away'] = 0;
             $totals[$key . 'for_venue'] = 0;
             $totals[$key . 'against'] = 0;
             $totals[$key . 'against_home'] = 0;
             $totals[$key . 'against_away'] = 0;
             $totals[$key . 'against_venue'] = 0;
         }
         foreach ($outcome_labels as $key => $value) {
             $totals[$key] = 0;
             $totals[$key . '_home'] = 0;
             $totals[$key . '_away'] = 0;
             $totals[$key . '_venue'] = 0;
         }
         // Initialize streaks counter
         $streak = array('name' => '', 'count' => 0, 'fire' => 1);
         // Initialize last counters
         $last5 = array();
         $last10 = array();
         // Initialize record counters
         $homerecord = array();
         $awayrecord = array();
         // Add outcome types to last and record counters
         foreach ($outcome_labels as $key => $value) {
             $last5[$key] = 0;
             $last10[$key] = 0;
             $homerecord[$key] = 0;
             $awayrecord[$key] = 0;
         }
         // Get all events involving the team in current season
         $args = array('post_type' => 'sp_event', 'numberposts' => -1, 'posts_per_page' => -1, 'orderby' => 'post_date', 'order' => 'DESC', 'meta_query' => array('relation' => 'AND', array('key' => 'sp_team', 'value' => $this->ID), array('key' => 'sp_format', 'value' => apply_filters('sportspress_competitive_event_formats', array('league')), 'compare' => 'IN')), 'tax_query' => array('relation' => 'AND'));
         if ($league_id) {
             $args['tax_query'][] = array('taxonomy' => 'sp_league', 'field' => 'id', 'terms' => $league_id);
         }
         if ($div_id) {
             $args['tax_query'][] = array('taxonomy' => 'sp_season', 'field' => 'id', 'terms' => $div_id);
         }
         $args = apply_filters('sportspress_team_data_event_args', $args);
         $events = get_posts($args);
         $e = 0;
         foreach ($events as $event) {
             $results = (array) get_post_meta($event->ID, 'sp_results', true);
             $minutes = get_post_meta($event->ID, 'sp_minutes', true);
             if ($minutes === '') {
                 $minutes = get_option('sportspress_event_minutes', 90);
             }
             $i = 0;
             foreach ($results as $team_id => $team_result) {
                 if (is_array($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);
                                 }
                                 foreach ($value as $outcome) {
                                     // Increment events played and outcome count
                                     if (array_key_exists($outcome, $totals)) {
                                         $totals['eventsplayed']++;
                                         $totals['eventminutes'] += $minutes;
                                         $totals[$outcome]++;
                                         // Add to home or away stats
                                         if (0 === $i) {
                                             $totals['eventsplayed_home']++;
                                             $totals['eventminutes_home'] += $minutes;
                                             $totals[$outcome . '_home']++;
                                         } else {
                                             $totals['eventsplayed_away']++;
                                             $totals['eventminutes_away'] += $minutes;
                                             $totals[$outcome . '_away']++;
                                         }
                                         // Add to venue stats
                                         if (sp_is_home_venue($team_id, $event->ID)) {
                                             $totals['eventsplayed_venue']++;
                                             $totals['eventminutes_venue'] += $minutes;
                                             $totals[$outcome . '_venue']++;
                                         }
                                     }
                                     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;
                                         }
                                         // Add to last 5 counter if sum is less than 5
                                         if (array_key_exists($outcome, $last5) && array_sum($last5) < 5) {
                                             $last5[$outcome]++;
                                         }
                                         // Add to last 10 counter if sum is less than 10
                                         if (array_key_exists($outcome, $last10) && array_sum($last10) < 10) {
                                             $last10[$outcome]++;
                                         }
                                         // Add to home or away record
                                         if (0 === $i) {
                                             if (array_key_exists($outcome, $homerecord)) {
                                                 $homerecord[$outcome]++;
                                             }
                                         } else {
                                             if (array_key_exists($outcome, $awayrecord)) {
                                                 $awayrecord[$outcome]++;
                                             }
                                         }
                                     }
                                 }
                             } else {
                                 if (array_key_exists($key . 'for', $totals)) {
                                     $totals[$key . 'for'] += $value;
                                     $totals[$key . 'for' . ($e + 1)] = $value;
                                     // Add to home or away stats
                                     if (0 === $i) {
                                         $totals[$key . 'for_home'] += $value;
                                     } else {
                                         $totals[$key . 'for_away'] += $value;
                                     }
                                     // Add to venue stats
                                     if (sp_is_home_venue($team_id, $event->ID)) {
                                         $totals[$key . 'for_venue'] += $value;
                                     }
                                 }
                             }
                         } else {
                             if ($key != 'outcome') {
                                 if (array_key_exists($key . 'against', $totals)) {
                                     $totals[$key . 'against'] += $value;
                                     $totals[$key . 'against' . ($e + 1)] = $value;
                                     // Add to home or away stats
                                     if (0 === $i) {
                                         $totals[$key . 'against_home'] += $value;
                                     } else {
                                         $totals[$key . 'against_away'] += $value;
                                     }
                                     // Add to venue stats
                                     if (sp_is_home_venue($team_id, $event->ID)) {
                                         $totals[$key . 'against_venue'] += $value;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $i++;
             }
             $e++;
         }
         // Compile streaks counter and add to totals
         $args = array('name' => $streak['name'], 'post_type' => 'sp_outcome', 'post_status' => 'publish', 'posts_per_page' => 1, 'orderby' => 'menu_order', 'order' => 'ASC');
         $outcomes = get_posts($args);
         if ($outcomes) {
             $outcome = reset($outcomes);
             $abbreviation = get_post_meta($outcome->ID, 'sp_abbreviation', true);
             if (!$abbreviation) {
                 $abbreviation = substr($outcome->post_title, 0, 1);
             }
             $totals['streak'] = $abbreviation . $streak['count'];
         }
         // Add last and record counters to totals
         $totals['last5'] = $last5;
         $totals['last10'] = $last10;
         $totals['homerecord'] = $homerecord;
         $totals['awayrecord'] = $awayrecord;
         // Generate array of placeholder values for each league
         $placeholders[$div_id] = array();
         foreach ($equations as $key => $value) {
             if ('$gamesback' == $value['equation']) {
                 $placeholders[$div_id][$key] = __('(Auto)', 'sportspress');
             } else {
                 $placeholders[$div_id][$key] = sp_solve($value['equation'], $totals, $value['precision']);
             }
         }
     }
     // Get columns from column variables
     $columns = sp_get_var_labels('sp_column');
     return array($columns, $data, $placeholders);
 }
function sportspress_get_var_labels($post_type)
{
    return sp_get_var_labels($post_type);
}