Exemple #1
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);
}
function dsq_request_handler()
{
    global $dsq_response;
    global $dsq_api;
    global $post;
    global $wpdb;
    if (!empty($_GET['cf_action'])) {
        switch ($_GET['cf_action']) {
            case 'sync_comments':
                if (!($post_id = $_GET['post_id'])) {
                    header("HTTP/1.0 400 Bad Request");
                    die;
                }
                // schedule the event for 30 seconds from now in case they
                // happen to make a quick post
                if (DISQUS_DEBUG) {
                    dsq_sync_post($post_id);
                    $response = dsq_sync_forum();
                    if (!$response) {
                        die('// error: ' . $dsq_api->get_last_error());
                    } else {
                        list($last_comment_id, $comments) = $response;
                        die('// synced ' . $comments . ' comments');
                    }
                } else {
                    wp_schedule_single_event(time(), 'dsq_sync_post', array($post_id));
                    wp_schedule_single_event(time(), 'dsq_sync_forum');
                    die('// sync scheduled');
                }
                break;
            case 'export_comments':
                if (current_user_can('manage_options') && DISQUS_CAN_EXPORT) {
                    $timestamp = intval($_GET['timestamp']);
                    $post_id = intval($_GET['post_id']);
                    global $wpdb, $dsq_api;
                    $post = $wpdb->get_results($wpdb->prepare("\n                        SELECT *\n                        FROM {$wpdb->posts}\n                        WHERE post_type != 'revision'\n                        AND post_status = 'publish'\n                        AND comment_count > 0\n                        AND ID > %d\n                        ORDER BY ID ASC\n                        LIMIT 1\n                    ", $post_id));
                    $post = $post[0];
                    $post_id = $post->ID;
                    $max_post_id = $wpdb->get_var($wpdb->prepare("\n                        SELECT MAX(ID)\n                        FROM {$wpdb->posts}\n                        WHERE post_type != 'revision'\n                        AND post_status = 'publish'\n                        AND comment_count > 0\n                    ", $post_id));
                    $eof = (int) ($post_id == $max_post_id);
                    if ($eof) {
                        $status = 'complete';
                        $msg = 'Your comments have been sent to Disqus and queued for import!<br/><a href="' . DISQUS_IMPORTER_URL . '" target="_blank">See the status of your import at Disqus</a>';
                    } else {
                        $status = 'partial';
                        $msg = dsq_i('Processed comments on post #%s&hellip;', $post_id);
                    }
                    $result = 'fail';
                    $response = null;
                    if ($post) {
                        require_once dirname(__FILE__) . '/export.php';
                        $wxr = dsq_export_wp($post);
                        $response = $dsq_api->import_wordpress_comments($wxr, $timestamp, $eof);
                        if (!($response['group_id'] > 0)) {
                            $result = 'fail';
                            $msg = '<p class="status dsq-export-fail">' . dsq_i('Sorry, something unexpected happened with the export. Please <a href="#" id="dsq_export_retry">try again</a></p><p>If your API key has changed, you may need to reinstall Disqus (deactivate the plugin and then reactivate it). If you are still having issues, refer to the <a href="%s" onclick="window.open(this.href); return false">WordPress help page</a>.', 'http://disqus.com/help/wordpress') . '</p>';
                            $response = $dsq_api->get_last_error();
                        } else {
                            if ($eof) {
                                $msg = dsq_i('Your comments have been sent to Disqus and queued for import!<br/><a href="%s" target="_blank">See the status of your import at Disqus</a>', $response['link']);
                            }
                            $result = 'success';
                        }
                    }
                    // send AJAX response
                    $response = compact('result', 'timestamp', 'status', 'post_id', 'msg', 'eof', 'response');
                    header('Content-type: text/javascript');
                    echo cf_json_encode($response);
                    die;
                }
                break;
            case 'import_comments':
                if (current_user_can('manage_options')) {
                    if (!isset($_GET['last_comment_id'])) {
                        $last_comment_id = false;
                    } else {
                        $last_comment_id = $_GET['last_comment_id'];
                    }
                    if ($_GET['wipe'] == '1') {
                        $wpdb->query("DELETE FROM `" . $wpdb->prefix . "commentmeta` WHERE meta_key IN ('dsq_post_id', 'dsq_parent_post_id')");
                        $wpdb->query("DELETE FROM `" . $wpdb->prefix . "comments` WHERE comment_agent LIKE 'Disqus/%%'");
                    }
                    ob_start();
                    $response = dsq_sync_forum($last_comment_id);
                    $debug = ob_get_clean();
                    if (!$response) {
                        $status = 'error';
                        $result = 'fail';
                        $error = $dsq_api->get_last_error();
                        $msg = '<p class="status dsq-export-fail">' . dsq_i('There was an error downloading your comments from Disqus.') . '<br/>' . htmlspecialchars($error) . '</p>';
                    } else {
                        list($comments, $last_comment_id) = $response;
                        if (!$comments) {
                            $status = 'complete';
                            $msg = dsq_i('Your comments have been downloaded from Disqus and saved in your local database.');
                        } else {
                            $status = 'partial';
                            $msg = dsq_i('Import in progress (last post id: %s) &hellip;', $last_comment_id);
                        }
                        $result = 'success';
                    }
                    $debug = explode("\n", $debug);
                    $response = compact('result', 'status', 'comments', 'msg', 'last_comment_id', 'debug');
                    header('Content-type: text/javascript');
                    echo cf_json_encode($response);
                    die;
                }
                break;
        }
    }
}