Exemplo n.º 1
0
 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());
 }
Exemplo n.º 2
0
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 5 minutes from now in case they
                // happen to make a quick post
                dsq_add_pending_post_id($post_id);
                if (DISQUS_DEBUG) {
                    $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 {
                    $ts = time() + 300;
                    wp_schedule_single_event($ts, 'dsq_sync_forum');
                    die('// sync scheduled');
                }
                break;
            case 'export_comments':
                if (current_user_can('manage_options') && DISQUS_CAN_EXPORT) {
                    $msg = '';
                    $result = '';
                    $response = null;
                    $timestamp = intval($_GET['timestamp']);
                    $post_id = intval($_GET['post_id']);
                    if (isset($_GET['_dsqexport_wpnonce']) === false) {
                        $msg = dsq_i('Unable to export comments. Make sure you are accessing this page from the Wordpress dashboard.');
                        $result = 'fail';
                    } else {
                        // Check nonce
                        check_admin_referer('dsq-wpnonce_export', '_dsqexport_wpnonce');
                        global $wpdb, $dsq_api;
                        $post = $wpdb->get_results($wpdb->prepare("\r\n                            SELECT *\r\n                            FROM {$wpdb->posts}\r\n                            WHERE post_type != 'revision'\r\n                            AND post_status = 'publish'\r\n                            AND comment_count > 0\r\n                            AND ID > %d\r\n                            ORDER BY ID ASC\r\n                            LIMIT 1\r\n                        ", $post_id));
                        $post = $post[0];
                        $post_id = $post->ID;
                        $max_post_id = $wpdb->get_var("\r\n                            SELECT MAX(Id)\r\n                            FROM {$wpdb->posts}\r\n                            WHERE post_type != 'revision'\r\n                            AND post_status = 'publish'\r\n                            AND comment_count > 0\r\n                        ");
                        $eof = (int) ($post_id == $max_post_id);
                        if ($eof) {
                            $status = 'complete';
                            $msg = dsq_i('Your comments have been sent to Disqus and queued for import!');
                            $msg .= '<br/><a href="' . DISQUS_IMPORTER_URL . '" target="_blank">';
                            $msg .= dsq_i('See the status of your import at Disqus') . '</a>';
                        } else {
                            $status = 'partial';
                            $msg = dsq_i('Processed comments on post') . ' #' . $post_id . '&hellip;';
                        }
                        $result = 'fail';
                        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">';
                                $msg .= dsq_i('Sorry, something unexpected happened with the export. Please try again.');
                                $msg .= '</p><p>';
                                $msg .= dsq_i('If your API key has changed, you may need to reinstall Disqus (deactivate the plugin and then reactivate it).');
                                $msg .= dsq_i('If you are still having issues, refer to the %s WordPress help page', '<a href="https://help.disqus.com/customer/portal/articles/472005" onclick="window.open(this.href); return false">');
                                $msg .= '</a></p>';
                                $response = $dsq_api->get_last_error();
                            } else {
                                if ($eof) {
                                    $msg = dsq_i('Your comments have been sent to Disqus and queued for import!');
                                    $msg .= '<br/><a href="' . $response['link'] . '" target="_blank">';
                                    $msg .= dsq_i('See the status of your import at Disqus');
                                    $msg .= '</a>';
                                }
                                $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')) {
                    $msg = '';
                    $result = '';
                    $response = null;
                    if (isset($_GET['_dsqimport_wpnonce']) === false) {
                        $msg = dsq_i('Unable to import comments. Make sure you are accessing this page from the Wordpress dashboard.');
                        $result = 'fail';
                    } else {
                        // Check nonce
                        check_admin_referer('dsq-wpnonce_import', '_dsqimport_wpnonce');
                        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, true);
                        $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/>' . esc_attr($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)', $last_comment_id) . ' &hellip;';
                            }
                            $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;
        }
    }
}