Move post type specific functions into classes

This commit is contained in:
Brian Miyaji
2014-04-26 17:52:43 +10:00
parent b6342b97e3
commit 284b0f1a5f
36 changed files with 2234 additions and 3062 deletions

View File

@@ -68,7 +68,7 @@ class SP_Admin_Menus {
*/ */
public function menu_highlight() { public function menu_highlight() {
global $typenow, $submenu; global $typenow, $submenu;
if ( sp_is_config_type( $typenow ) ) if ( is_sp_config_type( $typenow ) )
$this->highlight_admin_menu(); $this->highlight_admin_menu();
elseif ( $typenow == 'sp_calendar' ) elseif ( $typenow == 'sp_calendar' )
$this->highlight_admin_menu( 'edit.php?post_type=sp_event', 'edit.php?post_type=sp_calendar' ); $this->highlight_admin_menu( 'edit.php?post_type=sp_event', 'edit.php?post_type=sp_calendar' );

View File

@@ -54,7 +54,7 @@ class SP_Admin_Post_Types {
public function post_updated_messages( $messages ) { public function post_updated_messages( $messages ) {
global $typenow, $post; global $typenow, $post;
if ( sp_is_config_type( $typenow ) ): if ( is_sp_config_type( $typenow ) ):
$obj = get_post_type_object( $typenow ); $obj = get_post_type_object( $typenow );
for ( $i = 0; $i <= 10; $i++ ): for ( $i = 0; $i <= 10; $i++ ):

View File

@@ -19,8 +19,10 @@ class SP_Meta_Box_Calendar_Data {
* Output the metabox * Output the metabox
*/ */
public static function output( $post ) { public static function output( $post ) {
list( $data, $usecolumns ) = sp_get_calendar_data( $post->ID, true ); $calendar = new SP_Calendar( $post );
sp_edit_calendar_table( $data, $usecolumns ); $data = $calendar->data();
$usecolumns = $calendar->columns;
self::table( $data, $usecolumns );
} }
/** /**
@@ -29,4 +31,132 @@ class SP_Meta_Box_Calendar_Data {
public static function save( $post_id, $post ) { public static function save( $post_id, $post ) {
update_post_meta( $post_id, 'sp_columns', sp_array_value( $_POST, 'sp_columns', array() ) ); update_post_meta( $post_id, 'sp_columns', sp_array_value( $_POST, 'sp_columns', array() ) );
} }
/**
* Admin edit table
*/
public static function table( $data = array(), $usecolumns = null ) {
if ( is_array( $usecolumns ) )
$usecolumns = array_filter( $usecolumns );
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table sp-calendar-table">
<thead>
<tr>
<th class="column-date">
<?php _e( 'Date', 'sportspress' ); ?>
</th>
<th class="column-event">
<label for="sp_columns_event">
<input type="checkbox" name="sp_columns[]" value="event" id="sp_columns_event" <?php checked( ! is_array( $usecolumns ) || in_array( 'event', $usecolumns ) ); ?>>
<?php _e( 'Event', 'sportspress' ); ?>
</label>
</th>
<th class="column-teams">
<label for="sp_columns_teams">
<input type="checkbox" name="sp_columns[]" value="teams" id="sp_columns_teams" <?php checked( ! is_array( $usecolumns ) || in_array( 'teams', $usecolumns ) ); ?>>
<?php _e( 'Teams', 'sportspress' ); ?>
</label>
</th>
<th class="column-time">
<label for="sp_columns_time">
<input type="checkbox" name="sp_columns[]" value="time" id="sp_columns_time" <?php checked( ! is_array( $usecolumns ) || in_array( 'time', $usecolumns ) ); ?>>
<?php _e( 'Time', 'sportspress' ); ?>
</label>
</th>
<th class="column-venue">
<label for="sp_columns_venue">
<input type="checkbox" name="sp_columns[]" value="venue" id="sp_columns_venue" <?php checked( ! is_array( $usecolumns ) || in_array( 'venue', $usecolumns ) ); ?>>
<?php _e( 'Venue', 'sportspress' ); ?>
</label>
</th>
<th class="column-article">
<label for="sp_columns_article">
<input type="checkbox" name="sp_columns[]" value="article" id="sp_columns_article" <?php checked( ! is_array( $usecolumns ) || in_array( 'article', $usecolumns ) ); ?>>
<?php _e( 'Article', 'sportspress' ); ?>
</label>
</th>
</tr>
</thead>
<tbody>
<?php
if ( is_array( $data ) && sizeof( $data ) > 0 ):
$main_result = get_option( 'sportspress_primary_result', null );
$i = 0;
foreach ( $data as $event ):
$teams = get_post_meta( $event->ID, 'sp_team' );
$results = get_post_meta( $event->ID, 'sp_results', true );
$video = get_post_meta( $event->ID, 'sp_video', true );
?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td><?php echo get_post_time( get_option( 'date_format' ), false, $event ); ?></td>
<td><?php echo $event->post_title; ?></td>
<td>
<?php
foreach ( $teams as $team ):
$name = get_the_title( $team );
if ( $name ):
$team_results = sp_array_value( $results, $team, null );
if ( $main_result ):
$team_result = sp_array_value( $team_results, $main_result, null );
else:
if ( is_array( $team_results ) ):
end( $team_results );
$team_result = prev( $team_results );
else:
$team_result = null;
endif;
endif;
if ( $team_result != null ):
unset( $team_results['outcome'] );
$team_results = implode( ' | ', $team_results );
echo '<a class="result tips" title="' . $team_results . '" href="' . get_edit_post_link( $event->ID ) . '">' . $team_result . '</a> ';
endif;
echo $name . '<br>';
endif;
endforeach;
?>
</td>
<td><?php echo get_post_time( get_option( 'time_format' ), false, $event ); ?></td>
<td><?php the_terms( $event->ID, 'sp_venue' ); ?></td>
<td>
<a href="<?php echo get_edit_post_link( $event->ID ); ?>#sp_articlediv">
<?php if ( $video ): ?>
<div class="dashicons dashicons-video-alt"></div>
<?php elseif ( has_post_thumbnail( $event->ID ) ): ?>
<div class="dashicons dashicons-camera"></div>
<?php endif; ?>
<?php
if ( $event->post_content == null ):
_e( 'None', 'sportspress' );
elseif ( $event->post_status == 'publish' ):
_e( 'Recap', 'sportspress' );
else:
_e( 'Preview', 'sportspress' );
endif;
?>
</a>
</td>
</tr>
<?php
$i++;
endforeach;
else:
?>
<tr class="sp-row alternate">
<td colspan="6">
<?php printf( __( 'Select %s', 'sportspress' ), __( 'Details', 'sportspress' ) ); ?>
</td>
</tr>
<?php
endif;
?>
</tbody>
</table>
</div>
<?php
}
} }

View File

@@ -5,15 +5,18 @@
* @author ThemeBoy * @author ThemeBoy
* @category Admin * @category Admin
* @package SportsPress/Admin/Meta Boxes * @package SportsPress/Admin/Meta Boxes
* @version 0.7 * @version 0.8
*/ */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'SP_Meta_Box_Config' ) )
include( 'class-sp-meta-box-config.php' );
/** /**
* SP_Meta_Box_Column_Details * SP_Meta_Box_Column_Details
*/ */
class SP_Meta_Box_Column_Details { class SP_Meta_Box_Column_Details extends SP_Meta_Box_Config {
/** /**
* Output the metabox * Output the metabox
@@ -37,7 +40,7 @@ class SP_Meta_Box_Column_Details {
<p class="sp-equation-selector"> <p class="sp-equation-selector">
<?php <?php
foreach ( $equation as $piece ): foreach ( $equation as $piece ):
sp_equation_selector( $post->ID, $piece, array( 'team_event', 'outcome', 'result' ) ); self::select( $post->ID, $piece, array( 'team_event', 'outcome', 'result' ) );
endforeach; endforeach;
?> ?>
</p> </p>
@@ -75,7 +78,7 @@ class SP_Meta_Box_Column_Details {
* Save meta box data * Save meta box data
*/ */
public static function save( $post_id, $post ) { public static function save( $post_id, $post ) {
sp_delete_duplicate_post( $_POST ); self::delete_duplicate( $_POST );
update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) ); update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) );
update_post_meta( $post_id, 'sp_precision', (int) sp_array_value( $_POST, 'sp_precision', 1 ) ); update_post_meta( $post_id, 'sp_precision', (int) sp_array_value( $_POST, 'sp_precision', 1 ) );
update_post_meta( $post_id, 'sp_priority', sp_array_value( $_POST, 'sp_priority', '0' ) ); update_post_meta( $post_id, 'sp_priority', sp_array_value( $_POST, 'sp_priority', '0' ) );

View File

@@ -0,0 +1,156 @@
<?php
/**
* Config type meta box functions
*
* @author ThemeBoy
* @category Admin
* @package SportsPress/Admin/Meta Boxes
* @version 0.8
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* SP_Meta_Box_Config
*/
class SP_Meta_Box_Config {
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
self::delete_duplicate( $_POST );
}
public static function select( $postid, $selected = null, $groups = array() ) {
if ( ! isset( $postid ) )
return;
// Initialize options array
$options = array();
// Add groups to options
foreach ( $groups as $group ):
switch ( $group ):
case 'player_event':
$options[ __( 'Events', 'sportspress' ) ] = array( '$eventsattended' => __( 'Attended', 'sportspress' ), '$eventsplayed' => __( 'Played', 'sportspress' ) );
break;
case 'team_event':
$options[ __( 'Events', 'sportspress' ) ] = array( '$eventsplayed' => __( 'Played', 'sportspress' ) );
break;
case 'result':
$options[ __( 'Results', 'sportspress' ) ] = self::optgroup( $postid, 'sp_result', array( 'for' => '&rarr;', 'against' => '&larr;' ), null, false );
break;
case 'outcome':
$options[ __( 'Outcomes', 'sportspress' ) ] = self::optgroup( $postid, 'sp_outcome', array() );
$options[ __( 'Outcomes', 'sportspress' ) ]['$streak'] = __( 'Streak', 'sportspress' );
$options[ __( 'Outcomes', 'sportspress' ) ]['$last5'] = __( 'Last 5', 'sportspress' );
$options[ __( 'Outcomes', 'sportspress' ) ]['$last10'] = __( 'Last 10', 'sportspress' );
break;
case 'performance':
$options[ __( 'Performance', 'sportspress' ) ] = self::optgroup( $postid, 'sp_performance' );
break;
case 'metric':
$options[ __( 'Metric', 'sportspress' ) ] = self::optgroup( $postid, 'sp_metric' );
break;
endswitch;
endforeach;
// Create array of operators
$operators = array( '+' => '&plus;', '-' => '&minus;', '*' => '&times;', '/' => '&divide;', '(' => '(', ')' => ')' );
// Add operators to options
$options[ __( 'Operators', 'sportspress' ) ] = $operators;
// Create array of constants
$max = 10;
$constants = array();
for ( $i = 1; $i <= $max; $i ++ ):
$constants[$i] = $i;
endfor;
// Add 100 to constants
$constants[100] = 100;
// Add constants to options
$options[ __( 'Constants', 'sportspress' ) ] = (array) $constants;
?>
<select name="sp_equation[]">
<option value=""><?php _e( '&mdash; Select &mdash;', 'sportspress' ); ?></option>
<?php
foreach ( $options as $label => $option ):
printf( '<optgroup label="%s">', $label );
foreach ( $option as $key => $value ):
printf( '<option value="%s" %s>%s</option>', $key, selected( true, $key == $selected, false ), $value );
endforeach;
echo '</optgroup>';
endforeach;
?>
</select>
<?php
}
public static function optgroup( $postid, $type = null, $variations = null, $defaults = null, $totals = true ) {
$arr = array();
// Get posts
$args = array(
'post_type' => $type,
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'exclude' => $postid
);
$vars = get_posts( $args );
// Add extra vars to the array
if ( isset( $defaults ) && is_array( $defaults ) ):
foreach ( $defaults as $key => $value ):
$arr[ $key ] = $value;
endforeach;
endif;
// Add vars to the array
if ( isset( $variations ) && is_array( $variations ) ):
foreach ( $vars as $var ):
if ( $totals ) $arr[ '$' . $var->post_name ] = $var->post_title;
foreach ( $variations as $key => $value ):
$arr[ '$' . $var->post_name . $key ] = $var->post_title . ' ' . $value;
endforeach;
endforeach;
else:
foreach ( $vars as $var ):
$arr[ '$' . $var->post_name ] = $var->post_title;
endforeach;
endif;
return (array) $arr;
}
public static function delete_duplicate( &$post ) {
global $wpdb;
$key = isset( $post['sp_key'] ) ? $post['sp_key'] : null;
if ( ! $key ) $key = $post['post_title'];
$id = sp_array_value( $post, 'post_ID', 'var' );
$title = sp_get_eos_safe_slug( $key, $id );
$check_sql = "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $title, $post['post_type'], $id ) );
if ( $post_name_check ):
wp_delete_post( $post_name_check, true );
$post['post_status'] = 'draft';
endif;
return $post_name_check;
}
}

View File

@@ -5,7 +5,7 @@
* @author ThemeBoy * @author ThemeBoy
* @category Admin * @category Admin
* @package SportsPress/Admin/Meta Boxes * @package SportsPress/Admin/Meta Boxes
* @version 0.7 * @version 0.8
*/ */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@@ -35,7 +35,7 @@ class SP_Meta_Box_Event_Performance {
?> ?>
<div> <div>
<p><strong><?php echo get_the_title( $team_id ); ?></strong></p> <p><strong><?php echo get_the_title( $team_id ); ?></strong></p>
<?php sp_edit_event_players_table( $columns, $data, $team_id ); ?> <?php self::table( $columns, $data, $team_id ); ?>
</div> </div>
<?php <?php
@@ -48,4 +48,115 @@ class SP_Meta_Box_Event_Performance {
public static function save( $post_id, $post ) { public static function save( $post_id, $post ) {
update_post_meta( $post_id, 'sp_players', sp_array_value( $_POST, 'sp_players', array() ) ); update_post_meta( $post_id, 'sp_players', sp_array_value( $_POST, 'sp_players', array() ) );
} }
/**
* Admin edit table
*/
public static function table( $columns = array(), $data = array(), $team_id ) {
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table sp-performance-table">
<thead>
<tr>
<th>#</th>
<th><?php _e( 'Player', 'sportspress' ); ?></th>
<?php foreach ( $columns as $label ): ?>
<th><?php echo $label; ?></th>
<?php endforeach; ?>
<th><?php _e( 'Status', 'sportspress' ); ?></th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ( $data as $player_id => $player_performance ):
if ( !$player_id ) continue;
$number = get_post_meta( $player_id, 'sp_number', true );
?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>" data-player="<?php echo $player_id; ?>">
<td><?php echo ( $number ? $number : '&nbsp;' ); ?></td>
<td><?php echo get_the_title( $player_id ); ?></td>
<?php foreach( $columns 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 endforeach; ?>
<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>
</tr>
<?php
$i++;
endforeach;
?>
<tr class="sp-row sp-total<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td>&nbsp;</td>
<td><strong><?php _e( 'Total', 'sportspress' ); ?></strong></td>
<?php foreach( $columns 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 endforeach; ?>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
</div>
<?php
}
/**
* Status selector
*/
public static function status_select( $team_id, $player_id, $value = null ) {
if ( ! $team_id || ! $player_id )
return '&mdash;';
$options = array(
'lineup' => __( 'Starting Lineup', 'sportspress' ),
'sub' => __( 'Substitute', 'sportspress' ),
);
$output = '<select name="sp_players[' . $team_id . '][' . $player_id . '][status]">';
foreach( $options as $key => $name ):
$output .= '<option value="' . $key . '"' . ( $key == $value ? ' selected' : '' ) . '>' . $name . '</option>';
endforeach;
$output .= '</select>';
return $output;
}
/**
* Substitute selector
*/
public static function sub_select( $team_id, $player_id, $value, $data = array() ) {
if ( ! $team_id || ! $player_id )
return '&mdash;';
$output = '<select name="sp_players[' . $team_id . '][' . $player_id . '][sub]" style="display: none;">';
$output .= '<option value="0">' . __( 'None', 'sportspress' ) . '</option>';
// Add players as selectable options
foreach( $data as $id => $performance ):
if ( ! $id || $id == $player_id ) continue;
$number = get_post_meta( $id, 'sp_number', true );
$output .= '<option value="' . $id . '"' . ( $id == $value ? ' selected' : '' ) . '>' . ( $number ? $number . '. ' : '' ) . get_the_title( $id ) . '</option>';
endforeach;
$output .= '</select>';
return $output;
}
} }

View File

@@ -5,7 +5,7 @@
* @author ThemeBoy * @author ThemeBoy
* @category Admin * @category Admin
* @package SportsPress/Admin/Meta Boxes * @package SportsPress/Admin/Meta Boxes
* @version 0.7 * @version 0.8
*/ */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@@ -31,7 +31,7 @@ class SP_Meta_Box_Event_Results {
?> ?>
<div> <div>
<?php sp_edit_event_results_table( $columns, $data ); ?> <?php self::table( $columns, $data ); ?>
</div> </div>
<?php <?php
} }
@@ -43,4 +43,66 @@ class SP_Meta_Box_Event_Results {
$results = (array)sp_array_value( $_POST, 'sp_results', array() ); $results = (array)sp_array_value( $_POST, 'sp_results', array() );
update_post_meta( $post_id, 'sp_results', $results ); update_post_meta( $post_id, 'sp_results', $results );
} }
/**
* Admin edit table
*/
public static function table( $columns = array(), $data = array() ) {
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table">
<thead>
<tr>
<th class="column-team"><?php _e( 'Team', 'sportspress' ); ?></th>
<?php foreach ( $columns as $key => $label ): ?>
<th class="outcome-<?php echo $key; ?>"><?php echo $label; ?></th>
<?php endforeach; ?>
<th class="column-outcome"><?php _e( 'Outcome', 'sportspress' ); ?></th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ( $data as $team_id => $team_results ):
if ( !$team_id ) continue;
?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td>
<?php echo get_the_title( $team_id ); ?>
</td>
<?php foreach( $columns as $column => $label ):
$value = sp_array_value( $team_results, $column, '' );
?>
<td><input type="text" name="sp_results[<?php echo $team_id; ?>][<?php echo $column; ?>]" value="<?php echo $value; ?>" /></td>
<?php endforeach; ?>
<td>
<?php
$values = sp_array_value( $team_results, 'outcome', '' );
if ( ! is_array( $values ) )
$values = array( $values );
$args = array(
'post_type' => 'sp_outcome',
'name' => 'sp_results[' . $team_id . '][outcome][]',
'option_none_value' => '',
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
'selected' => $values,
'class' => 'sp-outcome',
'property' => 'multiple',
'chosen' => true,
);
sp_dropdown_pages( $args );
?>
</td>
</tr>
<?php
$i++;
endforeach;
?>
</tbody>
</table>
</div>
<?php
}
} }

