function fbc_update_facebook_data($force = false) { $last_cache_update = get_option(FBC_LAST_UPDATED_CACHE_OPTION); $delta = time() - $last_cache_update; if ($delta < 24 * 60 * 60 && !$force) { return; } update_option(FBC_LAST_UPDATED_CACHE_OPTION, time()); global $wpdb; $sql = "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'fbuid'"; $res = $wpdb->get_results($wpdb->prepare($sql), ARRAY_A); if (!$res) { return -1; } $fbuid_to_wpuid = array(); foreach ($res as $result) { $fbuid_to_wpuid[$result['meta_value']] = $result['user_id']; } try { $userinfo = fbc_anon_api_client()->users_getInfo(array_keys($fbuid_to_wpuid), fbc_userinfo_keys()); } catch (Exception $e) { return -1; } $userinfo_by_fbuid = array(); foreach ($userinfo as $info) { $wpuid = $fbuid_to_wpuid[$info['uid']]; $userdata = fbc_userinfo_to_wp_user($info); $userdata['ID'] = $wpuid; wp_update_user($userdata); } return count($userinfo); }
function fbc_insert_user($fbuid) { $userinfo = fbc_anon_api_client()->users_getInfo(array($fbuid), fbc_userinfo_keys()); if ($userinfo === null) { error_log('wp-fbconnect: empty query result for user ' . $fbuid); } $userinfo = $userinfo[0]; $fbusername = '******' . $fbuid; if (username_exists($fbusername)) { return FBC_ERROR_USERNAME_EXISTS; } $userdata = fbc_userinfo_to_wp_user($userinfo); $userdata += array('user_pass' => wp_generate_password(), 'user_email' => $fbusername . '@wp-fbconnect.fake', 'user_login' => $fbusername, 'role' => 'subscriber'); $wpuid = wp_insert_user($userdata); // $wpuid might be an instance of WP_Error if ($wpuid && is_integer($wpuid)) { update_usermeta($wpuid, 'fbuid', "{$fbuid}"); } return $wpuid; }