/** * 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, ' '); $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; } }
function prosports_get_var_equations($post_type) { return sp_get_var_equations($post_type); }
/** * 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); }