View File

@@ -10,10 +10,13 @@
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'SP_Meta_Box_Config' ) )
include( 'class-sp-meta-box-config.php' );
/** /**
* SP_Meta_Box_Metric_Details * SP_Meta_Box_Metric_Details
*/ */
class SP_Meta_Box_Metric_Details { class SP_Meta_Box_Metric_Details extends SP_Meta_Box_Config {
/** /**
* Output the metabox * Output the metabox
@@ -29,11 +32,4 @@ class SP_Meta_Box_Metric_Details {
</p> </p>
<?php <?php
} }
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
sp_delete_duplicate_post( $_POST );
}
} }

View File

@@ -10,10 +10,13 @@
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'SP_Meta_Box_Config' ) )
include( 'class-sp-meta-box-config.php' );
/** /**
* SP_Meta_Box_Outcome_Details * SP_Meta_Box_Outcome_Details
*/ */
class SP_Meta_Box_Outcome_Details { class SP_Meta_Box_Outcome_Details extends SP_Meta_Box_Config {
/** /**
* Output the metabox * Output the metabox
@@ -28,11 +31,4 @@ class SP_Meta_Box_Outcome_Details {
</p> </p>
<?php <?php
} }
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
sp_delete_duplicate_post( $_POST );
}
} }

View File

@@ -10,17 +10,19 @@
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'SP_Meta_Box_Config' ) )
include( 'class-sp-meta-box-config.php' );
/** /**
* SP_Meta_Box_Performance_Details * SP_Meta_Box_Performance_Details
*/ */
class SP_Meta_Box_Performance_Details { class SP_Meta_Box_Performance_Details extends SP_Meta_Box_Config {
/** /**
* Output the metabox * Output the metabox
*/ */
public static function output( $post ) { public static function output( $post ) {
wp_nonce_field( 'sportspress_save_data', 'sportspress_meta_nonce' ); wp_nonce_field( 'sportspress_save_data', 'sportspress_meta_nonce' );
$calculate = get_post_meta( $post->ID, 'sp_calculate', true );
?> ?>
<p><strong><?php _e( 'Variable', 'sportspress' ); ?></strong></p> <p><strong><?php _e( 'Variable', 'sportspress' ); ?></strong></p>
<p> <p>
@@ -29,11 +31,4 @@ class SP_Meta_Box_Performance_Details {
</p> </p>
<?php <?php
} }
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
sp_delete_duplicate_post( $_POST );
}
} }

View File

@@ -5,7 +5,7 @@
* @author ThemeBoy * @author ThemeBoy
* @category Admin * @category Admin
* @package SportsPress/Admin/Meta Boxes * @package SportsPress/Admin/Meta Boxes
* @version 0.7 * @version 0.8
*/ */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
@@ -32,9 +32,11 @@ class SP_Meta_Box_Player_Performance {
<?php <?php
endif; endif;
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = sp_get_player_performance_data( $post->ID, $league->term_id, true ); $player = new SP_Player( $post );
sp_edit_player_performance_table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, ! current_user_can( 'edit_sp_teams' ) ); list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( $league->term_id, true );
self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, ! current_user_can( 'edit_sp_teams' ) );
endforeach; else: endforeach; else:
@@ -51,4 +53,85 @@ class SP_Meta_Box_Player_Performance {
if ( current_user_can( 'edit_sp_teams' ) ) if ( current_user_can( 'edit_sp_teams' ) )
update_post_meta( $post_id, 'sp_performance', sp_array_value( $_POST, 'sp_performance', array() ) ); update_post_meta( $post_id, 'sp_performance', sp_array_value( $_POST, 'sp_performance', array() ) );
} }
/**
* Admin edit table
*/
public static function table( $id = null, $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $leagues = array(), $readonly = true ) {
if ( ! $id )
$id = get_the_ID();
$teams = array_filter( get_post_meta( $id, 'sp_team', false ) );
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table">
<thead>
<tr>
<th><?php _e( 'Season', 'sportspress' ); ?></th>
<th><?php _e( 'Team', 'sportspress' ); ?></th>
<?php foreach ( $columns as $label ): ?>
<th><?php echo $label; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ( $data as $div_id => $div_stats ):
if ( !$div_id || $div_id == 'performance' ) continue;
$div = get_term( $div_id, 'sp_season' );
?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td>
<?php echo $div->name; ?>
</td>
<td>
<?php
$value = sp_array_value( $leagues, $div_id, '-1' );
$args = array(
'post_type' => 'sp_team',
'name' => 'sp_leagues[' . $league_id . '][' . $div_id . ']',
'show_option_none' => __( '&mdash; None &mdash;', 'sportspress' ),
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
'selected' => $value,
'values' => 'ID',
'include' => $teams,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'sp_league',
'terms' => $league_id,
'field' => 'id',
),
array(
'taxonomy' => 'sp_season',
'terms' => $div_id,
'field' => 'id',
),
),
);
if ( ! sp_dropdown_pages( $args ) ):
_e( 'No results found.', 'sportspress' );
endif;
?>
</td>
<?php foreach( $columns as $column => $label ):
?>
<td><?php
$value = sp_array_value( sp_array_value( $data, $div_id, array() ), $column, null );
$placeholder = sp_array_value( sp_array_value( $placeholders, $div_id, array() ), $column, 0 );
echo '<input type="text" name="sp_performance[' . $league_id . '][' . $div_id . '][' . $column . ']" value="' . $value . '" placeholder="' . $placeholder . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' />';
?></td>
<?php endforeach; ?>
</tr>
<?php
$i++;
endforeach;
?>
</tbody>
</table>
</div>
<?php
}
} }

View File

@@ -32,9 +32,10 @@ class SP_Meta_Box_Player_Statistics {
<?php <?php
endif; endif;
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = sp_get_player_statistics_data( $post->ID, $league->term_id, true ); $player = new SP_Player( $post );
list( $columns, $data, $placeholders, $merged, $seasons_teams ) = $player->data( $league->term_id, true );
sp_edit_player_statistics_table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, ! current_user_can( 'edit_sp_teams' ) ); self::table( $post->ID, $league->term_id, $columns, $data, $placeholders, $merged, $seasons_teams, ! current_user_can( 'edit_sp_teams' ) );
endforeach; else: endforeach; else:
@@ -51,4 +52,82 @@ class SP_Meta_Box_Player_Statistics {
if ( current_user_can( 'edit_sp_teams' ) ) if ( current_user_can( 'edit_sp_teams' ) )
update_post_meta( $post_id, 'sp_statistics', sp_array_value( $_POST, 'sp_statistics', array() ) ); update_post_meta( $post_id, 'sp_statistics', sp_array_value( $_POST, 'sp_statistics', array() ) );
} }
/**
* Admin edit table
*/
public static function table( $id = null, $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $leagues = array(), $readonly = true ) {
$teams = array_filter( get_post_meta( $id, 'sp_team', false ) );
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table">
<thead>
<tr>
<th><?php _e( 'Season', 'sportspress' ); ?></th>
<th><?php _e( 'Team', 'sportspress' ); ?></th>
<?php foreach ( $columns as $label ): ?>
<th><?php echo $label; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ( $data as $div_id => $div_stats ):
if ( !$div_id || $div_id == 'statistics' ) continue;
$div = get_term( $div_id, 'sp_season' );
?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td>
<?php echo $div->name; ?>
</td>
<td>
<?php
$value = sp_array_value( $leagues, $div_id, '-1' );
$args = array(
'post_type' => 'sp_team',
'name' => 'sp_leagues[' . $league_id . '][' . $div_id . ']',
'show_option_none' => __( '&mdash; None &mdash;', 'sportspress' ),
'sort_order' => 'ASC',
'sort_column' => 'menu_order',
'selected' => $value,
'values' => 'ID',
'include' => $teams,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'sp_league',
'terms' => $league_id,
'field' => 'id',
),
array(
'taxonomy' => 'sp_season',
'terms' => $div_id,
'field' => 'id',
),
),
);
if ( ! sp_dropdown_pages( $args ) ):
_e( 'No results found.', 'sportspress' );
endif;
?>
</td>
<?php foreach( $columns as $column => $label ):
?>
<td><?php
$value = sp_array_value( sp_array_value( $data, $div_id, array() ), $column, null );
$placeholder = sp_array_value( sp_array_value( $placeholders, $div_id, array() ), $column, 0 );
echo '<input type="text" name="sp_statistics[' . $league_id . '][' . $div_id . '][' . $column . ']" value="' . $value . '" placeholder="' . $placeholder . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' />';
?></td>
<?php endforeach; ?>
</tr>
<?php
$i++;
endforeach;
?>
</tbody>
</table>
</div>
<?php
}
} }

