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(array $json) { $config = xfac_option_getConfig(); if (empty($config['clientId'])) { return; } $xfThreadIds = array(); $xfPostIds = array(); // phrase 1: preparation foreach ($json as &$pingRef) { if (empty($pingRef['client_id']) or $pingRef['client_id'] != $config['clientId']) { continue; } if (empty($pingRef['topic'])) { continue; } $parts = explode('_', $pingRef['topic']); $pingRef['topic_id'] = array_pop($parts); $pingRef['topic_type'] = implode('_', $parts); switch ($pingRef['topic_type']) { case 'thread_post': $xfThreadIds[] = $pingRef['topic_id']; $xfPostIds[] = $pingRef['object_data']; break; } } // phrase 2: fetch sync records $postSyncRecords = array(); if (!empty($xfPostIds)) { $postSyncRecords = xfac_sync_getRecordsByProviderTypeAndIds('', 'thread', $xfThreadIds); } $commentSyncRecords = array(); if (!empty($xfPostIds)) { $commentSyncRecords = xfac_sync_getRecordsByProviderTypeAndIds('', 'post', $xfPostIds); } // phrase 3: sync data foreach ($json as &$pingRef) { if (empty($pingRef['topic_type'])) { continue; } switch ($pingRef['topic_type']) { case 'thread_post': $postSyncRecord = null; $commentSyncRecord = null; foreach ($postSyncRecords as $_postSyncRecord) { if ($_postSyncRecord->provider_content_id == $pingRef['topic_id']) { $postSyncRecord = $_postSyncRecord; } } if (!empty($postSyncRecord)) { foreach ($commentSyncRecords as $_commentSyncRecord) { if ($_commentSyncRecord->provider_content_id == $pingRef['object_data']) { $commentSyncRecord = $_commentSyncRecord; } } $pingRef['result'] = _xfac_subscription_handleCallback_threadPost($config, $pingRef, $postSyncRecord, $commentSyncRecord); if (!empty($pingRef['result'])) { xfac_sync_updateRecordDate($postSyncRecord); if (!empty($commentSyncRecord)) { xfac_sync_updateRecordDate($commentSyncRecord); } } } break; case 'user_notification': $pingRef['result'] = _xfac_subscription_handleCallback_userNotification($config, $pingRef); break; case 'user': $pingRef['result'] = _xfac_subscription_handleCallback_user($config, $pingRef); break; } } // phrase 4: output results $results = array(); foreach ($json as $ping) { if (!empty($ping['result'])) { $results[] = $ping; xfac_log('xfac_subscription_handleCallback %s/%s -> %s', $ping['topic_type'], $ping['topic_id'], $ping['result']); } } echo json_encode($results); }