function xfac_get_avatar($avatar = '', $id_or_email, $size = 96, $default = '', $alt = '') { if (is_numeric($id_or_email)) { $wpUserId = (int) $id_or_email; } elseif (is_string($id_or_email) && ($user = get_user_by('email', $id_or_email))) { $wpUserId = $user->ID; } elseif (is_object($id_or_email) && !empty($id_or_email->user_id)) { $wpUserId = (int) $id_or_email->user_id; } if (empty($wpUserId)) { // cannot figure out the user id... return $avatar; } $apiRecords = xfac_user_getRecordsByUserId($wpUserId); if (empty($apiRecords)) { // no api records return $avatar; } $apiRecord = reset($apiRecords); if (empty($apiRecord->profile['links']['avatar'])) { // no avatar? return $avatar; } $avatar = $apiRecord->profile['links']['avatar']; $size = (int) $size; if (empty($alt)) { $alt = get_the_author_meta('display_name', $wpUserId); } $author_class = is_author($wpUserId) ? ' current-author' : ''; $avatar = "<img alt='" . esc_attr($alt) . "' src='" . esc_url($avatar) . "' class='avatar avatar-{$size}{$author_class} photo' height='{$size}' width='{$size}' />"; return $avatar; }
function xfac_admin_bar_jscount_menu($wpAdminBar) { if (is_admin()) { // don't add menu in Dashboard return; } $wpUser = wp_get_current_user(); if (empty($wpUser->ID)) { // don't add menu for guests return; } $config = xfac_option_getConfig(); if (empty($config)) { // don't add menu for site without configuration return; } $records = xfac_user_getRecordsByUserId($wpUser->ID); if (empty($records)) { // don't add menu for not-connected users return; } $record = reset($records); $meta = xfac_option_getMeta($config); $doNotifications = (!!get_option('xfac_top_bar_notifications') and !empty($meta['linkAlerts'])); $doConversations = (!!get_option('xfac_top_bar_conversations') and !empty($meta['linkConversations'])); $script = 'window.xfacClientId = ' . json_encode($config['clientId']) . ';'; $script .= 'window.xfacXenForoUserId = ' . json_encode(intval($record->identifier)) . ';'; $script .= 'window.xfacDoNotifications = ' . json_encode($doNotifications ? 1 : 0) . ';'; $script .= 'window.xfacDoConversations = ' . json_encode($doConversations ? 1 : 0) . ';'; $html = sprintf('<script>%s</script>', $script); if ($doNotifications) { $notificationsTitle = __('Alerts', 'xenforo-api-consumer'); if (!isset($_COOKIE['notificationCount'])) { $notificationsTitle .= ' <span id="xfacNotificationCount" class="xfacJsCount"></span>'; } else { $notificationsTitle .= call_user_func_array('sprintf', array(' <span id="xfacNotificationCount" class="xfacJsCount updated%s">%d</span>', $_COOKIE['notificationCount'] > 0 ? ' unread' : '', $_COOKIE['notificationCount'])); } $wpAdminBar->add_menu(array('id' => 'xfac-notifications', 'title' => $notificationsTitle, 'parent' => !!get_option('xfac_top_bar_replace') ? 'top-secondary' : '', 'href' => $meta['linkAlerts'], 'meta' => array('html' => $html))); // reset html $html = ''; } if ($doConversations) { $conversationTitle = __('Conversations', 'xenforo-api-consumer'); if (!isset($_COOKIE['conversationCount'])) { $conversationTitle .= ' <span id="xfacConversationCount" class="xfacJsCount"></span>'; } else { $conversationTitle .= call_user_func_array('sprintf', array(' <span id="xfacConversationCount" class="xfacJsCount updated%s">%d</span>', $_COOKIE['conversationCount'] > 0 ? ' unread' : '', $_COOKIE['conversationCount'])); } $wpAdminBar->add_menu(array('id' => 'xfac-conversations', 'title' => $conversationTitle, 'parent' => !!get_option('xfac_top_bar_replace') ? 'top-secondary' : '', 'href' => $meta['linkConversations'], 'meta' => array('html' => $html))); // reset html $html = ''; } }
?> "><?php _e('Click here to associate your account.', 'xenforo-api-consumer'); ?> </a> <?php } ?> <?php } else { ?> <?php _e('You need to associate an Administrative XenForo account and select it as Admin Account.', 'xenforo-api-consumer'); ?> <?php if (xfac_user_getRecordsByUserId(get_current_user_id())) { ?> <a href="<?php echo $optionsUrl; ?> "><?php _e('Click here to configure Admin Account.', 'xenforo-api-consumer'); ?> </a> <?php } else { ?> <a href="<?php echo $loginUrl; ?> "><?php
function xfac_profile_update_user_pass($wpUserId) { if (empty($GLOBALS['_xfac_syncLogin_pending_user_pass'])) { // no data return; } $pending = $GLOBALS['_xfac_syncLogin_pending_user_pass']; if (!is_array($pending) || count($pending) != 2) { // data unrecognized return; } if ($pending[0] != $wpUserId) { // user_id not matched return; } $config = xfac_option_getConfig(); if (empty($config)) { // no config return; } $adminAccessToken = xfac_user_getAdminAccessToken($config); if (empty($adminAccessToken)) { // no admin access token return; } $records = xfac_user_getRecordsByUserId($wpUserId); if (empty($records)) { // no user record return null; } $record = reset($records); xfac_api_postUserPassword($config, $adminAccessToken, $record->identifier, $pending[1]); xfac_log('xfac_profile_update_user_pass pushed password for $wpUser (#%d)', $wpUserId); }
function xfac_tools_connect() { /** @var wpdb $wpdb */ global $wpdb; $config = xfac_option_getConfig(); if (empty($config)) { wp_die(__('XenForo API configuration is missing.', 'xenforo-api-consumer')); } $adminAccessToken = xfac_user_getAdminAccessToken($config); if (empty($adminAccessToken)) { wp_die(__('Admin Account\'s access token cannot be obtained.', 'xenforo-api-consumer')); } if (!xfac_api_hasModuleVersion($config, 'forum', 2015030901) || !xfac_api_hasModuleVersion($config, 'oauth2', 2015030902)) { wp_die(__('Please update XenForo API to run this tool.', 'xenforo-api-consumer')); } $optionFilters = array('position' => array('filter' => FILTER_VALIDATE_INT, 'default' => 0), 'limit' => array('filter' => FILTER_VALIDATE_INT, 'default' => 10), 'associate' => array('filter' => FILTER_VALIDATE_INT, 'default' => 0), 'push' => array('filter' => FILTER_VALIDATE_INT, 'default' => 0)); $options = array(); foreach ($optionFilters as $optionKey => $optionFilter) { $optionValue = filter_input(INPUT_GET, $optionKey, $optionFilter['filter']); if (!empty($optionValue)) { $options[$optionKey] = $optionValue; } else { $options[$optionKey] = $optionFilter['default']; } } if (empty($options['associate']) && empty($options['push'])) { wp_die(__('At least one action must be selected: either associate or push', 'xenforo-api-consumer')); } $maxWpUserIds = $wpdb->get_var('SELECT MAX(ID) FROM ' . $wpdb->prefix . 'users'); if ($options['position'] >= $maxWpUserIds) { die(__('Done.', 'xenforo-api-consumer')); } $dbUsers = $wpdb->get_results(' SELECT * FROM ' . $wpdb->prefix . 'users WHERE ID > ' . $options['position'] . ' LIMIT ' . $options['limit']); foreach ($dbUsers as $dbUser) { $user = new WP_User($dbUser); $options['position'] = max($options['position'], $user->ID); $records = xfac_user_getRecordsByUserId($user->ID); if (!empty($records)) { // this user has connected continue; } printf(__('Processing user #%d (%s)', 'xenforo-api-consumer'), $user->ID, $user->user_login); echo "<br />\n"; $candidates = array(); $userLoginUsers = xfac_api_getUsersFind($config, $user->user_login); if (!empty($userLoginUsers['users'])) { foreach ($userLoginUsers['users'] as $userLoginUser) { // similar logic with includes/dashboard/profile.php if (strlen($userLoginUser['username']) == strlen($user->user_login)) { $candidates[$userLoginUser['user_id']] = $userLoginUser; } } } $emailUsers = xfac_api_getUsersFind($config, '', $user->user_email, $adminAccessToken); if (!empty($emailUsers['users'])) { foreach ($emailUsers['users'] as $emailUser) { $candidates[$emailUser['user_id']] = $emailUser; } } if (!empty($candidates) && !empty($options['associate'])) { foreach ($candidates as $candidate) { $userAccessToken = xfac_api_postOauthTokenAdmin($config, $adminAccessToken, $candidate['user_id']); if (!empty($userAccessToken)) { xfac_syncLogin_syncRole($config, $user, $candidate, false); xfac_user_updateRecord($user->ID, $config['root'], $candidate['user_id'], $candidate, $userAccessToken); xfac_log('xfac_tools_connect associated $wpUser (#%d) vs. $xfUser (#%d)', $user->ID, $candidate['user_id']); } else { $errors = xfac_api_getLastErrors(); if (!is_array($errors)) { $errors = array(__('Unknown error', 'xenforo-api-consumer')); } xfac_log('xfac_tools_connect failed to associate $wpUser (#%d) vs. $xfUser (#%d): %s', $user->ID, $candidate['user_id'], implode(', ', $errors)); } } } if (empty($candidates) && !empty($options['push'])) { $result = xfac_api_postUser($config, $user->user_email, $user->user_login, '', array('oauth_token' => $adminAccessToken)); if (!empty($result)) { $xfUser = $result['user']; $token = $result['token']; xfac_syncLogin_syncRole($config, $user, $xfUser, false); xfac_user_updateRecord($user->ID, $config['root'], $xfUser['user_id'], $xfUser, $token); xfac_log('xfac_tools_connect pushed $wpUser (#%d)', $user->ID); } else { $errors = xfac_api_getLastErrors(); if (!is_array($errors)) { $errors = array(__('Unknown error', 'xenforo-api-consumer')); } xfac_log('xfac_tools_connect failed to push $wpUser (#%d): %s', $user->ID, implode(', ', $errors)); } } } $optionsStr = ''; foreach ($options as $optionKey => $optionValue) { if ($optionValue !== $optionFilters[$optionKey]['default']) { $optionsStr .= sprintf('&%s=%s', $optionKey, rawurlencode($optionValue)); } } die(sprintf('<script>window.location = "%s";</script>', admin_url(sprintf('tools.php?action=xfac_tools_connect%s', $optionsStr)))); }
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; } } }
function xfac_user_getAccessToken($wpUserId) { $records = xfac_user_getRecordsByUserId($wpUserId); if (empty($records)) { return null; } $record = reset($records); return xfac_user_getAccessTokenForRecord($record); }
function xfac_dashboardProfile_admin_init() { if (!defined('IS_PROFILE_PAGE')) { return; } if (empty($_REQUEST['xfac'])) { return; } switch ($_REQUEST['xfac']) { case 'disconnect': if (empty($_REQUEST['id'])) { return; } $wpUser = wp_get_current_user(); if (empty($wpUser)) { // huh?! return; } $apiRecords = xfac_user_getRecordsByUserId($wpUser->ID); if (empty($apiRecords)) { return; } $requestedRecord = false; foreach ($apiRecords as $apiRecord) { if ($apiRecord->id == $_REQUEST['id']) { $requestedRecord = $apiRecord; } } if (empty($requestedRecord)) { return; } xfac_user_deleteRecord($requestedRecord); wp_redirect('profile.php?xfac=disconnected'); exit; break; } }