View File

@@ -5,15 +5,18 @@
* @author ThemeBoy * @author ThemeBoy
* @category Admin * @category Admin
* @package SportsPress/Admin/Meta Boxes * @package SportsPress/Admin/Meta Boxes
* @version 0.7 * @version 0.8
*/ */
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'SP_Meta_Box_Config' ) )
include( 'class-sp-meta-box-config.php' );
/** /**
* SP_Meta_Box_Result_Details * SP_Meta_Box_Result_Details
*/ */
class SP_Meta_Box_Result_Details { class SP_Meta_Box_Result_Details extends SP_Meta_Box_Config {
/** /**
* Output the metabox * Output the metabox
@@ -28,11 +31,4 @@ class SP_Meta_Box_Result_Details {
</p> </p>
<?php <?php
} }
/**
* Save meta box data
*/
public static function save( $post_id, $post ) {
sp_delete_duplicate_post( $_POST );
}
} }

View File

@@ -10,10 +10,13 @@
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'SP_Meta_Box_Config' ) )
include( 'class-sp-meta-box-config.php' );
/** /**
* SP_Meta_Box_Statistic_Details * SP_Meta_Box_Statistic_Details
*/ */
class SP_Meta_Box_Statistic_Details { class SP_Meta_Box_Statistic_Details extends SP_Meta_Box_Config {
/** /**
* Output the metabox * Output the metabox
@@ -35,7 +38,7 @@ class SP_Meta_Box_Statistic_Details {
<p class="sp-equation-selector"> <p class="sp-equation-selector">
<?php <?php
foreach ( $equation as $piece ): foreach ( $equation as $piece ):
sp_equation_selector( $post->ID, $piece, array( 'player_event', 'outcome', 'performance', 'metric' ) ); self::select( $post->ID, $piece, array( 'player_event', 'outcome', 'performance', 'metric' ) );
endforeach; endforeach;
?> ?>
</p> </p>
@@ -50,8 +53,9 @@ class SP_Meta_Box_Statistic_Details {
* Save meta box data * Save meta box data
*/ */
public static function save( $post_id, $post ) { public static function save( $post_id, $post ) {
sp_delete_duplicate_post( $_POST ); self::delete_duplicate( $_POST );
update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) ); update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) );
update_post_meta( $post_id, 'sp_precision', (int) sp_array_value( $_POST, 'sp_precision', 1 ) ); update_post_meta( $post_id, 'sp_precision', (int) sp_array_value( $_POST, 'sp_precision', 1 ) );
} }
} }

View File

@@ -19,7 +19,7 @@ class SP_Meta_Box_Table_Data {
* Output the metabox * Output the metabox
*/ */
public static function output( $post ) { public static function output( $post ) {
$table = new SP_Table( $post ); $table = new SP_League_Table( $post );
list( $columns, $usecolumns, $data, $placeholders, $merged ) = $table->data( true ); list( $columns, $usecolumns, $data, $placeholders, $merged ) = $table->data( true );
$adjustments = $table->adjustments; $adjustments = $table->adjustments;
self::table( $columns, $usecolumns, $data, $placeholders, $adjustments ); self::table( $columns, $usecolumns, $data, $placeholders, $adjustments );

View File

@@ -34,9 +34,11 @@ class SP_Meta_Box_Team_Columns {
<?php <?php
endif; endif;
list( $columns, $data, $placeholders, $merged, $leagues_seasons ) = sp_get_team_columns_data( $post->ID, $league_id, true ); $team = new SP_Team( $post );
sp_edit_team_columns_table( $league_id, $columns, $data, $placeholders, $merged, $leagues_seasons, ! current_user_can( 'edit_sp_tables' ) ); list( $columns, $data, $placeholders, $merged, $leagues_seasons ) = $team->data( $league_id, true );
self::table( $league_id, $columns, $data, $placeholders, $merged, $leagues_seasons, ! current_user_can( 'edit_sp_tables' ) );
endforeach; else: endforeach; else:
@@ -53,4 +55,54 @@ class SP_Meta_Box_Team_Columns {
if ( current_user_can( 'edit_sp_tables' ) ) if ( current_user_can( 'edit_sp_tables' ) )
update_post_meta( $post_id, 'sp_columns', sp_array_value( $_POST, 'sp_columns', array() ) ); update_post_meta( $post_id, 'sp_columns', sp_array_value( $_POST, 'sp_columns', array() ) );
} }
/**
* Admin edit table
*/
public static function table( $league_id, $columns = array(), $data = array(), $placeholders = array(), $merged = array(), $seasons = array(), $readonly = true ) {
?>
<div class="sp-data-table-container">
<table class="widefat sp-data-table sp-select-all-range">
<thead>
<tr>
<th class="check-column"><input class="sp-select-all" type="checkbox"></th>
<th><?php _e( 'Season', 'sportspress' ); ?></th>
<?php foreach ( $columns as $label ): ?>
<th><?php echo $label; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ( $data as $div_id => $div_stats ):
if ( !$div_id ) continue;
$div = get_term( $div_id, 'sp_season' );
?>
<tr class="sp-row sp-post<?php if ( $i % 2 == 0 ) echo ' alternate'; ?>">
<td>
<input type="checkbox" name="sp_leagues[<?php echo $league_id; ?>][<?php echo $div_id; ?>]" id="sp_leagues_<?php echo $league_id; ?>_<?php echo $div_id; ?>" value="1" <?php checked( sp_array_value( $seasons, $div_id, 0 ), 1 ); ?>>
</td>
<td>
<label for="sp_leagues_<?php echo $league_id; ?>_<?php echo $div_id; ?>"><?php echo $div->name; ?></label>
</td>
<?php foreach( $columns as $column => $label ):
$value = sp_array_value( sp_array_value( $data, $div_id, array() ), $column, 0 );
?>
<td><?php
$value = sp_array_value( sp_array_value( $data, $div_id, array() ), $column, null );
$placeholder = sp_array_value( sp_array_value( $placeholders, $div_id, array() ), $column, 0 );
echo '<input type="text" name="sp_columns[' . $league_id . '][' . $div_id . '][' . $column . ']" value="' . $value . '" placeholder="' . $placeholder . '"' . ( $readonly ? ' disabled="disabled"' : '' ) . ' />';
?></td>
<?php endforeach; ?>
</tr>
<?php
$i++;
endforeach;
?>
</tbody>
</table>
</div>
<?php
}
} }

View File

@@ -0,0 +1,139 @@
<?php
/**
* Calendar Class
*
* The SportsPress calendar class handles individual calendar data.
*
* @class SP_Calendar
* @version 0.8
* @package SportsPress/Classes
* @category Class
* @author ThemeBoy
*/
class SP_Calendar {
/** @var int The calendar (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Calendar ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
/**
* Returns formatted data
*
* @access public
* @return array
*/
public function data() {
global $pagenow;
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'post_date',
'order' => 'ASC',
'post_status' => 'any',
'tax_query' => array(
'relation' => 'AND'
),
);
if ( $pagenow != 'post-new.php' ):
if ( $this->ID ):
$leagues = get_the_terms( $this->ID, 'sp_league' );
$seasons = get_the_terms( $this->ID, 'sp_season' );
$venues = get_the_terms( $this->ID, 'sp_venue' );
$team = get_post_meta( $this->ID, 'sp_team', true );
if ( $leagues ):
$league_ids = array();
foreach( $leagues as $league ):
$league_ids[] = $league->term_id;
endforeach;
$args['tax_query'][] = array(
'taxonomy' => 'sp_league',
'field' => 'id',
'terms' => $league_ids
);
endif;
if ( $seasons ):
$season_ids = array();
foreach( $seasons as $season ):
$season_ids[] = $season->term_id;
endforeach;
$args['tax_query'][] = array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $season_ids
);
endif;
if ( $venues ):
$venue_ids = array();
foreach( $venues as $venue ):
$venue_ids[] = $venue->term_id;
endforeach;
$args['tax_query'][] = array(
'taxonomy' => 'sp_venue',
'field' => 'id',
'terms' => $venue_ids
);
endif;
if ( $team ):
$args['meta_query'] = array(
array(
'key' => 'sp_team',
'value' => $team,
),
);
endif;
endif;
$events = get_posts( $args );
else:
$events = array();
endif;
return $events;
}
}

View File

