Improve CSV importer close #50
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
* @author ThemeBoy
|
||||
* @category Admin
|
||||
* @package SportsPress/Admin/Importers
|
||||
* @version 0.9
|
||||
* @version 1.3
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
@@ -21,112 +21,110 @@ if ( class_exists( 'WP_Importer' ) ) {
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->import_page = 'sportspress_staff_csv';
|
||||
$this->import_label = __( 'Import Staff', 'sportspress' );
|
||||
$this->columns = array(
|
||||
'post_title' => __( 'Name', 'sportspress' ),
|
||||
'sp_role' => __( 'Roles', 'sportspress' ),
|
||||
'sp_team' => __( 'Teams', 'sportspress' ),
|
||||
'sp_league' => __( 'Leagues', 'sportspress' ),
|
||||
'sp_season' => __( 'Seasons', 'sportspress' ),
|
||||
'sp_nationality' => __( 'Nationality', 'sportspress' ),
|
||||
);
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* import function.
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $file
|
||||
* @param array $array
|
||||
* @param array $columns
|
||||
* @return void
|
||||
*/
|
||||
function import( $file ) {
|
||||
global $wpdb;
|
||||
|
||||
function import( $array = array(), $columns = array( 'post_title' ) ) {
|
||||
$this->imported = $this->skipped = 0;
|
||||
|
||||
if ( ! is_file($file) ):
|
||||
if ( ! is_array( $array ) || ! sizeof( $array ) ):
|
||||
$this->footer();
|
||||
die();
|
||||
endif;
|
||||
|
||||
ini_set( 'auto_detect_line_endings', '1' );
|
||||
$rows = array_chunk( $array, sizeof( $columns ) );
|
||||
|
||||
if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ):
|
||||
foreach ( $rows as $row ):
|
||||
|
||||
$header = fgetcsv( $handle, 0, $this->delimiter );
|
||||
if ( empty( array_filter( $row ) ) ) continue;
|
||||
|
||||
if ( sizeof( $header ) == 5 ):
|
||||
$meta = array();
|
||||
|
||||
$loop = 0;
|
||||
foreach ( $columns as $index => $key ):
|
||||
$meta[ $key ] = sp_array_value( $row, $index );
|
||||
endforeach;
|
||||
|
||||
while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ):
|
||||
|
||||
list( $name, $teams, $leagues, $seasons, $nationality ) = $row;
|
||||
|
||||
$nationality = trim( strtoupper( $nationality ) );
|
||||
|
||||
if ( $nationality == '*' )
|
||||
$nationality = '';
|
||||
|
||||
if ( ! $name ):
|
||||
$this->skipped++;
|
||||
continue;
|
||||
endif;
|
||||
|
||||
$args = array( 'post_type' => 'sp_staff', 'post_status' => 'publish', 'post_title' => $name );
|
||||
|
||||
$id = wp_insert_post( $args );
|
||||
|
||||
// Flag as import
|
||||
update_post_meta( $id, '_sp_import', 1 );
|
||||
|
||||
// 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 staff
|
||||
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 '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong><br />';
|
||||
_e( 'The CSV is invalid.', 'sportspress' ) . '</p>';
|
||||
$this->footer();
|
||||
die();
|
||||
$name = sp_array_value( $meta, 'post_title' );
|
||||
|
||||
if ( ! $name ):
|
||||
$this->skipped++;
|
||||
continue;
|
||||
endif;
|
||||
|
||||
fclose( $handle );
|
||||
endif;
|
||||
$args = array( 'post_type' => 'sp_staff', 'post_status' => 'publish', 'post_title' => $name );
|
||||
|
||||
$id = wp_insert_post( $args );
|
||||
|
||||
// Flag as import
|
||||
update_post_meta( $id, '_sp_import', 1 );
|
||||
|
||||
// Update roles
|
||||
$roles = explode( '|', sp_array_value( $meta, 'sp_role' ) );
|
||||
wp_set_object_terms( $id, $roles, 'sp_role', false );
|
||||
|
||||
// Update leagues
|
||||
$leagues = explode( '|', sp_array_value( $meta, 'sp_league' ) );
|
||||
wp_set_object_terms( $id, $leagues, 'sp_league', false );
|
||||
|
||||
// Update seasons
|
||||
$seasons = explode( '|', sp_array_value( $meta, 'sp_season' ) );
|
||||
wp_set_object_terms( $id, $seasons, 'sp_season', false );
|
||||
|
||||
// Update teams
|
||||
$teams = (array)explode( '|', sp_array_value( $meta, 'sp_team' ) );
|
||||
$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 staff
|
||||
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
|
||||
$nationality = trim( strtoupper( sp_array_value( $meta, 'sp_nationality' ) ) );
|
||||
if ( $nationality == '*' ) $nationality = '';
|
||||
update_post_meta( $id, 'sp_nationality', $nationality );
|
||||
|
||||
$this->imported++;
|
||||
|
||||
endforeach;
|
||||
|
||||
// Show Result
|
||||
echo '<div class="updated settings-error below-h2"><p>
|
||||
@@ -162,57 +160,10 @@ if ( class_exists( 'WP_Importer' ) ) {
|
||||
* @return void
|
||||
*/
|
||||
function greet() {
|
||||
|
||||
echo '<div class="narrow">';
|
||||
echo '<p>' . __( 'Hi there! Choose a .csv file to upload, then click "Upload file and import".', 'sportspress' ).'</p>';
|
||||
|
||||
echo '<p>' . sprintf( __( 'Staff need to be defined with columns in a specific order (5 columns). <a href="%s">Click here to download a sample</a>.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/staff-sample.csv' ) . '</p>';
|
||||
|
||||
$action = 'admin.php?import=sportspress_staff_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'] ) ) :
|
||||
?><div class="error"><p><?php _e('Before you can upload your import file, you will need to fix the following error:', 'sportspress'); ?></p>
|
||||
<p><strong><?php echo $upload_dir['error']; ?></strong></p></div><?php
|
||||
else :
|
||||
?>
|
||||
<form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo esc_attr(wp_nonce_url($action, 'import-upload')); ?>">
|
||||
<table class="form-table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>
|
||||
<label for="upload"><?php _e( 'Choose a file from your computer:', 'sportspress' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<input type="file" id="upload" name="import" size="25" />
|
||||
<input type="hidden" name="action" value="save" />
|
||||
<input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
|
||||
<small><?php printf( __( 'Maximum size: %s', 'sportspress' ), $size ); ?></small>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>
|
||||
<label for="file_url"><?php _e( 'OR enter path to file:', 'sportspress' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<?php echo ' ' . ABSPATH . ' '; ?><input type="text" id="file_url" name="file_url" size="25" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><label><?php _e( 'Delimiter', 'sportspress' ); ?></label><br/></th>
|
||||
<td><input type="text" name="delimiter" placeholder="," size="2" /></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p class="submit">
|
||||
<input type="submit" class="button" value="<?php esc_attr_e( 'Upload file and import', 'sportspress' ); ?>" />
|
||||
</p>
|
||||
</form>
|
||||
<?php
|
||||
endif;
|
||||
|
||||
echo '<p>' . sprintf( __( 'Staff need to be defined with columns in a specific order (6 columns). <a href="%s">Click here to download a sample</a>.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/staff-sample.csv' ) . '</p>';
|
||||
wp_import_upload_form( 'admin.php?import=sportspress_staff_csv&step=1' );
|
||||
echo '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user