function xfac_authenticate_syncUserWpXf($user, $username, $password) { if (!is_a($user, 'WP_User')) { return $user; } $config = xfac_option_getConfig(); if (empty($config)) { return $user; } $records = xfac_user_getRecordsByUserId($user->ID); if (!empty($records)) { return $user; } $username = $user->user_login; $atPos = strpos($username, '@'); if ($atPos !== false) { // XenForo does not accept username in the email format // TODO: extra verification to make sure it is an address $username = substr($username, $atPos); } $postUserExtraParams = array(); if (!!get_option('xfac_sync_user_wp_xf_as_admin')) { $postUserExtraParams['oauth_token'] = xfac_user_getAdminAccessToken($config); } $result = xfac_api_postUser($config, $user->user_email, $username, $password, $postUserExtraParams); if (!empty($result)) { // yay! new account has been created in XenForo $xfUser = $result['user']; $token = $result['token']; if (!isset($xfUser['user_email'])) { // for some reason, user_email is not populated // we have to call another API request to get it // this is required to have all vital information regarding user $me = xfac_api_getUsersMe($config, $token['access_token']); if (!empty($me['user'])) { $xfUser = $me['user']; } } xfac_syncLogin_syncRole($config, $user, $xfUser, false); xfac_user_updateRecord($user->ID, $config['root'], $xfUser['user_id'], $xfUser, $token); xfac_log('xfac_authenticate_syncUserWpXf pushed $wpUser (#%d)', $user->ID); } else { $errors = xfac_api_getLastErrors(); if (!empty($errors['username'])) { // special case, a XenForo account with same username has already existed // TODO: improve this, there are other kind of username errors actually $token = xfac_api_getAccessTokenFromUsernamePassword($config, $username, $password); if (!empty($token)) { $me = xfac_api_getUsersMe($config, $token['access_token']); if (!empty($me['user'])) { $xfUser = $me['user']; xfac_syncLogin_syncRole($config, $user, $xfUser); xfac_user_updateRecord($user->ID, $config['root'], $xfUser['user_id'], $xfUser, $token); xfac_log('xfac_authenticate_syncUserWpXf connected $wpUser (#%d)', $user->ID); } } } } return $user; }
function xfac_dashboardOptions_admin_init() { if (empty($_REQUEST['page'])) { return; } if ($_REQUEST['page'] !== 'xfac') { return; } if (!empty($_REQUEST['cron'])) { switch ($_REQUEST['cron']) { case 'hourly': do_action('xfac_cron_hourly'); wp_redirect(admin_url('options-general.php?page=xfac&ran=hourly')); exit; } } elseif (!empty($_REQUEST['do'])) { switch ($_REQUEST['do']) { case 'xfac_meta': update_option('xfac_meta', array()); wp_redirect(admin_url('options-general.php?page=xfac&done=xfac_meta')); break; case 'xfac_xf_guest_account_submit': $config = xfac_option_getConfig(); if (empty($config)) { wp_die('no_config'); } $username = $_REQUEST['xfac_guest_username']; if (empty($username)) { wp_die('no_username'); } $password = $_REQUEST['xfac_guest_password']; if (empty($password)) { wp_die('no_password'); } $token = xfac_api_getAccessTokenFromUsernamePassword($config, $username, $password); if (empty($token)) { wp_die('no_token'); } $guest = xfac_api_getUsersMe($config, $token['access_token'], false); if (empty($guest['user'])) { wp_die('no_xf_user'); } xfac_user_updateRecord(0, $config['root'], $guest['user']['user_id'], $guest['user'], $token); $records = xfac_user_getRecordsByUserId(0); $record = reset($records); update_option('xfac_xf_guest_account', $record->id); // force meta rebuild update_option('xfac_meta', array()); wp_redirect(admin_url('options-general.php?page=xfac&done=xfac_xf_guest_account')); break; } } }