@@ -1,883 +0,0 @@
<?php
/**
* SportsPress data formatter
*
* The SportsPress data formatter class formats data for use in admin and templates.
*
* @class SP_Data_Formatter
* @version 0.8
* @package SportsPress/Classes
* @category Class
* @author ThemeBoy
*/
class SP_Data_Formatter {
private $table_priorities;
private $list_priorities;
/**
* Formats data for team columns
*/
function team_columns( $post_id, $league_id, $admin = false ) {
$seasons = (array)get_the_terms( $post_id, 'sp_season' );
$columns = (array)get_post_meta( $post_id, 'sp_columns', true );
$leagues_seasons = sp_array_value( (array)get_post_meta( $post_id, 'sp_leagues', true ), $league_id, array() );
// Get labels from result variables
$result_labels = (array)sp_get_var_labels( 'sp_result' );
// Get labels from outcome variables
$outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
// Generate array of all season ids and season names
$div_ids = array();
$season_names = array();
foreach ( $seasons as $season ):
if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ):
$div_ids[] = $season->term_id;
$season_names[ $season->term_id ] = $season->name;
endif;
endforeach;
$data = array();
// Get all seasons populated with data where available
$data = sp_array_combine( $div_ids, sp_array_value( $columns, $league_id, array() ) );
// Get equations from column variables
$equations = sp_get_var_equations( 'sp_column' );
// Initialize placeholders array
$placeholders = array();
foreach ( $div_ids as $div_id ):
$totals = array( 'eventsplayed' => 0, 'streak' => 0, 'last5' => null, 'last10' => null );
foreach ( $result_labels as $key => $value ):
$totals[ $key . 'for' ] = 0;
$totals[ $key . 'against' ] = 0;
endforeach;
foreach ( $outcome_labels as $key => $value ):
$totals[ $key ] = 0;
endforeach;
// Initialize streaks counter
$streak = array( 'name' => '', 'count' => 0, 'fire' => 1 );
// Initialize last counters
$last5 = array();
$last10 = array();
// Add outcome types to last counters
foreach( $outcome_labels as $key => $value ):
$last5[ $key ] = 0;
$last10[ $key ] = 0;
endforeach;
// Get all events involving the team in current season
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'sp_team',
'value' => $post_id
),
array(
'key' => 'sp_format',
'value' => 'league'
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'sp_league',
'field' => 'id',
'terms' => $league_id
),
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
),
)
);
$events = get_posts( $args );
foreach( $events as $event ):
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
foreach ( $results as $team_id => $team_result ):
foreach ( $team_result as $key => $value ):
if ( $team_id == $post_id ):
if ( $key == 'outcome' ):
// Convert to array
if ( ! is_array( $value ) ):
$value = array( $value );
endif;
foreach( $value as $outcome ):
// Increment events played and outcome count
if ( array_key_exists( $outcome, $totals ) ):
$totals['eventsplayed']++;
$totals[ $outcome ]++;
endif;
if ( $outcome && $outcome != '-1' ):
// Add to streak counter
if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $outcome ) ):
$streak['name'] = $outcome;
$streak['count'] ++;
else:
$streak['fire'] = 0;
endif;
// Add to last 5 counter if sum is less than 5
if ( array_key_exists( $outcome, $last5 ) && array_sum( $last5 ) < 5 ):
$last5[ $outcome ] ++;
endif;
// Add to last 10 counter if sum is less than 10
if ( array_key_exists( $outcome, $last10 ) && array_sum( $last10 ) < 10 ):
$last10[ $outcome ] ++;
endif;
endif;
endforeach;
else:
if ( array_key_exists( $key . 'for', $totals ) ):
$totals[ $key . 'for' ] += $value;
endif;
endif;
else:
if ( $key != 'outcome' ):
if ( array_key_exists( $key . 'against', $totals ) ):
$totals[ $key . 'against' ] += $value;
endif;
endif;
endif;
endforeach;
endforeach;
endforeach;
// Compile streaks counter and add to totals
$args=array(
'name' => $streak['name'],
'post_type' => 'sp_outcome',
'post_status' => 'publish',
'posts_per_page' => 1
);
$outcomes = get_posts( $args );
if ( $outcomes ):
$outcome = reset( $outcomes );
$totals['streak'] = $outcome->post_title . $streak['count'];
endif;
// Add last counters to totals
$totals['last5'] = $last5;
$totals['last10'] = $last10;
// Generate array of placeholder values for each league
$placeholders[ $div_id ] = array();
foreach ( $equations as $key => $value ):
$placeholders[ $div_id ][ $key ] = sp_solve( $value['equation'], $totals, $value['precision'] );
endforeach;
endforeach;
// Get columns from column variables
$columns = sp_get_var_labels( 'sp_column' );
// Merge the data and placeholders arrays
$merged = array();
foreach( $placeholders as $season_id => $season_data ):
if ( ! sp_array_value( $leagues_seasons, $season_id, 0 ) )
continue;
$season_name = sp_array_value( $season_names, $season_id, '&nbsp;' );
// Add season name to row
$merged[ $season_id ] = array(
'name' => $season_name
);
foreach( $season_data as $key => $value ):
// Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $season_id, $data ) && array_key_exists( $key, $data[ $season_id ] ) && $data[ $season_id ][ $key ] != '' ):
$merged[ $season_id ][ $key ] = $data[ $season_id ][ $key ];
else:
$merged[ $season_id ][ $key ] = $value;
endif;
endforeach;
endforeach;
if ( $admin ):
return array( $columns, $data, $placeholders, $merged, $leagues_seasons );
else:
$labels = array_merge( array( 'name' => SP()->text->string('Season') ), $columns );
$merged[0] = $labels;
return $merged;
endif;
}
/**
* Formats data for league tables
*/
public function league_table( $post_id, $admin = false ) {
$league_id = sp_get_the_term_id( $post_id, 'sp_league', 0 );
$div_id = sp_get_the_term_id( $post_id, 'sp_season', 0 );
$team_ids = (array)get_post_meta( $post_id, 'sp_team', false );
$table_stats = (array)get_post_meta( $post_id, 'sp_teams', true );
$usecolumns = get_post_meta( $post_id, 'sp_columns', true );
$adjustments = get_post_meta( $post_id, 'sp_adjustments', true );
// Get labels from result variables
$result_labels = (array)sp_get_var_labels( 'sp_result' );
// Get labels from outcome variables
$outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
// Get all leagues populated with stats where available
$tempdata = sp_array_combine( $team_ids, $table_stats );
// Create entry for each team in totals
$totals = array();
$placeholders = array();
// Initialize streaks counter
$streaks = array();
// Initialize last counters
$last5s = array();
$last10s = array();
foreach ( $team_ids as $team_id ):
if ( ! $team_id )
continue;
// Initialize team streaks counter
$streaks[ $team_id ] = array( 'name' => '', 'count' => 0, 'fire' => 1 );
// Initialize team last counters
$last5s[ $team_id ] = array();
$last10s[ $team_id ] = array();
// Add outcome types to team last counters
foreach( $outcome_labels as $key => $value ):
$last5s[ $team_id ][ $key ] = 0;
$last10s[ $team_id ][ $key ] = 0;
endforeach;
// Initialize team totals
$totals[ $team_id ] = array( 'eventsplayed' => 0, 'streak' => 0 );
foreach ( $result_labels as $key => $value ):
$totals[ $team_id ][ $key . 'for' ] = 0;
$totals[ $team_id ][ $key . 'against' ] = 0;
endforeach;
foreach ( $outcome_labels as $key => $value ):
$totals[ $team_id ][ $key ] = 0;
endforeach;
// Get static stats
$static = get_post_meta( $team_id, 'sp_columns', true );
// Add static stats to placeholders
$placeholders[ $team_id ] = sp_array_value( $static, $div_id, array() );
endforeach;
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'sp_league',
'field' => 'id',
'terms' => $league_id
),
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
)
)
);
$events = get_posts( $args );
// Event loop
foreach ( $events as $event ):
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
foreach ( $results as $team_id => $team_result ):
if ( ! in_array( $team_id, $team_ids ) )
continue;
foreach ( $team_result as $key => $value ):
if ( $key == 'outcome' ):
if ( ! is_array( $value ) ):
$value = array( $value );
endif;
foreach ( $value as $outcome ):
// Increment events played and outcome count
if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $outcome, $totals[ $team_id ] ) ):
$totals[ $team_id ]['eventsplayed']++;
$totals[ $team_id ][ $outcome ]++;
endif;
if ( $outcome && $outcome != '-1' ):
// Add to streak counter
if ( $streaks[ $team_id ]['fire'] && ( $streaks[ $team_id ]['name'] == '' || $streaks[ $team_id ]['name'] == $outcome ) ):
$streaks[ $team_id ]['name'] = $outcome;
$streaks[ $team_id ]['count'] ++;
else:
$streaks[ $team_id ]['fire'] = 0;
endif;
// Add to last 5 counter if sum is less than 5
if ( array_key_exists( $team_id, $last5s ) && array_key_exists( $outcome, $last5s[ $team_id ] ) && array_sum( $last5s[ $team_id ] ) < 5 ):
$last5s[ $team_id ][ $outcome ] ++;
endif;
// Add to last 10 counter if sum is less than 10
if ( array_key_exists( $team_id, $last10s ) && array_key_exists( $outcome, $last10s[ $team_id ] ) && array_sum( $last10s[ $team_id ] ) < 10 ):
$last10s[ $team_id ][ $outcome ] ++;
endif;
endif;
endforeach;
else:
if ( array_key_exists( $team_id, $totals ) && is_array( $totals[ $team_id ] ) && array_key_exists( $key . 'for', $totals[ $team_id ] ) ):
$totals[ $team_id ][ $key . 'for' ] += $value;
foreach( $results as $other_team_id => $other_result ):
if ( $other_team_id != $team_id && array_key_exists( $key . 'against', $totals[ $team_id ] ) ):
$totals[ $team_id ][ $key . 'against' ] += sp_array_value( $other_result, $key, 0 );
endif;
endforeach;
endif;
endif;
endforeach;
endforeach;
endforeach;
foreach ( $streaks as $team_id => $streak ):
// Compile streaks counter and add to totals
if ( $streak['name'] ):
$args = array(
'name' => $streak['name'],
'post_type' => 'sp_outcome',
'post_status' => 'publish',
'posts_per_page' => 1
);
$outcomes = get_posts( $args );
if ( $outcomes ):
$outcome = reset( $outcomes );
$totals[ $team_id ]['streak'] = $outcome->post_title . $streak['count'];
else:
$totals[ $team_id ]['streak'] = null;
endif;
else:
$totals[ $team_id ]['streak'] = null;
endif;
endforeach;
foreach ( $last5s as $team_id => $last5 ):
// Add last 5 to totals
$totals[ $team_id ]['last5'] = $last5;
endforeach;
foreach ( $last10s as $team_id => $last10 ):
// Add last 10 to totals
$totals[ $team_id ]['last10'] = $last10;
endforeach;
$args = array(
'post_type' => 'sp_column',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$stats = get_posts( $args );
$columns = array();
$this->table_priorities = array();
foreach ( $stats as $stat ):
// Get post meta
$meta = get_post_meta( $stat->ID );
// Add equation to object
$stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 );
$stat->precision = sp_array_value( sp_array_value( $meta, 'sp_precision', array() ), 0, 0 );
// Add column name to columns
$columns[ $stat->post_name ] = $stat->post_title;
// Add order to priorities if priority is set and does not exist in array already
$priority = sp_array_value( sp_array_value( $meta, 'sp_priority', array() ), 0, 0 );
if ( $priority && ! array_key_exists( $priority, $this->table_priorities ) ):
$this->table_priorities[ $priority ] = array(
'column' => $stat->post_name,
'order' => sp_array_value( sp_array_value( $meta, 'sp_order', array() ), 0, 'DESC' )
);
endif;
endforeach;
// Sort priorities in descending order
ksort( $this->table_priorities );
// Fill in empty placeholder values for each team
foreach ( $team_ids as $team_id ):
if ( ! $team_id )
continue;
foreach ( $stats as $stat ):
if ( sp_array_value( $placeholders[ $team_id ], $stat->post_name, '' ) == '' ):
// Solve
$placeholder = sp_solve( $stat->equation, sp_array_value( $totals, $team_id, array() ), $stat->precision );
// Adjustments
$placeholder += sp_array_value( sp_array_value( $adjustments, $team_id, array() ), $stat->post_name, 0 );
$placeholders[ $team_id ][ $stat->post_name ] = $placeholder;
endif;
endforeach;
endforeach;
// Merge the data and placeholders arrays
$merged = array();
foreach( $placeholders as $team_id => $team_data ):
// Add team name to row
$merged[ $team_id ] = array();
$team_data['name'] = get_the_title( $team_id );
foreach( $team_data as $key => $value ):
// Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $team_id, $tempdata ) && array_key_exists( $key, $tempdata[ $team_id ] ) && $tempdata[ $team_id ][ $key ] != '' ):
$merged[ $team_id ][ $key ] = $tempdata[ $team_id ][ $key ];
else:
$merged[ $team_id ][ $key ] = $value;
endif;
endforeach;
endforeach;
uasort( $merged, array( $this, 'sort_table' ) );
// Rearrange data array to reflect values
$data = array();
foreach( $merged as $key => $value ):
$data[ $key ] = $tempdata[ $key ];
endforeach;
if ( $admin ):
return array( $columns, $usecolumns, $data, $placeholders, $merged );
else:
if ( ! is_array( $usecolumns ) )
$usecolumns = array();
foreach ( $columns as $key => $label ):
if ( ! in_array( $key, $usecolumns ) ):
unset( $columns[ $key ] );
endif;
endforeach;
$labels = array_merge( array( 'name' => SP()->text->string('Team') ), $columns );
$merged[0] = $labels;
return $merged;
endif;
}
/**
* Formats data for player lists
*/
public static function player_list( $post_id, $admin = false ) {
$league_id = sp_get_the_term_id( $post_id, 'sp_league', 0 );
$div_id = sp_get_the_term_id( $post_id, 'sp_season', 0 );
$player_ids = (array)get_post_meta( $post_id, 'sp_player', false );
$list_stats = (array)get_post_meta( $post_id, 'sp_players', true );
$usecolumns = get_post_meta( $post_id, 'sp_columns', true );
$adjustments = get_post_meta( $post_id, 'sp_adjustments', true );
$orderby = get_post_meta( $post_id, 'sp_orderby', true );
$order = get_post_meta( $post_id, 'sp_order', true );
// Get labels from performance variables
$performance_labels = (array)sp_get_var_labels( 'sp_performance' );
// Get labels from outcome variables
$outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
// Get all leagues populated with stats where available
$tempdata = sp_array_combine( $player_ids, $list_stats );
// Create entry for each player in totals
$totals = array();
$placeholders = array();
// Initialize streaks counter
$streaks = array();
// Initialize last counters
$last5s = array();
$last10s = array();
foreach ( $player_ids as $player_id ):
if ( ! $player_id )
continue;
// Initialize player streaks counter
$streaks[ $player_id ] = array( 'name' => '', 'count' => 0, 'fire' => 1 );
// Initialize player last counters
$last5s[ $player_id ] = array();
$last10s[ $player_id ] = array();
// Add outcome types to player last counters
foreach( $outcome_labels as $key => $value ):
$last5s[ $player_id ][ $key ] = 0;
$last10s[ $player_id ][ $key ] = 0;
endforeach;
// Initialize player totals
$totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0, 'streak' => 0 );
foreach ( $performance_labels as $key => $value ):
$totals[ $player_id ][ $key ] = 0;
$totals[ $player_id ][ $key ] = 0;
endforeach;
foreach ( $outcome_labels as $key => $value ):
$totals[ $player_id ][ $key ] = 0;
endforeach;
// Get static stats
$static = get_post_meta( $player_id, 'sp_statistics', true );
// Add static stats to placeholders
$placeholders[ $player_id ] = sp_array_value( sp_array_value( $static, $league_id, array() ), $div_id, array() );
endforeach;
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'sp_league',
'field' => 'id',
'terms' => $league_id
),
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
)
)
);
$events = get_posts( $args );
// Event loop
foreach ( $events as $event ):
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
$team_performance = (array)get_post_meta( $event->ID, 'sp_players', true );
// Add all team performance
foreach ( $team_performance as $team_id => $players ):
foreach( $players as $player_id => $player_performance ):
if ( array_key_exists( $player_id, $totals ) && is_array( $totals[ $player_id ] ) ):
$player_performance = sp_array_value( $players, $player_id, array() );
foreach ( $player_performance as $key => $value ):
if ( array_key_exists( $key, $totals[ $player_id ] ) ):
$totals[ $player_id ][ $key ] += $value;
endif;
endforeach;
$team_results = sp_array_value( $results, $team_id, array() );
// Find the outcome
if ( array_key_exists( 'outcome', $team_results ) ):
$value = $team_results['outcome'];
// Convert to array
if ( ! is_array( $value ) ):
$value = array( $value );
endif;
foreach ( $value as $outcome ):
if ( $outcome && $outcome != '-1' ):
// Increment events attended and outcome count
if ( array_key_exists( $outcome, $totals[ $player_id ] ) ):
$totals[ $player_id ]['eventsattended']++;
$totals[ $player_id ][ $outcome ]++;
// Increment events played if active in event
if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ):
$totals[ $player_id ]['eventsplayed']++;
endif;
endif;
// Add to streak counter
if ( $streaks[ $player_id ]['fire'] && ( $streaks[ $player_id ]['name'] == '' || $streaks[ $player_id ]['name'] == $outcome ) ):
$streaks[ $player_id ]['name'] = $outcome;
$streaks[ $player_id ]['count'] ++;
else:
$streaks[ $player_id ]['fire'] = 0;
endif;
// Add to last 5 counter if sum is less than 5
if ( array_key_exists( $player_id, $last5s ) && array_key_exists( $outcome, $last5s[ $player_id ] ) && array_sum( $last5s[ $player_id ] ) < 5 ):
$last5s[ $player_id ][ $outcome ] ++;
endif;
// Add to last 10 counter if sum is less than 10
if ( array_key_exists( $player_id, $last10s ) && array_key_exists( $outcome, $last10s[ $player_id ] ) && array_sum( $last10s[ $player_id ] ) < 10 ):
$last10s[ $player_id ][ $outcome ] ++;
endif;
endif;
endforeach;
endif;
endif;
endforeach;
endforeach;
endforeach;
foreach ( $streaks as $player_id => $streak ):
// Compile streaks counter and add to totals
if ( $streak['name'] ):
$args = array(
'name' => $streak['name'],
'post_type' => 'sp_outcome',
'post_status' => 'publish',
'posts_per_page' => 1
);
$outcomes = get_posts( $args );
if ( $outcomes ):
$outcome = reset( $outcomes );
$totals[ $player_id ]['streak'] = $outcome->post_title . $streak['count'];
else:
$totals[ $player_id ]['streak'] = null;
endif;
else:
$totals[ $player_id ]['streak'] = null;
endif;
endforeach;
foreach ( $last5s as $player_id => $last5 ):
// Add last 5 to totals
$totals[ $player_id ]['last5'] = $last5;
endforeach;
foreach ( $last10s as $player_id => $last10 ):
// Add last 10 to totals
$totals[ $player_id ]['last10'] = $last10;
endforeach;
$args = array(
'post_type' => 'sp_statistic',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$stats = get_posts( $args );
$columns = array();
foreach ( $stats as $stat ):
// Get post meta
$meta = get_post_meta( $stat->ID );
// Add equation to object
$stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 );
$stat->precision = sp_array_value( sp_array_value( $meta, 'sp_precision', array() ), 0, 0 );
// Add column name to columns
$columns[ $stat->post_name ] = $stat->post_title;
endforeach;
// Fill in empty placeholder values for each player
foreach ( $player_ids as $player_id ):
if ( ! $player_id )
continue;
foreach ( $stats as $stat ):
if ( sp_array_value( $placeholders[ $player_id ], $stat->post_name, '' ) == '' ):
// Solve
$placeholder = sp_solve( $stat->equation, sp_array_value( $totals, $player_id, array() ), $stat->precision );
// Adjustments
$placeholder += sp_array_value( sp_array_value( $adjustments, $player_id, array() ), $stat->post_name, 0 );
$placeholders[ $player_id ][ $stat->post_name ] = $placeholder;
endif;
endforeach;
endforeach;
// Merge the data and placeholders arrays
$merged = array();
foreach( $placeholders as $player_id => $player_data ):
// Add player name to row
$merged[ $player_id ] = array();
$player_data['name'] = get_the_title( $player_id );
foreach( $player_data as $key => $value ):
// Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $player_id, $tempdata ) && array_key_exists( $key, $tempdata[ $player_id ] ) && $tempdata[ $player_id ][ $key ] != '' ):
$merged[ $player_id ][ $key ] = $tempdata[ $player_id ][ $key ];
else:
$merged[ $player_id ][ $key ] = $value;
endif;
endforeach;
endforeach;
if ( $orderby != 'number' || $order != 'ASC' ):
global $sportspress_statistic_priorities;
$sportspress_statistic_priorities = array(
array(
'key' => $orderby,
'order' => $order,
),
);
uasort( $merged, array( $this, 'sort_list' ) );
endif;
// Rearrange data array to reflect values
$data = array();
foreach( $merged as $key => $value ):
$data[ $key ] = $tempdata[ $key ];
endforeach;
if ( $admin ):
return array( $columns, $usecolumns, $data, $placeholders, $merged );
else:
if ( ! is_array( $usecolumns ) )
$usecolumns = array();
foreach ( $columns as $key => $label ):
if ( ! in_array( $key, $usecolumns ) ):
unset( $columns[ $key ] );
endif;
endforeach;
$labels = array_merge( array( 'name' => SP()->text->string('Player') ), $columns );
$merged[0] = $labels;
return $merged;
endif;
}
private function sort_table( $a, $b ) {
// Loop through priorities
foreach( $this->table_priorities as $priority ):
// Proceed if columns are not equal
if ( sp_array_value( $a, $priority['column'], 0 ) != sp_array_value( $b, $priority['column'], 0 ) ):
// Compare column values
$output = sp_array_value( $a, $priority['column'], 0 ) - sp_array_value( $b, $priority['column'], 0 );
// Flip value if descending order
if ( $priority['order'] == 'DESC' ) $output = 0 - $output;
return ( $output > 0 );
endif;
endforeach;
// Default sort by alphabetical
return strcmp( sp_array_value( $a, 'name', '' ), sp_array_value( $b, 'name', '' ) );
}
private function sort_list ( $a, $b ) {
// Loop through priorities
if ( is_array( $this->list_priorities ) ) : foreach( $this->list_priorities as $priority ):
// Proceed if columns are not equal
if ( sp_array_value( $a, $priority['key'], 0 ) != sp_array_value( $b, $priority['key'], 0 ) ):
if ( $priority['key'] == 'name' ):
$output = strcmp( sp_array_value( $a, 'name', null ), sp_array_value( $b, 'name', null ) );
else:
// Compare performance values
$output = sp_array_value( $a, $priority['key'], 0 ) - sp_array_value( $b, $priority['key'], 0 );
endif;
// Flip value if descending order
if ( $priority['order'] == 'DESC' ) $output = 0 - $output;
return ( $output > 0 );
endif;
endforeach; endif;
// Default sort by number
return sp_array_value( $a, 'number', 0 ) - sp_array_value( $b, 'number', 0 );
}
}

