Add abbreviation to config post types and calculate streak
This commit is contained in:
@@ -132,17 +132,28 @@
|
||||
<tr>
|
||||
<th><?php _e( 'Label', 'sportspress' ); ?></th>
|
||||
<th><?php _e( 'Key', 'sportspress' ); ?></th>
|
||||
<th><?php _e( 'Abbreviation', 'sportspress' ); ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<?php $i = 0; foreach ( $data as $row ): ?>
|
||||
<tr<?php if ( $i % 2 ) echo ' class="alternate"'; ?>>
|
||||
<td class="row-title"><?php echo $row->post_title; ?></td>
|
||||
<td><?php echo $row->post_name; ?></td>
|
||||
<td>
|
||||
<?php
|
||||
$abbreviation = get_post_meta ( $row->ID, 'sp_abbreviation', true );
|
||||
if ( $abbreviation ):
|
||||
echo $abbreviation;
|
||||
else:
|
||||
echo $row->post_title;
|
||||
endif;
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php $i++; endforeach; ?>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<th colspan="2"><a href="<?php echo admin_url( 'edit.php?post_type=sp_result' ); ?>"><?php printf( __( 'Edit %s', 'sportspress' ), __( 'Results', 'sportspress' ) ); ?></a></th>
|
||||
<th colspan="3"><a href="<?php echo admin_url( 'edit.php?post_type=sp_result' ); ?>"><?php printf( __( 'Edit %s', 'sportspress' ), __( 'Results', 'sportspress' ) ); ?></a></th>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
@@ -163,17 +174,28 @@
|
||||
<tr>
|
||||
<th><?php _e( 'Label', 'sportspress' ); ?></th>
|
||||
<th><?php _e( 'Key', 'sportspress' ); ?></th>
|
||||
<th><?php _e( 'Abbreviation', 'sportspress' ); ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<?php $i = 0; foreach ( $data as $row ): ?>
|
||||
<tr<?php if ( $i % 2 ) echo ' class="alternate"'; ?>>
|
||||
<td class="row-title"><?php echo $row->post_title; ?></td>
|
||||
<td><?php echo $row->post_name; ?></td>
|
||||
<td>
|
||||
<?php
|
||||
$abbreviation = get_post_meta ( $row->ID, 'sp_abbreviation', true );
|
||||
if ( $abbreviation ):
|
||||
echo $abbreviation;
|
||||
else:
|
||||
echo $row->post_title;
|
||||
endif;
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php $i++; endforeach; ?>
|
||||
<tfoot>
|
||||
<tr>
|
||||
<th colspan="2"><a href="<?php echo admin_url( 'edit.php?post_type=sp_outcome' ); ?>"><?php printf( __( 'Edit %s', 'sportspress' ), __( 'Outcomes', 'sportspress' ) ); ?></a></th>
|
||||
<th colspan="3"><a href="<?php echo admin_url( 'edit.php?post_type=sp_outcome' ); ?>"><?php printf( __( 'Edit %s', 'sportspress' ), __( 'Outcomes', 'sportspress' ) ); ?></a></th>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
@@ -25,7 +25,8 @@ function sp_column_edit_columns() {
|
||||
'title' => __( 'Label', 'sportspress' ),
|
||||
'sp_equation' => __( 'Equation', 'sportspress' ),
|
||||
'sp_order' => __( 'Sort Order', 'sportspress' ),
|
||||
'sp_key' => __( 'Key', 'sportspress' )
|
||||
'sp_key' => __( 'Key', 'sportspress' ),
|
||||
'sp_abbreviation' => __( 'Abbreviation', 'sportspress' )
|
||||
);
|
||||
return $columns;
|
||||
}
|
||||
@@ -39,6 +40,7 @@ function sp_column_details_meta( $post ) {
|
||||
$equation = explode( ' ', get_post_meta( $post->ID, 'sp_equation', true ) );
|
||||
$order = get_post_meta( $post->ID, 'sp_order', true );
|
||||
$priority = get_post_meta( $post->ID, 'sp_priority', true );
|
||||
$abbreviation = get_post_meta( $post->ID, 'sp_abbreviation', true );
|
||||
?>
|
||||
<p><strong><?php _e( 'Equation', 'sportspress' ); ?></strong></p>
|
||||
<p class="sp-equation-selector">
|
||||
@@ -67,6 +69,10 @@ function sp_column_details_meta( $post ) {
|
||||
?>
|
||||
</select>
|
||||
</p>
|
||||
<p><strong><?php _e( 'Abbreviation', 'sportspress' ); ?></strong></p>
|
||||
<p>
|
||||
<input name="sp_abbreviation" type="text" size="4" id="sp_abbreviation" value="<?php echo $abbreviation; ?>">
|
||||
</p>
|
||||
<?php
|
||||
sp_nonce();
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ function sp_outcome_edit_columns() {
|
||||
$columns = array(
|
||||
'cb' => '<input type="checkbox" />',
|
||||
'title' => __( 'Label', 'sportspress' ),
|
||||
'sp_abbreviation' => __( 'Abbreviation', 'sportspress' ),
|
||||
'sp_key' => __( 'Key', 'sportspress' )
|
||||
'sp_key' => __( 'Key', 'sportspress' ),
|
||||
'sp_abbreviation' => __( 'Abbreviation', 'sportspress' )
|
||||
);
|
||||
return $columns;
|
||||
}
|
||||
@@ -39,7 +39,7 @@ function sp_outcome_details_meta( $post ) {
|
||||
?>
|
||||
<p><strong><?php _e( 'Abbreviation', 'sportspress' ); ?></strong></p>
|
||||
<p>
|
||||
<input name="sp_abbreviation" type="text" size="4" id="sp_abbreviation" value="<?php echo $abbreviation; ?>" placeholder="<?php echo get_the_title( $post->ID ); ?>">
|
||||
<input name="sp_abbreviation" type="text" size="4" id="sp_abbreviation" value="<?php echo $abbreviation; ?>">
|
||||
</p>
|
||||
<?php
|
||||
sp_nonce();
|
||||
|
||||
@@ -12,6 +12,7 @@ function sp_result_cpt_init() {
|
||||
'show_in_menu' => false,
|
||||
'hierarchical' => false,
|
||||
'supports' => array( 'title', 'page-attributes' ),
|
||||
'register_meta_box_cb' => 'sp_result_meta_init',
|
||||
'capability_type' => 'sp_config'
|
||||
);
|
||||
register_post_type( 'sp_result', $args );
|
||||
@@ -22,9 +23,25 @@ function sp_result_edit_columns() {
|
||||
$columns = array(
|
||||
'cb' => '<input type="checkbox" />',
|
||||
'title' => __( 'Label', 'sportspress' ),
|
||||
'sp_key' => __( 'Key', 'sportspress' )
|
||||
'sp_key' => __( 'Key', 'sportspress' ),
|
||||
'sp_abbreviation' => __( 'Abbreviation', 'sportspress' )
|
||||
);
|
||||
return $columns;
|
||||
}
|
||||
add_filter( 'manage_edit-sp_result_columns', 'sp_result_edit_columns' );
|
||||
|
||||
function sp_result_meta_init() {
|
||||
add_meta_box( 'sp_detailsdiv', __( 'Details', 'sportspress' ), 'sp_result_details_meta', 'sp_result', 'normal', 'high' );
|
||||
}
|
||||
|
||||
function sp_result_details_meta( $post ) {
|
||||
$abbreviation = get_post_meta( $post->ID, 'sp_abbreviation', true );
|
||||
?>
|
||||
<p><strong><?php _e( 'Abbreviation', 'sportspress' ); ?></strong></p>
|
||||
<p>
|
||||
<input name="sp_abbreviation" type="text" size="4" id="sp_abbreviation" value="<?php echo $abbreviation; ?>">
|
||||
</p>
|
||||
<?php
|
||||
sp_nonce();
|
||||
}
|
||||
?>
|
||||
@@ -24,7 +24,8 @@ function sp_statistic_edit_columns() {
|
||||
'cb' => '<input type="checkbox" />',
|
||||
'title' => __( 'Label', 'sportspress' ),
|
||||
'sp_equation' => __( 'Equation', 'sportspress' ),
|
||||
'sp_key' => __( 'Key', 'sportspress' )
|
||||
'sp_key' => __( 'Key', 'sportspress' ),
|
||||
'sp_abbreviation' => __( 'Abbreviation', 'sportspress' )
|
||||
);
|
||||
return $columns;
|
||||
}
|
||||
@@ -36,16 +37,19 @@ function sp_statistic_meta_init() {
|
||||
|
||||
function sp_statistic_equation_meta( $post ) {
|
||||
$equation = explode( ' ', get_post_meta( $post->ID, 'sp_equation', true ) );
|
||||
$abbreviation = get_post_meta( $post->ID, 'sp_abbreviation', true );
|
||||
?>
|
||||
<div>
|
||||
<p class="sp-equation-selector">
|
||||
<?php
|
||||
foreach ( $equation as $piece ):
|
||||
sp_get_equation_selector( $post->ID, $piece, array( 'player_event' ) );
|
||||
endforeach;
|
||||
?>
|
||||
</p>
|
||||
</div>
|
||||
<p class="sp-equation-selector">
|
||||
<?php
|
||||
foreach ( $equation as $piece ):
|
||||
sp_get_equation_selector( $post->ID, $piece, array( 'player_event' ) );
|
||||
endforeach;
|
||||
?>
|
||||
</p>
|
||||
<p><strong><?php _e( 'Abbreviation', 'sportspress' ); ?></strong></p>
|
||||
<p>
|
||||
<input name="sp_abbreviation" type="text" size="4" id="sp_abbreviation" value="<?php echo $abbreviation; ?>">
|
||||
</p>
|
||||
<?php
|
||||
sp_nonce();
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ function sp_team_columns_meta( $post ) {
|
||||
|
||||
foreach ( $div_ids as $div_id ):
|
||||
|
||||
$totals = array( 'eventsplayed' => 0 );
|
||||
$totals = array( 'eventsplayed' => 0, 'streak' => 0 );
|
||||
|
||||
foreach ( $result_labels as $key => $value ):
|
||||
$totals[ $key . 'for' ] = 0;
|
||||
@@ -89,6 +89,7 @@ function sp_team_columns_meta( $post ) {
|
||||
'post_type' => 'sp_event',
|
||||
'numberposts' => -1,
|
||||
'posts_per_page' => -1,
|
||||
'order' => 'ASC',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => 'sp_team',
|
||||
@@ -105,6 +106,9 @@ function sp_team_columns_meta( $post ) {
|
||||
);
|
||||
$events = get_posts( $args );
|
||||
|
||||
// Initialize streaks counter
|
||||
$streak = array( 'name' => '', 'count' => 0 );
|
||||
|
||||
foreach( $events as $event ):
|
||||
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
|
||||
foreach ( $results as $team_id => $team_result ):
|
||||
@@ -115,6 +119,14 @@ function sp_team_columns_meta( $post ) {
|
||||
$totals['eventsplayed']++;
|
||||
$totals[ $value ]++;
|
||||
endif;
|
||||
if ( $value && $value != '-1' ):
|
||||
if ( $streak['name'] == $value ):
|
||||
$streak['count'] ++;
|
||||
else:
|
||||
$streak['name'] = $value;
|
||||
$streak['count'] = 1;
|
||||
endif;
|
||||
endif;
|
||||
else:
|
||||
if ( array_key_exists( $key . 'for', $totals ) ):
|
||||
$totals[ $key . 'for' ] += $value;
|
||||
@@ -131,6 +143,21 @@ function sp_team_columns_meta( $post ) {
|
||||
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 = $outcomes[0];
|
||||
$abbreviation = get_post_meta( $outcome->ID, 'sp_abbreviation', true );
|
||||
$totals['streak'] = ( $abbreviation ? $abbreviation : $outcome->post_title ) . $streak['count'];
|
||||
endif;
|
||||
|
||||
// Generate array of placeholder values for each league
|
||||
$placeholders[ $div_id ] = array();
|
||||
foreach ( $equations as $key => $value ):
|
||||
|
||||
@@ -207,8 +207,8 @@ function sp_save_post( $post_id ) {
|
||||
|
||||
case ( 'sp_result' ):
|
||||
|
||||
// Update equation as string
|
||||
update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) );
|
||||
// Update abbreviation as string
|
||||
update_post_meta( $post_id, 'sp_abbreviation', sp_array_value( $_POST, 'sp_abbreviation', '' ) );
|
||||
|
||||
break;
|
||||
|
||||
@@ -230,6 +230,9 @@ function sp_save_post( $post_id ) {
|
||||
// Update sort order as string
|
||||
update_post_meta( $post_id, 'sp_order', sp_array_value( $_POST, 'sp_order', 'DESC' ) );
|
||||
|
||||
// Update abbreviation as string
|
||||
update_post_meta( $post_id, 'sp_abbreviation', sp_array_value( $_POST, 'sp_abbreviation', '' ) );
|
||||
|
||||
break;
|
||||
|
||||
case ( 'sp_statistic' ):
|
||||
@@ -237,6 +240,9 @@ function sp_save_post( $post_id ) {
|
||||
// Update equation as string
|
||||
update_post_meta( $post_id, 'sp_equation', implode( ' ', sp_array_value( $_POST, 'sp_equation', array() ) ) );
|
||||
|
||||
// Update abbreviation as string
|
||||
update_post_meta( $post_id, 'sp_abbreviation', sp_array_value( $_POST, 'sp_abbreviation', '' ) );
|
||||
|
||||
break;
|
||||
|
||||
case ( 'sp_player' ):
|
||||
|
||||
@@ -331,6 +331,7 @@ if ( !function_exists( 'sp_get_equation_selector' ) ) {
|
||||
break;
|
||||
case 'outcome':
|
||||
$options[ __( 'Outcomes', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_outcome', array( 'max' => '↑', 'min' => '↓' ) );
|
||||
$options[ __( 'Outcomes', 'sportspress' ) ]['$streak'] = __( 'Streak', 'sportspress' );
|
||||
break;
|
||||
case 'column':
|
||||
$options[ __( 'Columns', 'sportspress' ) ] = sp_get_equation_optgroup_array( $postid, 'sp_column' );
|
||||
@@ -767,6 +768,10 @@ if ( !function_exists( 'sportspress_render_option_field' ) ) {
|
||||
if ( !function_exists( 'sp_solve' ) ) {
|
||||
function sp_solve( $equation, $vars ) {
|
||||
|
||||
// Return direct value if streak
|
||||
if ( str_replace( ' ', '', $equation ) == '$streak' )
|
||||
return sp_array_value( $vars, 'streak', 0 );
|
||||
|
||||
// Clearance to begin calculating remains true if all equation variables are in vars
|
||||
$clearance = true;
|
||||
|
||||
@@ -784,7 +789,7 @@ if ( !function_exists( 'sp_solve' ) ) {
|
||||
$eos = new eqEOS();
|
||||
|
||||
// Solve using EOS
|
||||
return $eos->solveIF( str_replace( ' ', '', $equation ), $vars );
|
||||
return round( $eos->solveIF( str_replace( ' ', '', $equation ), $vars ), 3 ); // TODO: add precision setting to each column with default set to 3
|
||||
else:
|
||||
return 0;
|
||||
endif;
|
||||
@@ -812,11 +817,16 @@ if ( !function_exists( 'sp_get_table' ) ) {
|
||||
$totals = array();
|
||||
$placeholders = array();
|
||||
|
||||
// Initialize streaks counter
|
||||
$streaks = array();
|
||||
|
||||
foreach ( $team_ids as $team_id ):
|
||||
if ( ! $team_id )
|
||||
continue;
|
||||
|
||||
$totals[ $team_id ] = array( 'eventsplayed' => 0 );
|
||||
$streaks[ $team_id ] = array( 'name' => '', 'count' => 0 );
|
||||
|
||||
$totals[ $team_id ] = array( 'eventsplayed' => 0, 'streak' => 0 );
|
||||
|
||||
foreach ( $result_labels as $key => $value ):
|
||||
$totals[ $team_id ][ $key . 'for' ] = 0;
|
||||
@@ -844,6 +854,7 @@ if ( !function_exists( 'sp_get_table' ) ) {
|
||||
'post_type' => 'sp_event',
|
||||
'numberposts' => -1,
|
||||
'posts_per_page' => -1,
|
||||
'order' => 'ASC',
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'sp_league',
|
||||
@@ -855,21 +866,27 @@ if ( !function_exists( 'sp_get_table' ) ) {
|
||||
$events = get_posts( $args );
|
||||
|
||||
// Event loop
|
||||
foreach( $events as $event ):
|
||||
foreach ( $events as $event ):
|
||||
|
||||
$results = (array)get_post_meta( $event->ID, 'sp_results', true );
|
||||
|
||||
foreach ( $results as $team_id => $team_result ):
|
||||
|
||||
// Increment events played
|
||||
$totals[ $team_id ]['eventsplayed']++;
|
||||
|
||||
foreach ( $team_result as $key => $value ):
|
||||
|
||||
if ( $key == 'outcome' ):
|
||||
if ( array_key_exists( $value, $totals[ $team_id ] ) ):
|
||||
$totals[ $team_id ]['eventsplayed']++;
|
||||
$totals[ $team_id ][ $value ]++;
|
||||
endif;
|
||||
if ( $value && $value != '-1' ):
|
||||
if ( $streaks[ $team_id ]['name'] == $value ):
|
||||
$streaks[ $team_id ]['count'] ++;
|
||||
else:
|
||||
$streaks[ $team_id ]['name'] = $value;
|
||||
$streaks[ $team_id ]['count'] = 1;
|
||||
endif;
|
||||
endif;
|
||||
else:
|
||||
if ( array_key_exists( $key . 'for', $totals[ $team_id ] ) ):
|
||||
$totals[ $team_id ][ $key . 'for' ] += $value;
|
||||
@@ -882,6 +899,23 @@ if ( !function_exists( 'sp_get_table' ) ) {
|
||||
|
||||
endforeach;
|
||||
|
||||
foreach ( $streaks as $team_id => $streak ):
|
||||
// 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 = $outcomes[0];
|
||||
$abbreviation = get_post_meta( $outcome->ID, 'sp_abbreviation', true );
|
||||
$totals[ $team_id ]['streak'] = ( $abbreviation ? $abbreviation : $outcome->post_title ) . $streak['count'];
|
||||
endif;
|
||||
endforeach;
|
||||
|
||||
$args = array(
|
||||
'post_type' => 'sp_column',
|
||||
'numberposts' => -1,
|
||||
|
||||
Reference in New Issue
Block a user