Add automatic player statistic column setting

This commit is contained in:
Brian Miyaji
2016-04-01 01:22:01 +11:00
parent 8029293d30
commit fdb780aa28
5 changed files with 64 additions and 30 deletions

View File

@@ -24,6 +24,7 @@ class SP_Admin_Post_Types {
add_action( 'admin_init', array( $this, 'include_post_type_handlers' ) ); add_action( 'admin_init', array( $this, 'include_post_type_handlers' ) );
add_action( 'save_post', array( $this, 'unflag_post' ) ); add_action( 'save_post', array( $this, 'unflag_post' ) );
add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) ); add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
add_filter( 'sportspress_meta_boxes', array( $this, 'meta_boxes' ) );
} }
/** /**
@@ -105,6 +106,16 @@ class SP_Admin_Post_Types {
return $messages; return $messages;
} }
/**
* Remove meta boxes as needed
*/
public static function meta_boxes( $meta_boxes ) {
if ( 'manual' != get_option( 'sportspress_player_columns', 'auto' ) ) {
unset( $meta_boxes['sp_player']['columns'] );
}
return $meta_boxes;
}
} }
endif; endif;

View File

@@ -43,17 +43,6 @@ class SP_Meta_Box_Player_Statistics {
} else { } else {
$section_order = array( __( 'Offense', 'sportspress' ), __( 'Defense', 'sportspress' ) ); $section_order = array( __( 'Offense', 'sportspress' ), __( 'Defense', 'sportspress' ) );
} }
// Get labels by section
$args = array(
'post_type' => 'sp_performance',
'numberposts' => 100,
'posts_per_page' => 100,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$columns = get_posts( $args );
$s = 0; $s = 0;
foreach ( $section_order as $section_id => $section_label ) { foreach ( $section_order as $section_id => $section_label ) {

View File

@@ -25,10 +25,15 @@ class SP_Meta_Box_Statistic_Details extends SP_Meta_Box_Config {
wp_nonce_field( 'sportspress_save_data', 'sportspress_meta_nonce' ); wp_nonce_field( 'sportspress_save_data', 'sportspress_meta_nonce' );
$precision = get_post_meta( $post->ID, 'sp_precision', true ); $precision = get_post_meta( $post->ID, 'sp_precision', true );
$section = get_post_meta( $post->ID, 'sp_section', true ); $section = get_post_meta( $post->ID, 'sp_section', true );
$visibility = get_post_meta( $post->ID, 'sp_visibility', true );
// Options
$visibility_options = apply_filters( 'sportspress_statistic_visibility_options', array( 'sp_event', 'sp_player', 'sp_list' ) );
// Defaults // Defaults
if ( '' === $precision ) $precision = 0; if ( '' === $precision ) $precision = 0;
if ( '' === $section ) $section = -1; if ( '' === $section ) $section = -1;
if ( ! is_array( $visibility ) ) $visibility = $visibility_options;
?> ?>
<p><strong><?php _e( 'Key', 'sportspress' ); ?></strong></p> <p><strong><?php _e( 'Key', 'sportspress' ); ?></strong></p>
<p> <p>
@@ -50,6 +55,17 @@ class SP_Meta_Box_Statistic_Details extends SP_Meta_Box_Config {
?> ?>
</select> </select>
</p> </p>
<p><strong><?php _e( 'Visibility', 'sportspress' ); ?></strong></p>
<ul class="categorychecklist form-no-clear">
<?php foreach ( $visibility_options as $option ) { $object = get_post_type_object( $option ); ?>
<li>
<label class="selectit">
<input name="sp_visibility[]" id="sp_visibility_<?php echo $option; ?>" type="checkbox" value="<?php echo $option; ?>" <?php checked( in_array( $option, $visibility ) ); ?>>
<?php echo $object->labels->singular_name; ?>
</label>
</li>
<?php } ?>
</ul>
<?php <?php
} }
@@ -60,6 +76,7 @@ class SP_Meta_Box_Statistic_Details extends SP_Meta_Box_Config {
self::delete_duplicate( $_POST ); self::delete_duplicate( $_POST );
update_post_meta( $post_id, 'sp_section', (int) sp_array_value( $_POST, 'sp_section', -1 ) ); update_post_meta( $post_id, 'sp_section', (int) sp_array_value( $_POST, 'sp_section', -1 ) );
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_visibility', (array) sp_array_value( $_POST, 'sp_visibility', array() ) );
} }
} }