303
includes/class-sp-event.php Normal file
View File

@@ -0,0 +1,303 @@
<?php
/**
* Event Class
*
* The SportsPress event class handles individual event data.
*
* @class SP_Event
* @version 0.8
* @package SportsPress/Classes
* @category Class
* @author ThemeBoy
*/
class SP_Event {
/** @var int The event (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Event ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
/**
* Returns formatted data
*
* @access public
* @param int $league_id
* @param bool $admin
* @return array
*/
public function data( $league_id, $admin = false ) {
$seasons = (array)get_the_terms( $this->ID, 'sp_season' );
$stats = (array)get_post_meta( $this->ID, 'sp_statistics', true );
$leagues = sp_array_value( (array)get_post_meta( $this->ID, 'sp_leagues', true ), $league_id, array() );
// Get labels from performance variables
$performance_labels = (array)sp_get_var_labels( 'sp_performance' );
// Get labels from outcome variables
$outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
// Generate array of all season ids and season names
$div_ids = array();
$season_names = array();
foreach ( $seasons as $season ):
if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ):
$div_ids[] = $season->term_id;
$season_names[ $season->term_id ] = $season->name;
endif;
endforeach;
$data = array();
// Get all seasons populated with data where available
$data = sp_array_combine( $div_ids, sp_array_value( $stats, $league_id, array() ) );
// Get equations from statistic variables
$equations = sp_get_var_equations( 'sp_statistic' );
// Initialize placeholders array
$placeholders = array();
foreach ( $div_ids as $div_id ):
$totals = array( 'eventsattended' => 0, 'eventsplayed' => 0, 'streak' => 0, 'last5' => null, 'last10' => null );
foreach ( $performance_labels as $key => $value ):
$totals[ $key ] = 0;
endforeach;
foreach ( $outcome_labels as $key => $value ):
$totals[ $key ] = 0;
endforeach;
// Initialize streaks counter
$streak = array( 'name' => '', 'count' => 0, 'fire' => 1 );
// Initialize last counters
$last5 = array();
$last10 = array();
// Add outcome types to last counters
foreach( $outcome_labels as $key => $value ):
$last5[ $key ] = 0;
$last10[ $key ] = 0;
endforeach;
// Get all events involving the team in current season
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'sp_event',
'value' => $this->ID
),
array(
'key' => 'sp_format',
'value' => 'league'
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'sp_league',
'field' => 'id',
'terms' => $league_id
),
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
),
)
);
$events = get_posts( $args );
// Event loop
foreach( $events as $event ):
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
$team_performance = (array)get_post_meta( $event->ID, 'sp_events', true );
// Add all team performance
foreach ( $team_performance as $team_id => $events ):
if ( array_key_exists( $this->ID, $events ) ):
$event_performance = sp_array_value( $events, $this->ID, array() );
foreach ( $event_performance as $key => $value ):
if ( array_key_exists( $key, $totals ) ):
$totals[ $key ] += $value;
endif;
endforeach;
$team_results = sp_array_value( $results, $team_id, array() );
// Find the outcome
if ( array_key_exists( 'outcome', $team_results ) ):
$value = $team_results['outcome'];
// Convert to array
if ( ! is_array( $value ) ):
$value = array( $value );
endif;
foreach( $value as $outcome ):
if ( $outcome && $outcome != '-1' ):
// Increment events attended and outcome count
if ( array_key_exists( $outcome, $totals ) ):
$totals['eventsattended']++;
$totals[ $outcome ]++;
// Increment events played if active in event
if ( sp_array_value( $event_performance, 'status' ) != 'sub' || sp_array_value( $event_performance, 'sub', 0 ) ):
$totals['eventsplayed']++;
endif;
endif;
// Add to streak counter
if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $outcome ) ):
$streak['name'] = $outcome;
$streak['count'] ++;
else:
$streak['fire'] = 0;
endif;
// Add to last 5 counter if sum is less than 5
if ( array_key_exists( $outcome, $last5 ) && array_sum( $last5 ) < 5 ):
$last5[ $outcome ] ++;
endif;
// Add to last 10 counter if sum is less than 10
if ( array_key_exists( $outcome, $last10 ) && array_sum( $last10 ) < 10 ):
$last10[ $outcome ] ++;
endif;
endif;
endforeach;
endif;
endif;
endforeach;
endforeach;
// Compile streaks counter and add to totals
$args = array(
'name' => $streak['name'],
'post_type' => 'sp_outcome',
'post_status' => 'publish',
'posts_per_page' => 1
);
$outcomes = get_posts( $args );
if ( $outcomes ):
$outcome = reset( $outcomes );
$totals['streak'] = $outcome->post_title . $streak['count'];
endif;
// Add last counters to totals
$totals['last5'] = $last5;
$totals['last10'] = $last10;
// Generate array of placeholder values for each league
$placeholders[ $div_id ] = array();
foreach ( $equations as $key => $value ):
$placeholders[ $div_id ][ $key ] = sp_solve( $value['equation'], $totals, $value['precision'] );
endforeach;
endforeach;
// Get stats from statistic variables
$stats = sp_get_var_labels( 'sp_statistic' );
// Merge the data and placeholders arrays
$merged = array();
foreach( $placeholders as $season_id => $season_data ):
if ( ! sp_array_value( $leagues, $season_id, 0 ) )
continue;
$season_name = sp_array_value( $season_names, $season_id, '&nbsp;' );
$team_id = sp_array_value( $leagues, $season_id, array() );
if ( ! $team_id || $team_id == '-1' )
continue;
$team_name = get_the_title( $team_id );
if ( get_option( 'sportspress_event_link_teams', 'no' ) == 'yes' ? true : false ):
$team_permalink = get_permalink( $team_id );
$team_name = '<a href="' . $team_permalink . '">' . $team_name . '</a>';
endif;
// Add season name to row
$merged[ $season_id ] = array(
'name' => $season_name,
'team' => $team_name
);
foreach( $season_data as $key => $value ):
// Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $season_id, $data ) && array_key_exists( $key, $data[ $season_id ] ) && $data[ $season_id ][ $key ] != '' ):
$merged[ $season_id ][ $key ] = $data[ $season_id ][ $key ];
else:
$merged[ $season_id ][ $key ] = $value;
endif;
endforeach;
endforeach;
if ( $admin ):
return array( $stats, $data, $placeholders, $merged, $leagues );
else:
$labels = array_merge( array( 'name' => SP()->text->string('Season'), 'team' => SP()->text->string('Team') ), $stats );
$merged[0] = $labels;
return $merged;
endif;
}
}

View File

