Improve CSV importer close #50

This commit is contained in:
Brian Miyaji
2014-08-26 02:17:00 +10:00
parent a72c045a5b
commit 6b4a421ec2
15 changed files with 903 additions and 811 deletions

View File

@@ -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
@@ -20,6 +20,22 @@ if ( class_exists( 'WP_Importer' ) ) {
var $posts = array();
var $imported;
var $skipped;
var $import_label;
var $columns = array();
/**
* Constructor.
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
}
/**
* Enqueue scripts
*/
public function admin_scripts() {
wp_enqueue_script( 'sportspress-admin', SP()->plugin_url() . '/assets/js/admin/sportspress-admin.js', array( 'jquery', 'chosen', 'jquery-ui-core', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'jquery-tiptip', 'jquery-caret', 'jquery-countdown' ), SP_VERSION, true );
}
/**
* Registered callback function for the WordPress Importer
@@ -58,13 +74,125 @@ if ( class_exists( 'WP_Importer' ) ) {
@ob_flush();
@flush();
$this->import( $file );
$this->table( $file );
endif;
break;
case 2:
check_admin_referer( 'import-upload' );
if ( isset( $_POST['sp_import'] ) ):
$this->import( $_POST['sp_import'], array_values( array_filter( sp_array_value( $_POST, 'sp_columns', array( 'post_title' ) ) ) ) );
endif;
break;
endswitch;
$this->footer();
}
/**
* dropdown function.
*
* @access public
* @param mixed $file
* @return void
*/
function dropdown( $selected ) {
?>
<select name="sp_columns[]" data-index="<?php echo array_search( $selected, array_keys( $this->columns ) ); ?>">
<option value="0">&mdash; <?php _e( 'Disable', 'sportspress' ); ?> &mdash;</option>
<?php foreach ( $this->columns as $key => $label ): ?>
<option value="<?php echo $key; ?>" <?php selected( $selected, $key ); ?>><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
<?php
}
/**
* table function.
*
* @access public
* @param mixed $file
* @return void
*/
function table( $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 ) >= 1 ):
$action = 'admin.php?import=' . $this->import_page . '&step=2';
?>
<form enctype="multipart/form-data" id="import-upload-form" class="sportspress" method="post" action="<?php echo esc_attr(wp_nonce_url($action, 'import-upload')); ?>">
<?php $this->options(); ?>
<table class="wp-list-table sp-data-table sp-import-table widefat fixed pages">
<thead>
<tr>
<?php foreach ( $this->columns as $key => $label ): ?>
<th scope="col" class="manage-column">
<?php $this->dropdown( $key ); ?>
</th>
<?php endforeach; ?>
<th scope="col" class="sp-actions-column">
<a href="#" title="<?php _e( 'Insert row after', 'sportspress' ); ?>" class="dashicons dashicons-plus-alt sp-add-row sp-add-first"></a>
</th>
</tr>
</thead>
<tbody>
<?php while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ): ?>
<tr>
<?php $index = 0; foreach ( $this->columns as $key => $label ): $value = sp_array_value( $row, $index ); ?>
<td>
<input type="text" class="widefat" value="<?php echo $value; ?>" name="sp_import[]">
</td>
<?php $index ++; endforeach; ?>
<td class="sp-actions-column">
<a href="#" title="<?php _e( 'Delete row', 'sportspress' ); ?>" class="sp-icon-trash sp-delete-row"></a>
<a href="#" title="<?php _e( 'Insert row after', 'sportspress' ); ?>" class="dashicons dashicons-plus-alt sp-add-row"></a>
</td>
</tr>
<?php $this->imported++; endwhile; ?>
<tr>
<?php foreach ( $this->columns as $key => $label ): ?>
<td>
<input type="text" class="widefat" name="sp_import[]">
</td>
<?php endforeach; ?>
<td class="sp-actions-column">
<a href="#" title="<?php _e( 'Insert row after', 'sportspress' ); ?>" class="dashicons dashicons-plus-alt sp-add-row"></a>
</td>
</tr>
</tbody>
</table>
<p class="sp-post-count alignright">
<?php printf( __( 'Displaying %s&#8211;%s of %s', 'sportspress' ), 1, $this->imported+1, $this->imported+1 ); ?>
</p>
<p class="submit">
<input type="submit" class="button button-primary button-hero" value="<?php echo esc_attr( $this->import_label ); ?>" />
</p>
</form>
<?php
else:
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong><br />';
_e( 'The CSV is invalid.', 'sportspress' ) . '</p>';
$this->footer();
die();
endif;
fclose( $handle );
endif;
}
/**
* format_data_from_csv function.
*
@@ -122,7 +250,7 @@ if ( class_exists( 'WP_Importer' ) ) {
* @return void
*/
function header() {
echo '<div class="wrap"><h2>' . __( 'Import', 'sportspress' ) . '</h2>';
echo '<div class="wrap"><h2>' . $this->import_label . '</h2>';
}
/**
@@ -135,6 +263,15 @@ if ( class_exists( 'WP_Importer' ) ) {
echo '</div>';
}
/**
* options function.
*
* @access public
* @return void
*/
function options() {
}
/**
* Added to http_request_timeout filter to force timeout at 60 seconds during import
* @param int $val