From c1914b7eaa8227bd93409f434c3759d099cd940b Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Sat, 15 Feb 2014 23:46:04 +1100 Subject: [PATCH] Add columns to players importer --- admin/importers/player-importer.php | 68 +++++++++++++++++++++-------- dummy-data/players-sample.csv | 5 +++ dummy-data/sample_players.csv | 5 --- 3 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 dummy-data/players-sample.csv delete mode 100644 dummy-data/sample_players.csv diff --git a/admin/importers/player-importer.php b/admin/importers/player-importer.php index 82c8eb38..6a4d2895 100644 --- a/admin/importers/player-importer.php +++ b/admin/importers/player-importer.php @@ -1,6 +1,6 @@ delimiter ); - if ( sizeof( $header ) == 4 ): + if ( sizeof( $header ) == 7 ): $loop = 0; while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ): - list( $name, $number, $team, $nationality ) = $row; + list( $number, $name, $positions, $teams, $leagues, $seasons, $nationality ) = $row; $nationality = trim( strtoupper( $nationality ) ); if ( $nationality == '*' ) $nationality = ''; + if ( ! $name ): + $this->skipped++; + continue; + endif; + $args = array( 'post_type' => 'sp_player', 'post_status' => 'publish', 'post_title' => $name ); $id = wp_insert_post( $args ); // Flag as import - update_post_meta( $id, 'sp_import', 1 ); + update_post_meta( $id, '_sp_import', 1 ); // Update number update_post_meta( $id, 'sp_number', $number ); - // Get or insert team - $team_object = get_page_by_path( $team, OBJECT, 'sp_team' ); - if ( $team_object ): - if ( $team_object->post_status != 'publish' ): - wp_update_post( array( 'ID' => $team_object->ID, 'post_status' => 'publish' ) ); - endif; - $team_id = $team_object->ID; - else: - $team_id = wp_insert_post( array( 'post_type' => 'sp_team', 'post_status' => 'publish', 'post_title' => $team ) ); - endif; + // Update positions + $positions = explode( '|', $positions ); + wp_set_object_terms( $id, $positions, 'sp_position', false ); - // Update team - update_post_meta( $id, 'sp_team', $team_id ); - update_post_meta( $id, 'sp_current_team', $team_id ); + // Update leagues + $leagues = explode( '|', $leagues ); + wp_set_object_terms( $id, $leagues, 'sp_league', false ); + + // Update seasons + $seasons = explode( '|', $seasons ); + wp_set_object_terms( $id, $seasons, 'sp_season', false ); + + // Update teams + $teams = (array)explode( '|', $teams ); + $i = 0; + foreach ( $teams as $team ): + // Get or insert team + $team_object = get_page_by_path( $team, OBJECT, 'sp_team' ); + if ( $team_object ): + if ( $team_object->post_status != 'publish' ): + wp_update_post( array( 'ID' => $team_object->ID, 'post_status' => 'publish' ) ); + endif; + $team_id = $team_object->ID; + else: + $team_id = wp_insert_post( array( 'post_type' => 'sp_team', 'post_status' => 'publish', 'post_title' => $team ) ); + wp_set_object_terms( $team_id, $leagues, 'sp_league', false ); + wp_set_object_terms( $team_id, $seasons, 'sp_season', false ); + endif; + + // Add team to player + add_post_meta( $id, 'sp_team', $team_id ); + + // Update current team if first in array + if ( $i == 0 ): + update_post_meta( $id, 'sp_current_team', $team_id ); + endif; + + $i++; + endforeach; // Update nationality update_post_meta( $id, 'sp_nationality', $nationality ); @@ -231,7 +261,7 @@ if ( class_exists( 'WP_Importer' ) ) { * @return void */ function header() { - echo '

' . sprintf( __( 'Import %s', 'sportspress' ), __( 'Players', 'sportspress' ) ) . '

'; + echo '

' . sprintf( __( 'Import %s', 'sportspress' ), __( 'Players', 'sportspress' ) ) . '

'; } /** @@ -255,7 +285,7 @@ if ( class_exists( 'WP_Importer' ) ) { echo '
'; echo '

' . __( 'Hi there! Choose a .csv file to upload, then click "Upload file and import".', 'sportspress' ).'

'; - echo '

' . sprintf( __( 'Players need to be defined with columns in a specific order (4 columns). Click here to download a sample.', 'sportspress' ), SPORTSPRESS_PLUGIN_URL . 'dummy-data/sample_players.csv' ) . '

'; + echo '

' . sprintf( __( 'Players need to be defined with columns in a specific order (4 columns). Click here to download a sample.', 'sportspress' ), SPORTSPRESS_PLUGIN_URL . 'dummy-data/players-sample.csv' ) . '

'; $action = 'admin.php?import=sportspress_player_csv&step=1'; diff --git a/dummy-data/players-sample.csv b/dummy-data/players-sample.csv new file mode 100644 index 00000000..780f0e6a --- /dev/null +++ b/dummy-data/players-sample.csv @@ -0,0 +1,5 @@ +Number,Name,Positions,Teams,Leagues,Seasons,Nationality +1,Joe Allen,Goalkeeper,Essendon Eagles,Primary League|Secondary League,2014,AU +3,Steven Gerrard,Defender,Carlton Kangaroos,Primary League,2014|2013,US +5,Daniel Sturridge,Midfielder|Forward,Southbank Sharks,Primary League|Secondary League,2014|2013,GB +6,Gabrielle Gonzalez,Forward,Carlton Kangaroos|Southbank Sharks,Primary League,2014,ES diff --git a/dummy-data/sample_players.csv b/dummy-data/sample_players.csv deleted file mode 100644 index 6bef750b..00000000 --- a/dummy-data/sample_players.csv +++ /dev/null @@ -1,5 +0,0 @@ -Name,Number,Team,Nationality -Mark Downer,1,Essendon Eagles,AU -Brian Jones,3,Essendon Eagles,US -Andrew Johnson,5,Southbank Sharks,GB -Gabrielle Gonzalez,6,Carlton Kangaroos,ES