function widget($args, $instance) { if (!is_search()) { return ''; } /** @var WP_Query $wp_query */ global $wp_query; extract($args); $limit = !empty($instance['limit']) ? absint($instance['limit']) : 5; if (empty($limit)) { $limit = 5; } $title = !empty($instance['title']) ? $instance['title'] : __('Forum Results'); $title = apply_filters('widget_title', $title, $instance, $this->id_base); $config = xfac_option_getConfig(); $threads = array(); if (!empty($config)) { $accessToken = xfac_user_getSystemAccessToken($config, true); $results = xfac_api_postSearchThreads($config, $accessToken, $wp_query->get('s'), $limit); if (!empty($results['data'])) { $threads = $results['data']; } } require xfac_template_locateTemplate('widget_search.php'); }
function xfac_search_indexComment($config, $commentId, $latestSyncDate = 0) { $comment = get_comment($commentId); if (empty($comment->comment_approved)) { return false; } $date = mysql2date('U', $comment->comment_date_gmt); if ($date <= $latestSyncDate) { return false; } $post = get_post($comment->comment_post_ID); if (empty($post)) { return false; } $accessToken = ''; if ($comment->user_id > 0) { $accessToken = xfac_user_getAccessToken($comment->user_id); } if (empty($accessToken)) { $accessToken = xfac_user_getSystemAccessToken($config, true); } $link = get_comment_link($comment); return xfac_api_postSearchIndexing($config, $accessToken, 'comment', $comment->comment_ID, $post->post_title, $comment->comment_content, $date, $link); }
function xfac_api_getUserGroups($config, $userId = 0, $accessToken = '') { if ($accessToken === '') { $accessToken = xfac_user_getSystemAccessToken($config, true); } $curl = _xfac_api_curl(call_user_func_array('sprintf', array('%s/index.php?users/%sgroups/&oauth_token=%s', rtrim($config['root'], '/'), $userId > 0 ? $userId . '/' : '', rawurlencode($accessToken)))); extract($curl); if (isset($parts['user_groups'])) { return $parts; } else { return _xfac_api_getFailedResponse($curl); } }
function xfac_option_getMeta($config) { static $rebuiltCount = 0; if (empty($config)) { return array(); } $meta = get_option('xfac_meta'); $rebuild = false; if (empty($meta) or empty($meta['linkIndex'])) { $rebuild = true; } else { foreach ($config as $configKey => $configValue) { if (empty($meta[$configKey]) or $meta[$configKey] !== $configValue) { $rebuild = true; break; } } } $xfAdminAccountOption = intval(get_option('xfac_xf_admin_account')); $xfAdminAccountMeta = empty($meta['xfac_xf_admin_account']) ? 0 : intval($meta['xfac_xf_admin_account']); if ($xfAdminAccountMeta !== $xfAdminAccountOption) { $rebuild = true; } if ($rebuild and !empty($_REQUEST['oauth_token'])) { // looks like admin enter WordPress url as the root, abort rebuilding $rebuild = false; } if ($rebuild and $rebuiltCount > 0) { // we rebuild once, only retry if $meta is empty if (!empty($meta)) { $rebuild = false; } } if ($rebuild) { xfac_updateNotice('xf_guest_account'); xfac_updateNotice('xf_admin_account'); $meta = $config; $meta['linkIndex'] = xfac_api_getPublicLink($config, 'index'); $meta['modules'] = array(); $meta['forums'] = array(); if (!empty($meta['linkIndex'])) { if ($xfAdminAccountOption) { $adminAccessToken = xfac_user_getAdminAccessToken($config); if (empty($adminAccessToken)) { // unable to obtain admin access token // probably a missing record or expired refresh token // reset the option update_option('xfac_xf_admin_account', 0); $xfAdminAccountOption = 0; } } $xfGuestAccountOption = intval(get_option('xfac_xf_guest_account')); if ($xfGuestAccountOption) { $guestAccessToken = xfac_user_getSystemAccessToken($config); if (empty($guestAccessToken)) { // unable to obtain guest access token // probably an expired refresh token // reset the option update_option('xfac_xf_guest_account', 0); } else { $mappedTags = xfac_syncPost_getMappedTags(); if (!empty($mappedTags)) { // make sures the guest account follows required forums // and have the needed notification subscription xfac_syncPost_followForums($config, $guestAccessToken, array_keys($mappedTags)); } } } $meta['modules'] = xfac_api_getModules($config); $meta['linkAlerts'] = xfac_api_getPublicLink($config, 'account/alerts'); $meta['linkConversations'] = xfac_api_getPublicLink($config, 'conversations'); $meta['linkLogin'] = xfac_api_getPublicLink($config, 'login'); $meta['linkLoginLogin'] = xfac_api_getPublicLink($config, 'login/login'); $meta['linkRegister'] = xfac_api_getPublicLink($config, 'register'); $forums = xfac_api_getForums($config); if (!empty($forums['forums'])) { $meta['forums'] = $forums['forums']; } $meta['xfac_xf_admin_account'] = $xfAdminAccountOption; if (!empty($meta['xfac_xf_admin_account'])) { $userGroups = xfac_api_getUserGroups($config, 0, xfac_user_getAdminAccessToken($config)); if (!empty($userGroups['user_groups'])) { $meta['userGroups'] = $userGroups['user_groups']; } } } $rebuiltCount++; update_option('xfac_meta', $meta); xfac_log('xfac_option_getMeta rebuilt $meta=%s', $meta); } return $meta; }
function xfac_update_option_tag_forum_mappings($option, $oldValue, $newValue) { if ($option === 'xfac_tag_forum_mappings') { $forumIds = array(); foreach ($newValue as $tagForumMapping) { if (!empty($tagForumMapping['term_id']) and !empty($tagForumMapping['forum_id'])) { $forumIds[] = intval($tagForumMapping['forum_id']); } } $config = xfac_option_getConfig(); if (!empty($config)) { $accessToken = xfac_user_getSystemAccessToken($config); if (!empty($accessToken)) { xfac_syncPost_followForums($config, $accessToken, $forumIds); } } } }
function _xfac_subscription_handleCallback_userNotification($config, $ping) { $accessToken = xfac_user_getSystemAccessToken($config); if (empty($accessToken)) { return false; } if (empty($ping['object_data']['notification_id'])) { return false; } $notification = $ping['object_data']; if (empty($notification['notification_type'])) { return false; } if (!preg_match('/^post_(?<postId>\\d+)_insert$/', $notification['notification_type'], $matches) || intval(get_option('xfac_sync_post_xf_wp')) == 0) { // currently we only handle post pull here return false; } $postId = $matches['postId']; $xfPost = xfac_api_getPost($config, $postId, $accessToken); if (empty($xfPost['post']['thread_id'])) { return false; } $postSyncRecords = xfac_sync_getRecordsByProviderTypeAndIds('', 'thread', array($xfPost['post']['thread_id'])); if (!empty($postSyncRecords)) { return false; } $xfThread = xfac_api_getThread($config, $xfPost['post']['thread_id'], $accessToken); if (empty($xfThread['thread'])) { return false; } $wpTags = xfac_syncPost_getMappedTags($xfThread['thread']['forum_id']); if (empty($wpTags)) { return false; } $wpPostId = xfac_syncPost_pullPost($config, $xfThread['thread'], $wpTags, 'subscription'); if ($wpPostId > 0) { return 'created new post'; } return false; }