From dbe2e0b48e9c2732748dbb8a3eea8aece3d74a0c Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Sat, 29 Aug 2015 05:06:14 +1000 Subject: [PATCH] Add ability to choose multiple nationalities for players and staff --- .../class-sp-meta-box-player-details.php | 23 ++++++++++-------- .../class-sp-meta-box-staff-details.php | 23 ++++++++++-------- includes/class-sp-player.php | 12 +++++++++- includes/class-sp-staff.php | 12 +++++++++- templates/player-details.php | 24 +++++++++++-------- templates/staff-details.php | 24 +++++++++++-------- 6 files changed, 76 insertions(+), 42 deletions(-) diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-details.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-details.php index 65f9cdbc..c313afb9 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-details.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-player-details.php @@ -5,7 +5,7 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Meta_Boxes - * @version 1.7 + * @version 1.8.9 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -23,12 +23,15 @@ class SP_Meta_Box_Player_Details { $continents = SP()->countries->continents; $number = get_post_meta( $post->ID, 'sp_number', true ); - $nationality = get_post_meta( $post->ID, 'sp_nationality', true ); - if ( 2 == strlen( $nationality ) ): - $legacy = SP()->countries->legacy; - $nationality = strtolower( $nationality ); - $nationality = sp_array_value( $legacy, $nationality, null ); - endif; + $nationalities = get_post_meta( $post->ID, 'sp_nationality', false ); + foreach ( $nationalities as $index => $nationality ): + if ( 2 == strlen( $nationality ) ): + $legacy = SP()->countries->legacy; + $nationality = strtolower( $nationality ); + $nationality = sp_array_value( $legacy, $nationality, null ); + $nationalities[ $index ] = $nationality; + endif; + endforeach; $leagues = get_the_terms( $post->ID, 'sp_league' ); $league_ids = array(); @@ -62,12 +65,12 @@ class SP_Meta_Box_Player_Details {

-

$countries ): ?> $country ): ?> - + @@ -157,7 +160,7 @@ class SP_Meta_Box_Player_Details { */ public static function save( $post_id, $post ) { update_post_meta( $post_id, 'sp_number', sp_array_value( $_POST, 'sp_number', '' ) ); - update_post_meta( $post_id, 'sp_nationality', sp_array_value( $_POST, 'sp_nationality', '' ) ); + sp_update_post_meta_recursive( $post_id, 'sp_nationality', sp_array_value( $_POST, 'sp_nationality', array() ) ); sp_update_post_meta_recursive( $post_id, 'sp_current_team', sp_array_value( $_POST, 'sp_current_team', array() ) ); sp_update_post_meta_recursive( $post_id, 'sp_past_team', sp_array_value( $_POST, 'sp_past_team', array() ) ); sp_update_post_meta_recursive( $post_id, 'sp_team', array_merge( array( sp_array_value( $_POST, 'sp_current_team', array() ) ), sp_array_value( $_POST, 'sp_past_team', array() ) ) ); diff --git a/includes/admin/post-types/meta-boxes/class-sp-meta-box-staff-details.php b/includes/admin/post-types/meta-boxes/class-sp-meta-box-staff-details.php index 4ca13c43..efb6510b 100644 --- a/includes/admin/post-types/meta-boxes/class-sp-meta-box-staff-details.php +++ b/includes/admin/post-types/meta-boxes/class-sp-meta-box-staff-details.php @@ -5,7 +5,7 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Meta_Boxes - * @version 1.8 + * @version 1.8.9 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -22,12 +22,15 @@ class SP_Meta_Box_Staff_Details { wp_nonce_field( 'sportspress_save_data', 'sportspress_meta_nonce' ); $continents = SP()->countries->continents; - $nationality = get_post_meta( $post->ID, 'sp_nationality', true ); - if ( 2 == strlen( $nationality ) ): - $legacy = SP()->countries->legacy; - $nationality = strtolower( $nationality ); - $nationality = sp_array_value( $legacy, $nationality, null ); - endif; + $nationalities = get_post_meta( $post->ID, 'sp_nationality', false ); + foreach ( $nationalities as $index => $nationality ): + if ( 2 == strlen( $nationality ) ): + $legacy = SP()->countries->legacy; + $nationality = strtolower( $nationality ); + $nationality = sp_array_value( $legacy, $nationality, null ); + $nationalities[ $index ] = $nationality; + endif; + endforeach; $leagues = get_the_terms( $post->ID, 'sp_league' ); $league_ids = array(); @@ -75,12 +78,12 @@ class SP_Meta_Box_Staff_Details { ?>

-

$countries ): ?> $country ): ?> - + @@ -155,8 +158,8 @@ class SP_Meta_Box_Staff_Details { */ public static function save( $post_id, $post ) { wp_set_post_terms( $post_id, sp_array_value( $_POST, 'sp_role', null ), 'sp_role', false ); - update_post_meta( $post_id, 'sp_nationality', sp_array_value( $_POST, 'sp_nationality', '' ) ); + sp_update_post_meta_recursive( $post_id, 'sp_nationality', sp_array_value( $_POST, 'sp_nationality', array() ) ); sp_update_post_meta_recursive( $post_id, 'sp_current_team', sp_array_value( $_POST, 'sp_current_team', array() ) ); sp_update_post_meta_recursive( $post_id, 'sp_past_team', sp_array_value( $_POST, 'sp_past_team', array() ) ); sp_update_post_meta_recursive( $post_id, 'sp_team', array_merge( array( sp_array_value( $_POST, 'sp_current_team', array() ) ), sp_array_value( $_POST, 'sp_past_team', array() ) ) ); diff --git a/includes/class-sp-player.php b/includes/class-sp-player.php index 48d9a873..a61941d9 100644 --- a/includes/class-sp-player.php +++ b/includes/class-sp-player.php @@ -5,7 +5,7 @@ * The SportsPress player class handles individual player data. * * @class SP_Player - * @version 1.7.3 + * @version 1.8.9 * @package SportsPress/Classes * @category Class * @author ThemeBoy @@ -42,6 +42,16 @@ class SP_Player extends SP_Custom_Post { return get_post_meta( $this->ID, 'sp_past_team', false ); } + /** + * Returns nationalities + * + * @access public + * @return array + */ + public function nationalities() { + return get_post_meta( $this->ID, 'sp_nationality', false ); + } + /** * Returns formatted player metrics * diff --git a/includes/class-sp-staff.php b/includes/class-sp-staff.php index 46894189..1489746d 100644 --- a/includes/class-sp-staff.php +++ b/includes/class-sp-staff.php @@ -5,7 +5,7 @@ * The SportsPress staff class handles individual staff data. * * @class SP_Staff - * @version 1.6 + * @version 1.8.9 * @package SportsPress/Classes * @category Class * @author ThemeBoy @@ -32,6 +32,16 @@ class SP_Staff extends SP_Custom_Post { return get_post_meta( $this->ID, 'sp_past_team', false ); } + /** + * Returns nationalities + * + * @access public + * @return array + */ + public function nationalities() { + return get_post_meta( $this->ID, 'sp_nationality', false ); + } + /** * Returns role * diff --git a/templates/player-details.php b/templates/player-details.php index 7ed18d18..3b2dfe4a 100644 --- a/templates/player-details.php +++ b/templates/player-details.php @@ -4,7 +4,7 @@ * * @author ThemeBoy * @package SportsPress/Templates - * @version 1.8 + * @version 1.8.9 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -28,7 +28,7 @@ $countries = SP()->countries->countries; $player = new SP_Player( $id ); -$nationality = $player->nationality; +$nationalities = $player->nationalities(); $positions = $player->positions(); $current_teams = $player->current_teams(); $past_teams = $player->past_teams(); @@ -36,14 +36,18 @@ $metrics_before = $player->metrics( true ); $metrics_after = $player->metrics( false ); $common = array(); -if ( $show_nationality && $nationality ): - if ( 2 == strlen( $nationality ) ): - $legacy = SP()->countries->legacy; - $nationality = strtolower( $nationality ); - $nationality = sp_array_value( $legacy, $nationality, null ); - endif; - $country_name = sp_array_value( $countries, $nationality, null ); - $common[ __( 'Nationality', 'sportspress' ) ] = $country_name ? ( $show_nationality_flags ? '' . $nationality . ' ' : '' ) . $country_name : '—'; +if ( $show_nationality && $nationalities && is_array( $nationalities ) ): + $values = array(); + foreach ( $nationalities as $nationality ): + if ( 2 == strlen( $nationality ) ): + $legacy = SP()->countries->legacy; + $nationality = strtolower( $nationality ); + $nationality = sp_array_value( $legacy, $nationality, null ); + endif; + $country_name = sp_array_value( $countries, $nationality, null ); + $values[] = $country_name ? ( $show_nationality_flags ? '' . $nationality . ' ' : '' ) . $country_name : '—'; + endforeach; + $common[ __( 'Nationality', 'sportspress' ) ] = implode( '
', $values ); endif; if ( $show_positions && $positions ): diff --git a/templates/staff-details.php b/templates/staff-details.php index ea182249..3096d247 100644 --- a/templates/staff-details.php +++ b/templates/staff-details.php @@ -4,7 +4,7 @@ * * @author ThemeBoy * @package SportsPress/Templates - * @version 1.6 + * @version 1.8.9 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly @@ -27,19 +27,23 @@ $countries = SP()->countries->countries; $staff = new SP_Staff( $id ); -$nationality = $staff->nationality; +$nationalities = $staff->nationalities(); $current_teams = $staff->current_teams(); $past_teams = $staff->past_teams(); $data = array(); -if ( $show_nationality && $nationality ): - if ( 2 == strlen( $nationality ) ): - $legacy = SP()->countries->legacy; - $nationality = strtolower( $nationality ); - $nationality = sp_array_value( $legacy, $nationality, null ); - endif; - $country_name = sp_array_value( $countries, $nationality, null ); - $data[ __( 'Nationality', 'sportspress' ) ] = $country_name ? ( $show_nationality_flags ? '' . $nationality . ' ' : '' ) . $country_name : '—'; +if ( $show_nationality && $nationalities && is_array( $nationalities ) ): + $values = array(); + foreach ( $nationalities as $nationality ): + if ( 2 == strlen( $nationality ) ): + $legacy = SP()->countries->legacy; + $nationality = strtolower( $nationality ); + $nationality = sp_array_value( $legacy, $nationality, null ); + endif; + $country_name = sp_array_value( $countries, $nationality, null ); + $values[] = $country_name ? ( $show_nationality_flags ? '' . $nationality . ' ' : '' ) . $country_name : '—'; + endforeach; + $data[ __( 'Nationality', 'sportspress' ) ] = implode( '
', $values ); endif; if ( $show_current_teams && $current_teams ):