$field ) { if ( !in_array( $name, self::$fields ) ) { self::$fields[] = $name; } } return self::$fields; } /** * Returns an array with registration fields from Affiliates > Settings > Registration. * * @return array of affiliate registration fields */ public static function get_affiliates_registration_fields() { $registration_fields = array(); if ( defined( 'AFFILIATES_CORE_LIB' ) ) { include_once AFFILIATES_CORE_LIB . '/class-affiliates-settings.php'; include_once AFFILIATES_CORE_LIB . '/class-affiliates-settings-registration.php'; if ( class_exists( 'Affiliates_Settings_Registration' ) && method_exists( 'Affiliates_Settings_Registration', 'get_fields' ) ) { $registration_fields = Affiliates_Settings_Registration::get_fields(); } } return $registration_fields; } /** * Prints admin notices. */ public static function admin_notices() { if ( !empty( self::$admin_messages ) ) { echo '
'; echo $msg; echo '
'; } echo '%s', 'affiliates-import' ), $line_number, esc_html( implode( ', ', $fields ) ) );
} else {
preg_match_all( '/(meta:)?([a-zA-Z0-9_-]+)/', $line, $matches );
if ( isset( $matches[0] ) && is_array( $matches[0] ) ) {
$fields = array();
$i = 0;
foreach( $matches[0] as $field ) {
if ( in_array( $field, self::get_fields() ) ) {
$fields[] = $field;
} else {
// We don't handle meta: entries for now.
// if ( isset( $matches[1] ) && isset( $matches[1][$i] ) && ( $matches[1][$i] == 'meta:' ) ) {
// if ( isset( $matches[2] ) && !empty( $matches[2][$i] ) ) {
// $meta_key = $matches[2][$i];
// $fields[] = 'meta:' . $meta_key;
// }
// }
}
$i++;
}
self::$admin_messages[] = sprintf( __( 'Column declaration found on line %d, the following column order is assumed: %s', 'affiliates-import' ), $line_number, esc_html( implode( ', ', $fields ) ) );
}
}
continue;
}
// data values
$data = explode( "\t", $line );
$userdata = array();
foreach( $fields as $i => $field ) {
if ( isset( $data[$i] ) ) {
$value = trim( $data[$i] );
$userdata[$field] = $value;
}
}
$user_exists = false;
// email checks
if ( empty( $userdata['user_email'] ) ) {
self::$admin_messages[] = sprintf( __( 'Error on line %d, missing email address.', 'affiliates-import' ), $line_number );
$errors++;
$skip = true;
} else if ( !is_email( $userdata['user_email'] ) ) {
self::$admin_messages[] = sprintf( __( 'Error on line %d, %s is not a valid email address.', 'affiliates-import' ), $line_number, esc_html( $userdata['user_email'] ) );
$errors++;
$skip = true;
} else if ( get_user_by( 'email', $userdata['user_email'] ) ) {
$user_exists = true;
if ( !$update_users ) {
if ( !$suppress_warnings ) {
self::$admin_messages[] = sprintf( __( 'Warning on line %d, a user with the email address %s already exists.', 'affiliates-import' ), $line_number, esc_html( $userdata['user_email'] ) );
}
$warnings++;
$skip = true;
}
}
// username check
if ( empty( $userdata['user_login'] ) && !empty( $userdata['user_email'] ) ) {
$userdata['user_login'] = $userdata['user_email'];
}
if ( !empty( $userdata['user_login'] ) && get_user_by( 'login', $userdata['user_login'] ) ) {
$user_exists = true;
if ( !$update_users ) {
if ( !$suppress_warnings ) {
self::$admin_messages[] = sprintf( __( 'Warning on line %d, the username %s already exists.', 'affiliates-import' ), $line_number, esc_html( $userdata['user_login'] ) );
}
$warnings++;
$skip = true;
}
}
// generate a password for new users
if ( empty( $userdata['password'] ) && !empty( $userdata['user_pass'] ) ) {
$userdata['password'] = $userdata['user_pass'];
}
if ( empty( $userdata['password'] ) ) {
if ( !$user_exists ) {
$userdata['password'] = wp_generate_password();
} else {
unset( $userdata['password'] );
}
}
// import or skip
if ( !$skip ) {
if ( !$user_exists && !empty( $userdata['user_login'] ) && !empty( $userdata['user_email'] ) ) {
$valid++;
if ( !$test ) {
if ( self::insert_user( $userdata ) ) {
$imported++;
if ( ( $imported + $updated ) >= $limit ) {
break;
}
}
}
} else if ( $update_users ) {
$valid++;
if ( !$test ) {
if ( self::update_user( $userdata ) ) {
$updated++;
if ( ( $imported + $updated ) >= $limit ) {
break;
}
}
}
}
} else {
$skipped++;
}
if ( $stop_on_errors && ( $errors > 0 ) ) {
break;
}
if ( !$skip_limit_checks ) {
// memory guard
if ( is_numeric( $memory_limit ) ) {
$old_bytes = $bytes;
$bytes = memory_get_usage( true );
$remaining = $memory_limit - $bytes;
$delta = self::BASE_DELTA;
if ( $bytes > $old_bytes ) {
$delta += intval( ( $bytes - $old_bytes ) * self::DELTA_F );
}
if ( $remaining < $delta ) {
self::$admin_messages[] = sprintf( __( 'Warning, stopped after line %d to avoid exhausting the available memory for PHP. Consider raising memory_limit or reducing the number of records imported.', 'affiliates-import' ), $line_number );
break;
}
}
// time guard
if ( function_exists( 'getrusage' ) ) {
$resource_usage = getrusage();
if ( isset( $resource_usage['ru_utime.tv_sec'] ) ) {
$execution_time = $resource_usage['ru_stime.tv_sec'] + $resource_usage['ru_utime.tv_sec'] + 2; // add 2 as top value for the sum of ru_stime.tv_usec and ru_utime.tv_usec
$d = ceil( $execution_time - $initial_execution_time );
if ( intval( $d * self::DELTA_F ) > ( $max_execution_time - $d ) ) {
self::$admin_messages[] = sprintf( __( 'Warning, stopped after line %d to avoid reaching the maximum execution time for PHP. Consider raising max_execution_time or reducing the number of records imported.', 'affiliates-import' ), $line_number );
break;
}
}
}
}
}
@fclose( $h );
self::$admin_messages[] = sprintf( _n( '1 valid entry has been read.', '%d valid entries have been read.', $valid, 'affiliates-import' ), $valid );
self::$admin_messages[] = sprintf( _n( '1 entry has been skipped.', '%d entries have been skipped.', $skipped, 'affiliates-import' ), $skipped );
self::$admin_messages[] = sprintf( _n( '1 user has been imported.', '%d users have been imported.', $imported, 'affiliates-import' ), $imported );
self::$admin_messages[] = sprintf( _n( '1 user has been updated.', '%d users have been updated.', $updated, 'affiliates-import' ), $updated );
} else {
self::$admin_messages[] = __( 'Import failed (error opening temporary file).', 'affiliates-import' );
}
}
} else if ( $_FILES['file']['error'] == UPLOAD_ERR_NO_FILE ) {
self::$admin_messages[] = __( 'Please choose a file to import from.', 'affiliates-import' );
}
}
}
/**
* Create a new user account and add it as an affiliate.
*
* @param array $userdata
*/
private static function insert_user( $userdata = array() ) {
if ( ! (
function_exists( 'affiliates_user_is_affiliate' ) &&
class_exists( 'Affiliates_Registration' ) &&
method_exists( 'Affiliates_Registration', 'store_affiliate' ) &&
method_exists( 'Affiliates_Registration', 'create_affiliate' ) &&
method_exists( 'Affiliates_Registration', 'update_affiliate_user' )
) ) {
return null;
}
if ( empty( $userdata['user_email'] ) || empty( $userdata['password'] ) ) {
return null;
}
$result = null;
if ( empty( $userdata['user_login'] ) ) {
$userdata['user_login'] = $userdata['user_email'];
}
if ( empty( $userdata['first_name'] ) ) {
$userdata['first_name'] = '';
}
if ( empty( $userdata['last_name'] ) ) {
$userdata['last_name'] = '';
}
$user_id = Affiliates_Registration::create_affiliate( $userdata );
if ( !is_wp_error( $user_id ) ) {
if ( !affiliates_user_is_affiliate( $user_id ) ) {
add_filter( 'pre_option_aff_notify_admin', array( __CLASS__, 'pre_option_aff_notify_admin' ), 10, 2 );
if ( $affiliate_id = Affiliates_Registration::store_affiliate( $user_id, $userdata, 'active' ) ) {
$result = $user_id;
Affiliates_Registration::update_affiliate_user( $affiliate_id, $userdata );
}
remove_filter( 'pre_option_aff_notify_admin', array( __CLASS__, 'pre_option_aff_notify_admin' ), 10 );
}
}
return $result;
}
/**
* Add an existing user account as an affiliate.
*
* @param array $userdata
*/
private static function update_user( $userdata = array() ) {
if ( ! (
function_exists( 'affiliates_user_is_affiliate' ) &&
class_exists( 'Affiliates_Registration' ) &&
method_exists( 'Affiliates_Registration', 'store_affiliate' ) &&
method_exists( 'Affiliates_Registration', 'update_affiliate_user' )
) ) {
return null;
}
$result = null;
$user = false;
if ( !empty( $userdata['user_email'] ) ) {
$user = get_user_by( 'email', $userdata['user_email'] );
}
if ( !$user && !empty( $userdata['user_login'] ) ) {
$user = get_user_by( 'login', $userdata['user_login'] );
}
if ( $user !== false ) {
$user_id = $user->ID;
if ( !affiliates_user_is_affiliate( $user_id ) ) {
add_filter( 'pre_option_aff_notify_admin', array( __CLASS__, 'pre_option_aff_notify_admin' ), 10, 2 );
if ( $affiliate_id = Affiliates_Registration::store_affiliate( $user_id, $userdata, 'active' ) ) {
$result = $user_id;
Affiliates_Registration::update_affiliate_user( $affiliate_id, $userdata );
}
remove_filter( 'pre_option_aff_notify_admin', array( __CLASS__, 'pre_option_aff_notify_admin' ), 10 );
}
}
return $result;
}
/**
* Filters the aff_notify_admin option to avoid administrator notifications on imported affiliates.
*
* @return null (can't return false as that won't take any effect)
*/
public static function pre_option_aff_notify_admin( $value, $option ) {
return null;
}
}
Affiliates_Import_Process::init();