/**
  * Import user.
  *
  * @param User $user
  *
  * @todo Here we are assuming that there cannot be two users with the
  * same user_login. This might be wrong. Investigate!
  * Consider using WP function get_user_by.
  *
  * @see http://codex.wordpress.org/Function_Reference/get_user_by
  */
 public function import_user(User $user)
 {
     // Database table base prefix for production and content stage.
     $prod_prefix = $this->custom_dao->get_table_base_prefix();
     $stage_prefix = $this->batch->get_custom_data('sme_table_base_prefix');
     // Change database table base prefix from content staging prefix to
     // production prefix.
     $meta = array_map(function ($record) use($stage_prefix, $prod_prefix) {
         if (isset($record['meta_key']) && strpos($record['meta_key'], $stage_prefix) === 0) {
             $record['meta_key'] = substr_replace($record['meta_key'], $prod_prefix, 0, strlen($stage_prefix));
         }
         return $record;
     }, $user->get_meta());
     // Update user with new meta.
     $user->set_meta($meta);
     // See if user exists in database.
     $existing = $this->user_dao->get_user_by_user_login($user->get_login());
     // Create if user does not exist, update otherwise.
     if (empty($existing)) {
         $this->user_dao->insert($user);
     } else {
         $user->set_id($existing->get_id());
         $this->user_dao->update_user($user);
     }
 }
 /**
  * Update user meta.
  *
  * @param User $user
  */
 public function update_all_user_meta(User $user)
 {
     $insert_keys = array();
     $stage_keys = array();
     $insert = array();
     $update = array();
     $delete = array();
     $stage_records = $user->get_meta();
     $prod_records = $this->get_user_meta($user->get_id());
     /*
      * Go through each meta record we got from stage. If a meta_key exists
      * more then once, then we will not try to update any records with that
      * meta key.
      */
     foreach ($stage_records as $key => $prod_record) {
         if (in_array($prod_record['meta_key'], $stage_keys)) {
             $insert[] = $prod_record;
             $insert_keys[] = $prod_record['meta_key'];
             unset($stage_records[$key]);
         } else {
             $stage_keys[] = $prod_record['meta_key'];
         }
     }
     /*
      * Go through each meta record we got from production. If a meta_key
      * exist that is already part of the keys scheduled for insertion or if a
      * key that is found that is not part of the keys from stage, then
      * schedule that record for deletion.
      *
      * Records left in $stage_records is candidates for being updated. Go
      * through them and see if they already exist in $prod_records.
      */
     foreach ($prod_records as $prod_key => $prod_record) {
         if (!in_array($prod_record['meta_key'], $stage_keys) || in_array($prod_record['meta_key'], $insert_keys)) {
             $delete[] = $prod_record;
             unset($prod_records[$prod_key]);
         } else {
             foreach ($stage_records as $stage_key => $stage_record) {
                 if ($stage_record['meta_key'] == $prod_record['meta_key']) {
                     $stage_record['user_id'] = $prod_record['user_id'];
                     $stage_record['umeta_id'] = $prod_record['umeta_id'];
                     $update[] = $stage_record;
                     unset($stage_records[$stage_key]);
                     unset($prod_records[$prod_key]);
                 }
             }
         }
     }
     // Records left in $stage_records should be inserted.
     foreach ($stage_records as $record) {
         $insert[] = $record;
     }
     // Records left in $prod_records should be deleted.
     foreach ($prod_records as $record) {
         $delete[] = $record;
     }
     foreach ($delete as $record) {
         $this->delete_user_meta($record['umeta_id']);
     }
     foreach ($insert as $record) {
         $this->insert_user_meta($record);
     }
     foreach ($update as $record) {
         $this->update_user_meta($record);
     }
 }