exportData();
}
}
private function toXMLItem($user){
echo("\t
verbose = isset($_POST['verbose']);
/*
* The behavior when a wordpress user is found
* it could be:
* - skip
* - replace
* - update
*/
$this->exists = isset($_POST['exists'])?$_POST['exists']:'skip';
$this->importMetadata = isset($_POST['metadata']);
$this->importOptions = isset($_POST['options']);
$this->importXProfile = isset($_POST['xprofile']);
if($theFile['error'] != 0){
/*
* Some errors occours
*/
echo $this->decodeFileError($theFile['error']);
}else{
/*
* Starting import
*/
_e('Import started.', self::LANG_DOMAIN); echo "\n\n";
_e('Please do not refresh the page and not go anywhere.'); echo "\n";
_e('Interruption of page execution could cause data loss and user table to break!'); echo "\n";
$parser = new AEIOU_Parser_SimpleXML();
$users = $parser->parse($theFile['tmp_name']);
foreach($users as $user){
/*
* Increase the time limit up to 10 seconds per user
*/
set_time_limit(10);
$login = $user['user_login'];
echo("\n$login ");
$usr = get_user_by('login', $login);
$userID = 0;
$userMeta = (isset($user['metadata'])) ? $user['metadata'] : array();
/*
* v0.2: Added user options and xprofile import
*/
$userOptions = (isset($user['options'])) ? $user['options'] : array();
$xprofile = (isset($user['xprofile'])) ? $user['xprofile'] : array();
/*
* Cleanup extra keys
*/
if(isset($user['metadata'])) unset($user['metadata']);
if(isset($user['options'])) unset($user['options']);
if(isset($user['xprofile'])) unset($user['xprofile']);
do_action('aeiou_before_import_user', $user);
if($usr && $this->exists != 'skip'){
$usr = (array) $usr->data;
if($this->exists == 'update'){
/*
* Keeping metadata and removing from array
*/
foreach($user as $key => $value){
/*
* Removing all non empty key from import
*/
if(isset($usr[$key]) && !empty($usr[$key])){
unset($user[$key]);
}
}
}
if(count($user)>0){
$changes = array_keys($user);
$changes = "".implode(", ", $changes)."";
echo sprintf(__("has new %s value(s).", self::LANG_DOMAIN),$changes) . "\n";
$user['ID'] = $usr['ID'];
/*
* TODO: If password is defined in the XML file we should to import it
*/
wp_update_user($user);
}else{
_e("unchanged.", self::LANG_DOMAIN) . "\n";
}
$userID = $user['ID'];
}else{
if(!$usr){
/*
* New User must be created
*/
$userID = wp_insert_user($user);
$user['ID'] = $userID;
/*
* TODO: If password is defined in the XML file we should to import it
*/
wp_update_user($user);
echo sprintf(__("inserted with ID %d", self::LANG_DOMAIN),$userID) . "\n";
}else{
echo __("skipped due it's already in the database", self::LANG_DOMAIN) . "\n";
}
}
if($this->importMetadata && $userID != 0){
do_action('aeiou_before_import_metadata', $user);
$this->importUserMetaData($userMeta, $userID, $login);
}
if($this->importOptions && $userID != 0){
do_action('aeiou_before_import_options', $user);
echo __("Importing user options\n", self::LANG_DOMAIN);
$this->importUserOptions($userOptions, $userID, $login);
}
if($this->importXProfile && $userID != 0 && class_exists('BP_XProfile_Component')){
do_action('aeiou_before_import_xprofile', $user);
echo __("Importing XProfile data\n", self::LANG_DOMAIN);
$this->importXProfileData($xprofile, $userID);
}
do_action('aeiou_after_import_user', $user);
}
}
}
}
?>
$value){
$metaKey = get_user_meta($userID, $key);
if(empty($metaKey[0])) $metaKey = false;
if(is_serialized($value)) $value = unserialize($value);
if(!$metaKey || $this->exists=='replace'){
delete_user_meta($userID, $key);
if( !empty( $value ) ){
$count+=1;
if($this->verbose) echo sprintf(__("Metadata %s updated for user %s", self::LANG_DOMAIN), $key, $login) . "\n";
update_user_meta($userID, $key, $value );
}else{
if($metaKey && $this->verbose) echo sprintf(__("Metadata %s removed for user %s", self::LANG_DOMAIN), $key, $login) . "\n";
}
}
}
if($count>0)
echo sprintf(__("Updated %d metadatas for user %s", self::LANG_DOMAIN), $count, $login) . "\n";
}
private function importUserOptions($userOptions, $userID, $login){
/*
* Admin asked for options import
*/
$count = 0;
foreach($userOptions as $key => $data){
list($value, $global) = $data;
$option = get_user_option( $key, $userID);
$optExists = !empty($option);
if(is_serialized($option)) $option = unserialize($option);
if(!$optExists || $this->exists=='replace'){
delete_user_option($userID, $key);
if( !empty( $value ) ){
$count+=1;
if($this->verbose) echo sprintf(__("Option %s updated for user %s", self::LANG_DOMAIN), $key, $login) . "\n";
update_user_option($userID, $key, $value, $global );
}else{
if($optExists && $this->verbose) echo sprintf(__("Option %s removed for user %s", self::LANG_DOMAIN), $key, $login) . "\n";
}
}
}
if($count>0)
echo sprintf(__("Updated %d metadatas for user %s", self::LANG_DOMAIN), $count, $login) . "\n";
}
private function importXProfileData($xprofile, $userID){
/*
* Admin asked for xprofile import
*/
$count = 0;
foreach($xprofile as $name => $data){
$value = $data[0];
$group = $data[1];
if($this->verbose) echo sprintf(__("Importing field %s of group %s", self::LANG_DOMAIN), $name, $group) . "\n";
/*
* Get the XProfile Fields Group ID
* If it's not available the will be created as new.
*/
$groupId = $this->getXProfileGroupIDByName($group);
/*
* Get the XProfile Field ID
* If it's not available the will be created as new.
*/
$fieldId = $this->getXProfileFieldIDByName($name, $groupId);
$fieldData = is_serialized($value)? unserialize($value): $value;
$oldValue = BP_XProfile_ProfileData::get_value_byid($fieldId, $userID);
if(is_null($oldValue) || ($oldValue == '' && $this->exists == 'update') || ($this->exists == 'replace') ){
/*
* Saving data if not exists or need to be updated
*/
$data = new BP_XProfile_ProfileData();
$data->field_id = $fieldId;
$data->user_id = $userID;
$data->value = is_serialized($value)? unserialize($value): $value;
$data->save();
}
}
}
private function getXProfileFieldIDByName($name, $groupId = 0, $create = true){
$out = BP_XProfile_Field::get_id_from_name($name);
if(is_null($out) && $create){
$field = new BP_XProfile_Field();
$field->name = $name;
$field->group_id = $groupId;
/*
* TODO: Store in the XML the type.
* Aactually i will create a generic text type if the field is not available.
*/
$field->type = "text";
$out = $field->save();
}
return $out;
}
private function getXProfileGroupIDByName($name, $create = true){
global $wpdb, $bp;
$sql = "select id from {$bp->profile->table_name_groups} where name = %s";
$sql = $wpdb->prepare($sql, $name);
$out = $wpdb->get_var($sql);
if( is_null( $out ) && $create ){
$newGroup = new BP_XProfile_Group();
$newGroup->name = $name;
$out = $newGroup->save();
}
return $out;
}
}
$aeiou = new AEIOU();
}
?>