From 0463c61dee0ca217958f5f11607f33c13575352d Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Mon, 12 May 2014 07:32:03 +1000 Subject: [PATCH] Enable metrics and stats in player list widget --- includes/class-sp-player-list.php | 20 ++++++++++---- .../widgets/class-sp-widget-player-list.php | 26 +++++++++---------- templates/player-gallery.php | 5 ++-- templates/player-list.php | 15 +++++++---- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/includes/class-sp-player-list.php b/includes/class-sp-player-list.php index 4c640341..74c1580c 100644 --- a/includes/class-sp-player-list.php +++ b/includes/class-sp-player-list.php @@ -12,9 +12,20 @@ */ class SP_Player_List extends SP_Custom_Post { + /** @var array The columns array. */ + public $columns; + /** @var array The sort priorities array. */ public $priorities; + /** + * Constructor + */ + public function __construct( $post ) { + parent::__construct( $post ); + $this->columns = get_post_meta( $this->ID, 'sp_columns', true ); + } + /** * 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 ); $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 ); @@ -332,12 +342,12 @@ class SP_Player_List extends SP_Custom_Post { endforeach; if ( $admin ): - return array( $columns, $usecolumns, $data, $placeholders, $merged ); + return array( $columns, $this->columns, $data, $placeholders, $merged ); else: - if ( ! is_array( $usecolumns ) ) - $usecolumns = array(); + if ( ! is_array( $this->columns ) ) + $this->columns = array(); foreach ( $columns as $key => $label ): - if ( ! in_array( $key, $usecolumns ) ): + if ( ! in_array( $key, $this->columns ) ): unset( $columns[ $key ] ); endif; endforeach; diff --git a/includes/widgets/class-sp-widget-player-list.php b/includes/widgets/class-sp-widget-player-list.php index f433d4bc..b17b817f 100644 --- a/includes/widgets/class-sp-widget-player-list.php +++ b/includes/widgets/class-sp-widget-player-list.php @@ -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); $id = empty($instance['id']) ? null : $instance['id']; $number = empty($instance['number']) ? null : $instance['number']; - $performance = $instance['performance']; + $columns = $instance['columns']; $orderby = empty($instance['orderby']) ? 'default' : $instance['orderby']; $order = empty($instance['order']) ? 'ASC' : $instance['order']; $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 ) echo $before_title . $title . $after_title; echo '
'; - 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 '
'; echo $after_widget; } @@ -29,7 +29,7 @@ class SP_Widget_Player_list extends WP_Widget { $instance['title'] = strip_tags($new_instance['title']); $instance['id'] = intval($new_instance['id']); $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['order'] = strip_tags($new_instance['order']); $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 ) { - $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']); $id = intval($instance['id']); $number = intval($instance['number']); - $performance = $instance['performance']; + $columns = $instance['columns']; $orderby = strip_tags($instance['orderby']); $order = strip_tags($instance['order']); $show_all_players_link = $instance['show_all_players_link']; @@ -73,20 +73,20 @@ class SP_Widget_Player_list extends WP_Widget {
'sp_performance', + 'post_type' => array( 'sp_metric', 'sp_performance', 'sp_statistic' ), 'numberposts' => -1, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC' ); - $the_performance = get_posts( $args ); + $the_columns = get_posts( $args ); - $field_name = $this->get_field_name('performance') . '[]'; - $field_id = $this->get_field_id('performance'); + $field_name = $this->get_field_name('columns') . '[]'; + $field_id = $this->get_field_id('columns'); ?> - - - + + +

@@ -99,7 +99,7 @@ class SP_Widget_Player_list extends WP_Widget { 'name' => __( 'Name', 'sportspress' ), 'eventsplayed' => __( 'Played', 'sportspress' ) ), - 'post_type' => 'sp_performance', + 'post_type' => array( 'sp_metric', 'sp_performance', 'sp_statistic' ), 'name' => $this->get_field_name('orderby'), 'id' => $this->get_field_id('orderby'), 'selected' => $orderby, diff --git a/templates/player-gallery.php b/templates/player-gallery.php index ca75621a..7404a2aa 100644 --- a/templates/player-gallery.php +++ b/templates/player-gallery.php @@ -45,12 +45,13 @@ $float = is_rtl() ? 'right' : 'left'; $selector = 'sp-player-gallery-' . $id; $list = new SP_Player_List( $id ); +$list->columns = $performance; $data = $list->data(); // Remove the first row to leave us with the actual data unset( $data[0] ); -if ( ! $grouping || $grouping == 'default' ): +if ( $grouping === null || $grouping === 'default' ): $grouping = $list->grouping; endif; @@ -95,7 +96,7 @@ echo apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div ); if ( is_int( $number ) && $number > 0 ) $limit = $number; -if ( $grouping == 'position' ): +if ( $grouping === 'position' ): $groups = get_terms( 'sp_position' ); else: $group = new stdClass(); diff --git a/templates/player-list.php b/templates/player-list.php index 09cb905e..080d97fd 100644 --- a/templates/player-list.php +++ b/templates/player-list.php @@ -12,7 +12,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly $defaults = array( 'id' => get_the_ID(), 'number' => -1, - 'performance' => null, + 'columns' => null, 'grouping' => null, 'orderby' => 'default', 'order' => 'ASC', @@ -26,7 +26,12 @@ $defaults = array( extract( $defaults, EXTR_SKIP ); +// Backward compatibility +if ( isset( $performance ) ) + $columns = $performance; + $list = new SP_Player_List( $id ); +$list->columns = $columns; $data = $list->data(); // 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 unset( $data[0] ); -if ( ! $grouping || $grouping == 'default' ): +if ( $grouping === null || $grouping === 'default' ): $grouping = $list->grouping; endif; @@ -52,7 +57,7 @@ else: uasort( $data, array( $list, 'sort' ) ); endif; -if ( $grouping == 'position' ): +if ( $grouping === 'position' ): $groups = get_terms( 'sp_position' ); else: $group = new stdClass(); @@ -80,7 +85,7 @@ foreach ( $groups as $group ): endif; 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 .= ''. $label . ''; endforeach; @@ -117,7 +122,7 @@ foreach ( $groups as $group ): foreach( $labels as $key => $value ): if ( $key == 'name' ) continue; - if ( ! is_array( $performance ) || in_array( $key, $performance ) ) + if ( ! is_array( $columns ) || in_array( $key, $columns ) ) $output .= '' . sp_array_value( $row, $key, '—' ) . ''; endforeach;