import_page = 'sportspress_player_csv'; } /** * import function. * * @access public * @param mixed $file * @return void */ function import( $file ) { global $wpdb; $this->imported = $this->skipped = 0; if ( ! is_file($file) ): $this->footer(); die(); endif; ini_set( 'auto_detect_line_endings', '1' ); if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ): $header = fgetcsv( $handle, 0, $this->delimiter ); if ( sizeof( $header ) == 7 ): $loop = 0; while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ): 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 number update_post_meta( $id, 'sp_number', $number ); // Update positions $positions = explode( '|', $positions ); wp_set_object_terms( $id, $positions, 'sp_position', false ); // 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_title( $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 ) ); // Flag as import update_post_meta( $team_id, '_sp_import', 1 ); 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 ); $loop ++; $this->imported++; endwhile; else: echo '
' . __( 'Sorry, there has been an error.', 'sportspress' ) . '
';
_e( 'The CSV is invalid.', 'sportspress' ) . '
'.sprintf( __( 'Import complete - imported %s players and skipped %s.', 'sportspress' ), $this->imported, $this->skipped ).'
' . __( 'All done!', 'sportspress' ) . ' ' . __( 'View Players', 'sportspress' ) . '' . '
'; do_action( 'import_end' ); } /** * header function. * * @access public * @return void */ function header() { 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 (7 columns). Click here to download a sample.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/players-sample.csv' ) . '
'; $action = 'admin.php?import=sportspress_player_csv&step=1'; $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() ); $size = size_format( $bytes ); $upload_dir = wp_upload_dir(); if ( ! empty( $upload_dir['error'] ) ) : ?>