diff --git a/class-readcsv.php b/class-readcsv.php
index 266ba61..2ba4f86 100644
--- a/class-readcsv.php
+++ b/class-readcsv.php
@@ -15,12 +15,12 @@
*/
class ReadCSV {
- const field_start = 0;
- const unquoted_field = 1;
- const quoted_field = 2;
- const found_quote = 3;
- const found_cr_q = 4;
- const found_cr = 5;
+ protected $field_start = 0;
+ protected $unquoted_field = 1;
+ protected $quoted_field = 2;
+ protected $found_quote = 3;
+ protected $found_cr_q = 4;
+ protected $found_cr = 5;
private $file;
private $sep;
@@ -72,12 +72,12 @@ public function get_row() {
$row=array();
$field="";
- $state=self::field_start;
+ $state=$this->field_start;
while (1) {
$char = $this->next_char();
- if ($state == self::quoted_field) {
+ if ($state == $this->quoted_field) {
if ($char === FALSE) {
// EOF. (TODO: error case - no closing quote)
$row[]=$field;
@@ -86,64 +86,64 @@ public function get_row() {
// Fall through to accumulate quoted chars in switch() {...}
} elseif ($char === FALSE || $char == "\n") {
// End of record.
- // (TODO: error case if $state==self::field_start here - trailing comma)
+ // (TODO: error case if $state==$this->field_start here - trailing comma)
$row[] = $field;
return $row;
} elseif ($char == "\r") {
// Possible start of \r\n line end, but might be just part of foo\rbar
- $state = ($state == self::found_quote)? self::found_cr_q: self::found_cr;
+ $state = ($state == $this->found_quote)? $this->found_cr_q: $this->found_cr;
continue;
} elseif ($char == $this->sep &&
- ($state == self::field_start ||
- $state == self::found_quote ||
- $state == self::unquoted_field)) {
+ ($state == $this->field_start ||
+ $state == $this->found_quote ||
+ $state == $this->unquoted_field)) {
// End of current field, start of next field
$row[]=$field;
$field="";
- $state=self::field_start;
+ $state=$this->field_start;
continue;
}
switch ($state) {
- case self::field_start:
+ case $this->field_start:
if ($char == '"')
- $state = self::quoted_field;
+ $state = $this->quoted_field;
else {
- $state = self::unquoted_field;
+ $state = $this->unquoted_field;
$field .= $char;
}
break;
- case self::quoted_field:
+ case $this->quoted_field:
if ($char == '"')
- $state = self::found_quote;
+ $state = $this->found_quote;
else
$field .= $char;
break;
- case self::unquoted_field:
+ case $this->unquoted_field:
$field .= $char;
// (TODO: error case if '"' in middle of unquoted field)
break;
- case self::found_quote:
+ case $this->found_quote:
// Found '"' escape sequence
$field .= $char;
- $state = self::quoted_field;
+ $state = $this->quoted_field;
// (TODO: error case if $char!='"' - non-separator char after single quote)
break;
- case self::found_cr:
+ case $this->found_cr:
// Lone \rX instead of \r\n. Treat as literal \rX. (TODO: error case?)
$field .= "\r".$char;
- $state = self::unquoted_field;
+ $state = $this->unquoted_field;
break;
- case self::found_cr_q:
+ case $this->found_cr_q:
// (TODO: error case: "foo"\rX instead of "foo"\r\n or "foo"\n)
$field .= "\r".$char;
- $state = self::quoted_field;
+ $state = $this->quoted_field;
break;
}
}
diff --git a/import-users-from-csv.php b/import-users-from-csv.php
index ed096c5..c9b14b3 100644
--- a/import-users-from-csv.php
+++ b/import-users-from-csv.php
@@ -39,21 +39,21 @@
* @since 0.1
**/
class IS_IU_Import_Users {
- private static $log_dir_path = '';
- private static $log_dir_url = '';
+ protected $log_dir_path = '';
+ protected $log_dir_url = '';
/**
* Initialization
*
* @since 0.1
**/
- public function init() {
- add_action( 'admin_menu', array( __CLASS__, 'add_admin_pages' ) );
- add_action( 'init', array( __CLASS__, 'process_csv' ) );
+ public function __construct() {
+ add_action( 'admin_menu', array( $this, 'add_admin_pages' ) );
+ add_action( 'init', array( $this, 'process_csv' ) );
$upload_dir = wp_upload_dir();
- self::$log_dir_path = trailingslashit( $upload_dir['basedir'] );
- self::$log_dir_url = trailingslashit( $upload_dir['baseurl'] );
+ $this->log_dir_path = trailingslashit( $upload_dir['basedir'] );
+ $this->log_dir_url = trailingslashit( $upload_dir['baseurl'] );
}
/**
@@ -62,7 +62,7 @@ public function init() {
* @since 0.1
**/
public function add_admin_pages() {
- add_users_page( __( 'Import From CSV' , 'import-users-from-csv'), __( 'Import From CSV' , 'import-users-from-csv'), 'create_users', 'import-users-from-csv', array( __CLASS__, 'users_page' ) );
+ add_users_page( __( 'Import From CSV' , 'import-users-from-csv'), __( 'Import From CSV' , 'import-users-from-csv'), 'create_users', 'import-users-from-csv', array( $this, 'users_page' ) );
}
/**
@@ -74,14 +74,14 @@ public function process_csv() {
if ( isset( $_POST['_wpnonce-is-iu-import-users-users-page_import'] ) ) {
check_admin_referer( 'is-iu-import-users-users-page_import', '_wpnonce-is-iu-import-users-users-page_import' );
- if ( !empty( $_FILES['users_csv']['tmp_name'] ) ) {
+ if ( isset( $_FILES['users_csv']['tmp_name'] ) ) {
// Setup settings variables
$filename = $_FILES['users_csv']['tmp_name'];
$password_nag = isset( $_POST['password_nag'] ) ? $_POST['password_nag'] : false;
$users_update = isset( $_POST['users_update'] ) ? $_POST['users_update'] : false;
$new_user_notification = isset( $_POST['new_user_notification'] ) ? $_POST['new_user_notification'] : false;
- $results = self::import_csv( $filename, array(
+ $results = $this->import_csv( $filename, array(
'password_nag' => $password_nag,
'new_user_notification' => $new_user_notification,
'users_update' => $users_update
@@ -120,12 +120,12 @@ public function users_page() {
log_dir_path . 'is_iu_errors.log';
+ $error_log_url = $this->log_dir_url . 'is_iu_errors.log';
if ( ! file_exists( $error_log_file ) ) {
if ( ! @fopen( $error_log_file, 'x' ) )
- echo '
' . sprintf( __( 'Notice: please make the directory %s writable so that you can see the error log.' , 'import-users-from-csv'), self::$log_dir_path ) . '
';
+ echo '
' . sprintf( __( 'Notice: please make the directory %s writable so that you can see the error log.' , 'import-users-from-csv'), $this->log_dir_path ) . '
';
}
if ( isset( $_GET['import'] ) ) {
@@ -231,121 +231,117 @@ public static function import_csv( $filename, $args ) {
include( plugin_dir_path( __FILE__ ) . 'class-readcsv.php' );
// Loop through the file lines
- $file_handle = @fopen( $filename, 'r' );
- if($file_handle) {
- $csv_reader = new ReadCSV( $file_handle, IS_IU_CSV_DELIMITER, "\xEF\xBB\xBF" ); // Skip any UTF-8 byte order mark.
-
- $first = true;
- $rkey = 0;
- while ( ( $line = $csv_reader->get_row() ) !== NULL ) {
-
- // If the first line is empty, abort
- // If another line is empty, just skip it
- if ( empty( $line ) ) {
- if ( $first )
- break;
- else
- continue;
- }
-
- // If we are on the first line, the columns are the headers
- if ( $first ) {
- $headers = $line;
- $first = false;
+ $file_handle = fopen( $filename, 'r' );
+ $csv_reader = new ReadCSV( $file_handle, IS_IU_CSV_DELIMITER, "\xEF\xBB\xBF" ); // Skip any UTF-8 byte order mark.
+
+ $first = true;
+ $rkey = 0;
+ while ( ( $line = $csv_reader->get_row() ) !== NULL ) {
+
+ // If the first line is empty, abort
+ // If another line is empty, just skip it
+ if ( empty( $line ) ) {
+ if ( $first )
+ break;
+ else
continue;
- }
-
- // Separate user data from meta
- $userdata = $usermeta = array();
- foreach ( $line as $ckey => $column ) {
- $column_name = $headers[$ckey];
- $column = trim( $column );
-
- if ( in_array( $column_name, $userdata_fields ) ) {
- $userdata[$column_name] = $column;
- } else {
- $usermeta[$column_name] = $column;
- }
- }
-
- // A plugin may need to filter the data and meta
- $userdata = apply_filters( 'is_iu_import_userdata', $userdata, $usermeta );
- $usermeta = apply_filters( 'is_iu_import_usermeta', $usermeta, $userdata );
+ }
- // If no user data, bailout!
- if ( empty( $userdata ) )
- continue;
+ // If we are on the first line, the columns are the headers
+ if ( $first ) {
+ $headers = $line;
+ $first = false;
+ continue;
+ }
- // Something to be done before importing one user?
- do_action( 'is_iu_pre_user_import', $userdata, $usermeta );
+ // Separate user data from meta
+ $userdata = $usermeta = array();
+ foreach ( $line as $ckey => $column ) {
+ $column_name = $headers[$ckey];
+ $column = trim( $column );
- $user = $user_id = false;
+ if ( in_array( $column_name, $userdata_fields ) ) {
+ $userdata[$column_name] = $column;
+ } else {
+ $usermeta[$column_name] = $column;
+ }
+ }
- if ( isset( $userdata['ID'] ) )
- $user = get_user_by( 'ID', $userdata['ID'] );
+ // A plugin may need to filter the data and meta
+ $userdata = apply_filters( 'is_iu_import_userdata', $userdata, $usermeta );
+ $usermeta = apply_filters( 'is_iu_import_usermeta', $usermeta, $userdata );
- if ( ! $user && $users_update ) {
- if ( isset( $userdata['user_login'] ) )
- $user = get_user_by( 'login', $userdata['user_login'] );
+ // If no user data, bailout!
+ if ( empty( $userdata ) )
+ continue;
- if ( ! $user && isset( $userdata['user_email'] ) )
- $user = get_user_by( 'email', $userdata['user_email'] );
- }
+ // Something to be done before importing one user?
+ do_action( 'is_iu_pre_user_import', $userdata, $usermeta );
- $update = false;
- if ( $user ) {
- $userdata['ID'] = $user->ID;
- $update = true;
- }
+ $user = $user_id = false;
- // If creating a new user and no password was set, let auto-generate one!
- if ( ! $update && empty( $userdata['user_pass'] ) )
- $userdata['user_pass'] = wp_generate_password( 12, false );
+ if ( isset( $userdata['ID'] ) )
+ $user = get_user_by( 'ID', $userdata['ID'] );
- if ( $update )
- $user_id = wp_update_user( $userdata );
- else
- $user_id = wp_insert_user( $userdata );
+ if ( ! $user && $users_update ) {
+ if ( isset( $userdata['user_login'] ) )
+ $user = get_user_by( 'login', $userdata['user_login'] );
- // Is there an error o_O?
- if ( is_wp_error( $user_id ) ) {
- $errors[$rkey] = $user_id;
- } else {
- // If no error, let's update the user meta too!
- if ( $usermeta ) {
- foreach ( $usermeta as $metakey => $metavalue ) {
- $metavalue = maybe_unserialize( $metavalue );
- update_user_meta( $user_id, $metakey, $metavalue );
- }
- }
+ if ( ! $user && isset( $userdata['user_email'] ) )
+ $user = get_user_by( 'email', $userdata['user_email'] );
+ }
- // If we created a new user, maybe set password nag and send new user notification?
- if ( ! $update ) {
- if ( $password_nag )
- update_user_option( $user_id, 'default_password_nag', true, true );
+ $update = false;
+ if ( $user ) {
+ $userdata['ID'] = $user->ID;
+ $update = true;
+ }
- if ( $new_user_notification )
- wp_new_user_notification( $user_id, $userdata['user_pass'] );
+ // If creating a new user and no password was set, let auto-generate one!
+ if ( ! $update && empty( $userdata['user_pass'] ) )
+ $userdata['user_pass'] = wp_generate_password( 12, false );
+
+ if ( $update )
+ $user_id = wp_update_user( $userdata );
+ else
+ $user_id = wp_insert_user( $userdata );
+
+ // Is there an error o_O?
+ if ( is_wp_error( $user_id ) ) {
+ $errors[$rkey] = $user_id;
+ } else {
+ // If no error, let's update the user meta too!
+ if ( $usermeta ) {
+ foreach ( $usermeta as $metakey => $metavalue ) {
+ $metavalue = maybe_unserialize( $metavalue );
+ update_user_meta( $user_id, $metakey, $metavalue );
}
+ }
- // Some plugins may need to do things after one user has been imported. Who know?
- do_action( 'is_iu_post_user_import', $user_id );
+ // If we created a new user, maybe set password nag and send new user notification?
+ if ( ! $update ) {
+ if ( $password_nag )
+ update_user_option( $user_id, 'default_password_nag', true, true );
- $user_ids[] = $user_id;
+ if ( $new_user_notification )
+ wp_new_user_notification( $user_id, $userdata['user_pass'] );
}
- $rkey++;
+ // Some plugins may need to do things after one user has been imported. Who know?
+ do_action( 'is_iu_post_user_import', $user_id );
+
+ $user_ids[] = $user_id;
}
- fclose( $file_handle );
- } else {
- $errors[] = new WP_Error('file_read', 'Unable to open CSV file.');
+
+ $rkey++;
}
+ fclose( $file_handle );
// One more thing to do after all imports?
do_action( 'is_iu_post_users_import', $user_ids, $errors );
// Let's log the errors
- self::log_errors( $errors );
+ $this->log_errors( $errors );
return array(
'user_ids' => $user_ids,
@@ -362,7 +358,7 @@ private static function log_errors( $errors ) {
if ( empty( $errors ) )
return;
- $log = @fopen( self::$log_dir_path . 'is_iu_errors.log', 'a' );
+ $log = @fopen( $this->log_dir_path . 'is_iu_errors.log', 'a' );
@fwrite( $log, sprintf( __( 'BEGIN %s' , 'import-users-from-csv'), date( 'Y-m-d H:i:s', time() ) ) . "\n" );
foreach ( $errors as $key => $error ) {
@@ -375,4 +371,4 @@ private static function log_errors( $errors ) {
}
}
-IS_IU_Import_Users::init();
+new IS_IU_Import_Users();
\ No newline at end of file