From 07fe53e0dad7b00050ad20e57e0ebd283331398d Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Mon, 12 May 2014 22:33:12 +1000 Subject: [PATCH] Add staff importer --- includes/admin/class-sp-admin-importers.php | 55 +++--- .../importers/class-sp-event-importer.php | 125 +----------- .../admin/importers/class-sp-importer.php | 147 ++++++++++++++ .../importers/class-sp-player-importer.php | 125 +----------- ...porter.php => class-sp-staff-importer.php} | 185 +++++------------- 5 files changed, 230 insertions(+), 407 deletions(-) create mode 100644 includes/admin/importers/class-sp-importer.php rename includes/admin/importers/{class-sp-team-importer.php => class-sp-staff-importer.php} (55%) diff --git a/includes/admin/class-sp-admin-importers.php b/includes/admin/class-sp-admin-importers.php index 0a708576..65739328 100644 --- a/includes/admin/class-sp-admin-importers.php +++ b/includes/admin/class-sp-admin-importers.php @@ -31,22 +31,15 @@ class SP_Admin_Importers { register_importer( 'sportspress_event_csv', __( 'SportsPress Events (CSV)', 'sportspress' ), __( 'Import events from a csv file.', 'sportspress'), array( $this, 'events_importer' ) ); register_importer( 'sportspress_team_csv', __( 'SportsPress Teams (CSV)', 'sportspress' ), __( 'Import teams from a csv file.', 'sportspress'), array( $this, 'teams_importer' ) ); register_importer( 'sportspress_player_csv', __( 'SportsPress Players (CSV)', 'sportspress' ), __( 'Import players from a csv file.', 'sportspress'), array( $this, 'players_importer' ) ); + register_importer( 'sportspress_staff_csv', __( 'SportsPress Staff (CSV)', 'sportspress' ), __( 'Import staff from a csv file.', 'sportspress'), array( $this, 'staff_importer' ) ); } /** * Add menu item */ public function events_importer() { - // Load Importer API - 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 + $this->includes(); + require 'importers/class-sp-event-importer.php'; // Dispatch @@ -58,16 +51,8 @@ class SP_Admin_Importers { * Add menu item */ public function teams_importer() { - // Load Importer API - 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 + $this->includes(); + require 'importers/class-sp-team-importer.php'; // Dispatch @@ -79,6 +64,29 @@ class SP_Admin_Importers { * Add menu item */ public function players_importer() { + $this->includes(); + + require 'importers/class-sp-player-importer.php'; + + // Dispatch + $importer = new SP_Player_Importer(); + $importer->dispatch(); + } + + /** + * Add menu item + */ + public function staff_importer() { + $this->includes(); + + require 'importers/class-sp-staff-importer.php'; + + // Dispatch + $importer = new SP_Staff_Importer(); + $importer->dispatch(); + } + + public static function includes() { // Load Importer API require_once ABSPATH . 'wp-admin/includes/import.php'; @@ -88,12 +96,7 @@ class SP_Admin_Importers { require $class_wp_importer; } - // Includes - require 'importers/class-sp-player-importer.php'; - - // Dispatch - $importer = new SP_Player_Importer(); - $importer->dispatch(); + require 'importers/class-sp-importer.php'; } } diff --git a/includes/admin/importers/class-sp-event-importer.php b/includes/admin/importers/class-sp-event-importer.php index a08f772a..15a4bccc 100644 --- a/includes/admin/importers/class-sp-event-importer.php +++ b/includes/admin/importers/class-sp-event-importer.php @@ -5,21 +5,13 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Importers - * @version 0.5 + * @version 0.9 */ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly if ( class_exists( 'WP_Importer' ) ) { - class SP_Event_Importer extends WP_Importer { - - var $id; - var $file_url; - var $import_page; - var $delimiter; - var $posts = array(); - var $imported; - var $skipped; + class SP_Event_Importer extends SP_Importer { /** * __construct function. @@ -31,62 +23,6 @@ if ( class_exists( 'WP_Importer' ) ) { $this->import_page = 'sportspress_event_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. * @@ -449,44 +385,6 @@ if ( class_exists( 'WP_Importer' ) ) { 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. * @@ -497,16 +395,6 @@ if ( class_exists( 'WP_Importer' ) ) { echo '

' . __( 'Import Events', 'sportspress' ) . '

'; } - /** - * footer function. - * - * @access public - * @return void - */ - function footer() { - echo '
'; - } - /** * greet function. * @@ -607,14 +495,5 @@ if ( class_exists( 'WP_Importer' ) ) { echo ''; } - - /** - * 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/includes/admin/importers/class-sp-importer.php b/includes/admin/importers/class-sp-importer.php new file mode 100644 index 00000000..34391765 --- /dev/null +++ b/includes/admin/importers/class-sp-importer.php @@ -0,0 +1,147 @@ +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 ); + } + + /** + * 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 '

' . __( 'Import', 'sportspress' ) . '

'; + } + + /** + * footer function. + * + * @access public + * @return void + */ + function footer() { + echo '
'; + } + + /** + * 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/includes/admin/importers/class-sp-player-importer.php b/includes/admin/importers/class-sp-player-importer.php index a485e5ed..cb4d24fc 100644 --- a/includes/admin/importers/class-sp-player-importer.php +++ b/includes/admin/importers/class-sp-player-importer.php @@ -5,21 +5,13 @@ * @author ThemeBoy * @category Admin * @package SportsPress/Admin/Importers - * @version 0.2.11 + * @version 0.9 */ 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; + class SP_Player_Importer extends SP_Importer { /** * __construct function. @@ -31,62 +23,6 @@ if ( class_exists( 'WP_Importer' ) ) { $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. * @@ -216,44 +152,6 @@ if ( class_exists( 'WP_Importer' ) ) { 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. * @@ -264,16 +162,6 @@ if ( class_exists( 'WP_Importer' ) ) { echo '

' . __( 'Import Players', 'sportspress' ) . '

'; } - /** - * footer function. - * - * @access public - * @return void - */ - function footer() { - echo '
'; - } - /** * greet function. * @@ -334,14 +222,5 @@ if ( class_exists( 'WP_Importer' ) ) { echo ''; } - - /** - * 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/includes/admin/importers/class-sp-team-importer.php b/includes/admin/importers/class-sp-staff-importer.php similarity index 55% rename from includes/admin/importers/class-sp-team-importer.php rename to includes/admin/importers/class-sp-staff-importer.php index ffa58c17..a0298a6d 100644 --- a/includes/admin/importers/class-sp-team-importer.php +++ b/includes/admin/importers/class-sp-staff-importer.php @@ -1,25 +1,17 @@ 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 ); + $this->import_page = 'sportspress_staff_csv'; } /** @@ -110,22 +46,25 @@ if ( class_exists( 'WP_Importer' ) ) { $header = fgetcsv( $handle, 0, $this->delimiter ); - if ( sizeof( $header ) == 3 ): + if ( sizeof( $header ) == 5 ): $loop = 0; while ( ( $row = fgetcsv( $handle, 0, $this->delimiter ) ) !== FALSE ): - list( $name, $leagues, $seasons ) = $row; + list( $name, $teams, $leagues, $seasons, $nationality ) = $row; - $team_object = get_page_by_title( $name, OBJECT, 'sp_team' ); + $nationality = trim( strtoupper( $nationality ) ); - if ( ! $name || $team_object ): + if ( $nationality == '*' ) + $nationality = ''; + + if ( ! $name ): $this->skipped++; continue; endif; - $args = array( 'post_type' => 'sp_team', 'post_status' => 'publish', 'post_title' => $name ); + $args = array( 'post_type' => 'sp_staff', 'post_status' => 'publish', 'post_title' => $name ); $id = wp_insert_post( $args ); @@ -140,6 +79,39 @@ if ( class_exists( 'WP_Importer' ) ) { $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; @@ -158,7 +130,7 @@ if ( class_exists( 'WP_Importer' ) ) { // Show Result echo '

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

'; $this->import_end(); @@ -168,49 +140,11 @@ if ( class_exists( 'WP_Importer' ) ) { * Performs post-import cleanup of files and the cache */ function import_end() { - echo '

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

'; + echo '

' . __( 'All done!', 'sportspress' ) . ' ' . __( 'View Staff', '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. * @@ -218,17 +152,7 @@ if ( class_exists( 'WP_Importer' ) ) { * @return void */ function header() { - echo '

' . __( 'Import Teams', 'sportspress' ) . '

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

' . __( 'Import Staff', 'sportspress' ) . '

'; } /** @@ -242,9 +166,9 @@ if ( class_exists( 'WP_Importer' ) ) { 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 (3 columns). Click here to download a sample.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/teams-sample.csv' ) . '

'; + echo '

' . sprintf( __( 'Staff need to be defined with columns in a specific order (5 columns). Click here to download a sample.', 'sportspress' ), plugin_dir_url( SP_PLUGIN_FILE ) . 'dummy-data/staff-sample.csv' ) . '

'; - $action = 'admin.php?import=sportspress_team_csv&step=1'; + $action = 'admin.php?import=sportspress_staff_csv&step=1'; $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() ); $size = size_format( $bytes ); @@ -291,14 +215,5 @@ if ( class_exists( 'WP_Importer' ) ) { echo '
'; } - - /** - * 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; - } } }