Enable metrics and stats in player list widget

This commit is contained in:
Brian Miyaji
2014-05-12 07:32:03 +10:00
parent c09ba720af
commit 0463c61dee
4 changed files with 41 additions and 25 deletions

View File

@@ -12,9 +12,20 @@
*/ */
class SP_Player_List extends SP_Custom_Post { class SP_Player_List extends SP_Custom_Post {
/** @var array The columns array. */
public $columns;
/** @var array The sort priorities array. */ /** @var array The sort priorities array. */
public $priorities; public $priorities;
/**
* Constructor
*/
public function __construct( $post ) {
parent::__construct( $post );
$this->columns = get_post_meta( $this->ID, 'sp_columns', true );
}
/** /**
* Returns formatted data * Returns formatted data
* *
@@ -27,7 +38,6 @@ class SP_Player_List extends SP_Custom_Post {
$div_id = sp_get_the_term_id( $this->ID, 'sp_season', 0 ); $div_id = sp_get_the_term_id( $this->ID, 'sp_season', 0 );
$player_ids = (array)get_post_meta( $this->ID, 'sp_player', false ); $player_ids = (array)get_post_meta( $this->ID, 'sp_player', false );
$list_stats = (array)get_post_meta( $this->ID, 'sp_players', true ); $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 ); $adjustments = get_post_meta( $this->ID, 'sp_adjustments', true );
$orderby = get_post_meta( $this->ID, 'sp_orderby', true ); $orderby = get_post_meta( $this->ID, 'sp_orderby', true );
$order = get_post_meta( $this->ID, 'sp_order', true ); $order = get_post_meta( $this->ID, 'sp_order', true );
@@ -332,12 +342,12 @@ class SP_Player_List extends SP_Custom_Post {
endforeach; endforeach;
if ( $admin ): if ( $admin ):
return array( $columns, $usecolumns, $data, $placeholders, $merged ); return array( $columns, $this->columns, $data, $placeholders, $merged );
else: else:
if ( ! is_array( $usecolumns ) ) if ( ! is_array( $this->columns ) )
$usecolumns = array(); $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;

View File

@@ -11,7 +11,7 @@ class SP_Widget_Player_list extends WP_Widget {
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
$id = empty($instance['id']) ? null : $instance['id']; $id = empty($instance['id']) ? null : $instance['id'];
$number = empty($instance['number']) ? null : $instance['number']; $number = empty($instance['number']) ? null : $instance['number'];
$performance = $instance['performance']; $columns = $instance['columns'];
$orderby = empty($instance['orderby']) ? 'default' : $instance['orderby']; $orderby = empty($instance['orderby']) ? 'default' : $instance['orderby'];
$order = empty($instance['order']) ? 'ASC' : $instance['order']; $order = empty($instance['order']) ? 'ASC' : $instance['order'];
$show_all_players_link = empty($instance['show_all_players_link']) ? false : $instance['show_all_players_link']; $show_all_players_link = empty($instance['show_all_players_link']) ? false : $instance['show_all_players_link'];
@@ -19,7 +19,7 @@ class SP_Widget_Player_list extends WP_Widget {
if ( $title ) if ( $title )
echo $before_title . $title . $after_title; echo $before_title . $title . $after_title;
echo '<div id="sp_player_list_wrap">'; echo '<div id="sp_player_list_wrap">';
sp_get_template( 'player-list.php', array( 'id' => $id, 'number' => $number, 'performance' => $performance, 'orderby' => $orderby, 'order' => $order, 'show_all_players_link' => $show_all_players_link ) ); sp_get_template( 'player-list.php', array( 'id' => $id, 'number' => $number, 'columns' => $columns, 'orderby' => $orderby, 'order' => $order, 'grouping' => 0, 'show_all_players_link' => $show_all_players_link ) );
echo '</div>'; echo '</div>';
echo $after_widget; echo $after_widget;
} }
@@ -29,7 +29,7 @@ class SP_Widget_Player_list extends WP_Widget {
$instance['title'] = strip_tags($new_instance['title']); $instance['title'] = strip_tags($new_instance['title']);
$instance['id'] = intval($new_instance['id']); $instance['id'] = intval($new_instance['id']);
$instance['number'] = intval($new_instance['number']); $instance['number'] = intval($new_instance['number']);
$instance['performance'] = (array)$new_instance['performance']; $instance['columns'] = (array)$new_instance['columns'];
$instance['orderby'] = strip_tags($new_instance['orderby']); $instance['orderby'] = strip_tags($new_instance['orderby']);
$instance['order'] = strip_tags($new_instance['order']); $instance['order'] = strip_tags($new_instance['order']);
$instance['show_all_players_link'] = $new_instance['show_all_players_link']; $instance['show_all_players_link'] = $new_instance['show_all_players_link'];
@@ -38,11 +38,11 @@ class SP_Widget_Player_list extends WP_Widget {
} }
function form( $instance ) { function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'id' => '', 'number' => 5, 'performance' => null, 'orderby' => 'default', 'order' => 'ASC', 'show_all_players_link' => true ) ); $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'id' => '', 'number' => 5, 'columns' => null, 'orderby' => 'default', 'order' => 'ASC', 'show_all_players_link' => true ) );
$title = strip_tags($instance['title']); $title = strip_tags($instance['title']);
$id = intval($instance['id']); $id = intval($instance['id']);
$number = intval($instance['number']); $number = intval($instance['number']);
$performance = $instance['performance']; $columns = $instance['columns'];
$orderby = strip_tags($instance['orderby']); $orderby = strip_tags($instance['orderby']);
$order = strip_tags($instance['order']); $order = strip_tags($instance['order']);
$show_all_players_link = $instance['show_all_players_link']; $show_all_players_link = $instance['show_all_players_link'];
@@ -73,20 +73,20 @@ class SP_Widget_Player_list extends WP_Widget {
<?php _e( 'Performance:', 'sportspress' ); ?><br> <?php _e( 'Performance:', 'sportspress' ); ?><br>
<?php <?php
$args = array( $args = array(
'post_type' => 'sp_performance', 'post_type' => array( 'sp_metric', 'sp_performance', 'sp_statistic' ),
'numberposts' => -1, 'numberposts' => -1,
'posts_per_page' => -1, 'posts_per_page' => -1,
'orderby' => 'menu_order', 'orderby' => 'menu_order',
'order' => 'ASC' 'order' => 'ASC'
); );
$the_performance = get_posts( $args ); $the_columns = get_posts( $args );
$field_name = $this->get_field_name('performance') . '[]'; $field_name = $this->get_field_name('columns') . '[]';
$field_id = $this->get_field_id('performance'); $field_id = $this->get_field_id('columns');
?> ?>
<label class="button"><input name="<?php echo $field_name; ?>" type="checkbox" id="<?php echo $field_id . '-' . 'eventsplayed'; ?>" value="<?php echo 'eventsplayed'; ?>" <?php if ( is_array( $performance) && in_array( 'eventsplayed', $performance ) ): ?>checked="checked"<?php endif; ?>><?php _e( 'Played', 'sportspress' ); ?></label> <label class="button"><input name="<?php echo $field_name; ?>" type="checkbox" id="<?php echo $field_id . '-' . 'eventsplayed'; ?>" value="<?php echo 'eventsplayed'; ?>" <?php if ( is_array( $columns) && in_array( 'eventsplayed', $columns ) ): ?>checked="checked"<?php endif; ?>><?php _e( 'Played', 'sportspress' ); ?></label>
<?php foreach ( $the_performance as $column ): ?> <?php foreach ( $the_columns as $column ): ?>
<label class="button"><input name="<?php echo $field_name; ?>" type="checkbox" id="<?php echo $field_id . '-' . $column->post_name; ?>" value="<?php echo $column->post_name; ?>" <?php if ( $performance === null || in_array( $column->post_name, $performance ) ): ?>checked="checked"<?php endif; ?>><?php echo $column->post_title; ?></label> <label class="button"><input name="<?php echo $field_name; ?>" type="checkbox" id="<?php echo $field_id . '-' . $column->post_name; ?>" value="<?php echo $column->post_name; ?>" <?php if ( $columns === null || in_array( $column->post_name, $columns ) ): ?>checked="checked"<?php endif; ?>><?php echo $column->post_title; ?></label>
<?php endforeach; ?> <?php endforeach; ?>
</p> </p>
@@ -99,7 +99,7 @@ class SP_Widget_Player_list extends WP_Widget {
'name' => __( 'Name', 'sportspress' ), 'name' => __( 'Name', 'sportspress' ),
'eventsplayed' => __( 'Played', 'sportspress' ) 'eventsplayed' => __( 'Played', 'sportspress' )
), ),
'post_type' => 'sp_performance', 'post_type' => array( 'sp_metric', 'sp_performance', 'sp_statistic' ),
'name' => $this->get_field_name('orderby'), 'name' => $this->get_field_name('orderby'),
'id' => $this->get_field_id('orderby'), 'id' => $this->get_field_id('orderby'),
'selected' => $orderby, 'selected' => $orderby,

View File

@@ -45,12 +45,13 @@ $float = is_rtl() ? 'right' : 'left';
$selector = 'sp-player-gallery-' . $id; $selector = 'sp-player-gallery-' . $id;
$list = new SP_Player_List( $id ); $list = new SP_Player_List( $id );
$list->columns = $performance;
$data = $list->data(); $data = $list->data();
// 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 ( ! $grouping || $grouping == 'default' ): if ( $grouping === null || $grouping === 'default' ):
$grouping = $list->grouping; $grouping = $list->grouping;
endif; endif;
@@ -95,7 +96,7 @@ echo apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
if ( is_int( $number ) && $number > 0 ) if ( is_int( $number ) && $number > 0 )
$limit = $number; $limit = $number;
if ( $grouping == 'position' ): if ( $grouping === 'position' ):
$groups = get_terms( 'sp_position' ); $groups = get_terms( 'sp_position' );
else: else:
$group = new stdClass(); $group = new stdClass();

View File

@@ -12,7 +12,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
$defaults = array( $defaults = array(
'id' => get_the_ID(), 'id' => get_the_ID(),
'number' => -1, 'number' => -1,
'performance' => null, 'columns' => null,
'grouping' => null, 'grouping' => null,
'orderby' => 'default', 'orderby' => 'default',
'order' => 'ASC', 'order' => 'ASC',
@@ -26,7 +26,12 @@ $defaults = array(
extract( $defaults, EXTR_SKIP ); extract( $defaults, EXTR_SKIP );
// Backward compatibility
if ( isset( $performance ) )
$columns = $performance;
$list = new SP_Player_List( $id ); $list = new SP_Player_List( $id );
$list->columns = $columns;
$data = $list->data(); $data = $list->data();
// The first row should be column labels // The first row should be column labels
@@ -35,7 +40,7 @@ $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 ( ! $grouping || $grouping == 'default' ): if ( $grouping === null || $grouping === 'default' ):
$grouping = $list->grouping; $grouping = $list->grouping;
endif; endif;
@@ -52,7 +57,7 @@ else:
uasort( $data, array( $list, 'sort' ) ); uasort( $data, array( $list, 'sort' ) );
endif; endif;
if ( $grouping == 'position' ): if ( $grouping === 'position' ):
$groups = get_terms( 'sp_position' ); $groups = get_terms( 'sp_position' );
else: else:
$group = new stdClass(); $group = new stdClass();
@@ -80,7 +85,7 @@ foreach ( $groups as $group ):
endif; endif;
foreach( $labels as $key => $label ): foreach( $labels as $key => $label ):
if ( ! is_array( $performance ) || $key == 'name' || in_array( $key, $performance ) ) if ( ! is_array( $columns ) || $key == 'name' || in_array( $key, $columns ) )
$output .= '<th class="data-' . $key . '">'. $label . '</th>'; $output .= '<th class="data-' . $key . '">'. $label . '</th>';
endforeach; endforeach;
@@ -117,7 +122,7 @@ foreach ( $groups as $group ):
foreach( $labels as $key => $value ): foreach( $labels as $key => $value ):
if ( $key == 'name' ) if ( $key == 'name' )
continue; continue;
if ( ! is_array( $performance ) || in_array( $key, $performance ) ) if ( ! is_array( $columns ) || in_array( $key, $columns ) )
$output .= '<td class="data-' . $key . '">' . sp_array_value( $row, $key, '&mdash;' ) . '</td>'; $output .= '<td class="data-' . $key . '">' . sp_array_value( $row, $key, '&mdash;' ) . '</td>';
endforeach; endforeach;