function xfac_syncPost_processThreads($config, array $threads, array $mappedTags) { $threadIds = array(); foreach ($threads as $thread) { $threadIds[] = $thread['thread_id']; } $syncRecords = xfac_sync_getRecordsByProviderTypeAndIds('', 'thread', $threadIds); foreach ($threads as $thread) { $synced = false; foreach ($syncRecords as $syncRecord) { if ($syncRecord->provider_content_id == $thread['thread_id']) { $synced = true; } } if (!$synced) { $tagNames = array(); if (!empty($thread['forum_id']) && isset($mappedTags[$thread['forum_id']])) { $tagNames = $mappedTags[$thread['forum_id']]; } if (!empty($tagNames)) { xfac_syncPost_pullPost($config, $thread, $tagNames); } } } }
function xfac_syncComment_processPostSyncRecord($config, $postSyncRecord) { $page = 1; $pulledSomething = false; if (time() - $postSyncRecord->sync_date < 60) { // do not try to sync every minute... return false; } if (!empty($postSyncRecord->syncData['subscribed'])) { if (time() - $postSyncRecord->sync_date < 86400) { // do not try to sync every day with subscribed thread return false; } } $wpUserData = xfac_user_getUserDataByApiData($config['root'], $postSyncRecord->syncData['thread']['creator_user_id']); $accessToken = xfac_user_getAccessToken($wpUserData->ID); $xfPosts = xfac_api_getPostsInThread($config, $postSyncRecord->provider_content_id, $accessToken); if (empty($xfPosts['subscription_callback']) and !empty($xfPosts['_headerLinkHub'])) { if (xfac_api_postSubscription($config, $accessToken, $xfPosts['_headerLinkHub'])) { $postSyncRecord->syncData['subscribed'] = array('hub' => $xfPosts['_headerLinkHub'], 'time' => time()); xfac_sync_updateRecord('', $postSyncRecord->provider_content_type, $postSyncRecord->provider_content_id, $postSyncRecord->sync_id, 0, $postSyncRecord->syncData); xfac_log('xfac_syncComment_processPostSyncRecord subscribed for posts in thread (#%d)', $postSyncRecord->provider_content_id); } else { xfac_log('xfac_syncComment_processPostSyncRecord failed subscribing for posts in thread (#%d)', $postSyncRecord->provider_content_id); } } if (empty($xfPosts['posts'])) { return false; } $xfPostIds = array(); foreach ($xfPosts['posts'] as $xfPost) { $xfPostIds[] = $xfPost['post_id']; } $commentSyncRecords = xfac_sync_getRecordsByProviderTypeAndIds('', 'post', $xfPostIds); foreach ($xfPosts['posts'] as $xfPost) { if (!empty($xfPost['post_is_first_post'])) { // do not pull first post continue; } $synced = false; foreach ($commentSyncRecords as $commentSyncRecord) { if ($commentSyncRecord->provider_content_id == $xfPost['post_id']) { $synced = true; } } if (!$synced) { $commentId = xfac_syncComment_pullComment($config, $xfPost, $postSyncRecord->sync_id); if ($commentId > 0) { $pulledSomething = true; } } } if ($pulledSomething) { xfac_sync_updateRecordDate($postSyncRecord); } return $pulledSomething; }
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; }