@@ -2,15 +2,15 @@
/** /**
* League Table Class * League Table Class
* *
* The SportsPress table class handles individual league table data. * The SportsPress league table class handles individual league table data.
* *
* @class SP_Table * @class SP_League_Table
* @version 0.8 * @version 0.8
* @package SportsPress/Abstracts * @package SportsPress/Classes
* @category Class * @category Class
* @author ThemeBoy * @author ThemeBoy
*/ */
class SP_Table { class SP_League_Table {
/** @var int The league table (post) ID. */ /** @var int The league table (post) ID. */
public $ID; public $ID;
@@ -19,7 +19,7 @@ class SP_Table {
public $post; public $post;
/** @var array The sort priorities array. */ /** @var array The sort priorities array. */
private $priorities; public $priorities;
/** /**
* __construct function. * __construct function.
@@ -28,7 +28,7 @@ class SP_Table {
* @param mixed $post * @param mixed $post
*/ */
public function __construct( $post ) { public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Table ): if ( $post instanceof WP_Post || $post instanceof SP_League_Table ):
$this->ID = absint( $post->ID ); $this->ID = absint( $post->ID );
$this->post = $post; $this->post = $post;
else: else:
@@ -356,7 +356,7 @@ class SP_Table {
* @param array $b * @param array $b
* @return int * @return int
*/ */
private function sort( $a, $b ) { public function sort( $a, $b ) {
// Loop through priorities // Loop through priorities
foreach( $this->priorities as $priority ): foreach( $this->priorities as $priority ):

View File

@@ -0,0 +1,397 @@
<?php
/**
* Player List Class
*
* The SportsPress player list class handles individual player list data.
*
* @class SP_Player_List
* @version 0.8
* @package SportsPress/Classes
* @category Class
* @author ThemeBoy
*/
class SP_Player_List {
/** @var int The player list (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/** @var array The sort priorities array. */
public $priorities;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Player_List ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
/**
* Returns formatted data
*
* @access public
* @param bool $admin
* @return array
*/
public function data( $admin = false ) {
$league_id = sp_get_the_term_id( $this->ID, 'sp_league', 0 );
$div_id = sp_get_the_term_id( $this->ID, 'sp_season', 0 );
$player_ids = (array)get_post_meta( $this->ID, 'sp_player', false );
$list_stats = (array)get_post_meta( $this->ID, 'sp_players', true );
$usecolumns = get_post_meta( $this->ID, 'sp_columns', true );
$adjustments = get_post_meta( $this->ID, 'sp_adjustments', true );
$orderby = get_post_meta( $this->ID, 'sp_orderby', true );
$order = get_post_meta( $this->ID, 'sp_order', true );
// Get labels from performance variables
$performance_labels = (array)sp_get_var_labels( 'sp_performance' );
// Get labels from outcome variables
$outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
// Get all leagues populated with stats where available
$tempdata = sp_array_combine( $player_ids, $list_stats );
// Create entry for each player in totals
$totals = array();
$placeholders = array();
// Initialize streaks counter
$streaks = array();
// Initialize last counters
$last5s = array();
$last10s = array();
foreach ( $player_ids as $player_id ):
if ( ! $player_id )
continue;
// Initialize player streaks counter
$streaks[ $player_id ] = array( 'name' => '', 'count' => 0, 'fire' => 1 );
// Initialize player last counters
$last5s[ $player_id ] = array();
$last10s[ $player_id ] = array();
// Add outcome types to player last counters
foreach( $outcome_labels as $key => $value ):
$last5s[ $player_id ][ $key ] = 0;
$last10s[ $player_id ][ $key ] = 0;
endforeach;
// Initialize player totals
$totals[ $player_id ] = array( 'eventsattended' => 0, 'eventsplayed' => 0, 'streak' => 0 );
foreach ( $performance_labels as $key => $value ):
$totals[ $player_id ][ $key ] = 0;
$totals[ $player_id ][ $key ] = 0;
endforeach;
foreach ( $outcome_labels as $key => $value ):
$totals[ $player_id ][ $key ] = 0;
endforeach;
// Get static stats
$static = get_post_meta( $player_id, 'sp_statistics', true );
// Add static stats to placeholders
$placeholders[ $player_id ] = sp_array_value( sp_array_value( $static, $league_id, array() ), $div_id, array() );
endforeach;
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'sp_league',
'field' => 'id',
'terms' => $league_id
),
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
)
)
);
$events = get_posts( $args );
// Event loop
foreach ( $events as $event ):
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
$team_performance = (array)get_post_meta( $event->ID, 'sp_players', true );
// Add all team performance
foreach ( $team_performance as $team_id => $players ):
foreach( $players as $player_id => $player_performance ):
if ( array_key_exists( $player_id, $totals ) && is_array( $totals[ $player_id ] ) ):
$player_performance = sp_array_value( $players, $player_id, array() );
foreach ( $player_performance as $key => $value ):
if ( array_key_exists( $key, $totals[ $player_id ] ) ):
$totals[ $player_id ][ $key ] += $value;
endif;
endforeach;
$team_results = sp_array_value( $results, $team_id, array() );
// Find the outcome
if ( array_key_exists( 'outcome', $team_results ) ):
$value = $team_results['outcome'];
// Convert to array
if ( ! is_array( $value ) ):
$value = array( $value );
endif;
foreach ( $value as $outcome ):
if ( $outcome && $outcome != '-1' ):
// Increment events attended and outcome count
if ( array_key_exists( $outcome, $totals[ $player_id ] ) ):
$totals[ $player_id ]['eventsattended']++;
$totals[ $player_id ][ $outcome ]++;
// Increment events played if active in event
if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ):
$totals[ $player_id ]['eventsplayed']++;
endif;
endif;
// Add to streak counter
if ( $streaks[ $player_id ]['fire'] && ( $streaks[ $player_id ]['name'] == '' || $streaks[ $player_id ]['name'] == $outcome ) ):
$streaks[ $player_id ]['name'] = $outcome;
$streaks[ $player_id ]['count'] ++;
else:
$streaks[ $player_id ]['fire'] = 0;
endif;
// Add to last 5 counter if sum is less than 5
if ( array_key_exists( $player_id, $last5s ) && array_key_exists( $outcome, $last5s[ $player_id ] ) && array_sum( $last5s[ $player_id ] ) < 5 ):
$last5s[ $player_id ][ $outcome ] ++;
endif;
// Add to last 10 counter if sum is less than 10
if ( array_key_exists( $player_id, $last10s ) && array_key_exists( $outcome, $last10s[ $player_id ] ) && array_sum( $last10s[ $player_id ] ) < 10 ):
$last10s[ $player_id ][ $outcome ] ++;
endif;
endif;
endforeach;
endif;
endif;
endforeach;
endforeach;
endforeach;
foreach ( $streaks as $player_id => $streak ):
// Compile streaks counter and add to totals
if ( $streak['name'] ):
$args = array(
'name' => $streak['name'],
'post_type' => 'sp_outcome',
'post_status' => 'publish',
'posts_per_page' => 1
);
$outcomes = get_posts( $args );
if ( $outcomes ):
$outcome = reset( $outcomes );
$totals[ $player_id ]['streak'] = $outcome->post_title . $streak['count'];
else:
$totals[ $player_id ]['streak'] = null;
endif;
else:
$totals[ $player_id ]['streak'] = null;
endif;
endforeach;
foreach ( $last5s as $player_id => $last5 ):
// Add last 5 to totals
$totals[ $player_id ]['last5'] = $last5;
endforeach;
foreach ( $last10s as $player_id => $last10 ):
// Add last 10 to totals
$totals[ $player_id ]['last10'] = $last10;
endforeach;
$args = array(
'post_type' => 'sp_statistic',
'numberposts' => -1,
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$stats = get_posts( $args );
$columns = array();
foreach ( $stats as $stat ):
// Get post meta
$meta = get_post_meta( $stat->ID );
// Add equation to object
$stat->equation = sp_array_value( sp_array_value( $meta, 'sp_equation', array() ), 0, 0 );
$stat->precision = sp_array_value( sp_array_value( $meta, 'sp_precision', array() ), 0, 0 );
// Add column name to columns
$columns[ $stat->post_name ] = $stat->post_title;
endforeach;
// Fill in empty placeholder values for each player
foreach ( $player_ids as $player_id ):
if ( ! $player_id )
continue;
foreach ( $stats as $stat ):
if ( sp_array_value( $placeholders[ $player_id ], $stat->post_name, '' ) == '' ):
// Solve
$placeholder = sp_solve( $stat->equation, sp_array_value( $totals, $player_id, array() ), $stat->precision );
// Adjustments
$placeholder += sp_array_value( sp_array_value( $adjustments, $player_id, array() ), $stat->post_name, 0 );
$placeholders[ $player_id ][ $stat->post_name ] = $placeholder;
endif;
endforeach;
endforeach;
// Merge the data and placeholders arrays
$merged = array();
foreach( $placeholders as $player_id => $player_data ):
// Add player name to row
$merged[ $player_id ] = array();
$player_data['name'] = get_the_title( $player_id );
foreach( $player_data as $key => $value ):
// Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $player_id, $tempdata ) && array_key_exists( $key, $tempdata[ $player_id ] ) && $tempdata[ $player_id ][ $key ] != '' ):
$merged[ $player_id ][ $key ] = $tempdata[ $player_id ][ $key ];
else:
$merged[ $player_id ][ $key ] = $value;
endif;
endforeach;
endforeach;
if ( $orderby != 'number' || $order != 'ASC' ):
$this->priorities = array(
array(
'key' => $orderby,
'order' => $order,
),
);
uasort( $merged, array( $this, 'sort' ) );
endif;
// Rearrange data array to reflect values
$data = array();
foreach( $merged as $key => $value ):
$data[ $key ] = $tempdata[ $key ];
endforeach;
if ( $admin ):
return array( $columns, $usecolumns, $data, $placeholders, $merged );
else:
if ( ! is_array( $usecolumns ) )
$usecolumns = array();
foreach ( $columns as $key => $label ):
if ( ! in_array( $key, $usecolumns ) ):
unset( $columns[ $key ] );
endif;
endforeach;
$labels = array_merge( array( 'name' => SP()->text->string('Player') ), $columns );
$merged[0] = $labels;
return $merged;
endif;
}
/**
* Sort the table by priorities.
*
* @param array $a
* @param array $b
* @return int
*/
public function sort( $a, $b ) {
// Loop through priorities
if ( is_array( $this->priorities ) ) : foreach( $this->priorities as $priority ):
// Proceed if columns are not equal
if ( sp_array_value( $a, $priority['key'], 0 ) != sp_array_value( $b, $priority['key'], 0 ) ):
if ( $priority['key'] == 'name' ):
$output = strcmp( sp_array_value( $a, 'name', null ), sp_array_value( $b, 'name', null ) );
else:
// Compare performance values
$output = sp_array_value( $a, $priority['key'], 0 ) - sp_array_value( $b, $priority['key'], 0 );
endif;
// Flip value if descending order
if ( $priority['order'] == 'DESC' ) $output = 0 - $output;
return ( $output > 0 );
endif;
endforeach; endif;
// Default sort by number
return sp_array_value( $a, 'number', 0 ) - sp_array_value( $b, 'number', 0 );
}
}

View File

@@ -0,0 +1,324 @@
<?php
/**
* Player Class
*
* The SportsPress player class handles individual player data.
*
* @class SP_Player
* @version 0.8
* @package SportsPress/Classes
* @category Class
* @author ThemeBoy
*/
class SP_Player {
/** @var int The player (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Player ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
elseif ( 'past_teams' == $key ):
return get_post_meta( $this->ID, 'sp_past_team', false );
elseif ( 'metrics' == $key ):
$metrics = (array)get_post_meta( $this->ID, 'sp_metrics', true );
// Get labels from metric variables
$metric_labels = (array)sp_get_var_labels( 'sp_metric' );
$data = array();
foreach( $metric_labels as $key => $value ):
$metric = sp_array_value( $metrics, $key, null );
if ( $metric == null )
continue;
$data[ $value ] = sp_array_value( $metrics, $key, '&nbsp;' );
endforeach;
return $data;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
/**
* Returns formatted data
*
* @access public
* @param int $league_id
* @param bool $admin
* @return array
*/
public function data( $league_id, $admin = false ) {
$seasons = (array)get_the_terms( $this->ID, 'sp_season' );
$stats = (array)get_post_meta( $this->ID, 'sp_statistics', true );
$leagues = sp_array_value( (array)get_post_meta( $this->ID, 'sp_leagues', true ), $league_id, array() );
// Get labels from performance variables
$performance_labels = (array)sp_get_var_labels( 'sp_performance' );
// Get labels from outcome variables
$outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
// Generate array of all season ids and season names
$div_ids = array();
$season_names = array();
foreach ( $seasons as $season ):
if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ):
$div_ids[] = $season->term_id;
$season_names[ $season->term_id ] = $season->name;
endif;
endforeach;
$data = array();
// Get all seasons populated with data where available
$data = sp_array_combine( $div_ids, sp_array_value( $stats, $league_id, array() ) );
// Get equations from statistic variables
$equations = sp_get_var_equations( 'sp_statistic' );
// Initialize placeholders array
$placeholders = array();
foreach ( $div_ids as $div_id ):
$totals = array( 'eventsattended' => 0, 'eventsplayed' => 0, 'streak' => 0, 'last5' => null, 'last10' => null );
foreach ( $performance_labels as $key => $value ):
$totals[ $key ] = 0;
endforeach;
foreach ( $outcome_labels as $key => $value ):
$totals[ $key ] = 0;
endforeach;
// Initialize streaks counter
$streak = array( 'name' => '', 'count' => 0, 'fire' => 1 );
// Initialize last counters
$last5 = array();
$last10 = array();
// Add outcome types to last counters
foreach( $outcome_labels as $key => $value ):
$last5[ $key ] = 0;
$last10[ $key ] = 0;
endforeach;
// Get all events involving the team in current season
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'sp_player',
'value' => $this->ID
),
array(
'key' => 'sp_format',
'value' => 'league'
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'sp_league',
'field' => 'id',
'terms' => $league_id
),
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
),
)
);
$events = get_posts( $args );
// Event loop
foreach( $events as $event ):
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
$team_performance = (array)get_post_meta( $event->ID, 'sp_players', true );
// Add all team performance
foreach ( $team_performance as $team_id => $players ):
if ( array_key_exists( $this->ID, $players ) ):
$player_performance = sp_array_value( $players, $this->ID, array() );
foreach ( $player_performance as $key => $value ):
if ( array_key_exists( $key, $totals ) ):
$totals[ $key ] += $value;
endif;
endforeach;
$team_results = sp_array_value( $results, $team_id, array() );
// Find the outcome
if ( array_key_exists( 'outcome', $team_results ) ):
$value = $team_results['outcome'];
// Convert to array
if ( ! is_array( $value ) ):
$value = array( $value );
endif;
foreach( $value as $outcome ):
if ( $outcome && $outcome != '-1' ):
// Increment events attended and outcome count
if ( array_key_exists( $outcome, $totals ) ):
$totals['eventsattended']++;
$totals[ $outcome ]++;
// Increment events played if active in event
if ( sp_array_value( $player_performance, 'status' ) != 'sub' || sp_array_value( $player_performance, 'sub', 0 ) ):
$totals['eventsplayed']++;
endif;
endif;
// Add to streak counter
if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $outcome ) ):
$streak['name'] = $outcome;
$streak['count'] ++;
else:
$streak['fire'] = 0;
endif;
// Add to last 5 counter if sum is less than 5
if ( array_key_exists( $outcome, $last5 ) && array_sum( $last5 ) < 5 ):
$last5[ $outcome ] ++;
endif;
// Add to last 10 counter if sum is less than 10
if ( array_key_exists( $outcome, $last10 ) && array_sum( $last10 ) < 10 ):
$last10[ $outcome ] ++;
endif;
endif;
endforeach;
endif;
endif;
endforeach;
endforeach;
// Compile streaks counter and add to totals
$args = array(
'name' => $streak['name'],
'post_type' => 'sp_outcome',
'post_status' => 'publish',
'posts_per_page' => 1
);
$outcomes = get_posts( $args );
if ( $outcomes ):
$outcome = reset( $outcomes );
$totals['streak'] = $outcome->post_title . $streak['count'];
endif;
// Add last counters to totals
$totals['last5'] = $last5;
$totals['last10'] = $last10;
// Generate array of placeholder values for each league
$placeholders[ $div_id ] = array();
foreach ( $equations as $key => $value ):
$placeholders[ $div_id ][ $key ] = sp_solve( $value['equation'], $totals, $value['precision'] );
endforeach;
endforeach;
// Get stats from statistic variables
$stats = sp_get_var_labels( 'sp_statistic' );
// Merge the data and placeholders arrays
$merged = array();
foreach( $placeholders as $season_id => $season_data ):
if ( ! sp_array_value( $leagues, $season_id, 0 ) )
continue;
$season_name = sp_array_value( $season_names, $season_id, '&nbsp;' );
$team_id = sp_array_value( $leagues, $season_id, array() );
if ( ! $team_id || $team_id == '-1' )
continue;
$team_name = get_the_title( $team_id );
if ( get_option( 'sportspress_player_link_teams', 'no' ) == 'yes' ? true : false ):
$team_permalink = get_permalink( $team_id );
$team_name = '<a href="' . $team_permalink . '">' . $team_name . '</a>';
endif;
// Add season name to row
$merged[ $season_id ] = array(
'name' => $season_name,
'team' => $team_name
);
foreach( $season_data as $key => $value ):
// Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $season_id, $data ) && array_key_exists( $key, $data[ $season_id ] ) && $data[ $season_id ][ $key ] != '' ):
$merged[ $season_id ][ $key ] = $data[ $season_id ][ $key ];
else:
$merged[ $season_id ][ $key ] = $value;
endif;
endforeach;
endforeach;
if ( $admin ):
return array( $stats, $data, $placeholders, $merged, $leagues );
else:
$labels = array_merge( array( 'name' => SP()->text->string('Season'), 'team' => SP()->text->string('Team') ), $stats );
$merged[0] = $labels;
return $merged;
endif;
}
}

