 bakery.module | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/bakery.module b/bakery.module
index 72a41bc..c01168e 100644
--- a/bakery.module
+++ b/bakery.module
@@ -137,6 +137,11 @@ function bakery_user_logout($account) {
  */
 function bakery_user_presave(&$edit, $account, $category) {
   if (variable_get('bakery_is_master', 0)) {
+    // Invoke implementations of Bakery transmit hook for syncing arbitrary data.
+    $transmit = module_invoke_all('bakery_transmit', $account);
+    if (!empty($transmit)) {
+      $_SESSION['bakery'] = $transmit;
+    }    
     // We store email/name if they changed. We want to wait with doing
     // anything else until the changes are saved locally.
     foreach (variable_get('bakery_supported_fields', array('mail' => 'mail', 'name' => 'name')) as $type => $enabled) {
@@ -1004,7 +1009,7 @@ function bakery_taste_stroopwafel_cookie() {
 
     if ($signature == $cookie['signature'] && $cookie['timestamp'] + variable_get('bakery_freshness', '3600') >= $_SERVER['REQUEST_TIME']) {
       $valid = TRUE;
-      $_SESSION['bakery'] = unserialize($cookie['data']);
+      $_SESSION['bakery']['data'] = unserialize($cookie['data']);
       $_SESSION['bakery']['uid'] = $cookie['uid'];
       $_SESSION['bakery']['category'] = $cookie['category'];
     }
@@ -1119,24 +1124,26 @@ function bakery_eat_stroopwafel_cookie() {
       if ($value) {
         // If the field is set in the cookie it's being updated, otherwise we'll
         // populate $fields with the existing values so nothing is lost.
-        if (isset($stroopwafel[$type])) {
-          $fields[$type] = $stroopwafel[$type];
-        }
-        else {
-          $fields[$type] = $account->$type;
-        }
+        $fields[$type] = isset($stroopwafel['data'][$type]) ? $stroopwafel['data'][$type] : $account->$type;
+        // Unset this value since we've put it into $fields.
+        unset($stroopwafel['data'][$type]);
       }
     }
     $status = user_save($account, $fields);
     if ($status === FALSE) {
-      watchdog('bakery', 'User update from name %name_old to %name_new, mail %mail_old to %mail_new failed.', array('%name_old' => $account->name, '%name_new' => $stroopwafel['name'], '%mail_old' => $account->mail, '%mail_new' => $stroopwafel['mail']), WATCHDOG_ERROR);
+      watchdog('bakery', 'User update from name %name_old to %name_new, mail %mail_old to %mail_new failed.', array('%name_old' => $account->name, '%name_new' => $fields['name'], '%mail_old' => $account->mail, '%mail_new' => $fields['mail']), WATCHDOG_ERROR);
       $message = t('There was a problem updating your account on %slave. Please contact the administrator.', array('%slave' => variable_get('site_name', '')));
       header('HTTP/1.1 409 Conflict');
     }
     else {
-      watchdog('bakery', 'user updated name %name_old to %name_new, mail %mail_old to %mail_new.', array('%name_old' => $account->name, '%name_new' => $stroopwafel['name'], '%mail_old' => $account->mail, '%mail_new' => $stroopwafel['mail']));
+      watchdog('bakery', 'user updated name %name_old to %name_new, mail %mail_old to %mail_new.', array('%name_old' => $account->name, '%name_new' => $fields['name'], '%mail_old' => $account->mail, '%mail_new' => $fields['mail']));
       $message = t('Successfully updated account on %slave.', array('%slave' => variable_get('site_name', '')));
     }
+    
+    if (!empty($stroopwafel['data'])) {
+      // More data to sync, invoke Bakery receive hook.
+      module_invoke_all('bakery_receive', $account, $stroopwafel['data']);
+    }
   }
 
   module_invoke_all('exit');
