From a2282d37a22d8e5a7317f408cca018dff4f28d27 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Fri, 4 May 2018 17:55:09 +1000 Subject: [PATCH] Add player assignments with fallback for legacy players --- includes/class-sp-player-list.php | 29 ++------- modules/sportspress-player-assignments.php | 69 ++++++++++++++++++++-- 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/includes/class-sp-player-list.php b/includes/class-sp-player-list.php index def9ecbf..2f43c78c 100644 --- a/includes/class-sp-player-list.php +++ b/includes/class-sp-player-list.php @@ -47,15 +47,6 @@ class SP_Player_List extends SP_Secondary_Post { $crop = get_post_meta( $this->ID, 'sp_crop', true ); $order = get_post_meta( $this->ID, 'sp_order', true ); $select = get_post_meta( $this->ID, 'sp_select', true ); - //Player Assignments - $assignments = array(); - foreach ( $league_ids as $l_id ) { - foreach ( $season_ids as $s_id ) { - if ( $team != '0' ) { - $assignments[] = $l_id.'_'.$s_id.'_'.$team; - } - } - } $this->date = $this->__get( 'date' ); @@ -108,17 +99,7 @@ class SP_Player_List extends SP_Secondary_Post { 'relation' => 'AND', ), ); - //Use the Player Assignments to filter players - if ( false ) { - $args['meta_query'] = array( - array( - 'key' => 'sp_assignments', - 'value' => $assignments, - 'compare' => 'IN' - ), - ); - }else{ - //Use the legacy way to filter players + if ( $league_ids ): $args['tax_query'][] = array( 'taxonomy' => 'sp_league', @@ -152,8 +133,6 @@ class SP_Player_List extends SP_Secondary_Post { ), ); endif; - - } if ( $position_ids ): $args['tax_query'][] = array( @@ -163,7 +142,11 @@ class SP_Player_List extends SP_Secondary_Post { ); endif; - $players = get_posts( $args ); + $args = apply_filters( 'sportspress_player_list_args', $args, $team ); + + $players = (array) get_posts( $args ); + + $players = apply_filters( 'sportspress_player_list_players', $players, $args, $team ); if ( $players && is_array( $players ) ) { foreach ( $players as $player ) { diff --git a/modules/sportspress-player-assignments.php b/modules/sportspress-player-assignments.php index 03f38fe4..daed2c69 100644 --- a/modules/sportspress-player-assignments.php +++ b/modules/sportspress-player-assignments.php @@ -26,6 +26,10 @@ class SportsPress_Player_Assignments { // Actions add_action( 'sportspress_save_meta_player_statistics', array( $this, 'save_additional_statistics' ), 10, 2 ); + + // Filters + add_filter( 'sportspress_player_list_args', array( $this, 'add_args' ), 10 ); + add_filter( 'sportspress_player_list_players', array( $this, 'add_players' ), 10, 3 ); } /** * Define constants. @@ -48,14 +52,69 @@ class SportsPress_Player_Assignments { $leagues = $post_data['sp_leagues']; foreach ( $leagues as $l_id => $season ) { + if ( 0 === $l_id ) continue; foreach ( $season as $s_id => $team_id ) { - if ( $team_id != '-1' ) { - $serialized = $l_id.'_'.$s_id.'_'.$team_id; - add_post_meta( $post_id, 'sp_assignments', $serialized, false ); - } + if ( 0 >= $team_id ) continue; + $serialized = $l_id.'_'.$s_id.'_'.$team_id; + add_post_meta( $post_id, 'sp_assignments', $serialized, false ); } } } + + /** + * Add args to filter out assigned players + */ + public function add_args( $args = array() ) { + $args['meta_query'][] = array( + 'key' => 'sp_assignments', + 'value' => '', + 'compare' => 'NOT EXISTS', + ); + + $args['meta_query'][] = array( + 'relation' => 'OR', + ); + + return $args; + } + + /** + * Add assigned players to player list + */ + public function add_players( $players = array(), $args = array(), $team = false ) { + $tax_query = (array) sp_array_value( $args, 'tax_query', array() ); + $league_ids = array(); + $season_ids = array(); + + foreach ( $tax_query as $param ) { + if ( 'sp_league' === sp_array_value( $param, 'taxonomy' ) ) $league_ids = sp_array_value( $param, 'terms', array() ); + if ( 'sp_season' === sp_array_value( $param, 'taxonomy' ) ) $season_ids = sp_array_value( $param, 'terms', array() ); + } + + $assignments = array(); + foreach ( $league_ids as $l_id ) { + foreach ( $season_ids as $s_id ) { + if ( $team && $team != '0' ) { + $assignments[] = $l_id.'_'.$s_id.'_'.$team; + } + } + } + + $args['meta_query'] = array( + array( + 'key' => 'sp_assignments', + 'value' => $assignments, + 'compare' => 'IN' + ), + ); + + $assigned_players = (array) get_posts( $args ); + + $players = array_merge( $assigned_players, $players ); + + return $players; + } } endif; -//new SportsPress_Player_Assignments(); \ No newline at end of file + +new SportsPress_Player_Assignments(); \ No newline at end of file