278
includes/class-sp-team.php Normal file
View File

@@ -0,0 +1,278 @@
<?php
/**
* Team Class
*
* The SportsPress team class handles individual team data.
*
* @class SP_Team
* @version 0.8
* @package SportsPress/Classes
* @category Class
* @author ThemeBoy
*/
class SP_Team {
/** @var int The team (post) ID. */
public $ID;
/** @var object The actual post object. */
public $post;
/**
* __construct function.
*
* @access public
* @param mixed $post
*/
public function __construct( $post ) {
if ( $post instanceof WP_Post || $post instanceof SP_Team ):
$this->ID = absint( $post->ID );
$this->post = $post;
else:
$this->ID = absint( $post );
$this->post = get_post( $this->ID );
endif;
}
/**
* __get function.
*
* @access public
* @param mixed $key
* @return bool
*/
public function __get( $key ) {
if ( ! isset( $key ) ):
return $this->post;
else:
$value = get_post_meta( $this->ID, 'sp_' . $key, true );
endif;
return $value;
}
/**
* Returns formatted data
*
* @access public
* @param bool $admin
* @return array
*/
public function data( $league_id, $admin = false ) {
$seasons = (array)get_the_terms( $this->ID, 'sp_season' );
$columns = (array)get_post_meta( $this->ID, 'sp_columns', true );
$leagues_seasons = sp_array_value( (array)get_post_meta( $this->ID, 'sp_leagues', true ), $league_id, array() );
// Get labels from result variables
$result_labels = (array)sp_get_var_labels( 'sp_result' );
// Get labels from outcome variables
$outcome_labels = (array)sp_get_var_labels( 'sp_outcome' );
// Generate array of all season ids and season names
$div_ids = array();
$season_names = array();
foreach ( $seasons as $season ):
if ( is_object( $season ) && property_exists( $season, 'term_id' ) && property_exists( $season, 'name' ) ):
$div_ids[] = $season->term_id;
$season_names[ $season->term_id ] = $season->name;
endif;
endforeach;
$data = array();
// Get all seasons populated with data where available
$data = sp_array_combine( $div_ids, sp_array_value( $columns, $league_id, array() ) );
// Get equations from column variables
$equations = sp_get_var_equations( 'sp_column' );
// Initialize placeholders array
$placeholders = array();
foreach ( $div_ids as $div_id ):
$totals = array( 'eventsplayed' => 0, 'streak' => 0, 'last5' => null, 'last10' => null );
foreach ( $result_labels as $key => $value ):
$totals[ $key . 'for' ] = 0;
$totals[ $key . 'against' ] = 0;
endforeach;
foreach ( $outcome_labels as $key => $value ):
$totals[ $key ] = 0;
endforeach;
// Initialize streaks counter
$streak = array( 'name' => '', 'count' => 0, 'fire' => 1 );
// Initialize last counters
$last5 = array();
$last10 = array();
// Add outcome types to last counters
foreach( $outcome_labels as $key => $value ):
$last5[ $key ] = 0;
$last10[ $key ] = 0;
endforeach;
// Get all events involving the team in current season
$args = array(
'post_type' => 'sp_event',
'numberposts' => -1,
'posts_per_page' => -1,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'sp_team',
'value' => $this->ID
),
array(
'key' => 'sp_format',
'value' => 'league'
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'sp_league',
'field' => 'id',
'terms' => $league_id
),
array(
'taxonomy' => 'sp_season',
'field' => 'id',
'terms' => $div_id
),
)
);
$events = get_posts( $args );
foreach( $events as $event ):
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
foreach ( $results as $team_id => $team_result ):
foreach ( $team_result as $key => $value ):
if ( $team_id == $this->ID ):
if ( $key == 'outcome' ):
// Convert to array
if ( ! is_array( $value ) ):
$value = array( $value );
endif;
foreach( $value as $outcome ):
// Increment events played and outcome count
if ( array_key_exists( $outcome, $totals ) ):
$totals['eventsplayed']++;
$totals[ $outcome ]++;
endif;
if ( $outcome && $outcome != '-1' ):
// Add to streak counter
if ( $streak['fire'] && ( $streak['name'] == '' || $streak['name'] == $outcome ) ):
$streak['name'] = $outcome;
$streak['count'] ++;
else:
$streak['fire'] = 0;
endif;
// Add to last 5 counter if sum is less than 5
if ( array_key_exists( $outcome, $last5 ) && array_sum( $last5 ) < 5 ):
$last5[ $outcome ] ++;
endif;
// Add to last 10 counter if sum is less than 10
if ( array_key_exists( $outcome, $last10 ) && array_sum( $last10 ) < 10 ):
$last10[ $outcome ] ++;
endif;
endif;
endforeach;
else:
if ( array_key_exists( $key . 'for', $totals ) ):
$totals[ $key . 'for' ] += $value;
endif;
endif;
else:
if ( $key != 'outcome' ):
if ( array_key_exists( $key . 'against', $totals ) ):
$totals[ $key . 'against' ] += $value;
endif;
endif;
endif;
endforeach;
endforeach;
endforeach;
// Compile streaks counter and add to totals
$args=array(
'name' => $streak['name'],
'post_type' => 'sp_outcome',
'post_status' => 'publish',
'posts_per_page' => 1
);
$outcomes = get_posts( $args );
if ( $outcomes ):
$outcome = reset( $outcomes );
$totals['streak'] = $outcome->post_title . $streak['count'];
endif;
// Add last counters to totals
$totals['last5'] = $last5;
$totals['last10'] = $last10;
// Generate array of placeholder values for each league
$placeholders[ $div_id ] = array();
foreach ( $equations as $key => $value ):
$placeholders[ $div_id ][ $key ] = sp_solve( $value['equation'], $totals, $value['precision'] );
endforeach;
endforeach;
// Get columns from column variables
$columns = sp_get_var_labels( 'sp_column' );
// Merge the data and placeholders arrays
$merged = array();
foreach( $placeholders as $season_id => $season_data ):
if ( ! sp_array_value( $leagues_seasons, $season_id, 0 ) )
continue;
$season_name = sp_array_value( $season_names, $season_id, '&nbsp;' );
// Add season name to row
$merged[ $season_id ] = array(
'name' => $season_name
);
foreach( $season_data as $key => $value ):
// Use static data if key exists and value is not empty, else use placeholder
if ( array_key_exists( $season_id, $data ) && array_key_exists( $key, $data[ $season_id ] ) && $data[ $season_id ][ $key ] != '' ):
$merged[ $season_id ][ $key ] = $data[ $season_id ][ $key ];
else:
$merged[ $season_id ][ $key ] = $value;
endif;
endforeach;
endforeach;
if ( $admin ):
return array( $columns, $data, $placeholders, $merged, $leagues_seasons );
else:
$labels = array_merge( array( 'name' => SP()->text->string('Season') ), $columns );
$merged[0] = $labels;
return $merged;
endif;
}
}

View File

