ID, 'sp_league' ); $season_ids = sp_get_the_term_ids( $this->ID, 'sp_season' ); $table_stats = (array)get_post_meta( $this->ID, 'sp_teams', true ); $usecolumns = get_post_meta( $this->ID, 'sp_columns', true ); $adjustments = get_post_meta( $this->ID, 'sp_adjustments', true ); $select = get_post_meta( $this->ID, 'sp_select', true ); $link_events = get_option( 'sportspress_link_events', 'yes' ) === 'yes' ? true : false; $form_limit = (int) get_option( 'sportspress_form_limit', 5 ); $this->date = $this->__get( 'date' ); if ( ! $this->date ) $this->date = 0; // Apply defaults if ( empty( $select ) ) $select = 'auto'; if ( 'range' == $this->date ) { $this->relative = get_post_meta( $this->ID, 'sp_date_relative', true ); if ( $this->relative ) { $this->past = get_post_meta( $this->ID, 'sp_date_past', true ); } else { $this->from = get_post_meta( $this->ID, 'sp_date_from', true ); $this->to = get_post_meta( $this->ID, 'sp_date_to', 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 post type $post_type = sp_get_post_mode_type( $this->ID ); // Determine if main loop if ( $team_ids ) { $is_main_loop = false; } else { // Get teams automatically if set to auto if ( 'auto' == $select ) { $team_ids = array(); $args = array( 'post_type' => $post_type, 'numberposts' => -1, 'posts_per_page' => -1, 'order' => 'ASC', 'tax_query' => array( 'relation' => 'AND', ), 'fields' => 'ids', ); if ( $league_ids ): $args['tax_query'][] = array( 'taxonomy' => 'sp_league', 'field' => 'term_id', 'terms' => $league_ids ); endif; if ( $season_ids ): $args['tax_query'][] = array( 'taxonomy' => 'sp_season', 'field' => 'term_id', 'terms' => $season_ids ); endif; $team_ids = get_posts( $args ); } else { $team_ids = (array)get_post_meta( $this->ID, 'sp_team', false ); } $is_main_loop = true; } // Get all leagues populated with stats where available $tempdata = sp_array_combine( $team_ids, $table_stats ); // Create entry for each team in totals $totals = array(); $placeholders = array(); // Initialize incremental counter $this->pos = 0; $this->counter = 0; // Initialize team compare $this->compare = null; // Initialize streaks counter $streaks = array(); // Initialize form counter $forms = array(); // Initialize last counters $last5s = array(); $last10s = array(); // Initialize record counters $homerecords = array(); $awayrecords = array(); foreach ( $team_ids as $team_id ): if ( ! $team_id ) continue; // Initialize team streaks counter $streaks[ $team_id ] = array( 'name' => '', 'count' => 0, 'fire' => 1 ); // Initialize team form counter $forms[ $team_id ] = array(); // Initialize team last counters $last5s[ $team_id ] = array(); $last10s[ $team_id ] = array(); // Initialize team record counters $homerecords[ $team_id ] = array(); $awayrecords[ $team_id ] = array(); // Add outcome types to team last and record counters foreach( $outcome_labels as $key => $value ): $last5s[ $team_id ][ $key ] = 0; $last10s[ $team_id ][ $key ] = 0; $homerecords[ $team_id ][ $key ] = 0; $awayrecords[ $team_id ][ $key ] = 0; endforeach; // Initialize team totals $totals[ $team_id ] = array( 'eventsplayed' => 0, 'eventsplayed_home' => 0, 'eventsplayed_away' => 0, 'eventsplayed_venue' => 0, 'eventminutes' => 0, 'eventminutes_home' => 0, 'eventminutes_away' => 0, 'eventminutes_venue' => 0, 'streak' => 0, 'streak_home' => 0, 'streak_away' => 0, 'streak_venue' => 0, ); foreach ( $result_labels as $key => $value ): $totals[ $team_id ][ $key . 'for' ] = 0; $totals[ $team_id ][ $key . 'for_home' ] = 0; $totals[ $team_id ][ $key . 'for_away' ] = 0; $totals[ $team_id ][ $key . 'for_venue' ] = 0; $totals[ $team_id ][ $key . 'against' ] = 0; $totals[ $team_id ][ $key . 'against_home' ] = 0; $totals[ $team_id ][ $key . 'against_away' ] = 0; $totals[ $team_id ][ $key . 'against_venue' ] = 0; endforeach; foreach ( $outcome_labels as $key => $value ): $totals[ $team_id ][ $key ] = 0; $totals[ $team_id ][ $key . '_home' ] = 0; $totals[ $team_id ][ $key . '_away' ] = 0; $totals[ $team_id ][ $key . '_venue' ] = 0; endforeach; // Get static stats $static = get_post_meta( $team_id, 'sp_columns', true ); if ( 'yes' == get_option( 'sportspress_team_column_editing', 'no' ) && $league_ids && $season_ids ): // Add static stats to placeholders foreach ( $league_ids as $league_id ): foreach ( $season_ids as $season_id ): $placeholders[ $team_id ] = (array) sp_array_value( sp_array_value( $static, $league_id, array() ), $season_id, array() ); endforeach; endforeach; endif; endforeach; // Get which event status to include $event_status = get_post_meta( $this->ID, 'sp_event_status', true ); if ( empty( $event_status ) ) { $event_status = array( 'publish', 'future' ); } if ( isset( $this->show_published_events ) ) { // If an attribute was pass through shortcode if ( $this->show_published_events == '1' ) { $event_status[] = 'publish'; }else{ if ( ( $status_key = array_search( 'publish', $event_status ) ) !== false ) { unset( $event_status[ $status_key ] ); } } } if ( isset( $this->show_future_events ) ) { // If an attribute was pass through shortcode if ( $this->show_future_events == '1' ) { $event_status[] = 'future'; }else{ if ( ( $status_key = array_search('future', $event_status) ) !== false ) { unset( $event_status[ $status_key ] ); } } } // Make sure to have unique values in the array $event_status = array_unique( $event_status ); $args = array( 'post_type' => 'sp_event', 'post_status' => $event_status, 'numberposts' => -1, 'posts_per_page' => -1, 'orderby' => 'post_date', 'order' => 'DESC', 'meta_query' => array( array( 'key' => 'sp_format', 'value' => apply_filters( 'sportspress_competitive_event_formats', array( 'league' ) ), 'compare' => 'IN', ), ), 'tax_query' => array( 'relation' => 'AND', ), ); if ( $league_ids ): $args['tax_query'][] = array( 'taxonomy' => 'sp_league', 'field' => 'term_id', 'terms' => $league_ids ); endif; if ( $season_ids ): $args['tax_query'][] = array( 'taxonomy' => 'sp_season', 'field' => 'term_id', 'terms' => $season_ids ); endif; if ( $this->date !== 0 ): if ( $this->date == 'w' ): $args['year'] = date_i18n('Y'); $args['w'] = date_i18n('W'); elseif ( $this->date == 'day' ): $args['year'] = date_i18n('Y'); $args['day'] = date_i18n('j'); $args['monthnum'] = date_i18n('n'); elseif ( $this->date == 'range' ): if ( $this->relative ): add_filter( 'posts_where', array( $this, 'relative' ) ); else: add_filter( 'posts_where', array( $this, 'range' ) ); endif; endif; endif; $args = apply_filters( 'sportspress_table_data_event_args', $args ); if ( ! $is_main_loop ): if ( sizeof( $team_ids ) ): $args['meta_query'][] = array( 'key' => 'sp_team', 'value' => $team_ids, 'compare' => 'IN', ); endif; endif; $events = get_posts( $args ); // Remove range filters remove_filter( 'posts_where', array( $this, 'range' ) ); remove_filter( 'posts_where', array( $this, 'relative' ) ); $e = 0; // Event loop foreach ( $events as $event ): $teams = (array)get_post_meta( $event->ID, 'sp_team', false ); $teams = array_filter( $teams ); if ( ! $is_main_loop && sizeof( array_diff( $teams, $team_ids ) ) ) continue; $results = (array)get_post_meta( $event->ID, 'sp_results', true ); $minutes = get_post_meta( $event->ID, 'sp_minutes', true ); if ( $minutes === '' ) $minutes = get_option( 'sportspress_event_minutes', 90 ); $i = 0; foreach ( $results as $team_id => $team_result ): if ( ! in_array( $team_id, $teams ) ) continue; if ( ! in_array( $team_id, $team_ids ) ) { $i++; continue; } if ( $team_result ): foreach ( $team_result as $key => $value ): if ( $key == 'outcome' ): if ( ! is_array( $value ) ): $value = array( $value ); endif; foreach ( $value as $outcome ): // Increment events played and outcome count if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $outcome, $totals[ $team_id ] ) ): $totals[ $team_id ]['eventsplayed'] ++; $totals[ $team_id ]['eventminutes'] += $minutes; $totals[ $team_id ][ $outcome ] ++; // Add to home or away stats if ( 0 === $i ): $totals[ $team_id ]['eventsplayed_home'] ++; $totals[ $team_id ]['eventminutes_home'] += $minutes; $totals[ $team_id ][ $outcome . '_home' ] ++; else: $totals[ $team_id ]['eventsplayed_away'] ++; $totals[ $team_id ]['eventminutes_away'] += $minutes; $totals[ $team_id ][ $outcome . '_away' ] ++; endif; // Add to venue stats if ( sp_is_home_venue( $team_id, $event->ID ) ): $totals[ $team_id ]['eventsplayed_venue'] ++; $totals[ $team_id ]['eventminutes_venue'] += $minutes; $totals[ $team_id ][ $outcome . '_venue' ] ++; endif; endif; if ( $outcome && $outcome != '-1' ): // Add to streak counter if ( $streaks[ $team_id ]['fire'] && ( $streaks[ $team_id ]['name'] == '' || $streaks[ $team_id ]['name'] == $outcome ) ): $streaks[ $team_id ]['name'] = $outcome; $streaks[ $team_id ]['count'] ++; else: $streaks[ $team_id ]['fire'] = 0; endif; // Add to form counter $forms[ $team_id ][] = array( 'id' => $event->ID, 'outcome' => $outcome, ); // Add to last 5 counter if sum is less than 5 if ( array_key_exists( $team_id, $last5s ) && array_key_exists( $outcome, $last5s[ $team_id ] ) && array_sum( $last5s[ $team_id ] ) < 5 ): $last5s[ $team_id ][ $outcome ] ++; endif; // Add to last 10 counter if sum is less than 10 if ( array_key_exists( $team_id, $last10s ) && array_key_exists( $outcome, $last10s[ $team_id ] ) && array_sum( $last10s[ $team_id ] ) < 10 ): $last10s[ $team_id ][ $outcome ] ++; endif; // Add to home or away record if ( 0 === $i ) { if ( array_key_exists( $team_id, $homerecords ) && array_key_exists( $outcome, $homerecords[ $team_id ] ) ) { $homerecords[ $team_id ][ $outcome ] ++; } } else { if ( array_key_exists( $team_id, $awayrecords ) && array_key_exists( $outcome, $awayrecords[ $team_id ] ) ) { $awayrecords[ $team_id ][ $outcome ] ++; } } endif; endforeach; else: if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $key . 'for', $totals[ $team_id ] ) ): // Get numeric value $value = floatval( $value ); $totals[ $team_id ][ $key . 'for' ] += $value; $totals[ $team_id ][ $key . 'for' . ( $e + 1 ) ] = $value; // Add to home or away stats if ( 0 === $i ): $totals[ $team_id ][ $key . 'for_home' ] += $value; else: $totals[ $team_id ][ $key . 'for_away' ] += $value; endif; // Add to venue stats if ( sp_is_home_venue( $team_id, $event->ID ) ): $totals[ $team_id ][ $key . 'for_venue' ] += $value; endif; foreach( $results as $other_team_id => $other_result ): if ( $other_team_id != $team_id && array_key_exists( $key . 'against', $totals[ $team_id ] ) ): // Get numeric value of other team's result $value = floatval( sp_array_value( $other_result, $key, 0 ) ); $totals[ $team_id ][ $key . 'against' ] += $value; $totals[ $team_id ][ $key . 'against' . ( $e + 1 ) ] = $value; // Add to home or away stats if ( 0 === $i ): $totals[ $team_id ][ $key . 'against_home' ] += $value; else: $totals[ $team_id ][ $key . 'against_away' ] += $value; endif; // Add to venue stats if ( sp_is_home_venue( $team_id, $event->ID ) ): $totals[ $team_id ][ $key . 'against_venue' ] += $value; endif; endif; endforeach; endif; endif; endforeach; endif; $i++; endforeach; $e++; endforeach; // Get outcomes $outcomes = array(); $args = array( 'post_type' => 'sp_outcome', 'post_status' => 'publish', 'posts_per_page' => -1, ); $posts = get_posts( $args ); if ( $posts ): foreach ( $posts as $post ): // Get ID $id = $post->ID; // Get title $title = $post->post_title; // Get abbreviation $abbreviation = get_post_meta( $id, 'sp_abbreviation', true ); if ( ! $abbreviation ): $abbreviation = substr( $title, 0, 1 ); endif; // Get color $color = get_post_meta( $id, 'sp_color', true ); if ( '' === $color ) $color = '#888888'; $outcomes[ $post->post_name ] = array( 'id' => $id, 'title' => $title, 'abbreviation' => $abbreviation, 'color' => $color, ); endforeach; endif; foreach ( $streaks as $team_id => $streak ): // Compile streaks counter and add to totals if ( $streak['name'] ): $outcome = sp_array_value( $outcomes, $streak['name'], false ); if ( $outcome ): $color = $outcome['color']; $totals[ $team_id ]['streak'] = '' . $outcome['abbreviation'] . $streak['count'] . ''; else: $totals[ $team_id ]['streak'] = null; endif; else: $totals[ $team_id ]['streak'] = null; endif; endforeach; foreach ( $forms as $team_id => $form ): // Apply form limit if ( $form_limit && sizeof( $form ) > $form_limit ): $form = array_slice( $form, 0, $form_limit ); endif; // Initialize team form array $team_form = array(); // Reverse form array to display in chronological order $form = array_reverse( $form ); // Loop through event form foreach ( $form as $form_event ): if ( $form_event['id'] ): $outcome = sp_array_value( $outcomes, $form_event['outcome'], false ); if ( $outcome ): $abbreviation = $outcome['abbreviation']; $color = $outcome['color']; if ( $link_events ): $abbreviation = '' . $abbreviation . ''; else: $abbreviation = '' . $abbreviation . ''; endif; // Add to team form $team_form[] = $abbreviation; endif; endif; endforeach; // Append to totals if ( sizeof( $team_form ) ): $totals[ $team_id ]['form'] = '