CSV import tool added for players
This commit is contained in:
@@ -1,4 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
|
function sportspress_player_importer() {
|
||||||
|
require_once ABSPATH . 'wp-admin/includes/import.php';
|
||||||
|
|
||||||
|
if ( ! class_exists( 'WP_Importer' ) ) {
|
||||||
|
$class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
|
||||||
|
if ( file_exists( $class_wp_importer ) )
|
||||||
|
require $class_wp_importer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// includes
|
||||||
|
require dirname( SPORTSPRESS_PLUGIN_FILE ) . '/admin/importers/player-importer.php';
|
||||||
|
|
||||||
|
// Dispatch
|
||||||
|
$importer = new SP_Player_Importer();
|
||||||
|
$importer->dispatch();
|
||||||
|
}
|
||||||
|
|
||||||
function sportspress_admin_init() {
|
function sportspress_admin_init() {
|
||||||
$post_types = array(
|
$post_types = array(
|
||||||
'sp_event',
|
'sp_event',
|
||||||
@@ -35,5 +52,7 @@ function sportspress_admin_init() {
|
|||||||
$administrator->add_cap( $cap . '_' . $post_type . 's' );
|
$administrator->add_cap( $cap . '_' . $post_type . 's' );
|
||||||
endforeach;
|
endforeach;
|
||||||
endforeach;
|
endforeach;
|
||||||
|
|
||||||
|
register_importer( 'sportspress_player_csv', __( 'SportsPress Players (CSV)', 'sportspress' ), __( 'Import <strong>players</strong> from a csv file.', 'sportspress'), 'sportspress_player_importer' );
|
||||||
}
|
}
|
||||||
add_action( 'admin_init', 'sportspress_admin_init' );
|
add_action( 'admin_init', 'sportspress_admin_init' );
|
||||||
|
|||||||
317
admin/importers/player-importer.php
Normal file
317
admin/importers/player-importer.php
Normal file
@@ -0,0 +1,317 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Players importer - import players into SportsPress.
|
||||||
|
*
|
||||||
|
* @author ThemeBoy
|
||||||
|
* @category Admin
|
||||||
|
* @package SportsPress/Admin/Importers
|
||||||
|
* @version 0.2.11
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||||
|
|
||||||
|
if ( class_exists( 'WP_Importer' ) ) {
|
||||||
|
class SP_Player_Importer extends WP_Importer {
|
||||||
|
|
||||||
|
var $id;
|
||||||
|
var $file_url;
|
||||||
|
var $import_page;
|
||||||
|
var $delimiter;
|
||||||
|
var $posts = array();
|
||||||
|
var $imported;
|
||||||
|
var $skipped;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* __construct function.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
$this->import_page = 'sportspress_player_csv';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registered callback function for the WordPress Importer
|
||||||
|
*
|
||||||
|
* Manages the three separate stages of the CSV import process
|
||||||
|
*/
|
||||||
|
function dispatch() {
|
||||||
|
$this->header();
|
||||||
|
|
||||||
|
if ( ! empty( $_POST['delimiter'] ) )
|
||||||
|
$this->delimiter = stripslashes( trim( $_POST['delimiter'] ) );
|
||||||
|
|
||||||
|
if ( ! $this->delimiter )
|
||||||
|
$this->delimiter = ',';
|
||||||
|
|
||||||
|
$step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
|
||||||
|
switch ( $step ):
|
||||||
|
case 0:
|
||||||
|
$this->greet();
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
check_admin_referer( 'import-upload' );
|
||||||
|
if ( $this->handle_upload() ):
|
||||||
|
|
||||||
|
if ( $this->id )
|
||||||
|
$file = get_attached_file( $this->id );
|
||||||
|
else
|
||||||
|
$file = ABSPATH . $this->file_url;
|
||||||
|
|
||||||
|
add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
|
||||||
|
|
||||||
|
if ( function_exists( 'gc_enable' ) )
|
||||||
|
gc_enable();
|
||||||
|
|
||||||
|
@set_time_limit(0);
|
||||||
|
@ob_flush();
|
||||||
|
@flush();
|
||||||
|
|
||||||
|
$this->import( $file );
|
||||||
|
endif;
|
||||||
|
break;
|
||||||
|
endswitch;
|
||||||
|
$this->footer();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* format_data_from_csv function.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param mixed $data
|
||||||
|
* @param string $enc
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function format_data_from_csv( $data, $enc ) {
|
||||||
|
return ( $enc == 'UTF-8' ) ? $data : utf8_encode( $data );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
$new_rates = array();
|
||||||
|
|
||||||
|
ini_set( 'auto_detect_line_endings', '1' );
|
||||||
|
|
||||||
|
if ( ( $handle = fopen( $file, "r" ) ) !== FALSE ):
|
||||||
|
|
||||||
|
$header = fgetcsv( $handle, 0, $this->delimiter );
|
||||||
|
|
||||||
|
if ( sizeof( $header ) == 4 ):
|
||||||
|
|
||||||
|
$loop = 0;
|
||||||
|
|
||||||
|
while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ):
|
||||||
|
|
||||||
|
list( $name, $number, $team, $nationality ) = $row;
|
||||||
|
|
||||||
|
$nationality = trim( strtoupper( $nationality ) );
|
||||||
|
|
||||||
|
if ( $nationality == '*' )
|
||||||
|
$nationality = '';
|
||||||
|
|
||||||
|
$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 );
|
||||||
|
|
||||||
|
// 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 team
|
||||||
|
update_post_meta( $id, 'sp_team', $team_id );
|
||||||
|
update_post_meta( $id, 'sp_current_team', $team_id );
|
||||||
|
|
||||||
|
// 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 />';
|
||||||
|
echo __( 'The CSV is invalid.', 'sportspress' ) . '</p>';
|
||||||
|
$this->footer();
|
||||||
|
die();
|
||||||
|
|
||||||
|
endif;
|
||||||
|
|
||||||
|
fclose( $handle );
|
||||||
|
endif;
|
||||||
|
|
||||||
|
// Show Result
|
||||||
|
echo '<div class="updated settings-error below-h2"><p>
|
||||||
|
'.sprintf( __( 'Import complete - imported <strong>%s</strong> players and skipped <strong>%s</strong>.', 'sportspress' ), $this->imported, $this->skipped ).'
|
||||||
|
</p></div>';
|
||||||
|
|
||||||
|
$this->import_end();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs post-import cleanup of files and the cache
|
||||||
|
*/
|
||||||
|
function import_end() {
|
||||||
|
echo '<p>' . __( 'All done!', 'sportspress' ) . ' <a href="' . admin_url('edit.php?post_type=sp_player') . '">' . sprintf( __( 'View %s', 'sportspress' ), __( 'Players', 'sportspress' ) ) . '</a>' . '</p>';
|
||||||
|
|
||||||
|
do_action( 'import_end' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the CSV upload and initial parsing of the file to prepare for
|
||||||
|
* displaying author import options
|
||||||
|
*
|
||||||
|
* @return bool False if error uploading or invalid file, true otherwise
|
||||||
|
*/
|
||||||
|
function handle_upload() {
|
||||||
|
|
||||||
|
if ( empty( $_POST['file_url'] ) ) {
|
||||||
|
|
||||||
|
$file = wp_import_handle_upload();
|
||||||
|
|
||||||
|
if ( isset( $file['error'] ) ) {
|
||||||
|
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong><br />';
|
||||||
|
echo esc_html( $file['error'] ) . '</p>';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->id = (int) $file['id'];
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if ( file_exists( ABSPATH . $_POST['file_url'] ) ) {
|
||||||
|
|
||||||
|
$this->file_url = esc_attr( $_POST['file_url'] );
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'sportspress' ) . '</strong></p>';
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* header function.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function header() {
|
||||||
|
echo '<h2>' . sprintf( __( 'Import %s', 'sportspress' ), __( 'Players', 'sportspress' ) ) . '</h2>';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* footer function.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function footer() {
|
||||||
|
echo '</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* greet function.
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @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( __( 'Players need to be defined with columns in a specific order (4 columns). <a href="%s">Click here to download a sample</a>.', 'sportspress' ), SPORTSPRESS_PLUGIN_URL . 'dummy-data/sample_players.csv' ) . '</p>';
|
||||||
|
|
||||||
|
$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'] ) ) :
|
||||||
|
?><div class="error"><p><?php _e('Before you can upload your import file, you will need to fix the following error:'); ?></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:' ); ?></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' ), $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' ); ?>" />
|
||||||
|
</p>
|
||||||
|
</form>
|
||||||
|
<?php
|
||||||
|
endif;
|
||||||
|
|
||||||
|
echo '</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Added to http_request_timeout filter to force timeout at 60 seconds during import
|
||||||
|
* @param int $val
|
||||||
|
* @return int 60
|
||||||
|
*/
|
||||||
|
function bump_request_timeout( $val ) {
|
||||||
|
return 60;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
dummy-data/sample_players.csv
Normal file
5
dummy-data/sample_players.csv
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
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
|
||||||
|
Reference in New Issue
Block a user