View File

@@ -133,6 +133,17 @@ class SP_Settings_Players extends SP_Settings_Page {
'checkboxgroup' => 'end', 'checkboxgroup' => 'end',
), ),
array(
'title' => __( 'Columns', 'sportspress' ),
'id' => 'sportspress_player_columns',
'default' => 'auto',
'type' => 'radio',
'options' => array(
'auto' => __( 'Auto', 'sportspress' ),
'manual' => __( 'Manual', 'sportspress' ),
),
),
array( array(
'title' => __( 'Statistics', 'sportspress' ), 'title' => __( 'Statistics', 'sportspress' ),
'id' => 'sportspress_player_performance_sections', 'id' => 'sportspress_player_performance_sections',

View File

@@ -115,11 +115,9 @@ class SP_Player extends SP_Custom_Post {
$metrics = (array)get_post_meta( $this->ID, 'sp_metrics', true ); $metrics = (array)get_post_meta( $this->ID, 'sp_metrics', true );
$stats = (array)get_post_meta( $this->ID, 'sp_statistics', true ); $stats = (array)get_post_meta( $this->ID, 'sp_statistics', true );
$leagues = sp_array_value( (array)get_post_meta( $this->ID, 'sp_leagues', true ), $league_id, array() ); $leagues = sp_array_value( (array)get_post_meta( $this->ID, 'sp_leagues', true ), $league_id, array() );
$usecolumns = get_post_meta( $this->ID, 'sp_columns', true );
// Get labels by section
$args = array( $args = array(
'post_type' => 'sp_performance', 'post_type' => array( 'sp_performance', 'sp_statistic' ),
'numberposts' => 100, 'numberposts' => 100,
'posts_per_page' => 100, 'posts_per_page' => 100,
'orderby' => 'menu_order', 'orderby' => 'menu_order',
@@ -128,6 +126,22 @@ class SP_Player extends SP_Custom_Post {
$posts = get_posts( $args ); $posts = get_posts( $args );
if ( 'manual' == get_option( 'sportspress_player_columns', 'auto' ) ) {
$usecolumns = get_post_meta( $this->ID, 'sp_columns', true );
} else {
$usecolumns = array();
if ( is_array( $posts ) ) {
foreach ( $posts as $post ) {
// Get visibility
$visibility = get_post_meta( $post->ID, 'sp_visibility', true );
if ( ! is_array( $visibility ) || in_array( 'sp_player', $visibility ) ) {
$usecolumns[] = $post->post_name;
}
}
}
}
// Get labels by section
$performance_labels = array(); $performance_labels = array();
foreach ( $posts as $post ): foreach ( $posts as $post ):
@@ -144,7 +158,6 @@ class SP_Player extends SP_Custom_Post {
$performance_labels[ $post->post_name ] = $post->post_title; $performance_labels[ $post->post_name ] = $post->post_title;
} }
} }
endforeach; endforeach;
// Get labels from outcome variables // Get labels from outcome variables
@@ -525,20 +538,13 @@ class SP_Player extends SP_Custom_Post {
endforeach; endif; endforeach; endif;
return array( $labels, $data, $placeholders, $merged, $leagues ); return array( $labels, $data, $placeholders, $merged, $leagues );
else: else:
if ( ! is_array( $this->columns ) ) if ( is_array( $usecolumns ) ):
$this->columns = array(); foreach ( $columns as $key => $label ):
foreach ( $columns as $key => $label ): if ( ! in_array( $key, $usecolumns ) ):
if ( ! in_array( $key, $this->columns ) ): unset( $columns[ $key ] );
unset( $columns[ $key ] ); endif;
endif; endforeach;
endforeach; endif;
if ( ! is_array( $usecolumns ) )
$usecolumns = array();
foreach ( $columns as $key => $label ):
if ( ! in_array( $key, $usecolumns ) ):
unset( $columns[ $key ] );
endif;
endforeach;
$labels = array(); $labels = array();