$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; print_line(' Exporting comments for post id %d', $post_id); $response = null; $query = $wpdb->get_results($wpdb->prepare("SELECT COUNT(*) as total FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_agent NOT LIKE 'Disqus/%%' LIMIT " . EXPORT_CHUNK_SIZE, $post_id)); $total_comments = $query[0]->total; $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_agent NOT LIKE 'Disqus/%%' LIMIT " . EXPORT_CHUNK_SIZE, $post_id)); $group_id = null; $at = 0; // we need to send empty files to ensure EOF happens while ($at === 0 && $post_id == $max_post_id || $at < $total_comments) { if ($post_id == $max_post_id && $at + EXPORT_CHUNK_SIZE >= $total_comments) { $eof = 1; } $wxr = dsq_export_wp($post, $comments); $response = $dsq_api->import_wordpress_comments($wxr, $timestamp, $eof); if (!($response['group_id'] > 0)) { print_line('---------------------------------------------------------'); print_line('There was an error communicating with DISQUS!'); print_line($dsq_api->get_last_error()); print_line('---------------------------------------------------------'); } $group_id = $response['group_id']; print_line(' %d comments exported', count($comments), $time); $total_exported += count($comments); $at += EXPORT_CHUNK_SIZE; $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_agent NOT LIKE 'Disqus/%%' LIMIT " . EXPORT_CHUNK_SIZE . " OFFSET {$at}", $post->ID)); // assuming the cache is the internal, reset it's value to empty to avoid // large memory consumption $wp_object_cache->cache = array();
global $wp_version; global $dsq_version; global $dsq_api; if (!current_user_can('manage_options')) { die; } // HACK: For old versions of WordPress if (!function_exists('wp_nonce_field')) { function wp_nonce_field() { } } // Handle export function. if (isset($_POST['export'])) { require_once dirname(__FILE__) . '/export.php'; dsq_export_wp(); } // Handle uninstallation. if (isset($_POST['uninstall'])) { update_option('disqus_forum_url', ''); update_option('disqus_api_key', ''); } // Clean-up POST parameters. foreach (array('dsq_forum_url', 'dsq_username', 'dsq_password') as $key) { if (isset($_POST[$key])) { $_POST[$key] = strip_tags($_POST[$key]); } } // Handle installation process. if (isset($_POST['dsq_forum_url']) && isset($_POST['dsq_username']) && isset($_POST['dsq_password'])) { $api_key = $dsq_api->get_forum_api_key($_POST['dsq_username'], $_POST['dsq_password'], $_POST['dsq_forum_url']);
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 . '…'; } $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) . ' …'; } $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; } } }
function dsq_request_handler() { if (!empty($_GET['cf_action'])) { switch ($_GET['cf_action']) { case 'export_comments': if (current_user_can('manage_options')) { // handle vars $post_id = intval($_GET['post_id']); $limit = 2; global $wpdb, $dsq_api; $posts = $wpdb->get_results("\n\t\t\t\t\t\tSELECT * \n\t\t\t\t\t\tFROM {$wpdb->posts} \n\t\t\t\t\t\tWHERE post_type != 'revision'\n\t\t\t\t\t\tAND post_status = 'publish'\n\t\t\t\t\t\tAND comment_count > 0\n\t\t\t\t\t\tAND ID > {$post_id}\n\t\t\t\t\t\tORDER BY ID ASC\n\t\t\t\t\t\tLIMIT {$limit}\n\t\t\t\t\t"); $first_post_id = $posts[0]->ID; $last_post_id = $posts[count($posts) - 1]->ID; $max_post_id = $wpdb->get_var("\n\t\t\t\t\t\tSELECT MAX(ID)\n\t\t\t\t\t\tFROM {$wpdb->posts} \n\t\t\t\t\t\tWHERE post_type != 'revision'\n\t\t\t\t\t\tAND post_status = 'publish'\n\t\t\t\t\t\tAND comment_count > 0\n\t\t\t\t\t\tAND ID > {$post_id}\n\t\t\t\t\t"); if ($last_post_id == $max_post_id) { $status = 'complete'; $msg = 'All comments sent to Disqus!'; } else { $status = 'partial'; if (count($posts) == 1) { $msg = 'Processed comments on post #' . $first_post_id . '…'; } else { $msg = 'Processed comments on posts #' . $first_post_id . '-' . $last_post_id . '…'; } } $result = 'fail'; if (count($posts)) { include_once dirname(__FILE__) . '/export.php'; $wxr = dsq_export_wp($posts); $import_id = $dsq_api->import_wordpress_comments($wxr); if ($import_id < 0) { $result = 'fail'; $msg = '<p class="status dsq-export-fail">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="http://disqus.com/help/wordpress" onclick="window.open(this.href); return false">WordPress help page</a>.</p>'; } else { $result = 'success'; } } // send AJAX response $response = compact('result', 'status', 'last_post_id', 'msg'); header('Content-type: text/javascript'); echo cf_json_encode($response); die; } break; } } }
function nxs_postNewComment($cmnt, $aa = false) { $cmnt['comment_post_ID'] = (int) $cmnt['comment_post_ID']; $cmnt['comment_parent'] = isset($cmnt['comment_parent']) ? absint($cmnt['comment_parent']) : 0; $parent_status = 0 < $cmnt['comment_parent'] ? wp_get_comment_status($cmnt['comment_parent']) : ''; $cmnt['comment_parent'] = 'approved' == $parent_status || 'unapproved' == $parent_status ? $cmnt['comment_parent'] : 0; $cmnt['comment_author_IP'] = ''; $cmnt['comment_agent'] = 'SNAP'; $cmnt['comment_date'] = get_date_from_gmt($cmnt['comment_date_gmt']); $cmnt = wp_filter_comment($cmnt); if ($aa) { $cmnt['comment_approved'] = 1; } else { $cmnt['comment_approved'] = wp_allow_comment($cmnt); } $cmntID = wp_insert_comment($cmnt); if ('spam' !== $cmnt['comment_approved']) { if ('0' == $cmnt['comment_approved']) { wp_notify_moderator($cmntID); } $post =& get_post($cmnt['comment_post_ID']); if (get_option('comments_notify') && $cmnt['comment_approved'] && (!isset($cmnt['user_id']) || $post->post_author != $cmnt['user_id'])) { wp_notify_postauthor($cmntID, isset($cmnt['comment_type']) ? $cmnt['comment_type'] : ''); } global $wpdb, $dsq_api; if (isset($dsq_api)) { $plugins_url = str_replace('social-networks-auto-poster-facebook-twitter-g/', '', plugin_dir_path(__FILE__)); require_once $plugins_url . 'disqus-comment-system/export.php'; if (function_exists('dsq_export_wp')) { $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_ID = " . $cmntID)); // prr($comments); $wxr = dsq_export_wp($post, $comments); $response = $dsq_api->import_wordpress_comments($wxr, time()); // prr($response); } } } return $cmntID; }
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']); $limit = 2; global $wpdb, $dsq_api; $posts = $wpdb->get_results($wpdb->prepare("\n\t\t\t\t\t\tSELECT * \n\t\t\t\t\t\tFROM {$wpdb->posts} \n\t\t\t\t\t\tWHERE post_type != 'revision'\n\t\t\t\t\t\tAND post_status = 'publish'\n\t\t\t\t\t\tAND comment_count > 0\n\t\t\t\t\t\tAND ID > %d\n\t\t\t\t\t\tORDER BY ID ASC\n\t\t\t\t\t\tLIMIT {$limit}\n\t\t\t\t\t", $post_id)); $first_post_id = $posts[0]->ID; $last_post_id = $posts[count($posts) - 1]->ID; $max_post_id = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\t\tSELECT MAX(ID)\n\t\t\t\t\t\tFROM {$wpdb->posts} \n\t\t\t\t\t\tWHERE post_type != 'revision'\n\t\t\t\t\t\tAND post_status = 'publish'\n\t\t\t\t\t\tAND comment_count > 0\n\t\t\t\t\t\tAND ID > %d\n\t\t\t\t\t", $post_id)); $eof = (int) ($last_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'; if (count($posts) == 1) { $msg = dsq_i('Processed comments on post #%s…', $first_post_id); } else { $msg = dsq_i('Processed comments on posts #%s-%s…', $first_post_id, $last_post_id); } } $result = 'fail'; $response = null; if (count($posts)) { include_once dirname(__FILE__) . '/export.php'; $wxr = dsq_export_wp($posts); $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', 'last_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) …', $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; } } }