/**
  * Save meta box data
  */
 public static function save($post_id, $post)
 {
     update_post_meta($post_id, 'sp_minutes', sp_array_value($_POST, 'sp_minutes', get_option('sportspress_event_minutes', 90)));
     $venues = array_filter(sp_array_value(sp_array_value($_POST, 'tax_input', array()), 'sp_venue', array()));
     if (empty($venues)) {
         $teams = sp_array_value($_POST, 'sp_team', array());
         $team = reset($teams);
         $venue = sp_get_the_term_id($team, 'sp_venue');
         wp_set_post_terms($post_id, $venue, 'sp_venue');
     }
 }
    /**
     * Output the metabox
     */
    public static function output($post)
    {
        $limit = get_option('prosports_event_teams', 2);
        $teams = (array) get_post_meta($post->ID, 'sp_team', false);
        $league_id = sp_get_the_term_id($post->ID, 'sp_league', 0);
        $season_id = sp_get_the_term_id($post->ID, 'sp_season', 0);
        for ($i = 0; $i < $limit; $i++) {
            $team = array_shift($teams);
            ?>
			<div class="sp-instance">
				<p class="sp-tab-select sp-tab-select-dummy">
					<?php 
            $args = array('taxonomy' => 'sp_league', 'name' => 'sp_league_dummy', 'class' => 'sp-dummy sp_league-dummy', 'show_option_all' => __('All', 'prosports'), 'selected' => $league_id, 'values' => 'term_id');
            sp_dropdown_taxonomies($args);
            ?>
				</p>
				<p class="sp-tab-select sp-tab-select-dummy">
					<?php 
            $args = array('taxonomy' => 'sp_season', 'name' => 'sp_season_dummy', 'class' => 'sp-dummy sp_season-dummy', 'show_option_all' => __('All', 'prosports'), 'selected' => $season_id, 'values' => 'term_id');
            sp_dropdown_taxonomies($args);
            ?>
				</p>
				<p class="sp-tab-select sp-title-generator">
				<?php 
            $args = array('post_type' => 'sp_team', 'name' => 'sp_team[]', 'class' => 'prosports-pages', 'show_option_none' => __('&mdash; None &mdash;', 'prosports'), 'show_option_all' => __('&mdash; Individual &mdash;', 'prosports'), 'values' => 'ID', 'selected' => $team);
            sp_dropdown_pages($args);
            ?>
				</p>
				<ul id="sp_team-tabs" class="wp-tab-bar sp-tab-bar">
					<li class="wp-tab-active"><a href="#sp_player-all"><?php 
            _e('Players', 'prosports');
            ?>
</a></li>
					<li class="wp-tab"><a href="#sp_staff-all"><?php 
            _e('Staff', 'prosports');
            ?>
</a></li>
				</ul>
				<?php 
            sp_post_checklist($post->ID, 'sp_player', 'block', array('sp_league', 'sp_season', 'sp_current_team'), $i);
            sp_post_checklist($post->ID, 'sp_staff', 'none', array('sp_league', 'sp_season', 'sp_current_team'), $i);
            ?>
			</div>
			<?php 
        }
    }
    /**
     * Output the metabox
     */
    public static function output($post)
    {
        wp_nonce_field('prosports_save_data', 'prosports_meta_nonce');
        $league_id = sp_get_the_term_id($post->ID, 'sp_league', 0);
        $season_id = sp_get_the_term_id($post->ID, 'sp_season', 0);
        ?>
		<div>
			<p><strong><?php 
        _e('Competition', 'prosports');
        ?>
</strong></p>
			<p class="sp-tab-select">
				<?php 
        $args = array('taxonomy' => 'sp_league', 'name' => 'sp_league', 'show_option_all' => __('All', 'prosports'), 'selected' => $league_id, 'values' => 'term_id');
        if (!sp_dropdown_taxonomies($args)) {
            sp_taxonomy_adder('sp_league', 'sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Season', 'prosports');
        ?>
</strong></p>
			<p class="sp-tab-select">
				<?php 
        $args = array('taxonomy' => 'sp_season', 'name' => 'sp_season', 'show_option_all' => __('All', 'prosports'), 'selected' => $season_id, 'values' => 'term_id');
        if (!sp_dropdown_taxonomies($args)) {
            sp_taxonomy_adder('sp_season', 'sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Teams', 'prosports');
        ?>
</strong></p>
			<?php 
        sp_post_checklist($post->ID, 'sp_team', 'block', array('sp_league', 'sp_season'));
        sp_post_adder('sp_team', __('Add New', 'prosports'));
        ?>
		</div>
		<?php 
    }
 if ($mode == 'icons') {
     echo '<td class="sp-performance-icons">';
 }
 foreach ($labels as $key => $label) {
     if ('name' == $key) {
         continue;
     }
     $format = sp_array_value($formats, $key, 'number');
     $placeholder = sp_get_format_placeholder($format);
     $value = '&mdash;';
     if ($key == 'position') {
         $positions = array();
         if (array_key_exists($key, $row) && $row[$key] != '') {
             $position_ids = (array) $row[$key];
         } else {
             $position_ids = (array) sp_get_the_term_id($player_id, 'sp_position');
         }
         foreach ($position_ids as $position_id) {
             $player_position = get_term_by('id', $position_id, 'sp_position');
             if ($player_position) {
                 $positions[] = $player_position->name;
             }
         }
         $positions = array_unique($positions);
         if (sizeof($positions)) {
             $value = implode(', ', $positions);
         }
     } else {
         if (array_key_exists($key, $row) && $row[$key] != '') {
             $value = $row[$key];
         } else {
 /**
  * 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);
     $team_ids = (array) get_post_meta($this->ID, 'sp_team', false);
     $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);
     // 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 incremental counter
     $this->pos = array(1);
     $this->counter = 1;
     // 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;
         }
         // Initialize team totals
         $totals[$team_id] = array('eventsplayed' => 0, 'eventminutes' => 0, 'streak' => 0);
         foreach ($result_labels as $key => $value) {
             $totals[$team_id][$key . 'for'] = 0;
             $totals[$team_id][$key . 'against'] = 0;
         }
         foreach ($outcome_labels as $key => $value) {
             $totals[$team_id][$key] = 0;
         }
         // Get static stats
         $static = get_post_meta($team_id, 'sp_columns', true);
         // Add static stats to placeholders
         $placeholders[$team_id] = sp_array_value(sp_array_value($static, $league_id, array()), $div_id, array());
     }
     $args = array('post_type' => 'sp_event', 'post_status' => array('publish', 'future'), 'numberposts' => -1, 'posts_per_page' => -1, 'orderby' => 'post_date', 'order' => 'DESC', 'tax_query' => array('relation' => 'AND'));
     if ($league_id) {
         $args['tax_query'][] = array('taxonomy' => 'sp_league', 'field' => 'id', 'terms' => $league_id);
     }
     if ($league_id) {
         $args['tax_query'][] = 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);
         $minutes = get_post_meta($event->ID, 'sp_minutes', true);
         if ($minutes === '') {
             $minutes = get_option('prosports_event_minutes', 90);
         }
         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]++;
                             }
                             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]++;
                                 }
                             }
                         }
                     } 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);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     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;
     }
     $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);
     // 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, '') == '') {
                 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 (!in_array($stat->equation, array('$streak', '$last5', '$last10'))) {
                         // 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;
             }
         }
     }
     // 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'));
     // Create temp array and calculate position of teams for ties
     $temp = $merged;
     uasort($temp, array($this, 'calculate_pos'));
     foreach ($merged as $team_id => $team_columns) {
         $merged[$team_id]['pos'] = array_shift($this->pos);
     }
     // Rearrange data array to reflect values
     $data = array();
     foreach ($merged as $key => $value) {
         $data[$key] = $tempdata[$key];
     }
     if ($admin) {
         return array($columns, $usecolumns, $data, $placeholders, $merged);
     } else {
         if (!is_array($usecolumns)) {
             $usecolumns = array();
         }
         $labels = array_merge(array('pos' => __('Pos', 'prosports'), 'name' => __('Team', 'prosports')), $columns);
         $merged[0] = $labels;
         return $merged;
     }
 }
    /**
     * Output the metabox
     */
    public static function output($post)
    {
        $limit = get_option('sportspress_event_teams', 2);
        $teams = (array) get_post_meta($post->ID, 'sp_team', false);
        for ($i = 0; $i < $limit; $i++) {
            $team = array_shift($teams);
            ?>
			<div class="sp-instance">
				<p class="sp-tab-select sp-title-generator">
				<?php 
            $args = array('post_type' => 'sp_team', 'name' => 'sp_team[]', 'class' => 'sportspress-pages', 'show_option_none' => __('&mdash; None &mdash;', 'sportspress'), 'values' => 'ID', 'selected' => $team, 'chosen' => true, 'tax_query' => array());
            if ('yes' == get_option('sportspress_event_filter_teams_by_league', 'no')) {
                $league_id = sp_get_the_term_id($post->ID, 'sp_league', 0);
                if ($league_id) {
                    $args['tax_query'][] = array('taxonomy' => 'sp_league', 'terms' => $league_id);
                }
            }
            if ('yes' == get_option('sportspress_event_filter_teams_by_season', 'no')) {
                $season_id = sp_get_the_term_id($post->ID, 'sp_season', 0);
                if ($season_id) {
                    $args['tax_query'][] = array('taxonomy' => 'sp_season', 'terms' => $season_id);
                }
            }
            if (!sp_dropdown_pages($args)) {
                unset($args['tax_query']);
                sp_dropdown_pages($args);
            }
            ?>
				</p>
				<?php 
            $tabs = apply_filters('sportspress_event_team_tabs', array('sp_player', 'sp_staff'));
            ?>
				<?php 
            if ($tabs) {
                ?>
				<ul id="sp_team-tabs" class="wp-tab-bar sp-tab-bar">
					<?php 
                foreach ($tabs as $index => $post_type) {
                    $object = get_post_type_object($post_type);
                    ?>
					<li class="wp-tab<?php 
                    if (0 == $index) {
                        ?>
-active<?php 
                    }
                    ?>
"><a href="#<?php 
                    echo $post_type;
                    ?>
-all"><?php 
                    echo $object->labels->name;
                    ?>
</a></li>
					<?php 
                }
                ?>
				</ul>
				<?php 
                foreach ($tabs as $index => $post_type) {
                    sp_post_checklist($post->ID, $post_type, 0 == $index ? 'block' : 'none', array('sp_league', 'sp_season', 'sp_current_team'), $i);
                }
                ?>
				<?php 
            }
            ?>
			</div>
			<?php 
        }
    }
    /**
     * Output the details metabox
     */
    public static function details($post)
    {
        $league_id = sp_get_the_term_id($post->ID, 'sp_league', 0);
        $season_id = sp_get_the_term_id($post->ID, 'sp_season', 0);
        $team_id = get_post_meta($post->ID, 'sp_team', true);
        ?>
		<div>
			<p><strong><?php 
        _e('Competition', 'prosports');
        ?>
</strong></p>
			<p>
				<?php 
        $args = array('taxonomy' => 'sp_league', 'name' => 'sp_league', 'show_option_all' => __('All', 'prosports'), 'selected' => $league_id, 'values' => 'term_id');
        if (!sp_dropdown_taxonomies($args)) {
            sp_taxonomy_adder('sp_league', 'sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Season', 'prosports');
        ?>
</strong></p>
			<p class="sp-tab-select">
				<?php 
        $args = array('taxonomy' => 'sp_season', 'name' => 'sp_season', 'show_option_all' => __('All', 'prosports'), 'selected' => $season_id, 'values' => 'term_id');
        if (!sp_dropdown_taxonomies($args)) {
            sp_taxonomy_adder('sp_season', 'sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Team', 'prosports');
        ?>
</strong></p>
			<p class="sp-tab-select">
				<?php 
        $args = array('post_type' => 'sp_team', 'name' => 'sp_team', 'show_option_all' => __('All', 'prosports'), 'selected' => $team_id, 'values' => 'ID');
        if (!sp_dropdown_pages($args)) {
            sp_post_adder('sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
		</div>
		<?php 
    }
 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'] = 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
         $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;
     }
 }
 public function performance($admin = false)
 {
     $teams = get_post_meta($this->ID, 'sp_team', false);
     $performance = (array) get_post_meta($this->ID, 'sp_players', true);
     $args = array('post_type' => 'sp_performance', 'numberposts' => 100, 'posts_per_page' => 100, 'orderby' => 'menu_order', 'order' => 'ASC');
     $vars = get_posts($args);
     $labels = array();
     foreach ($vars as $var) {
         $labels[$var->post_name] = $var->post_title;
         $format = get_post_meta($var->ID, 'sp_format', true);
         if ('' === $format) {
             $format = 'number';
         }
         $formats[$var->post_name] = $format;
     }
     $order = (array) get_post_meta($this->ID, 'sp_order', true);
     $labels = apply_filters('sportspress_event_performance_labels', $labels, $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'] = apply_filters('sportspress_event_performance_default_squad_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, $formats, $order);
     } 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']);
         }
         if ('no' == get_option('sportspress_event_show_player_numbers', 'yes')) {
             unset($labels['number']);
         }
         $performance[0] = $labels;
         return apply_filters('sportspress_get_event_performance', $performance);
     }
 }
function prosports_get_the_term_id($post_id, $taxonomy, $index)
{
    return sp_get_the_term_id($post_id, $taxonomy, $index);
}
    /**
     * Output the metabox
     */
    public static function output($post)
    {
        $league_id = sp_get_the_term_id($post->ID, 'sp_league', 0);
        $season_id = sp_get_the_term_id($post->ID, 'sp_season', 0);
        $team_id = get_post_meta($post->ID, 'sp_team', true);
        $grouping = get_post_meta($post->ID, 'sp_grouping', true);
        $orderby = get_post_meta($post->ID, 'sp_orderby', true);
        $order = get_post_meta($post->ID, 'sp_order', true);
        ?>
		<div>
			<p><strong><?php 
        _e('Competition', 'prosports');
        ?>
</strong></p>
			<p class="sp-tab-select">
				<?php 
        $args = array('taxonomy' => 'sp_league', 'name' => 'sp_league', 'show_option_all' => __('All', 'prosports'), 'selected' => $league_id, 'values' => 'term_id');
        if (!sp_dropdown_taxonomies($args)) {
            sp_taxonomy_adder('sp_league', 'sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Season', 'prosports');
        ?>
</strong></p>
			<p class="sp-tab-select">
				<?php 
        $args = array('taxonomy' => 'sp_season', 'name' => 'sp_season', 'show_option_all' => __('All', 'prosports'), 'selected' => $season_id, 'values' => 'term_id');
        if (!sp_dropdown_taxonomies($args)) {
            sp_taxonomy_adder('sp_season', 'sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Team', 'prosports');
        ?>
</strong></p>
			<p class="sp-tab-select">
				<?php 
        $args = array('post_type' => 'sp_team', 'name' => 'sp_team', 'show_option_all' => __('All', 'prosports'), 'selected' => $team_id, 'values' => 'ID');
        if (!sp_dropdown_pages($args)) {
            sp_post_adder('sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Grouping', 'prosports');
        ?>
</strong></p>
			<p>
			<select name="sp_grouping">
				<option value="0"><?php 
        _e('None', 'prosports');
        ?>
</option>
				<option value="position" <?php 
        selected($grouping, 'position');
        ?>
><?php 
        _e('Position', 'prosports');
        ?>
</option>
			</select>
			</p>
			<p><strong><?php 
        _e('Sort by', 'prosports');
        ?>
</strong></p>
			<p>
			<?php 
        $args = array('prepend_options' => array('number' => __('Number', 'prosports'), 'name' => __('Name', 'prosports')), 'post_type' => array('sp_performance', 'sp_metric', 'sp_statistic'), 'name' => 'sp_orderby', 'selected' => $orderby, 'values' => 'slug');
        sp_dropdown_pages($args);
        ?>
			</p>
			<p><strong><?php 
        _e('Sort Order', 'prosports');
        ?>
</strong></p>
			<p>
				<select name="sp_order">
					<option value="ASC" <?php 
        selected('ASC', $order);
        ?>
><?php 
        _e('Ascending', 'prosports');
        ?>
</option>
					<option value="DESC" <?php 
        selected('DESC', $order);
        ?>
><?php 
        _e('Descending', 'prosports');
        ?>
</option>
				</select>
			</p>
			<p><strong><?php 
        _e('Players', 'prosports');
        ?>
</strong></p>
			<?php 
        sp_post_checklist($post->ID, 'sp_player', 'block', array('sp_league', 'sp_season', 'sp_current_team'));
        sp_post_adder('sp_player', __('Add New', 'prosports'));
        ?>
		</div>
		<?php 
    }
    /**
     * Output the metabox
     */
    public static function output($post)
    {
        $limit = get_option('sportspress_event_teams', 2);
        $teams = (array) get_post_meta($post->ID, 'sp_team', false);
        if ($limit) {
            for ($i = 0; $i < $limit; $i++) {
                $team = array_shift($teams);
                ?>
				<div class="sp-instance">
					<p class="sp-tab-select sp-title-generator">
					<?php 
                $args = array('post_type' => 'sp_team', 'name' => 'sp_team[]', 'class' => 'sportspress-pages', 'show_option_none' => __('&mdash; None &mdash;', 'sportspress'), 'values' => 'ID', 'selected' => $team, 'chosen' => true, 'tax_query' => array());
                if ('yes' == get_option('sportspress_event_filter_teams_by_league', 'no')) {
                    $league_id = sp_get_the_term_id($post->ID, 'sp_league', 0);
                    if ($league_id) {
                        $args['tax_query'][] = array('taxonomy' => 'sp_league', 'terms' => $league_id);
                    }
                }
                if ('yes' == get_option('sportspress_event_filter_teams_by_season', 'no')) {
                    $season_id = sp_get_the_term_id($post->ID, 'sp_season', 0);
                    if ($season_id) {
                        $args['tax_query'][] = array('taxonomy' => 'sp_season', 'terms' => $season_id);
                    }
                }
                if (!sp_dropdown_pages($args)) {
                    unset($args['tax_query']);
                    sp_dropdown_pages($args);
                }
                ?>
					</p>
					<?php 
                $tabs = array();
                $sections = get_option('sportspress_event_performance_sections', -1);
                if (0 == $sections) {
                    $tabs['sp_offense'] = array('label' => __('Offense', 'sportspress'), 'post_type' => 'sp_player');
                    $tabs['sp_defense'] = array('label' => __('Defense', 'sportspress'), 'post_type' => 'sp_player');
                } elseif (1 == $sections) {
                    $tabs['sp_defense'] = array('label' => __('Defense', 'sportspress'), 'post_type' => 'sp_player');
                    $tabs['sp_offense'] = array('label' => __('Offense', 'sportspress'), 'post_type' => 'sp_player');
                } else {
                    $tabs['sp_player'] = array('label' => __('Players', 'sportspress'), 'post_type' => 'sp_player');
                }
                $tabs['sp_staff'] = array('label' => __('Staff', 'sportspress'), 'post_type' => 'sp_staff');
                ?>
					<?php 
                if ($tabs) {
                    ?>
					<ul id="sp_team-tabs" class="wp-tab-bar sp-tab-bar">
						<?php 
                    $j = 0;
                    foreach ($tabs as $slug => $tab) {
                        ?>
								<li class="wp-tab<?php 
                        if (0 == $j) {
                            ?>
-active<?php 
                        }
                        ?>
"><a href="#<?php 
                        echo $slug;
                        ?>
-all"><?php 
                        echo $tab['label'];
                        ?>
</a></li>
								<?php 
                        $j++;
                    }
                    ?>
					</ul>
					<?php 
                    $j = 0;
                    foreach ($tabs as $slug => $tab) {
                        do_action('sportspress_event_teams_meta_box_checklist', $post->ID, $tab['post_type'], 0 == $j ? 'block' : 'none', $team, $i, $slug);
                        $j++;
                    }
                    ?>
					<?php 
                }
                ?>
				</div>
				<?php 
            }
        } else {
            ?>
			<p><strong><?php 
            printf(__('Select %s:', 'sportspress'), __('Teams', 'sportspress'));
            ?>
</strong></p>
			<?php 
            $args = array('post_type' => 'sp_team', 'name' => 'sp_team[]', 'selected' => $teams, 'values' => 'ID', 'class' => 'widefat', 'property' => 'multiple', 'chosen' => true, 'placeholder' => __('None', 'sportspress'));
            if (!sp_dropdown_pages($args)) {
                sp_post_adder('sp_team', __('Add New', 'sportspress'));
            }
        }
        wp_nonce_field('sp-get-players', 'sp-get-players-nonce', false);
    }
 /**
  * Returns formatted data
  *
  * @access public
  * @param bool $admin
  * @return array
  */
 public function data($admin = false)
 {
     global $pagenow;
     $league_id = sp_get_the_term_id($this->ID, 'sp_league', 0);
     $season_id = sp_get_the_term_id($this->ID, 'sp_season', 0);
     $staffs = get_post_meta($this->ID, 'sp_staffs', true);
     $args = array('post_type' => 'sp_staff', 'numberposts' => -1, 'posts_per_page' => -1, 'orderby' => $this->orderby, 'order' => $this->order, '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');
             $team = get_post_meta($this->ID, 'sp_team', true);
             if ($leagues) {
                 $league_ids = array();
                 foreach ($leagues as $league) {
                     $league_ids[] = $league->term_id;
                 }
                 $args['tax_query'][] = array('taxonomy' => 'sp_league', 'field' => 'id', 'terms' => $league_ids);
             }
             if ($seasons) {
                 $season_ids = array();
                 foreach ($seasons as $season) {
                     $season_ids[] = $season->term_id;
                 }
                 $args['tax_query'][] = array('taxonomy' => 'sp_season', 'field' => 'id', 'terms' => $season_ids);
             }
             if ($team) {
                 $args['meta_query'] = array(array('key' => 'sp_team', 'value' => $team));
             }
         }
         $posts = get_posts($args);
     } else {
         $posts = null;
     }
     if ($posts === null) {
         $data = null;
     } else {
         $data = array();
         foreach ($posts as $post) {
             $staff = array('name' => get_the_title($post->ID));
             $staff_object = new SP_Staff($post->ID);
             $role = $staff_object->role();
             if (!empty($role)) {
                 $staff['role'] = $role->name;
             }
             $phone = get_post_meta($post->ID, 'sp_phone', true);
             if (!empty($phone)) {
                 $staff['phone'] = $phone;
             }
             $email = get_post_meta($post->ID, 'sp_email', true);
             if (!empty($email)) {
                 $staff['email'] = $email;
             }
             $data[$post->ID] = $staff;
         }
         // Sort them by manual order
         if (is_array($staffs)) {
             $prepend = array();
             foreach ($staffs as $staff) {
                 if (array_key_exists($staff, $data)) {
                     $prepend[$staff] = $data[$staff];
                     unset($data[$staff]);
                 }
             }
             $data = $prepend + $data;
         }
     }
     $labels = apply_filters('prosports_directory_labels', array('phone' => __('Phone', 'prosports'), 'email' => __('Email', 'prosports')));
     if ($admin) {
         return array($labels, $this->columns, $data);
     } else {
         if (!is_array($this->columns)) {
             $this->columns = array();
         }
         foreach ($labels as $key => $label) {
             if (!in_array($key, $this->columns)) {
                 unset($labels[$key]);
             }
         }
         $data[0] = array_merge(array('role' => __('Job', 'prosports'), 'name' => __('Name', 'prosports')), $labels);
         return $data;
     }
 }
    /**
     * Admin edit table row
     */
    public static function row($labels = array(), $player_id = 0, $player_performance = array(), $team_id = 0, $data = array(), $positions = true, $status = true, $sortable = true, $numbers = true)
    {
        if ($player_id <= 0) {
            return;
        }
        $number = get_post_meta($player_id, 'sp_number', true);
        $value = sp_array_value($player_performance, 'number', '');
        ?>
		<tr class="sp-row sp-post" data-player="<?php 
        echo $player_id;
        ?>
">
			<?php 
        if ($sortable) {
            ?>
				<td class="icon"><span class="dashicons dashicons-menu post-state-format"></span></td>
			<?php 
        }
        ?>
			<?php 
        if ($numbers) {
            ?>
				<td>
					<input class="small-text sp-player-number-input" type="text" name="sp_players[<?php 
            echo $team_id;
            ?>
][<?php 
            echo $player_id;
            ?>
][number]" value="<?php 
            echo esc_attr($value);
            ?>
" />
				</td>
			<?php 
        }
        ?>
			<td><?php 
        echo get_the_title($player_id);
        ?>
</td>
			<?php 
        if ($positions) {
            ?>
				<td>
					<?php 
            $selected = (array) sp_array_value($player_performance, 'position', null);
            if ($selected == null) {
                $selected = (array) sp_get_the_term_id($player_id, 'sp_position', 0);
            }
            $args = array('taxonomy' => 'sp_position', 'name' => 'sp_players[' . $team_id . '][' . $player_id . '][position][]', 'values' => 'term_id', 'orderby' => 'slug', 'selected' => $selected, 'class' => 'sp-position', 'property' => 'multiple', 'chosen' => true, 'include_children' => 'no' == get_option('sportspress_event_hide_child_positions', 'no'));
            sp_dropdown_taxonomies($args);
            ?>
				</td>
			<?php 
        }
        ?>
			<?php 
        foreach ($labels as $column => $label) {
            $value = sp_array_value($player_performance, $column, '');
            ?>
				<td>
					<input class="sp-player-<?php 
            echo $column;
            ?>
-input" type="text" name="sp_players[<?php 
            echo $team_id;
            ?>
][<?php 
            echo $player_id;
            ?>
][<?php 
            echo $column;
            ?>
]" value="<?php 
            echo esc_attr($value);
            ?>
" placeholder="0" />
				</td>
			<?php 
        }
        ?>
			<?php 
        if ($status) {
            ?>
				<td class="sp-status-selector">
					<?php 
            echo self::status_select($team_id, $player_id, sp_array_value($player_performance, 'status', null));
            ?>
					<?php 
            echo self::sub_select($team_id, $player_id, sp_array_value($player_performance, 'sub', null), $data);
            ?>
				</td>
			<?php 
        }
        ?>
		</tr>
		<?php 
    }
    /**
     * Admin edit table row
     */
    public static function row($labels = array(), $player_id = 0, $player_performance = array(), $team_id = 0, $data = array(), $positions = true, $status = true, $sortable = true, $numbers = true, $section = -1, $formats = array(), $player_timeline = array())
    {
        if ($player_id <= 0) {
            return;
        }
        $value = sp_array_value($player_performance, 'number', '');
        ?>
		<tr class="sp-row sp-post" data-player="<?php 
        echo $player_id;
        ?>
">
			<?php 
        if ($sortable) {
            ?>
				<td class="icon"><span class="dashicons dashicons-menu post-state-format"></span></td>
			<?php 
        }
        ?>
			<?php 
        if (apply_filters('sportspress_event_performance_show_numbers', $numbers, $section)) {
            ?>
				<td>
					<input class="small-text sp-player-number-input sp-sync-input" type="text" name="sp_players[<?php 
            echo $team_id;
            ?>
][<?php 
            echo $player_id;
            ?>
][number]" value="<?php 
            echo esc_attr($value);
            ?>
" />
				</td>
			<?php 
        }
        ?>
			<td>
				<?php 
        echo get_the_title($player_id);
        ?>
				<?php 
        if (1 == $section) {
            ?>
					<input type="hidden" name="sp_order[<?php 
            echo $team_id;
            ?>
][]" value="<?php 
            echo $player_id;
            ?>
">
				<?php 
        }
        ?>
			</td>
			<?php 
        if ($positions) {
            ?>
				<td>
					<?php 
            $selected = (array) sp_array_value($player_performance, 'position', null);
            if ($selected == null) {
                $selected = (array) sp_get_the_term_id($player_id, 'sp_position', 0);
            }
            $args = array('taxonomy' => 'sp_position', 'name' => 'sp_players[' . $team_id . '][' . $player_id . '][position][]', 'values' => 'term_id', 'orderby' => 'slug', 'selected' => $selected, 'class' => 'sp-position', 'property' => 'multiple', 'chosen' => true, 'include_children' => 'no' == get_option('sportspress_event_hide_child_positions', 'no'));
            sp_dropdown_taxonomies($args);
            ?>
				</td>
			<?php 
        }
        ?>
			<?php 
        foreach ($labels as $column => $label) {
            $value = sp_array_value($player_performance, $column, '');
            $intval = intval($value);
            $placeholder = sp_get_format_placeholder(sp_array_value($formats, $column, 'number'));
            ?>
				<td>
					<input class="sp-player-<?php 
            echo $column;
            ?>
-input sp-sync-input" type="text" name="sp_players[<?php 
            echo $team_id;
            ?>
][<?php 
            echo $player_id;
            ?>
][<?php 
            echo $column;
            ?>
]" value="<?php 
            echo esc_attr($value);
            ?>
" placeholder="<?php 
            echo $placeholder;
            ?>
" />
					<?php 
            if ($intval) {
                ?>
						<?php 
                // Get performance times
                if (is_array($player_timeline)) {
                    $times = sp_array_value($player_timeline, $column, array());
                } else {
                    $times = false;
                }
                if (is_array($times)) {
                    ?>
							<hr>
							<?php 
                    for ($i = 0; $i < $intval; $i++) {
                        ?>
<input class="sp-sync-input small-text" type="text" name="sp_timeline[<?php 
                        echo $team_id;
                        ?>
][<?php 
                        echo $player_id;
                        ?>
][<?php 
                        echo $column;
                        ?>
][<?php 
                        echo $i;
                        ?>
]" value="<?php 
                        echo esc_attr(sp_array_value($times, $i, ''));
                        ?>
" placeholder="-" /><?php 
                    }
                    ?>
							<span class="description"><?php 
                    _e('mins', 'sportspress');
                    ?>
</span>
							<?php 
                }
            }
            ?>
				</td>
			<?php 
        }
        ?>
			<?php 
        if (apply_filters('sportspress_event_performance_show_status', $status, $section)) {
            ?>
				<td class="sp-status-selector">
					<?php 
            // Get substitute times
            if (is_array($player_timeline)) {
                $times = sp_array_value($player_timeline, 'sub', array());
            } else {
                $times = false;
            }
            ?>
					<?php 
            echo self::status_select($team_id, $player_id, sp_array_value($player_performance, 'status', null));
            ?>
					<?php 
            echo self::sub_select($team_id, $player_id, sp_array_value($player_performance, 'sub', null), $data);
            ?>
					<input class="sp-sync-input small-text" type="text" name="sp_timeline[<?php 
            echo $team_id;
            ?>
][<?php 
            echo $player_id;
            ?>
][sub][]" value="<?php 
            echo esc_attr(sp_array_value($times, 0, ''));
            ?>
" placeholder="-" />
					<span class="description"><?php 
            _e('mins', 'sportspress');
            ?>
</span>
				</td>
			<?php 
        }
        ?>
		</tr>
		<?php 
    }
    /**
     * Output the metabox
     */
    public static function output($post)
    {
        $status = get_post_meta($post->ID, 'sp_status', true);
        $date = get_post_meta($post->ID, 'sp_date', true);
        $date_from = get_post_meta($post->ID, 'sp_date_from', true);
        $date_to = get_post_meta($post->ID, 'sp_date_to', true);
        $league_id = sp_get_the_term_id($post->ID, 'sp_league', 0);
        $season_id = sp_get_the_term_id($post->ID, 'sp_season', 0);
        $venue_id = sp_get_the_term_id($post->ID, 'sp_venue', 0);
        $team_id = get_post_meta($post->ID, 'sp_team', true);
        $order = get_post_meta($post->ID, 'sp_order', true);
        ?>
		<div>
			<p><strong><?php 
        _e('Status', 'prosports');
        ?>
</strong></p>
			<p>
				<?php 
        $args = array('name' => 'sp_status', 'id' => 'sp_status', 'selected' => $status);
        sp_dropdown_statuses($args);
        ?>
			</p>
			<div class="sp-date-selector">
				<p><strong><?php 
        _e('Date', 'prosports');
        ?>
</strong></p>
				<p>
					<?php 
        $args = array('name' => 'sp_date', 'id' => 'sp_date', 'selected' => $date);
        sp_dropdown_dates($args);
        ?>
				</p>
				<p class="sp-date-range">
					<input type="text" class="sp-datepicker-from" name="sp_date_from" value="<?php 
        echo $date_from ? $date_from : date_i18n('Y-m-d');
        ?>
" size="10">
					:
					<input type="text" class="sp-datepicker-to" name="sp_date_to" value="<?php 
        echo $date_to ? $date_to : date_i18n('Y-m-d');
        ?>
" size="10">
				</p>
			</div>
			<p><strong><?php 
        _e('Competition', 'prosports');
        ?>
</strong></p>
			<p>
				<?php 
        $args = array('show_option_all' => __('All', 'prosports'), 'taxonomy' => 'sp_league', 'name' => 'sp_league', 'selected' => $league_id, 'values' => 'term_id');
        if (!sp_dropdown_taxonomies($args)) {
            sp_taxonomy_adder('sp_league', 'sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Season', 'prosports');
        ?>
</strong></p>
			<p class="sp-tab-select">
				<?php 
        $args = array('show_option_all' => __('All', 'prosports'), 'taxonomy' => 'sp_season', 'name' => 'sp_season', 'selected' => $season_id, 'values' => 'term_id');
        if (!sp_dropdown_taxonomies($args)) {
            sp_taxonomy_adder('sp_season', 'sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Venue', 'prosports');
        ?>
</strong></p>
			<p>
				<?php 
        $args = array('show_option_all' => __('All', 'prosports'), 'taxonomy' => 'sp_venue', 'name' => 'sp_venue', 'selected' => $venue_id, 'values' => 'term_id');
        if (!sp_dropdown_taxonomies($args)) {
            sp_taxonomy_adder('sp_season', 'sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Team', 'prosports');
        ?>
</strong></p>
			<p>
				<?php 
        $args = array('show_option_all' => __('All', 'prosports'), 'post_type' => 'sp_team', 'name' => 'sp_team', 'selected' => $team_id, 'values' => 'ID');
        if (!sp_dropdown_pages($args)) {
            sp_post_adder('sp_team', __('Add New', 'prosports'));
        }
        ?>
			</p>
			<p><strong><?php 
        _e('Sort Order', 'prosports');
        ?>
</strong></p>
			<p>
				<select name="sp_order">
					<option value="ASC" <?php 
        selected('ASC', $order);
        ?>
><?php 
        _e('Ascending', 'prosports');
        ?>
</option>
					<option value="DESC" <?php 
        selected('DESC', $order);
        ?>
><?php 
        _e('Descending', 'prosports');
        ?>
</option>
				</select>
			</p>
		</div>
		<?php 
    }
 /**
  * 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);
     $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 players automatically if set to auto
     if ('auto' == $select) {
         $player_ids = array();
         $args = array('post_type' => 'sp_player', 'numberposts' => -1, 'posts_per_page' => -1, 'order' => 'ASC', '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);
         }
         if ($team) {
             $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;
             $totals[$player_id][$key] = 0;
         }
         foreach ($outcome_labels as $key => $value) {
             $totals[$player_id][$key] = 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);
         // Add metrics and static stats to placeholders
         $placeholders[$player_id] = array_merge($metrics, sp_array_value(sp_array_value($static, $league_id, array()), $div_id, array()));
     }
     $args = array('post_type' => 'sp_event', 'numberposts' => -1, 'posts_per_page' => -1, 'order' => 'ASC', '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_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_list_data_event_args', $args);
     $events = get_posts($args);
     // Event loop
     foreach ($events as $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());
                             // Find the outcome
                             if (array_key_exists('outcome', $team_results)) {
                                 // 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_results['outcome'];
                                     // 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]++;
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     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;
             }
         } 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();
         $player_data['number'] = get_post_meta($player_id, 'sp_number', true);
         $player_data['name'] = get_the_title($player_id);
         $player_data['team'] = get_post_meta($player_id, 'sp_team', true);
         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') {
                 $labels[$key] = __('Team', '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)) {
             $labels['team'] = __('Team', 'sportspress');
         }
         $merged[0] = array_merge($labels, $columns);
         return $merged;
     }
 }
    /**
     * Admin edit table
     */
    public static function table($labels = array(), $columns = array(), $data = array(), $team_id, $has_checkboxes = false)
    {
        ?>
		<div class="sp-data-table-container">
			<table class="widefat sp-data-table sp-performance-table sp-sortable-table">
				<thead>
					<tr>
						<th class="icon">&nbsp;</th>
						<th>#</th>
						<th><?php 
        _e('Player', 'prosports');
        ?>
</th>
						<th>
							<?php 
        if ($has_checkboxes) {
            ?>
								<label for="sp_columns_position">
									<input type="checkbox" name="sp_columns[]" value="position" id="sp_columns_position" <?php 
            checked(!is_array($columns) || in_array('position', $columns));
            ?>
>
									<?php 
            _e('Position', 'prosports');
            ?>
								</label>
							<?php 
        } else {
            ?>
								<?php 
            _e('Position', 'prosports');
            ?>
							<?php 
        }
        ?>
						</th>
						<?php 
        foreach ($labels as $key => $label) {
            ?>
							<th>
								<?php 
            if ($has_checkboxes) {
                ?>
									<label for="sp_columns_<?php 
                echo $key;
                ?>
">
										<input type="checkbox" name="sp_columns[]" value="<?php 
                echo $key;
                ?>
" id="sp_columns_<?php 
                echo $key;
                ?>
" <?php 
                checked(!is_array($columns) || in_array($key, $columns));
                ?>
>
										<?php 
                echo $label;
                ?>
									</label>
								<?php 
            } else {
                ?>
									<?php 
                echo $label;
                ?>
								<?php 
            }
            ?>
							</th>
						<?php 
        }
        ?>
						<?php 
        if ($team_id) {
            ?>
							<th>
								<?php 
            _e('Status', 'prosports');
            ?>
							</th>
						<?php 
        } else {
            ?>
							<th>
								<?php 
            _e('Outcome', 'prosports');
            ?>
							</th>
						<?php 
        }
        ?>
					</tr>
				</thead>
				<tfoot>
					<tr class="sp-row sp-total">
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td><strong><?php 
        _e('Total', 'prosports');
        ?>
</strong></td>
						<td>&nbsp;</td>
						<?php 
        foreach ($labels as $column => $label) {
            $player_id = 0;
            $player_performance = sp_array_value($data, 0, array());
            $value = sp_array_value($player_performance, $column, '');
            ?>
							<td><input type="text" name="sp_players[<?php 
            echo $team_id;
            ?>
][<?php 
            echo $player_id;
            ?>
][<?php 
            echo $column;
            ?>
]" value="<?php 
            echo $value;
            ?>
" placeholder="0" /></td>
						<?php 
        }
        ?>
						<td>&nbsp;</td>
					</tr>
				</tfoot>
				<tbody>
					<?php 
        foreach ($data as $player_id => $player_performance) {
            if (!$player_id) {
                continue;
            }
            $number = get_post_meta($player_id, 'sp_number', true);
            $value = sp_array_value($player_performance, 'number', '');
            ?>
						<tr class="sp-row sp-post" data-player="<?php 
            echo $player_id;
            ?>
">
							<td class="icon"><span class="dashicons dashicons-menu post-state-format"></span></td>
							<td>
								<input class="small-text sp-player-number-input" type="text" name="sp_players[<?php 
            echo $team_id;
            ?>
][<?php 
            echo $player_id;
            ?>
][number]" value="<?php 
            echo $value;
            ?>
" />
							</td>
							<td><?php 
            echo get_the_title($player_id);
            ?>
</td>
							<td>
								<?php 
            $selected = sp_array_value($player_performance, 'position', null);
            if ($selected == null) {
                $selected = sp_get_the_term_id($player_id, 'sp_position', 0);
            }
            $args = array('taxonomy' => 'sp_position', 'name' => 'sp_players[' . $team_id . '][' . $player_id . '][position]', 'values' => 'term_id', 'orderby' => 'slug', 'selected' => $selected);
            sp_dropdown_taxonomies($args);
            ?>
							</td>
							<?php 
            foreach ($labels as $column => $label) {
                $value = sp_array_value($player_performance, $column, '');
                ?>
								<td>
									<input class="sp-player-<?php 
                echo $column;
                ?>
-input" type="text" name="sp_players[<?php 
                echo $team_id;
                ?>
][<?php 
                echo $player_id;
                ?>
][<?php 
                echo $column;
                ?>
]" value="<?php 
                echo $value;
                ?>
" placeholder="0" />
								</td>
							<?php 
            }
            ?>
							<?php 
            if ($team_id) {
                ?>
								<td class="sp-status-selector">
									<?php 
                echo self::status_select($team_id, $player_id, sp_array_value($player_performance, 'status', null));
                ?>
									<?php 
                echo self::sub_select($team_id, $player_id, sp_array_value($player_performance, 'sub', null), $data);
                ?>
								</td>
							<?php 
            } else {
                ?>
								<td>
									<?php 
                $values = sp_array_value($player_performance, 'outcome', '');
                if (!is_array($values)) {
                    $values = array($values);
                }
                $args = array('post_type' => 'sp_outcome', 'name' => 'sp_players[' . $team_id . '][' . $player_id . '][outcome][]', 'option_none_value' => '', 'sort_order' => 'ASC', 'sort_column' => 'menu_order', 'selected' => $values, 'class' => 'sp-outcome', 'property' => 'multiple', 'chosen' => true, 'placeholder' => __('None', 'prosports'));
                sp_dropdown_pages($args);
                ?>
								</td>
							<?php 
            }
            ?>
						</tr>
						<?php 
        }
        ?>
				</tbody>
			</table>
		</div>
		<?php 
    }
    /**
     * Admin edit table
     */
    public static function table($labels = array(), $data = null, $rounds = 3, $rows = 23, $post_id = null)
    {
        $args = array('post_type' => 'sp_event', 'posts_per_page' => -1, 'post_status' => 'any', 'meta_key' => 'sp_format', 'meta_value' => 'tournament', 'tax_query' => array());
        // Filter by league if selected
        $league_id = sp_get_the_term_id($post_id, 'sp_league', 0);
        if ($league_id) {
            $args['tax_query'][] = array('taxonomy' => 'sp_league', 'field' => 'id', 'terms' => $league_id);
        }
        // Filter by season if selected
        $season_id = sp_get_the_term_id($post_id, 'sp_season', 0);
        if ($season_id) {
            $args['tax_query'][] = array('taxonomy' => 'sp_season', 'field' => 'id', 'terms' => $season_id);
        }
        // Get events
        $posts = get_posts($args);
        ?>
		<table class="widefat sp-tournament-container">
			<thead>
				<tr>
					<?php 
        for ($round = 0; $round < $rounds; $round++) {
            ?>
						<th>
							<input type="text" class="widefat" name="sp_labels[]" value="<?php 
            echo sp_array_value($labels, $round, '');
            ?>
" placeholder="<?php 
            printf(__('Round %s', 'prosports'), $round + 1);
            ?>
">
						</th>
					<?php 
        }
        ?>
				</tr>
			</thead>
			<tbody>
				<?php 
        for ($row = 0; $row < $rows; $row++) {
            ?>
					<tr>
						<?php 
            for ($round = 0; $round < $rounds; $round++) {
                $cell = sp_array_value(sp_array_value($data, $row, array()), $round, null);
                if ($cell === null) {
                    continue;
                }
                $index = sp_array_value($cell, 'index');
                $event = sp_array_value($cell, 'event', 0);
                if (sp_array_value($cell, 'type', null) === 'event') {
                    echo '<td rowspan="' . sp_array_value($cell, 'rows', 1) . '" class="sp-event' . ($round === 0 ? ' sp-first-round' : '') . ($round === $rounds - 1 ? ' sp-last-round' : '') . '">';
                    echo '<select class="postform sp-event-selector" name="sp_event[' . $index . ']" data-event="' . $index . '">';
                    echo '<option value="0" data-home="" data-away="">' . sprintf(__('Select %s', 'prosports'), __('Event', 'prosports')) . '</option>';
                    foreach ($posts as $post) {
                        $teams = get_post_meta($post->ID, 'sp_team');
                        $home = array_shift($teams);
                        $away = array_shift($teams);
                        echo '<option value="' . $post->ID . '" data-home="' . get_the_title($home) . '" data-away="' . get_the_title($away) . '"' . selected($event, $post->ID) . '>' . date_i18n(get_option('date_format')) . ' &mdash; ' . $post->post_title . '</option>';
                    }
                    echo '</select>';
                    echo '</td>';
                } elseif (sp_array_value($cell, 'type', null) === 'team') {
                    echo '<td rowspan="' . sp_array_value($cell, 'rows', 1) . '" class="sp-team' . ($round === 0 ? ' sp-first-round' : '') . ($round === $rounds - 1 ? ' sp-last-round' : '') . '">
									<input type="text" readonly="readonly" class="widefat sp-team-display" data-event="' . $index . '">
								</td>';
                } else {
                    echo '<td rowspan="' . sp_array_value($cell, 'rows', 1) . '">&nbsp;</td>';
                }
            }
            ?>
					</tr>
				<?php 
        }
        ?>
			</tbody>
		</table>
		<?php 
    }
 /**
  * Save meta box data
  */
 public static function save($post_id, $post)
 {
     update_post_meta($post_id, 'sp_minutes', sp_array_value($_POST, 'sp_minutes', get_option('prosports_event_minutes', 90)));
     $taxonomies = apply_filters('prosports_event_taxonomies', array('sp_league' => null, 'sp_season' => null, 'sp_venue' => 'sp_event'));
     foreach ($taxonomies as $taxonomy => $post_type) {
         $value = sp_array_value($_POST, $taxonomy, -1);
         if (0 == $value) {
             $teams = sp_array_value($_POST, 'sp_team', array());
             $team = reset($teams);
             $value = sp_get_the_term_id($team, $taxonomy);
         }
         wp_set_post_terms($post_id, $value, $taxonomy);
         update_post_meta($post_id, $taxonomy, $value);
     }
 }