public function test_pending_posts_api()
 {
     dsq_add_pending_post_id(1);
     $this->assertEquals(dsq_get_pending_post_ids(), array(1));
     dsq_clear_pending_post_ids(array(1));
     $this->assertEquals(dsq_get_pending_post_ids(), array());
 }
Example #2
0
function dsq_sync_forum($last_comment_id = false, $force = false)
{
    global $dsq_api, $wpdb;
    set_time_limit(DISQUS_SYNC_TIMEOUT);
    if ($force) {
        $sync_time = null;
    } else {
        $sync_time = (int) get_option('_disqus_sync_lock');
    }
    // lock expires after 1 hour
    if ($sync_time && $sync_time > time() - 60 * 60) {
        $dsq_api->api->last_error = 'Sync already in progress (lock found)';
        return false;
    } else {
        update_option('_disqus_sync_lock', time());
    }
    // sync all pending posts
    $post_ids = dsq_get_pending_post_ids();
    dsq_clear_pending_post_ids($post_ids);
    foreach ($post_ids as $post_id) {
        dsq_sync_post($post_id);
    }
    if ($last_comment_id === false) {
        $last_comment_id = get_option('disqus_last_comment_id');
        if (!$last_comment_id) {
            $last_comment_id = 0;
        }
    }
    if ($last_comment_id) {
        $last_comment_id++;
    }
    //$last_comment_id = 0;
    // Pull comments from API
    $dsq_response = $dsq_api->get_forum_posts($last_comment_id);
    if ($dsq_response < 0 || $dsq_response === false) {
        return false;
    }
    // Sync comments with database.
    dsq_sync_comments($dsq_response);
    $total = 0;
    if ($dsq_response) {
        foreach ($dsq_response as $comment) {
            $total += 1;
            if ($comment->id > $last_comment_id) {
                $last_comment_id = $comment->id;
            }
        }
        if ($last_comment_id > get_option('disqus_last_comment_id')) {
            update_option('disqus_last_comment_id', $last_comment_id);
        }
    }
    unset($comment);
    delete_option('_disqus_sync_lock');
    return array($total, $last_comment_id);
}