Plugin Directory

Changeset 654798


Ignore:
Timestamp:
01/18/2013 10:05:33 AM (13 years ago)
Author:
tosend.it
Message:

New 0.2 version is available

Location:
aeiou
Files:
3 edited
5 copied

Legend:

Unmodified
Added
Removed
  • aeiou/tags/0.2/index.php

    r648658 r654798  
    33Plugin Name: AEIOU - Advanced Export/Import (WordPress) Object Users
    44Plugin URI: http://wordpress.org/extend/plugins/aeiou/
    5 Description: AEIOU is a powerfull plugin that export all wordpress users info into an XML file including user metadatas and allow to import the same xml file into a new wordpress keeping the metadata of each user.
     5Description: AEIOU is a powerfull plugin that export all wordpress users info into an XML file including user metadatas and allow to import the same xml file into a new wordpress keeping the metadata of each user. The plugin is able to export buddypress xprofile data too.
    66Author: toSend.it di Luisa Marra
    7 Version: 0.1
     7Version: 0.2
    88Author URI: http://tosend.it
    99*/
     
    1111if(!class_exists('AEIOU')){
    1212    class AEIOU{
    13         const VERSION = '0.1';
     13        const VERSION = '0.2';
    1414        const LANG_DOMAIN = 'AEIOU';
     15       
     16        private $verbose;
     17        private $exists;
     18        private $importMetadata;
     19        private $importOptions;
     20        private $importXProfile;
     21       
     22        private $exportMetadata;
     23        private $exportOptions;
     24        private $exportXProfile;
    1525       
    1626        public function __construct(){
    1727            add_action('admin_init', array($this , 'init'));
    1828            add_action('admin_menu', array($this, 'createPages'));
    19             #add_action('request', array($this, 'exportData'));
     29           
    2030        }
    2131
     
    4353                echo("\t\t<display_name><![CDATA[{$user->display_name}]]></display_name>\n");
    4454               
    45                 if(isset($_GET['metadata'])){
     55                if($this->exportMetadata || $this->exportOptions){
    4656                    /*
    4757                     * I should export metadata (as requested by user)
    4858                    */
    4959                    echo("\t\t<metadata>");
    50                        
     60                    global $wpdb;
    5161                    $umeta = get_user_meta($user->ID);
    5262                    foreach($umeta as $key => $value){
     63                        $isOption = preg_match("#^" . preg_quote($wpdb->prefix) ."#", $key);
     64                        if( ($isOption && $this->exportOptions) || (!$isOption && $this->exportMetadata) ){
    5365                           
    54                         echo("\t\t\t<meta key=\"$key\"><![CDATA[");
    55                         $value = $value[0];
    56                         echo base64_encode($value);
    57                         echo("]]></meta>\n");
     66                            echo("\t\t\t<meta ");
     67                           
     68                            if(preg_match("#^" . preg_quote($wpdb->prefix) ."#", $key)){
     69                                /*
     70                                 * If the value is an option i should remove the db prefix from the option
     71                                 * name and add the attribute option to allow the importer to understand
     72                                 * when a node is an option or a setting.
     73                                 */
     74                                echo('option="true" ');
     75                                echo('global="false" ');
     76                                $key =substr($key, strlen($wpdb->prefix));
     77                            }
     78                            echo("key=\"$key\"><![CDATA[");
     79                            $value = $value[0];
     80                            echo base64_encode($value);
     81                            echo("]]></meta>\n");
     82                        }
    5883                       
    5984                    }
    6085                       
    6186                    echo("\t\t</metadata>");
     87                }
     88                if( class_exists('BuddyPress') ){
     89                    if(class_exists('BP_XProfile_Group') && $this->exportXProfile ){
     90                        /*
     91                         * I should export xprofile data too.
     92                         */
     93                        $groups = BP_XProfile_Group::get( array(
     94                            'fetch_fields' => true
     95                        ) );
     96                        /*
     97                         * Fetching all groups
     98                         */
     99                        echo("\t\t<xprofile>");
     100                        foreach($groups as $group){
     101                            $groupName = $group->name;
     102                            if(!empty($group->fields)){
     103                               
     104                                /*
     105                                 * Has fields in group
     106                                 */
     107                                foreach($group->fields as $field){
     108                                    $value = xprofile_get_field_data($field->id, $user->ID, 'array');
     109                                    echo("<field group=\"$groupName\" name=\"{$field->name}\">");
     110                                    $value = serialize($value);
     111                                    #$value = base64_encode($value);
     112                                    echo $value;
     113                                    echo("</field>");
     114                                }
     115                            }
     116                        }
     117                        echo("\t\t</xprofile>");
     118                    }
     119                   
    62120                }
    63121               
     
    83141                echo("<users version=\"". self::VERSION . "\">\n");
    84142               
     143                $this->exportMetadata   = isset($_GET['metadata']);
     144                $this->exportOptions    = isset($_GET['options']);
     145                $this->exportXProfile   = isset($_GET['xprofile']);
     146               
     147               
    85148                foreach($users as $user){
    86149                       
     
    96159        public function createPages(){
    97160            add_users_page("AEIOU - Panel", 'AEIOU', 'add_users', 'aeiou', array($this, 'thePage'));
    98            
    99161        }
    100162       
     
    172234            ?>
    173235            <form method="get" action="">
     236                <?php do_action('aeiou_before_export_form'); ?>
    174237                <div>
    175238                    <input type="hidden" name="page" value="aeiou" />
     
    185248                </div>
    186249                <div>
    187                     <input type="checkbox" name="metadata" id="aeiou_md" />
    188                     <label for="aeiou_md"><?php _e('Export users metadata', self::LANG_DOMAIN) ?></label>
     250                    <p>
     251                        <input type="checkbox" name="metadata" id="aeiou_md" />
     252                        <label for="aeiou_md"><?php _e('Export users metadata', self::LANG_DOMAIN) ?></label>
     253                    </p>
     254                    <p>
     255                        <input type="checkbox" name="options" id="aeiou_opt" />
     256                        <label for="aeiou_opt"><?php _e('Export users options', self::LANG_DOMAIN) ?></label>
     257                    </p>
    189258                </div>
     259                <?php
     260                if(class_exists('BuddyPress')){
     261                    /*
     262                     * New in 0.2 version (export Buddy Press XPorfile Data)
     263                     */
     264                    ?>
     265                    <div>
     266                        <p>
     267                            <input type="checkbox" name="xprofile" id="aeiou_xprofile" />
     268                            <label for="aeiou_xprofile"><?php _e('Include the BuddyPress Extended Profile data too', self::LANG_DOMAIN) ?></label>
     269                        </p>
     270                    </div>
     271                    <?php
     272                }
     273                ?>
     274                <?php do_action('aeiou_after_export_form'); ?>
    190275                <p>
    191                
    192276                    <input type="submit" class="primary" value="<?php _e("Export users", self::LANG_DOMAIN); ?>" />
    193277                </p>
     
    196280        }
    197281       
    198        
    199282        private function decodeFileError($errorCode){
    200283           
     
    205288            ?>
    206289            <form method="post" action="?page=aeiou&aeiou=import" enctype="multipart/form-data">
     290                <?php do_action('aeiou_before_import_form'); ?>
    207291                <div>
    208292                    <p>
     
    236320                    <input type="checkbox" name="metadata" id="aeiou_mdi" />
    237321                    <label for="aeiou_mdi"><?php _e('Import users metadata', self::LANG_DOMAIN); ?></label>
     322                </p>
     323                <p>
     324                    <input type="checkbox" name="options" id="aeiou_opti" />
     325                    <label for="aeiou_opti"><?php _e('Import users options', self::LANG_DOMAIN); ?></label>
     326                </p>
     327                <p>
     328                    <input type="checkbox" name="xprofile" id="aeiou_xp" />
     329                    <label for="aeiou_xp"><?php _e('Import BuddyPress XProfile user fields, data and groups', self::LANG_DOMAIN); ?></label>
    238330                </p>
    239331                <p>
     
    246338                    <input type="file" name="the_file" id="the_file" />
    247339                </p>
     340                <?php do_action('aeiou_after_import_form'); ?>
    248341                <p>
    249342                    <input type="submit" class="primary" value="<?php _e("Import users", self::LANG_DOMAIN); ?>" />
     
    251344            </form>
    252345            <pre class="output"><?php
    253            
    254                
    255            
     346               
    256347                if(isset($_GET['aeiou']) && $_GET['aeiou'] =='import'){
    257348                    /*
     
    266357                        $theFile = $_FILES['the_file'];
    267358                       
    268                         $verbose = isset($_POST['verbose']);
     359                        /*
     360                         * Setting the Class variables for import
     361                         */
     362                       
     363                        /*
     364                         * If the import should loud all it is doing
     365                         */
     366                        $this->verbose  = isset($_POST['verbose']);
     367                       
     368                        /*
     369                         * The behavior when a wordpress user is found
     370                         * it could be:
     371                         * - skip
     372                         * - replace
     373                         * - update
     374                         */
     375                        $this->exists           = isset($_POST['exists'])?$_POST['exists']:'skip';
     376                       
     377                       
     378                        $this->importMetadata   = isset($_POST['metadata']);
     379                        $this->importOptions    = isset($_POST['options']);
     380                        $this->importXProfile   = isset($_POST['xprofile']);
     381                       
    269382                       
    270383                        if($theFile['error'] != 0){
     
    283396                            $users = $parser->parse($theFile['tmp_name']);
    284397                           
    285                             $exists = isset($_POST['exists'])?$_POST['exists']:'skip';
    286                            
    287398                            foreach($users as $user){
     399                                /*
     400                                 * Increase the time limit up to 10 seconds per user
     401                                 */
     402                                set_time_limit(10);
     403                               
    288404                                $login = $user['user_login'];
    289405                                echo("\n<strong>$login</strong> ");
     
    292408                                $userID = 0;
    293409                               
    294                                 $userMeta = (isset($user['metadata'])) ? $user['metadata'] : array();
    295                                 if(isset($user['metadata'])) unset($user['metadata']);
    296                                
    297                                 if($usr && $exists != 'skip'){
     410                                $userMeta       = (isset($user['metadata']))    ? $user['metadata'] : array();
     411                                /*
     412                                 * v0.2: Added user options and xprofile import
     413                                 */
     414                                $userOptions    = (isset($user['options']))     ? $user['options'] : array();
     415                                $xprofile       = (isset($user['xprofile']))    ? $user['xprofile'] : array();
     416                               
     417                                /*
     418                                 * Cleanup extra keys
     419                                 */
     420                                if(isset($user['metadata']))    unset($user['metadata']);
     421                                if(isset($user['options']))     unset($user['options']);
     422                                if(isset($user['xprofile']))    unset($user['xprofile']);
     423                               
     424                                do_action('aeiou_before_import_user', $user);
     425                               
     426                                if($usr && $this->exists != 'skip'){
    298427                                    $usr = (array) $usr->data;
    299428                                   
    300                                     if($exists == 'update'){
     429                                    if($this->exists == 'update'){
    301430                                       
    302431                                        /*
     
    316445                                    if(count($user)>0){
    317446                                        $changes = array_keys($user);
    318                                         $changes = "<code>".implode("</code>,<code>", $changes)."</code>";
     447                                        $changes = "<code>".implode("</code>, <code>", $changes)."</code>";
    319448                                       
    320449                                        echo sprintf(__("has new %s value(s).", self::LANG_DOMAIN),$changes) . "\n";
    321450                                        $user['ID'] = $usr['ID'];
     451                                        /*
     452                                         * TODO: If password is defined in the XML file we should to import it
     453                                         */
    322454                                        wp_update_user($user);
    323455                                    }else{
     
    334466                                        $userID = wp_insert_user($user);
    335467                                        $user['ID'] = $userID;
     468                                        /*
     469                                         * TODO: If password is defined in the XML file we should to import it
     470                                         */
    336471                                        wp_update_user($user);
    337472                                        echo sprintf(__("inserted with ID %d", self::LANG_DOMAIN),$userID) . "\n";
     
    339474                                        echo __("skipped due it's already in the database", self::LANG_DOMAIN) . "\n";
    340475                                    }
    341                                        
    342476                                }
    343                                 if(isset($_POST['metadata']) && $userID != 0){
    344                                    
    345                                     /*
    346                                      * Admin asked for metadata import
    347                                     */
    348 
    349                                     $count = 0;
    350                                     foreach($userMeta as $key => $value){
    351                                         $metaKey = get_user_meta($userID, $key);
    352                                        
    353                                         if(empty($metaKey[0])) $metaKey = false;
    354                                         if(is_serialized($value)) $value = unserialize($value);
    355                                         if(!$metaKey || $exists=='replace'){
    356                                             delete_user_meta($userID, $key);
    357                                             if( !empty( $value ) ){
    358 
    359                                                 $count+=1;
    360                                                 if($verbose) echo sprintf(__("Metadata <code>%s</code> updated for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
    361 
    362                                                
    363                                                 update_user_meta($userID, $key, $value );
    364                                             }else{
    365                                                 if($metaKey && $verbose) echo sprintf(__("Metadata <code>%s</code> removed for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
    366                                             }
    367                                         }
    368                                     }
    369                                     if($count>0)
    370                                         echo sprintf(__("Updated %d metadatas for user <strong>%s</strong>", self::LANG_DOMAIN), $count, $login) . "\n";
    371                                    
     477                               
     478                                if($this->importMetadata && $userID != 0){
     479                                    do_action('aeiou_before_import_metadata', $user);
     480                                    $this->importUserMetaData($userMeta, $userID, $login);
    372481                                }
    373482                               
     483                                if($this->importOptions && $userID != 0){
     484                                    do_action('aeiou_before_import_options', $user);
     485                                    echo __("Importing user options\n", self::LANG_DOMAIN);
     486                                    $this->importUserOptions($userOptions, $userID, $login);
     487                                }
     488                               
     489                                if($this->importXProfile && $userID != 0 && class_exists('BP_XProfile_Component')){
     490                                    do_action('aeiou_before_import_xprofile', $user);
     491                                    echo __("Importing XProfile data\n", self::LANG_DOMAIN);
     492                                    $this->importXProfileData($xprofile, $userID);
     493                                }
     494                                do_action('aeiou_after_import_user', $user);
     495                               
    374496                            }
     497                           
    375498                           
    376499                        }
     
    381504            <?php
    382505        }
     506       
     507        private function importUserMetaData($userMeta, $userID, $login){
     508           
     509            /*
     510             * Admin asked for metadata import
     511            */
     512           
     513            $count = 0;
     514            foreach($userMeta as $key => $value){
     515                $metaKey = get_user_meta($userID, $key);
     516           
     517                if(empty($metaKey[0])) $metaKey = false;
     518                if(is_serialized($value)) $value = unserialize($value);
     519                if(!$metaKey || $this->exists=='replace'){
     520                    delete_user_meta($userID, $key);
     521                    if( !empty( $value ) ){
     522           
     523                        $count+=1;
     524                        if($this->verbose) echo sprintf(__("Metadata <code>%s</code> updated for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
     525           
     526                        update_user_meta($userID, $key, $value );
     527                    }else{
     528                        if($metaKey && $this->verbose) echo sprintf(__("Metadata <code>%s</code> removed for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
     529                    }
     530                }
     531            }
     532           
     533            if($count>0)
     534                echo sprintf(__("Updated %d metadatas for user <strong>%s</strong>", self::LANG_DOMAIN), $count, $login) . "\n";
     535           
     536        }
     537
     538        private function importUserOptions($userOptions, $userID, $login){
     539               
     540            /*
     541             * Admin asked for options import
     542            */
     543               
     544            $count = 0;
     545            foreach($userOptions as $key => $data){
     546               
     547                list($value, $global) = $data;
     548               
     549                $option = get_user_option( $key, $userID);
     550                   
     551                $optExists = !empty($option);
     552                if(is_serialized($option)) $option = unserialize($option);
     553                if(!$optExists || $this->exists=='replace'){
     554                    delete_user_option($userID, $key);
     555                    if( !empty( $value ) ){
     556                           
     557                        $count+=1;
     558                        if($this->verbose) echo sprintf(__("Option <code>%s</code> updated for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
     559                       
     560                       
     561                        update_user_option($userID, $key, $value, $global );
     562                    }else{
     563                        if($optExists && $this->verbose) echo sprintf(__("Option <code>%s</code> removed for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
     564                    }
     565                }
     566            }
     567               
     568            if($count>0)
     569                echo sprintf(__("Updated %d metadatas for user <strong>%s</strong>", self::LANG_DOMAIN), $count, $login) . "\n";
     570               
     571        }
     572       
     573       
     574       
     575        private function importXProfileData($xprofile, $userID){
     576            /*
     577             * Admin asked for xprofile import
     578            */
     579            $count = 0;
     580           
     581            foreach($xprofile as $name => $data){
     582                $value = $data[0];
     583                $group = $data[1];
     584               
     585                if($this->verbose) echo sprintf(__("Importing field <code>%s</code> of group <code>%s</code>", self::LANG_DOMAIN), $name, $group) . "\n";
     586               
     587                /*
     588                 * Get the XProfile Fields Group ID
     589                 * If it's not available the will be created as new.
     590                 */
     591                $groupId = $this->getXProfileGroupIDByName($group);
     592               
     593                /*
     594                 * Get the XProfile Field ID
     595                 * If it's not available the will be created as new.
     596                 */
     597                $fieldId = $this->getXProfileFieldIDByName($name, $groupId);
     598                $fieldData = is_serialized($value)? unserialize($value): $value;
     599                $oldValue = BP_XProfile_ProfileData::get_value_byid($fieldId, $userID);
     600                if(is_null($oldValue) || ($oldValue == '' && $this->exists == 'update') || ($this->exists == 'replace') ){
     601                    /*
     602                     * Saving data if not exists or need to be updated
     603                     */
     604                    $data = new BP_XProfile_ProfileData();
     605                    $data->field_id = $fieldId;
     606                    $data->user_id = $userID;
     607                    $data->value = is_serialized($value)? unserialize($value): $value;
     608                    $data->save();
     609                   
     610                }
     611            }
     612        }
     613        private function getXProfileFieldIDByName($name, $groupId = 0, $create = true){
     614            $out = BP_XProfile_Field::get_id_from_name($name);
     615            if(is_null($out) && $create){
     616               
     617                $field              = new BP_XProfile_Field();
     618                $field->name        = $name;
     619                $field->group_id    = $groupId;
     620                /*
     621                 *  TODO: Store in the XML the type.
     622                 *  Aactually i will create a generic text type if the field is not available.
     623                 */
     624                $field->type = "text";
     625                $out = $field->save();
     626            }
     627            return $out;
     628        }
     629       
     630        private function getXProfileGroupIDByName($name, $create = true){
     631            global $wpdb, $bp;
     632           
     633            $sql = "select id from {$bp->profile->table_name_groups} where name = %s";
     634            $sql = $wpdb->prepare($sql, $name);
     635            $out = $wpdb->get_var($sql);
     636           
     637            if( is_null( $out ) && $create ){
     638                $newGroup = new BP_XProfile_Group();
     639                $newGroup->name = $name;
     640                $out = $newGroup->save();
     641            }
     642            return $out;
     643           
     644        }
    383645    }
    384646   
  • aeiou/tags/0.2/parsers.php

    r648658 r654798  
    3636            $count +=1;
    3737            $md = array();
    38            
     38            $opt = array();
    3939            $user = $userArray->children( );
    4040            if(isset($user->metadata)){
     
    4545                    $key = (string) $metaArr['@attributes']['key'];
    4646                    $value = (string) $meta;
     47                    /*
     48                     * Make differences between options and settings
     49                     */
     50                    if ( isset($metaArr['@attributes']['option']) ) {
     51                        $isGlobalOption = ((string)$metaArr['@attributes']['global']) == 'true';
     52                        $isOption       = true;
     53                        $opt[$key] = array(base64_decode( $value ), $isGlobalOption);
     54                    }else{
     55                        $isOption       = false;
     56                        $md[$key] =base64_decode( $value );
     57                    }
    4758                   
    48                     $md[$key] =base64_decode( $value );
     59                }
     60               
     61            }
     62            /*
     63             * Since ver. 0.2 (xprofile data extraction)
     64             */
     65            $xp = array();
     66            if(isset($user->xprofile)){
     67               
     68                foreach($user->xprofile->field as $xField){
     69                       
     70                    $fieldArr = (array)$xField;
     71                    $name = (string) $fieldArr['@attributes']['name'];
     72                    $value = (string) $xField;
     73                    $group = (string) $fieldArr['@attributes']['group'];
     74                   
     75                    $xp[$name] = array( base64_decode( $value ), $group );
     76                   
    4977                }
    5078               
    5179            }
    5280           
    53             $users[] = array(
     81            $userRow = array(
    5482               
    5583                'user_login'            => (string) $user->user_login,
     
    6290                'user_status'           => (string) $user->user_status,
    6391                'display_name'          => (string) $user->display_name,
    64                 'metadata'              => $md
     92                'metadata'              => $md,
     93                'options'               => $opt,
     94                'xprofile'              => $xp,
    6595            );
     96            /*
     97             * Since version 0.2 (can extend import object threating further informations from xml file)
     98             */
     99            $userRow = apply_filters('aeiou_filter_row', $userRow, $user);
     100           
     101            if(is_array($userRow))
     102                $users[] = $userRow;
    66103        }
    67104       
  • aeiou/tags/0.2/readme.txt

    r648658 r654798  
    22Contributors: tosend.it
    33Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CRB85JPKCSNZ6
    4 Tags: user import, export, metadata
     4Tags: user, users, import, export, metadata, settings, options, BuddyPress, xprofile, Extended Profile, fields
    55Requires at least: 3.4
    66Tested up to: 3.5
    7 Stable tag: 0.1
     7Stable tag: 0.2
    88License: GPLv2
    99
     
    1212== Description ==
    1313
    14 AEIOU is a powerfull plugin that export all wordpress users info into an XML file including user metadatas and allow to import the same xml file into a new wordpress keeping the metadata of each user.
     14AEIOU is a powerfull plugin that export all wordpress users info into an XML file including user metadatas and allow to import the same xml file into a new wordpress keeping the metadata and Buddypress XProfile data of each user.
    1515
    1616**Note:** AEIOU requires SimpleXML PHP library to import data. Nothing more than wordpress is required for the export.
     
    1818== Installation ==
    19191. Install AEIOU either via the WordPress.org plugin directory, or by uploading the files to your server
    20 1. That's it. You're ready to export!
     201. That's it. You're ready to export/import users!
    2121
    2222
     
    2424
    2525It's too simple to do what the plugin does... However, have you got a question? Please ask us on forum.
     26
     27
     28= Why my imported XProfile fields becomes all of text type? =
     29
     30This is because the importer does not keep any information about the field except than Field Name, Field Group and Field Value.
     31Any other information is left behind. So the better way to make an import that grant the exact structure for xprofile field is
     32to create the XProfile Fields on the new Wordpress instance before the import is done then make the import.
     33
     34= Why the user are unable to login after I done the Import? =
     35
     36If you have imported the password field too then It was changed and you have to ensure that the same secret key is used in both old and new wordpress.
    2637
    2738== Screenshots ==
     
    3142== Changelog ==
    3243
     44= 0.2 (2013-01-18) =
     45* **New:** Now you can export and import BuddyPress XProfile data too. If fields or group does not exists they will be created and the field will become of generic type `text`
     46* **New:** Added action aeiou_before_import_user
     47* **New:** Added action aeiou_before_import_metadata
     48* **New:** Added action aeiou_before_import_xprofile
     49* **New:** Added action aeiou_after_import_user
     50* **New:** Added filter aeiou_filter_row
     51* **New:** Added action aeiou_before_import_form
     52* **New:** Added action aeiou_after_import_form
     53* **New:** Added action aeiou_before_export_form
     54* **New:** Added action aeiou_after_export_form
     55* **New:** Options exported with metadata
     56* **New:** Impoter allow import of options too
     57* **Update:** Better code organization.
     58* **Update:** Updated Italian translation file.
     59* **Update:** Updated translation template file.
     60* **Update:** Added FAQ to this page.
     61* **Bugfix:** Increased code execution timeout.
     62
    3363= 0.1 (2013-01-05) =
    3464* First plugin release.
  • aeiou/trunk/index.php

    r648658 r654798  
    33Plugin Name: AEIOU - Advanced Export/Import (WordPress) Object Users
    44Plugin URI: http://wordpress.org/extend/plugins/aeiou/
    5 Description: AEIOU is a powerfull plugin that export all wordpress users info into an XML file including user metadatas and allow to import the same xml file into a new wordpress keeping the metadata of each user.
     5Description: AEIOU is a powerfull plugin that export all wordpress users info into an XML file including user metadatas and allow to import the same xml file into a new wordpress keeping the metadata of each user. The plugin is able to export buddypress xprofile data too.
    66Author: toSend.it di Luisa Marra
    7 Version: 0.1
     7Version: 0.2
    88Author URI: http://tosend.it
    99*/
     
    1111if(!class_exists('AEIOU')){
    1212    class AEIOU{
    13         const VERSION = '0.1';
     13        const VERSION = '0.2';
    1414        const LANG_DOMAIN = 'AEIOU';
     15       
     16        private $verbose;
     17        private $exists;
     18        private $importMetadata;
     19        private $importOptions;
     20        private $importXProfile;
     21       
     22        private $exportMetadata;
     23        private $exportOptions;
     24        private $exportXProfile;
    1525       
    1626        public function __construct(){
    1727            add_action('admin_init', array($this , 'init'));
    1828            add_action('admin_menu', array($this, 'createPages'));
    19             #add_action('request', array($this, 'exportData'));
     29           
    2030        }
    2131
     
    4353                echo("\t\t<display_name><![CDATA[{$user->display_name}]]></display_name>\n");
    4454               
    45                 if(isset($_GET['metadata'])){
     55                if($this->exportMetadata || $this->exportOptions){
    4656                    /*
    4757                     * I should export metadata (as requested by user)
    4858                    */
    4959                    echo("\t\t<metadata>");
    50                        
     60                    global $wpdb;
    5161                    $umeta = get_user_meta($user->ID);
    5262                    foreach($umeta as $key => $value){
     63                        $isOption = preg_match("#^" . preg_quote($wpdb->prefix) ."#", $key);
     64                        if( ($isOption && $this->exportOptions) || (!$isOption && $this->exportMetadata) ){
    5365                           
    54                         echo("\t\t\t<meta key=\"$key\"><![CDATA[");
    55                         $value = $value[0];
    56                         echo base64_encode($value);
    57                         echo("]]></meta>\n");
     66                            echo("\t\t\t<meta ");
     67                           
     68                            if(preg_match("#^" . preg_quote($wpdb->prefix) ."#", $key)){
     69                                /*
     70                                 * If the value is an option i should remove the db prefix from the option
     71                                 * name and add the attribute option to allow the importer to understand
     72                                 * when a node is an option or a setting.
     73                                 */
     74                                echo('option="true" ');
     75                                echo('global="false" ');
     76                                $key =substr($key, strlen($wpdb->prefix));
     77                            }
     78                            echo("key=\"$key\"><![CDATA[");
     79                            $value = $value[0];
     80                            echo base64_encode($value);
     81                            echo("]]></meta>\n");
     82                        }
    5883                       
    5984                    }
    6085                       
    6186                    echo("\t\t</metadata>");
     87                }
     88                if( class_exists('BuddyPress') ){
     89                    if(class_exists('BP_XProfile_Group') && $this->exportXProfile ){
     90                        /*
     91                         * I should export xprofile data too.
     92                         */
     93                        $groups = BP_XProfile_Group::get( array(
     94                            'fetch_fields' => true
     95                        ) );
     96                        /*
     97                         * Fetching all groups
     98                         */
     99                        echo("\t\t<xprofile>");
     100                        foreach($groups as $group){
     101                            $groupName = $group->name;
     102                            if(!empty($group->fields)){
     103                               
     104                                /*
     105                                 * Has fields in group
     106                                 */
     107                                foreach($group->fields as $field){
     108                                    $value = xprofile_get_field_data($field->id, $user->ID, 'array');
     109                                    echo("<field group=\"$groupName\" name=\"{$field->name}\">");
     110                                    $value = serialize($value);
     111                                    #$value = base64_encode($value);
     112                                    echo $value;
     113                                    echo("</field>");
     114                                }
     115                            }
     116                        }
     117                        echo("\t\t</xprofile>");
     118                    }
     119                   
    62120                }
    63121               
     
    83141                echo("<users version=\"". self::VERSION . "\">\n");
    84142               
     143                $this->exportMetadata   = isset($_GET['metadata']);
     144                $this->exportOptions    = isset($_GET['options']);
     145                $this->exportXProfile   = isset($_GET['xprofile']);
     146               
     147               
    85148                foreach($users as $user){
    86149                       
     
    96159        public function createPages(){
    97160            add_users_page("AEIOU - Panel", 'AEIOU', 'add_users', 'aeiou', array($this, 'thePage'));
    98            
    99161        }
    100162       
     
    172234            ?>
    173235            <form method="get" action="">
     236                <?php do_action('aeiou_before_export_form'); ?>
    174237                <div>
    175238                    <input type="hidden" name="page" value="aeiou" />
     
    185248                </div>
    186249                <div>
    187                     <input type="checkbox" name="metadata" id="aeiou_md" />
    188                     <label for="aeiou_md"><?php _e('Export users metadata', self::LANG_DOMAIN) ?></label>
     250                    <p>
     251                        <input type="checkbox" name="metadata" id="aeiou_md" />
     252                        <label for="aeiou_md"><?php _e('Export users metadata', self::LANG_DOMAIN) ?></label>
     253                    </p>
     254                    <p>
     255                        <input type="checkbox" name="options" id="aeiou_opt" />
     256                        <label for="aeiou_opt"><?php _e('Export users options', self::LANG_DOMAIN) ?></label>
     257                    </p>
    189258                </div>
     259                <?php
     260                if(class_exists('BuddyPress')){
     261                    /*
     262                     * New in 0.2 version (export Buddy Press XPorfile Data)
     263                     */
     264                    ?>
     265                    <div>
     266                        <p>
     267                            <input type="checkbox" name="xprofile" id="aeiou_xprofile" />
     268                            <label for="aeiou_xprofile"><?php _e('Include the BuddyPress Extended Profile data too', self::LANG_DOMAIN) ?></label>
     269                        </p>
     270                    </div>
     271                    <?php
     272                }
     273                ?>
     274                <?php do_action('aeiou_after_export_form'); ?>
    190275                <p>
    191                
    192276                    <input type="submit" class="primary" value="<?php _e("Export users", self::LANG_DOMAIN); ?>" />
    193277                </p>
     
    196280        }
    197281       
    198        
    199282        private function decodeFileError($errorCode){
    200283           
     
    205288            ?>
    206289            <form method="post" action="?page=aeiou&aeiou=import" enctype="multipart/form-data">
     290                <?php do_action('aeiou_before_import_form'); ?>
    207291                <div>
    208292                    <p>
     
    236320                    <input type="checkbox" name="metadata" id="aeiou_mdi" />
    237321                    <label for="aeiou_mdi"><?php _e('Import users metadata', self::LANG_DOMAIN); ?></label>
     322                </p>
     323                <p>
     324                    <input type="checkbox" name="options" id="aeiou_opti" />
     325                    <label for="aeiou_opti"><?php _e('Import users options', self::LANG_DOMAIN); ?></label>
     326                </p>
     327                <p>
     328                    <input type="checkbox" name="xprofile" id="aeiou_xp" />
     329                    <label for="aeiou_xp"><?php _e('Import BuddyPress XProfile user fields, data and groups', self::LANG_DOMAIN); ?></label>
    238330                </p>
    239331                <p>
     
    246338                    <input type="file" name="the_file" id="the_file" />
    247339                </p>
     340                <?php do_action('aeiou_after_import_form'); ?>
    248341                <p>
    249342                    <input type="submit" class="primary" value="<?php _e("Import users", self::LANG_DOMAIN); ?>" />
     
    251344            </form>
    252345            <pre class="output"><?php
    253            
    254                
    255            
     346               
    256347                if(isset($_GET['aeiou']) && $_GET['aeiou'] =='import'){
    257348                    /*
     
    266357                        $theFile = $_FILES['the_file'];
    267358                       
    268                         $verbose = isset($_POST['verbose']);
     359                        /*
     360                         * Setting the Class variables for import
     361                         */
     362                       
     363                        /*
     364                         * If the import should loud all it is doing
     365                         */
     366                        $this->verbose  = isset($_POST['verbose']);
     367                       
     368                        /*
     369                         * The behavior when a wordpress user is found
     370                         * it could be:
     371                         * - skip
     372                         * - replace
     373                         * - update
     374                         */
     375                        $this->exists           = isset($_POST['exists'])?$_POST['exists']:'skip';
     376                       
     377                       
     378                        $this->importMetadata   = isset($_POST['metadata']);
     379                        $this->importOptions    = isset($_POST['options']);
     380                        $this->importXProfile   = isset($_POST['xprofile']);
     381                       
    269382                       
    270383                        if($theFile['error'] != 0){
     
    283396                            $users = $parser->parse($theFile['tmp_name']);
    284397                           
    285                             $exists = isset($_POST['exists'])?$_POST['exists']:'skip';
    286                            
    287398                            foreach($users as $user){
     399                                /*
     400                                 * Increase the time limit up to 10 seconds per user
     401                                 */
     402                                set_time_limit(10);
     403                               
    288404                                $login = $user['user_login'];
    289405                                echo("\n<strong>$login</strong> ");
     
    292408                                $userID = 0;
    293409                               
    294                                 $userMeta = (isset($user['metadata'])) ? $user['metadata'] : array();
    295                                 if(isset($user['metadata'])) unset($user['metadata']);
    296                                
    297                                 if($usr && $exists != 'skip'){
     410                                $userMeta       = (isset($user['metadata']))    ? $user['metadata'] : array();
     411                                /*
     412                                 * v0.2: Added user options and xprofile import
     413                                 */
     414                                $userOptions    = (isset($user['options']))     ? $user['options'] : array();
     415                                $xprofile       = (isset($user['xprofile']))    ? $user['xprofile'] : array();
     416                               
     417                                /*
     418                                 * Cleanup extra keys
     419                                 */
     420                                if(isset($user['metadata']))    unset($user['metadata']);
     421                                if(isset($user['options']))     unset($user['options']);
     422                                if(isset($user['xprofile']))    unset($user['xprofile']);
     423                               
     424                                do_action('aeiou_before_import_user', $user);
     425                               
     426                                if($usr && $this->exists != 'skip'){
    298427                                    $usr = (array) $usr->data;
    299428                                   
    300                                     if($exists == 'update'){
     429                                    if($this->exists == 'update'){
    301430                                       
    302431                                        /*
     
    316445                                    if(count($user)>0){
    317446                                        $changes = array_keys($user);
    318                                         $changes = "<code>".implode("</code>,<code>", $changes)."</code>";
     447                                        $changes = "<code>".implode("</code>, <code>", $changes)."</code>";
    319448                                       
    320449                                        echo sprintf(__("has new %s value(s).", self::LANG_DOMAIN),$changes) . "\n";
    321450                                        $user['ID'] = $usr['ID'];
     451                                        /*
     452                                         * TODO: If password is defined in the XML file we should to import it
     453                                         */
    322454                                        wp_update_user($user);
    323455                                    }else{
     
    334466                                        $userID = wp_insert_user($user);
    335467                                        $user['ID'] = $userID;
     468                                        /*
     469                                         * TODO: If password is defined in the XML file we should to import it
     470                                         */
    336471                                        wp_update_user($user);
    337472                                        echo sprintf(__("inserted with ID %d", self::LANG_DOMAIN),$userID) . "\n";
     
    339474                                        echo __("skipped due it's already in the database", self::LANG_DOMAIN) . "\n";
    340475                                    }
    341                                        
    342476                                }
    343                                 if(isset($_POST['metadata']) && $userID != 0){
    344                                    
    345                                     /*
    346                                      * Admin asked for metadata import
    347                                     */
    348 
    349                                     $count = 0;
    350                                     foreach($userMeta as $key => $value){
    351                                         $metaKey = get_user_meta($userID, $key);
    352                                        
    353                                         if(empty($metaKey[0])) $metaKey = false;
    354                                         if(is_serialized($value)) $value = unserialize($value);
    355                                         if(!$metaKey || $exists=='replace'){
    356                                             delete_user_meta($userID, $key);
    357                                             if( !empty( $value ) ){
    358 
    359                                                 $count+=1;
    360                                                 if($verbose) echo sprintf(__("Metadata <code>%s</code> updated for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
    361 
    362                                                
    363                                                 update_user_meta($userID, $key, $value );
    364                                             }else{
    365                                                 if($metaKey && $verbose) echo sprintf(__("Metadata <code>%s</code> removed for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
    366                                             }
    367                                         }
    368                                     }
    369                                     if($count>0)
    370                                         echo sprintf(__("Updated %d metadatas for user <strong>%s</strong>", self::LANG_DOMAIN), $count, $login) . "\n";
    371                                    
     477                               
     478                                if($this->importMetadata && $userID != 0){
     479                                    do_action('aeiou_before_import_metadata', $user);
     480                                    $this->importUserMetaData($userMeta, $userID, $login);
    372481                                }
    373482                               
     483                                if($this->importOptions && $userID != 0){
     484                                    do_action('aeiou_before_import_options', $user);
     485                                    echo __("Importing user options\n", self::LANG_DOMAIN);
     486                                    $this->importUserOptions($userOptions, $userID, $login);
     487                                }
     488                               
     489                                if($this->importXProfile && $userID != 0 && class_exists('BP_XProfile_Component')){
     490                                    do_action('aeiou_before_import_xprofile', $user);
     491                                    echo __("Importing XProfile data\n", self::LANG_DOMAIN);
     492                                    $this->importXProfileData($xprofile, $userID);
     493                                }
     494                                do_action('aeiou_after_import_user', $user);
     495                               
    374496                            }
     497                           
    375498                           
    376499                        }
     
    381504            <?php
    382505        }
     506       
     507        private function importUserMetaData($userMeta, $userID, $login){
     508           
     509            /*
     510             * Admin asked for metadata import
     511            */
     512           
     513            $count = 0;
     514            foreach($userMeta as $key => $value){
     515                $metaKey = get_user_meta($userID, $key);
     516           
     517                if(empty($metaKey[0])) $metaKey = false;
     518                if(is_serialized($value)) $value = unserialize($value);
     519                if(!$metaKey || $this->exists=='replace'){
     520                    delete_user_meta($userID, $key);
     521                    if( !empty( $value ) ){
     522           
     523                        $count+=1;
     524                        if($this->verbose) echo sprintf(__("Metadata <code>%s</code> updated for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
     525           
     526                        update_user_meta($userID, $key, $value );
     527                    }else{
     528                        if($metaKey && $this->verbose) echo sprintf(__("Metadata <code>%s</code> removed for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
     529                    }
     530                }
     531            }
     532           
     533            if($count>0)
     534                echo sprintf(__("Updated %d metadatas for user <strong>%s</strong>", self::LANG_DOMAIN), $count, $login) . "\n";
     535           
     536        }
     537
     538        private function importUserOptions($userOptions, $userID, $login){
     539               
     540            /*
     541             * Admin asked for options import
     542            */
     543               
     544            $count = 0;
     545            foreach($userOptions as $key => $data){
     546               
     547                list($value, $global) = $data;
     548               
     549                $option = get_user_option( $key, $userID);
     550                   
     551                $optExists = !empty($option);
     552                if(is_serialized($option)) $option = unserialize($option);
     553                if(!$optExists || $this->exists=='replace'){
     554                    delete_user_option($userID, $key);
     555                    if( !empty( $value ) ){
     556                           
     557                        $count+=1;
     558                        if($this->verbose) echo sprintf(__("Option <code>%s</code> updated for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
     559                       
     560                       
     561                        update_user_option($userID, $key, $value, $global );
     562                    }else{
     563                        if($optExists && $this->verbose) echo sprintf(__("Option <code>%s</code> removed for user <strong>%s</strong>", self::LANG_DOMAIN), $key, $login) . "\n";
     564                    }
     565                }
     566            }
     567               
     568            if($count>0)
     569                echo sprintf(__("Updated %d metadatas for user <strong>%s</strong>", self::LANG_DOMAIN), $count, $login) . "\n";
     570               
     571        }
     572       
     573       
     574       
     575        private function importXProfileData($xprofile, $userID){
     576            /*
     577             * Admin asked for xprofile import
     578            */
     579            $count = 0;
     580           
     581            foreach($xprofile as $name => $data){
     582                $value = $data[0];
     583                $group = $data[1];
     584               
     585                if($this->verbose) echo sprintf(__("Importing field <code>%s</code> of group <code>%s</code>", self::LANG_DOMAIN), $name, $group) . "\n";
     586               
     587                /*
     588                 * Get the XProfile Fields Group ID
     589                 * If it's not available the will be created as new.
     590                 */
     591                $groupId = $this->getXProfileGroupIDByName($group);
     592               
     593                /*
     594                 * Get the XProfile Field ID
     595                 * If it's not available the will be created as new.
     596                 */
     597                $fieldId = $this->getXProfileFieldIDByName($name, $groupId);
     598                $fieldData = is_serialized($value)? unserialize($value): $value;
     599                $oldValue = BP_XProfile_ProfileData::get_value_byid($fieldId, $userID);
     600                if(is_null($oldValue) || ($oldValue == '' && $this->exists == 'update') || ($this->exists == 'replace') ){
     601                    /*
     602                     * Saving data if not exists or need to be updated
     603                     */
     604                    $data = new BP_XProfile_ProfileData();
     605                    $data->field_id = $fieldId;
     606                    $data->user_id = $userID;
     607                    $data->value = is_serialized($value)? unserialize($value): $value;
     608                    $data->save();
     609                   
     610                }
     611            }
     612        }
     613        private function getXProfileFieldIDByName($name, $groupId = 0, $create = true){
     614            $out = BP_XProfile_Field::get_id_from_name($name);
     615            if(is_null($out) && $create){
     616               
     617                $field              = new BP_XProfile_Field();
     618                $field->name        = $name;
     619                $field->group_id    = $groupId;
     620                /*
     621                 *  TODO: Store in the XML the type.
     622                 *  Aactually i will create a generic text type if the field is not available.
     623                 */
     624                $field->type = "text";
     625                $out = $field->save();
     626            }
     627            return $out;
     628        }
     629       
     630        private function getXProfileGroupIDByName($name, $create = true){
     631            global $wpdb, $bp;
     632           
     633            $sql = "select id from {$bp->profile->table_name_groups} where name = %s";
     634            $sql = $wpdb->prepare($sql, $name);
     635            $out = $wpdb->get_var($sql);
     636           
     637            if( is_null( $out ) && $create ){
     638                $newGroup = new BP_XProfile_Group();
     639                $newGroup->name = $name;
     640                $out = $newGroup->save();
     641            }
     642            return $out;
     643           
     644        }
    383645    }
    384646   
  • aeiou/trunk/parsers.php

    r648658 r654798  
    3636            $count +=1;
    3737            $md = array();
    38            
     38            $opt = array();
    3939            $user = $userArray->children( );
    4040            if(isset($user->metadata)){
     
    4545                    $key = (string) $metaArr['@attributes']['key'];
    4646                    $value = (string) $meta;
     47                    /*
     48                     * Make differences between options and settings
     49                     */
     50                    if ( isset($metaArr['@attributes']['option']) ) {
     51                        $isGlobalOption = ((string)$metaArr['@attributes']['global']) == 'true';
     52                        $isOption       = true;
     53                        $opt[$key] = array(base64_decode( $value ), $isGlobalOption);
     54                    }else{
     55                        $isOption       = false;
     56                        $md[$key] =base64_decode( $value );
     57                    }
    4758                   
    48                     $md[$key] =base64_decode( $value );
     59                }
     60               
     61            }
     62            /*
     63             * Since ver. 0.2 (xprofile data extraction)
     64             */
     65            $xp = array();
     66            if(isset($user->xprofile)){
     67               
     68                foreach($user->xprofile->field as $xField){
     69                       
     70                    $fieldArr = (array)$xField;
     71                    $name = (string) $fieldArr['@attributes']['name'];
     72                    $value = (string) $xField;
     73                    $group = (string) $fieldArr['@attributes']['group'];
     74                   
     75                    $xp[$name] = array( base64_decode( $value ), $group );
     76                   
    4977                }
    5078               
    5179            }
    5280           
    53             $users[] = array(
     81            $userRow = array(
    5482               
    5583                'user_login'            => (string) $user->user_login,
     
    6290                'user_status'           => (string) $user->user_status,
    6391                'display_name'          => (string) $user->display_name,
    64                 'metadata'              => $md
     92                'metadata'              => $md,
     93                'options'               => $opt,
     94                'xprofile'              => $xp,
    6595            );
     96            /*
     97             * Since version 0.2 (can extend import object threating further informations from xml file)
     98             */
     99            $userRow = apply_filters('aeiou_filter_row', $userRow, $user);
     100           
     101            if(is_array($userRow))
     102                $users[] = $userRow;
    66103        }
    67104       
  • aeiou/trunk/readme.txt

    r648658 r654798  
    22Contributors: tosend.it
    33Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=CRB85JPKCSNZ6
    4 Tags: user import, export, metadata
     4Tags: user, users, import, export, metadata, settings, options, BuddyPress, xprofile, Extended Profile, fields
    55Requires at least: 3.4
    66Tested up to: 3.5
    7 Stable tag: 0.1
     7Stable tag: 0.2
    88License: GPLv2
    99
     
    1212== Description ==
    1313
    14 AEIOU is a powerfull plugin that export all wordpress users info into an XML file including user metadatas and allow to import the same xml file into a new wordpress keeping the metadata of each user.
     14AEIOU is a powerfull plugin that export all wordpress users info into an XML file including user metadatas and allow to import the same xml file into a new wordpress keeping the metadata and Buddypress XProfile data of each user.
    1515
    1616**Note:** AEIOU requires SimpleXML PHP library to import data. Nothing more than wordpress is required for the export.
     
    1818== Installation ==
    19191. Install AEIOU either via the WordPress.org plugin directory, or by uploading the files to your server
    20 1. That's it. You're ready to export!
     201. That's it. You're ready to export/import users!
    2121
    2222
     
    2424
    2525It's too simple to do what the plugin does... However, have you got a question? Please ask us on forum.
     26
     27
     28= Why my imported XProfile fields becomes all of text type? =
     29
     30This is because the importer does not keep any information about the field except than Field Name, Field Group and Field Value.
     31Any other information is left behind. So the better way to make an import that grant the exact structure for xprofile field is
     32to create the XProfile Fields on the new Wordpress instance before the import is done then make the import.
     33
     34= Why the user are unable to login after I done the Import? =
     35
     36If you have imported the password field too then It was changed and you have to ensure that the same secret key is used in both old and new wordpress.
    2637
    2738== Screenshots ==
     
    3142== Changelog ==
    3243
     44= 0.2 (2013-01-18) =
     45* **New:** Now you can export and import BuddyPress XProfile data too. If fields or group does not exists they will be created and the field will become of generic type `text`
     46* **New:** Added action aeiou_before_import_user
     47* **New:** Added action aeiou_before_import_metadata
     48* **New:** Added action aeiou_before_import_xprofile
     49* **New:** Added action aeiou_after_import_user
     50* **New:** Added filter aeiou_filter_row
     51* **New:** Added action aeiou_before_import_form
     52* **New:** Added action aeiou_after_import_form
     53* **New:** Added action aeiou_before_export_form
     54* **New:** Added action aeiou_after_export_form
     55* **New:** Options exported with metadata
     56* **New:** Impoter allow import of options too
     57* **Update:** Better code organization.
     58* **Update:** Updated Italian translation file.
     59* **Update:** Updated translation template file.
     60* **Update:** Added FAQ to this page.
     61* **Bugfix:** Increased code execution timeout.
     62
    3363= 0.1 (2013-01-05) =
    3464* First plugin release.
Note: See TracChangeset for help on using the changeset viewer.