From 5c58a06390af2a37bc319103646dd8c7187d4c0f Mon Sep 17 00:00:00 2001 From: savvasha Date: Mon, 26 Mar 2018 16:32:01 +0300 Subject: [PATCH 1/8] Add a hook --- .../meta-boxes/class-sp-meta-box-player-statistics.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php index 247ef847..ba7d5f94 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-statistics.php @@ -81,6 +81,7 @@ class SP_Meta_Box_Player_Statistics { * Save meta box data */ public static function save( $post_id, $post ) { + do_action( 'sportspress_save_meta_player_statistics', $post_id, $_POST ); update_post_meta( $post_id, 'sp_leagues', sp_array_value( $_POST, 'sp_leagues', array() ) ); update_post_meta( $post_id, 'sp_statistics', sp_array_value( $_POST, 'sp_statistics', array() ) ); } From 41aba6b4890d5fe4e436dd1690bf232299c9e35b Mon Sep 17 00:00:00 2001 From: savvasha Date: Mon, 26 Mar 2018 16:32:54 +0300 Subject: [PATCH 2/8] Create an empty basic module for the Player Assignments --- modules/sportspress-player-assignments.php | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 modules/sportspress-player-assignments.php diff --git a/modules/sportspress-player-assignments.php b/modules/sportspress-player-assignments.php new file mode 100644 index 00000000..beb0948d --- /dev/null +++ b/modules/sportspress-player-assignments.php @@ -0,0 +1,44 @@ +define_constants(); + // Actions + // Filters + } + /** + * Define constants. + */ + private function define_constants() { + if ( !defined( 'SP_PLAYER_ASSIGNMENTS_VERSION' ) ) + define( 'SP_PLAYER_ASSIGNMENTS_VERSION', '2.6.0' ); + if ( !defined( 'SP_PLAYER_ASSIGNMENTS_URL' ) ) + define( 'SP_PLAYER_ASSIGNMENTS_URL', plugin_dir_url( __FILE__ ) ); + if ( !defined( 'SP_PLAYER_ASSIGNMENTS_DIR' ) ) + define( 'SP_PLAYER_ASSIGNMENTS_DIR', plugin_dir_path( __FILE__ ) ); + } +} +endif; +if ( get_option( 'sportspress_load_player_assignments_module', 'yes' ) == 'yes' ) { //Is it needed? + new SportsPress_Player_Assignments(); +} \ No newline at end of file From 0f68b3042a4706cb9ab66f2c8518fed286fa0ae4 Mon Sep 17 00:00:00 2001 From: savvasha Date: Mon, 26 Mar 2018 21:05:05 +0300 Subject: [PATCH 3/8] Save sp_player_assignments meta --- modules/sportspress-player-assignments.php | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/modules/sportspress-player-assignments.php b/modules/sportspress-player-assignments.php index beb0948d..f8ee719f 100644 --- a/modules/sportspress-player-assignments.php +++ b/modules/sportspress-player-assignments.php @@ -24,6 +24,8 @@ class SportsPress_Player_Assignments { // Define constants $this->define_constants(); // Actions + add_action( 'sportspress_save_meta_player_statistics', array( $this, 'save_additional_statistics' ), 10, 2 ); + // Filters } /** @@ -37,6 +39,38 @@ class SportsPress_Player_Assignments { if ( !defined( 'SP_PLAYER_ASSIGNMENTS_DIR' ) ) define( 'SP_PLAYER_ASSIGNMENTS_DIR', plugin_dir_path( __FILE__ ) ); } + + /** + * Save Additional Statistics + */ + public function save_additional_statistics( $post_id, $post_data ) { + $old = get_post_meta($post_id, 'sp_player_assignments', true); + $new = array(); + + $leagues = $post_data['sp_leagues']; + $transfers = get_post_meta($post_id, 'sp_player_assignments', true); + + foreach ( $leagues as $l_id => $season ) { + foreach ( $season as $s_id => $team_id ) { + if ( $team_id != '-1' ) { + $new[$l_id][$s_id][] = $team_id; + } + //Check if there are any Mid-Season transfers + if ( isset( $transfers[$l_id][$s_id] ) ){ + foreach ( $transfers[$l_id][$s_id] as $t_id => $performance ) { + $new[$l_id][$s_id][] = $t_id; + } + } + } + } + + if ( !empty( $new ) && $new != $old ) { + update_post_meta( $post_id, 'sp_player_assignments', $new ); + } + elseif ( empty($new) && $old ) { + delete_post_meta( $post_id, 'sp_player_assignments', $old ); + } + } } endif; if ( get_option( 'sportspress_load_player_assignments_module', 'yes' ) == 'yes' ) { //Is it needed? From 75c20300b8af1531fd17d7343fdb1bb895191dfc Mon Sep 17 00:00:00 2001 From: savvasha Date: Tue, 27 Mar 2018 20:48:45 +0300 Subject: [PATCH 4/8] Add player_assignments as a string in format $league_$season_$team for easier post query. --- modules/sportspress-player-assignments.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/modules/sportspress-player-assignments.php b/modules/sportspress-player-assignments.php index f8ee719f..767dad5d 100644 --- a/modules/sportspress-player-assignments.php +++ b/modules/sportspress-player-assignments.php @@ -44,8 +44,7 @@ class SportsPress_Player_Assignments { * Save Additional Statistics */ public function save_additional_statistics( $post_id, $post_data ) { - $old = get_post_meta($post_id, 'sp_player_assignments', true); - $new = array(); + $old = get_post_custom_values( 'sp_player_assignments', $post_id ); $leagues = $post_data['sp_leagues']; $transfers = get_post_meta($post_id, 'sp_player_assignments', true); @@ -53,23 +52,22 @@ class SportsPress_Player_Assignments { foreach ( $leagues as $l_id => $season ) { foreach ( $season as $s_id => $team_id ) { if ( $team_id != '-1' ) { - $new[$l_id][$s_id][] = $team_id; + $serialized = $l_id.'_'.$s_id.'_'.$team_id; + if( !in_array( $serialized, $old ) ){ + add_post_meta( $post_id, 'sp_player_assignments', $serialized, false ); + } } //Check if there are any Mid-Season transfers if ( isset( $transfers[$l_id][$s_id] ) ){ foreach ( $transfers[$l_id][$s_id] as $t_id => $performance ) { - $new[$l_id][$s_id][] = $t_id; + $serialized = $l_id.'_'.$s_id.'_'.$t_id; + if( !in_array( $serialized, $old ) ){ + add_post_meta( $post_id, 'sp_player_assignments', $serialized, false ); + } } } } } - - if ( !empty( $new ) && $new != $old ) { - update_post_meta( $post_id, 'sp_player_assignments', $new ); - } - elseif ( empty($new) && $old ) { - delete_post_meta( $post_id, 'sp_player_assignments', $old ); - } } } endif; From c9afa326e25adbd48b6ddbb4abcf7f2a8a758849 Mon Sep 17 00:00:00 2001 From: savvasha Date: Tue, 27 Mar 2018 22:07:00 +0300 Subject: [PATCH 5/8] Add a global option to enable player_assignments --- modules/sportspress-player-assignments.php | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/modules/sportspress-player-assignments.php b/modules/sportspress-player-assignments.php index 767dad5d..0f705131 100644 --- a/modules/sportspress-player-assignments.php +++ b/modules/sportspress-player-assignments.php @@ -23,10 +23,12 @@ class SportsPress_Player_Assignments { public function __construct() { // Define constants $this->define_constants(); + // Actions add_action( 'sportspress_save_meta_player_statistics', array( $this, 'save_additional_statistics' ), 10, 2 ); // Filters + add_filter( 'sportspress_player_list_options', array( $this, 'add_settings' ) ); } /** * Define constants. @@ -69,6 +71,30 @@ class SportsPress_Player_Assignments { } } } + + /** + * Add settings. + * + * @return array + */ + public function add_settings( $settings ) { + + $settings = array_merge( $settings, + array( + array( + 'title' => __( 'Filter by player assignment', 'sportspress' ), + 'desc' => __( 'Use a stronger conncetion between leagues, seasons and teams', 'sportspress' ), + 'id' => 'sportspress_list_player_assignments', + 'default' => 'yes', + 'type' => 'checkbox', + ), + array( + array( 'type' => 'sectionend', 'id' => 'timelines_options' ), + ) + ) + ); + return $settings; + } } endif; if ( get_option( 'sportspress_load_player_assignments_module', 'yes' ) == 'yes' ) { //Is it needed? From 463912a186891ad0ab1cc450828a0c418e84f091 Mon Sep 17 00:00:00 2001 From: savvasha Date: Wed, 28 Mar 2018 08:58:37 +0300 Subject: [PATCH 6/8] Small typo fix --- modules/sportspress-player-assignments.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/sportspress-player-assignments.php b/modules/sportspress-player-assignments.php index 0f705131..8c39caca 100644 --- a/modules/sportspress-player-assignments.php +++ b/modules/sportspress-player-assignments.php @@ -83,7 +83,7 @@ class SportsPress_Player_Assignments { array( array( 'title' => __( 'Filter by player assignment', 'sportspress' ), - 'desc' => __( 'Use a stronger conncetion between leagues, seasons and teams', 'sportspress' ), + 'desc' => __( 'Use a stronger connection between leagues, seasons and teams', 'sportspress' ), 'id' => 'sportspress_list_player_assignments', 'default' => 'yes', 'type' => 'checkbox', From 967bd52e46d718f621a882ad2a9be76d48f668f2 Mon Sep 17 00:00:00 2001 From: savvasha Date: Wed, 28 Mar 2018 09:59:09 +0300 Subject: [PATCH 7/8] Enable filtering of players by assignment --- includes/class-sp-player-list.php | 44 ++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/includes/class-sp-player-list.php b/includes/class-sp-player-list.php index 1d563cdf..56e31a3d 100644 --- a/includes/class-sp-player-list.php +++ b/includes/class-sp-player-list.php @@ -47,6 +47,18 @@ 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 + $enable_assignments = get_option( 'sportspress_load_player_assignments_module', 'yes' ); + $assignments = array(); + if ( $enable_assignments == 'yes' ) { + 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' ); @@ -99,7 +111,17 @@ class SP_Player_List extends SP_Secondary_Post { 'relation' => 'AND', ), ); - + //Use the Player Assignments to filter players + if ( !empty( $assignments ) ) { + $args['meta_query'] = array( + array( + 'key' => 'sp_player_assignments', + 'value' => $assignments, + 'compare' => 'IN' + ), + ); + }else{ + //Use the legacy way to filter players if ( $league_ids ): $args['tax_query'][] = array( 'taxonomy' => 'sp_league', @@ -115,15 +137,7 @@ class SP_Player_List extends SP_Secondary_Post { 'terms' => $season_ids ); endif; - - if ( $position_ids ): - $args['tax_query'][] = array( - 'taxonomy' => 'sp_position', - 'field' => 'term_id', - 'terms' => $position_ids - ); - endif; - + if ( $team ): $team_key = 'sp_team'; switch ( $era ): @@ -141,6 +155,16 @@ class SP_Player_List extends SP_Secondary_Post { ), ); endif; + + } + + if ( $position_ids ): + $args['tax_query'][] = array( + 'taxonomy' => 'sp_position', + 'field' => 'term_id', + 'terms' => $position_ids + ); + endif; $players = get_posts( $args ); From ca63ee11a7485b29d74e208fb7188cc9fb99a87c Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Sat, 28 Apr 2018 21:24:25 +1000 Subject: [PATCH 8/8] Force array type to prevent parameter error --- modules/sportspress-player-assignments.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/sportspress-player-assignments.php b/modules/sportspress-player-assignments.php index 8c39caca..da0b061d 100644 --- a/modules/sportspress-player-assignments.php +++ b/modules/sportspress-player-assignments.php @@ -46,7 +46,7 @@ class SportsPress_Player_Assignments { * Save Additional Statistics */ public function save_additional_statistics( $post_id, $post_data ) { - $old = get_post_custom_values( 'sp_player_assignments', $post_id ); + $old = (array) get_post_custom_values( 'sp_player_assignments', $post_id ); $leagues = $post_data['sp_leagues']; $transfers = get_post_meta($post_id, 'sp_player_assignments', true);