From 3b9344232ac0e71e0570cbadcf61555d3a7f92ab Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Sat, 15 Feb 2014 23:46:18 +1100 Subject: [PATCH] Add teams importer --- admin/hooks/admin-init.php | 19 ++ admin/importers/team-importer.php | 306 ++++++++++++++++++++++++++++++ dummy-data/teams-sample.csv | 4 + 3 files changed, 329 insertions(+) create mode 100644 admin/importers/team-importer.php create mode 100644 dummy-data/teams-sample.csv diff --git a/admin/hooks/admin-init.php b/admin/hooks/admin-init.php index 20be156a..1dc3c75c 100644 --- a/admin/hooks/admin-init.php +++ b/admin/hooks/admin-init.php @@ -1,4 +1,21 @@ dispatch(); +} + function sportspress_player_importer() { require_once ABSPATH . 'wp-admin/includes/import.php'; @@ -53,6 +70,8 @@ function sportspress_admin_init() { endforeach; endforeach; + // Importers + register_importer( 'sportspress_team_csv', __( 'SportsPress Teams (CSV)', 'sportspress' ), __( 'Import teams from a csv file.', 'sportspress'), 'sportspress_team_importer' ); register_importer( 'sportspress_player_csv', __( 'SportsPress Players (CSV)', 'sportspress' ), __( 'Import players from a csv file.', 'sportspress'), 'sportspress_player_importer' ); } add_action( 'admin_init', 'sportspress_admin_init' ); diff --git a/admin/importers/team-importer.php b/admin/importers/team-importer.php new file mode 100644 index 00000000..f868cd97 --- /dev/null +++ b/admin/importers/team-importer.php @@ -0,0 +1,306 @@ +import_page = 'sportspress_team_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 ) == 3 ): + + $loop = 0; + + while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ): + + list( $name, $leagues, $seasons ) = $row; + + $team_object = get_page_by_path( $name, OBJECT, 'sp_team' ); + + if ( ! $name || $team_object ): + $this->skipped++; + continue; + endif; + + $args = array( 'post_type' => 'sp_team', '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 ); + + $loop ++; + $this->imported++; + endwhile; + + else: + + echo '

' . __( 'Sorry, there has been an error.', 'sportspress' ) . '
'; + echo __( 'The CSV is invalid.', 'sportspress' ) . '

'; + $this->footer(); + die(); + + endif; + + fclose( $handle ); + endif; + + // Show Result + echo '

+ '.sprintf( __( 'Import complete - imported %s teams and skipped %s.', 'sportspress' ), $this->imported, $this->skipped ).' +

'; + + $this->import_end(); + } + + /** + * Performs post-import cleanup of files and the cache + */ + function import_end() { + echo '

' . __( 'All done!', 'sportspress' ) . ' ' . sprintf( __( 'View %s', 'sportspress' ), __( 'Teams', 'sportspress' ) ) . '' . '

'; + + 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 '

' . __( 'Sorry, there has been an error.', 'sportspress' ) . '
'; + echo esc_html( $file['error'] ) . '

'; + 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 '

' . __( 'Sorry, there has been an error.', 'sportspress' ) . '

'; + return false; + + } + + } + + return true; + } + + /** + * header function. + * + * @access public + * @return void + */ + function header() { + echo '

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

'; + } + + /** + * footer function. + * + * @access public + * @return void + */ + function footer() { + echo '
'; + } + + /** + * greet function. + * + * @access public + * @return void + */ + function greet() { + + echo '
'; + echo '

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

'; + + echo '

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

'; + + $action = 'admin.php?import=sportspress_team_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'] ) ) : + ?>

+

+
+ + + + + + + + + + + + + + + +
+ + + + + + +
+ + + +

+

+ +

+
+ '; + } + + /** + * 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; + } + } +} diff --git a/dummy-data/teams-sample.csv b/dummy-data/teams-sample.csv new file mode 100644 index 00000000..7c726fbf --- /dev/null +++ b/dummy-data/teams-sample.csv @@ -0,0 +1,4 @@ +Name,Leagues,Seasons +Essendon Eagles,Primary League|Secondary League,2014 +Southbank Sharks,Primary League|Secondary League,2014|2013 +Carlton Kangaroos,Primary League,2014|2013