verbose;
if($verbose && self::$instance->verbose || !$verbose && !self::$instance->verbose){
echo($data);
}
flush();
$theDirectory = dirname(__FILE__) . '/logs/';
if(is_dir($theDirectory)){
$fileName = date('Y-m-d') . '.log';
file_put_contents($theDirectory . $fileName, $data, FILE_APPEND );
}
}
public function __construct(){
if(is_null(self::$instance) ){
/*
* Singleton pattern: The hooks will be invoked once!
*/
add_action('admin_init', array($this , 'init'));
add_action('admin_menu', array($this, 'createPages'));
self::$instance = $this;
}
return self::$instance;
}
public function init(){
load_plugin_textdomain(self::LANG_DOMAIN, false, basename(dirname( __FILE__ )).'/languages/' );
if(isset($_GET['aeiou']) && $_GET['aeiou'] == 'export'){
$this->exportData();
}
}
private function toXMLItem($user){
echo("\t
verbose = isset($_POST['verbose']);
/*
* The behavior when a wordpress user is found 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
*/
$this->outputLog( __('Import started.', self::LANG_DOMAIN) . "\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'];
$this->outputLog("\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)."`";
$this->outputLog( sprintf(__("has new %s value(s).", self::LANG_DOMAIN),$changes) . "\n");
$user['ID'] = $usr['ID'];
/*
* If password is defined in the XML file the script uses it (as is)!
*/
wp_update_user($user);
global $wpdb;
if(isset($usr['user_pass']))
$wpdb->query(
$wpdb->prepare("update {$wpdb->prefix}_users set user_pass='%s' where ID=%d", $user['user_pass'], $user['ID'])
);
}else{
_e("unchanged.", self::LANG_DOMAIN) . "\n";
}
$userID = $user['ID'];
}else{
if(!$usr){
/*
* New User must be created
*/
if(!isset($user['user_pass'])) $user['user_pass'] = '';
$userID = wp_insert_user($user);
/*
* If i was unable to import the user I will give the error message to the admin.
*/
if(is_wp_error($userID)){
$this->outputLog( $userID->get_error_message() . "\n" );
}else{
$user['ID'] = $userID;
/*
* If password is defined in the XML file we should to import it
*/
if(isset($usr['user_pass']))
$wpdb->query(
$wpdb->prepare("update {$wpdb->prefix}_users set user_pass='%s' where ID=%d", $user['user_pass'], $user['ID'])
);
$this->outputLog( sprintf(__("inserted with ID %d", self::LANG_DOMAIN),$userID) . "\n" );
}
}else{
$this->outputLog( __("skipped due it's already in the database", self::LANG_DOMAIN) . "\n" );
$userID = $usr->ID;
}
}
if(!is_wp_error($userID)){
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);
$this->outputLog( __("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);
$this->outputLog( __("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(!$metaKey || $this->exists=='replace'){
$value = maybe_unserialize($value);
delete_user_meta($userID, $key);
if( !empty( $value ) ){
$count+=1;
$this->outputLog( sprintf(__("Metadata `%s` updated for user [%s]", self::LANG_DOMAIN), $key, $login) . "\n", true);
update_user_meta($userID, $key, $value );
}else{
if($metaKey) $this->outputLog( sprintf(__("Metadata `%s` removed for user [%s]", self::LANG_DOMAIN), $key, $login) . "\n", true);
}
}
}
if($count>0)
$this->outputLog( 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);
$value = maybe_unserialize($value);
if(!$optExists || $this->exists=='replace'){
delete_user_option($userID, $key);
if( !empty( $value ) ){
$count+=1;
$this->outputLog( sprintf(__("Option `%s` updated for user [%s]", self::LANG_DOMAIN), $key, $login) . "\n" , true);
update_user_option($userID, $key, $value, $global );
}else{
if($optExists)
$this->outputLog( sprintf(__("Option `%s` removed for user [%s]", self::LANG_DOMAIN), $key, $login) . "\n" );
}
}
}
if($count>0)
$this->outputLog( 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];
$this->outputLog( sprintf(__("Importing field `%s` of group `%s`", self::LANG_DOMAIN), $name, $group) . "\n", true);
/*
* 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 = maybe_unserialize($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 = maybe_unserialize($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();
}
?>