@@ -22,6 +22,24 @@ function is_sportspress() {
return apply_filters( 'is_sportspress', ( is_singular( array( 'sp_event', 'sp_calendar', 'sp_team', 'sp_table', 'sp_player', 'sp_list', 'sp_staff' ) ) ) ? true : false ); return apply_filters( 'is_sportspress', ( is_singular( array( 'sp_event', 'sp_calendar', 'sp_team', 'sp_table', 'sp_player', 'sp_list', 'sp_staff' ) ) ) ? true : false );
} }
/**
* is_sp_config_type - Returns true if post is SportsPress config type
*
* @access public
* @return bool
*/
if ( ! function_exists( 'is_sp_config_type' ) ) {
function is_sp_config_type( $typenow = null ) {
if ( $typenow == null ) global $typenow;
$post_types = array( 'sp_result', 'sp_outcome', 'sp_column', 'sp_performance', 'sp_metric', 'sp_statistic' );
if ( in_array( $typenow, $post_types ) )
return true;
return false;
}
}
if ( ! function_exists( 'is_ajax' ) ) { if ( ! function_exists( 'is_ajax' ) ) {
/** /**

File diff suppressed because it is too large Load Diff

View File

@@ -155,9 +155,6 @@ function sportspress_sort_list_players ( $a, $b ) {
function sportspress_get_player_metrics_data( $post_id ) { function sportspress_get_player_metrics_data( $post_id ) {
return sp_get_player_metrics_data( $post_id ); return sp_get_player_metrics_data( $post_id );
} }
function sportspress_get_player_performance_data( $post_id, $league_id, $admin = false ) {
return sp_get_player_performance_data( $post_id, $league_id, $admin );
}
function sportspress_get_next_event( $args = array() ) { function sportspress_get_next_event( $args = array() ) {
return sp_get_next_event( $args ); return sp_get_next_event( $args );
} }
@@ -168,16 +165,30 @@ function sportspress_highlight_admin_menu( $p = 'options-general.php', $s = 'spo
return sp_highlight_admin_menu( $p, $s ); return sp_highlight_admin_menu( $p, $s );
} }
if ( !function_exists( 'sp_get_player_list_data' ) ) { if ( !function_exists( 'sp_get_team_columns_data' ) ) {
function sp_get_player_list_data( $post_id, $admin = false ) { function sp_get_team_columns_data( $post_id, $league_id, $admin = false ) {
$formatter = new SP_Data_Formatter; $team = new SP_Team( $post_id );
return $formatter->player_list( $post_id, $admin ); return $team->data( $league_id, $admin );
} }
} }
if ( !function_exists( 'sp_get_league_table_data' ) ) { if ( !function_exists( 'sp_get_league_table_data' ) ) {
function sp_get_league_table_data( $post_id, $admin = false ) { function sp_get_league_table_data( $post_id, $admin = false ) {
$table = new SP_Table( $post_id ); $table = new SP_League_Table( $post_id );
return $table->data( $admin ); return $table->data( $admin );
} }
} }
if ( !function_exists( 'sp_get_player_statistics_data' ) ) {
function sp_get_player_statistics_data( $post_id, $league_id, $admin = false ) {
$list = new SP_Player_List( $post_id );
return $list->data( $league_id, $admin );
}
}
if ( !function_exists( 'sp_get_player_list_data' ) ) {
function sp_get_player_list_data( $post_id, $admin = false ) {
$list = new SP_Player_List( $post_id );
return $list->data( $admin );
}
}

View File

@@ -101,7 +101,7 @@ function sportspress_gettext( $translated_text, $untranslated_text, $domain ) {
global $typenow; global $typenow;
if ( is_admin() ): if ( is_admin() ):
if ( sp_is_config_type( $typenow ) ): if ( is_sp_config_type( $typenow ) ):
switch ( $untranslated_text ): switch ( $untranslated_text ):
case 'Slug': case 'Slug':
$translated_text = ( in_array( $typenow, array( 'sp_column', 'sp_statistic' ) ) ) ? __( 'Key', 'sportspress' ) : __( 'Variable', 'sportspress' ); $translated_text = ( in_array( $typenow, array( 'sp_column', 'sp_statistic' ) ) ) ? __( 'Key', 'sportspress' ) : __( 'Variable', 'sportspress' );
@@ -146,7 +146,7 @@ function sportspress_pre_get_posts( $query ) {
endif; endif;
$post_type = $query->query['post_type']; $post_type = $query->query['post_type'];
if ( sp_is_config_type( $post_type ) ): if ( is_sp_config_type( $post_type ) ):
$query->set( 'orderby', 'menu_order' ); $query->set( 'orderby', 'menu_order' );
$query->set( 'order', 'ASC' ); $query->set( 'order', 'ASC' );
elseif ( $post_type == 'sp_event' ): elseif ( $post_type == 'sp_event' ):
@@ -178,7 +178,7 @@ function sportspress_sanitize_title( $title ) {
return $title; return $title;
elseif ( isset( $_POST ) && array_key_exists( 'post_type', $_POST ) && sp_is_config_type( $_POST['post_type'] ) ): elseif ( isset( $_POST ) && array_key_exists( 'post_type', $_POST ) && is_sp_config_type( $_POST['post_type'] ) ):
$key = isset( $_POST['sp_key'] ) ? $_POST['sp_key'] : null; $key = isset( $_POST['sp_key'] ) ? $_POST['sp_key'] : null;

View File

@@ -25,7 +25,8 @@ $defaults = array(
extract( $defaults, EXTR_SKIP ); extract( $defaults, EXTR_SKIP );
if ( isset( $id ) ): if ( isset( $id ) ):
$events = sp_get_calendar_data( $id ); $calendar = new SP_Calendar( $id );
$events = $calendar->data();
$event_ids = array(); $event_ids = array();
foreach ( $events as $event ): foreach ( $events as $event ):
$event_ids[] = $event->ID; $event_ids[] = $event->ID;

View File

@@ -27,7 +27,9 @@ extract( $defaults, EXTR_SKIP );
<thead> <thead>
<tr> <tr>
<?php <?php
list( $data, $usecolumns ) = sp_get_calendar_data( $id, true ); $calendar = new SP_Calendar( $id );
$data = $calendar->data();
$usecolumns = $calendar->columns;
if ( isset( $columns ) ) if ( isset( $columns ) )
$usecolumns = $columns; $usecolumns = $columns;

View File

@@ -27,7 +27,7 @@ extract( $defaults, EXTR_SKIP );
$output = '<div class="sp-table-wrapper">' . $output = '<div class="sp-table-wrapper">' .
'<table class="sp-league-table sp-data-table' . ( $responsive ? ' sp-responsive-table' : '' ) . ( $sortable ? ' sp-sortable-table' : '' ) . ( $paginated ? ' sp-paginated-table' : '' ) . '" data-sp-rows="' . $rows . '">' . '<thead>' . '<tr>'; '<table class="sp-league-table sp-data-table' . ( $responsive ? ' sp-responsive-table' : '' ) . ( $sortable ? ' sp-sortable-table' : '' ) . ( $paginated ? ' sp-paginated-table' : '' ) . '" data-sp-rows="' . $rows . '">' . '<thead>' . '<tr>';
$table = new SP_Table( $id ); $table = new SP_League_Table( $id );
$data = $table->data(); $data = $table->data();

View File

@@ -20,10 +20,12 @@ extract( $defaults, EXTR_SKIP );
$countries = SP()->countries->countries; $countries = SP()->countries->countries;
$nationality = get_post_meta( $id, 'sp_nationality', true ); $player = new SP_Player( $id );
$current_team = get_post_meta( $id, 'sp_current_team', true );
$past_teams = get_post_meta( $id, 'sp_past_team', false ); $nationality = $player->nationality;
$metrics = sp_get_player_metrics_data( $id ); $current_team = $player->current_team;
$past_teams = $player->past_teams;
$metrics = $player->metrics;
$common = array(); $common = array();
if ( $nationality ): if ( $nationality ):

View File

@@ -43,7 +43,8 @@ $float = is_rtl() ? 'right' : 'left';
$selector = 'sp-player-gallery-' . $id; $selector = 'sp-player-gallery-' . $id;
$data = sp_get_player_list_data( $id ); $list = new SP_Player_List( $id );
$data = $list->data();
// The first row should be column labels // The first row should be column labels
$labels = $data[0]; $labels = $data[0];
@@ -51,18 +52,14 @@ $labels = $data[0];
// Remove the first row to leave us with the actual data // Remove the first row to leave us with the actual data
unset( $data[0] ); unset( $data[0] );
if ( $orderby == 'default' ): if ( $orderby != 'default' ):
$orderby = get_post_meta( $id, 'sp_orderby', true ); $list->priorities = array(
$order = get_post_meta( $id, 'sp_order', true );
else:
global $sportspress_performance_priorities;
$sportspress_performance_priorities = array(
array( array(
'key' => $orderby, 'key' => $orderby,
'order' => $order, 'order' => $order,
), ),
); );
uasort( $data, 'sp_sort_list_players' ); uasort( $data, array( $list, 'sort' ) );
endif; endif;
$gallery_style = $gallery_div = ''; $gallery_style = $gallery_div = '';

View File

@@ -28,7 +28,8 @@ extract( $defaults, EXTR_SKIP );
$output = '<div class="sp-table-wrapper">' . $output = '<div class="sp-table-wrapper">' .
'<table class="sp-player-list sp-data-table' . ( $responsive ? ' sp-responsive-table' : '' ) . ( $sortable ? ' sp-sortable-table' : '' ) . ( $paginated ? ' sp-paginated-table' : '' ) . '" data-sp-rows="' . $rows . '">' . '<thead>' . '<tr>'; '<table class="sp-player-list sp-data-table' . ( $responsive ? ' sp-responsive-table' : '' ) . ( $sortable ? ' sp-sortable-table' : '' ) . ( $paginated ? ' sp-paginated-table' : '' ) . '" data-sp-rows="' . $rows . '">' . '<thead>' . '<tr>';
$data = sp_get_player_list_data( $id ); $list = new SP_Player_List( $id );
$data = $list->data();
// The first row should be column labels // The first row should be column labels
$labels = $data[0]; $labels = $data[0];
@@ -36,18 +37,14 @@ $labels = $data[0];
// Remove the first row to leave us with the actual data // Remove the first row to leave us with the actual data
unset( $data[0] ); unset( $data[0] );
if ( $orderby == 'default' ): if ( $orderby != 'default' ):
$orderby = get_post_meta( $id, 'sp_orderby', true ); $list->priorities = array(
$order = get_post_meta( $id, 'sp_order', true );
else:
global $sportspress_performance_priorities;
$sportspress_performance_priorities = array(
array( array(
'key' => $orderby, 'key' => $orderby,
'order' => $order, 'order' => $order,
), ),
); );
uasort( $data, 'sp_sort_list_players' ); uasort( $data, array( $list, 'sort' ) );
endif; endif;
if ( in_array( $orderby, array( 'number', 'name' ) ) ): if ( in_array( $orderby, array( 'number', 'name' ) ) ):

View File

@@ -1,107 +0,0 @@
<?php
/**
* Player Roster
*
* @author ThemeBoy
* @package SportsPress/Templates
* @version 0.8
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! $id )
$id = get_the_ID();
$defaults = array(
'performance' => null,
'orderby' => 'default',
'order' => 'ASC',
);
$r = wp_parse_args( $args, $defaults );
$output = '';
$data = sp_get_player_roster_data( $id );
// The first row should be column labels
$labels = $data[0];
// Remove the first row to leave us with the actual data
unset( $data[0] );
$performance = sp_array_value( $r, 'performance', null );
if ( $r['orderby'] == 'default' ):
$r['orderby'] = get_post_meta( $id, 'sp_orderby', true );
$r['order'] = get_post_meta( $id, 'sp_order', true );
else:
global $sportspress_performance_priorities;
$sportspress_performance_priorities = array(
array(
'key' => $r['orderby'],
'order' => $r['order'],
),
);
uasort( $data, 'sp_sort_list_players' );
endif;
$positions = get_terms ( 'sp_position' );
foreach ( $positions as $position ):
$rows = '';
$i = 0;
foreach ( $data as $player_id => $row ):
if ( ! in_array( $position->term_id, $row['positions']) )
continue;
$rows .= '<tr class="' . ( $i % 2 == 0 ? 'odd' : 'even' ) . '">';
// Rank or number
if ( isset( $r['orderby'] ) && $r['orderby'] != 'number' ):
$rows .= '<td class="data-rank">' . ( $i + 1 ) . '</td>';
else:
$number = get_post_meta( $player_id, 'sp_number', true );
$rows .= '<td class="data-number">' . ( $number ? $number : '&nbsp;' ) . '</td>';
endif;
// Name as link
$permalink = get_post_permalink( $player_id );
$name = sp_array_value( $row, 'name', sp_array_value( $row, 'name', '&nbsp;' ) );
$rows .= '<td class="data-name">' . '<a href="' . $permalink . '">' . $name . '</a></td>';
foreach( $labels as $key => $value ):
if ( $key == 'name' )
continue;
if ( ! is_array( $performance ) || in_array( $key, $performance ) )
$rows .= '<td class="data-' . $key . '">' . sp_array_value( $row, $key, '&mdash;' ) . '</td>';
endforeach;
$rows .= '</tr>';
$i++;
endforeach;
if ( ! empty( $rows ) ):
$output .= '<h4 class="sp-table-caption">' . $position->name . '</h4>';
$output .= '<div class="sp-table-wrapper">' .
'<table class="sp-player-list sp-player-roster sp-data-table sp-responsive-table">' . '<thead>' . '<tr>';
if ( in_array( $r['orderby'], array( 'number', 'name' ) ) ):
$output .= '<th class="data-number">#</th>';
else:
$output .= '<th class="data-rank">' . SP()->text->string('Rank') . '</th>';
endif;
foreach( $labels as $key => $label ):
if ( ! is_array( $performance ) || $key == 'name' || in_array( $key, $performance ) )
$output .= '<th class="data-' . $key . '">'. $label . '</th>';
endforeach;
$output .= '</tr>' . '</thead>' . '<tbody>' . $rows . '</tbody>' . '</table>' . '</div>';
endif;
endforeach;
echo apply_filters( 'sportspress_player_roster', $output );

View File

@@ -19,7 +19,8 @@ if ( is_array( $leagues ) ):
foreach ( $leagues as $league ): foreach ( $leagues as $league ):
$responsive = get_option( 'sportspress_enable_responsive_tables', 'yes' ) == 'yes' ? true : false; $responsive = get_option( 'sportspress_enable_responsive_tables', 'yes' ) == 'yes' ? true : false;
$data = sp_get_player_statistics_data( $id, $league->term_id ); $player = new SP_Player( $id );
$data = $player->data( $league->term_id );
// The first row should be column labels // The first row should be column labels
$labels = $data[0]; $labels = $data[0];

View File

@@ -22,7 +22,8 @@ $output = '';
// Loop through data for each league // Loop through data for each league
foreach ( $leagues as $league ): foreach ( $leagues as $league ):
$data = sp_get_team_columns_data( $id, $league->term_id ); $team = new SP_Team( $id );
$data = $team->data( $league->term_id );
if ( sizeof( $data ) <= 1 ) if ( sizeof( $data ) <= 1 )
continue; continue;