function xfac_syncPost_cron() { $config = xfac_option_getConfig(); if (empty($config)) { return; } $mappedTags = xfac_syncPost_getMappedTags(); if (empty($mappedTags)) { return; } $forumFollowedSyncRecords = xfac_sync_getRecordsByProviderTypeAndSyncId('', 'forums/followed', 0); if (empty($forumFollowedSyncRecords) or time() - $forumFollowedSyncRecords[0]->sync_date > 86400) { xfac_update_option_tag_forum_mappings('xfac_tag_forum_mappings', null, get_option('xfac_tag_forum_mappings')); } $forumIds = array_keys($mappedTags); // sync sticky threads first $stickyThreads = xfac_api_getThreadsInForums($config, $forumIds, '', 'sticky=1'); if (!empty($stickyThreads['threads'])) { xfac_syncPost_processThreads($config, $stickyThreads['threads'], $mappedTags); } // now start syncing normal threads $threads = xfac_api_getThreadsInForums($config, $forumIds); if (!empty($threads['threads'])) { xfac_syncPost_processThreads($config, $threads['threads'], $mappedTags); } }
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_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; }