Disqus comments are no different from regular WordPress comments. This means that checking the comments on an expertise page will be no different for Disqus comments as it is for standard WordPress comments. Can we still find something special to do with Disqus comments?
The Disqus comment system is a really well thought out commenting system. I had imagined I would need to use the Disqus API to check for new comments on our expertise pages but no such thing; When users submit a comment through Disqus, a local copy is kept as well according to a comment by avinash on MattFlies’ blog. Ofcourse I verified this before believing it by posting a comment:
mysql> select count(*) from wp_comments; +----------+ | count(*) | +----------+ | 84 | +----------+
Then I posted a comment:
mysql> select count(*) from wp_comments; +----------+ | count(*) | +----------+ | 84 | +----------+
So alas, it looks like it’s not true what’s being said! I checked into this with the Disqus support and they recommended that I try their beta version. I installed it but the same issue remained. However at some point, ‘magically’ the Disqus posts would indeed show up in my wp_comments. I guess I had assumed that the update in wp_comments would be immediate after somebody posts a message. This is not quite true; I inspected the code for the Disqus plugin:
add_filter('comments_template', 'dsq_comments_template');
function dsq_comments_template($value) {
...
// Sync comments with database.
dsq_sync_comments($post, $dsq_response['posts']);
...
}
This implies that the dsq_sync_comments() function only gets called when the comments_template hook is triggered. I tried this theory simply by reloading the page after I posted a comment and indeed, the counter on wp_comments increased! Well you could call this a bug or a feature I guess but for my purposes it means that I cannot rely on the WordPress comment functions to work properly under all circumstances. So I decided to mimic the dsq_sync_comments() behaviour in the Expertise plugin’s hourly message check instead:
// Require this in case we use Disqus on our site.
$expert_settings = get_option("expert_settings");
$disqusactive = 0;
if ($expert_settings['commentsmethod'] == 'disqus') {
define ('DISQUS_API_FILE', WP_PLUGIN_DIR . '/disqus-comment-system/lib/api.php');
// Check if API file exists and if plugin is activated
if (file_exists(DISQUS_API_FILE) && function_exists('dsq_sync_comments')) {
require_once(WP_PLUGIN_DIR . '/disqus-comment-system/lib/api.php');
$dsq_api = new DisqusAPI(get_option('disqus_forum_url'), get_option('disqus_api_key'));
$disqusactive = 1;
}
}
function hourly_msgcheck () {
global $disqusactive;
global $dsq_api;
if ($disqusactive) {
$args = array (
'post_type' => 'page',
'numberposts' => -1
);
$expert_posts = get_posts ($args);
if ($expert_posts) {
foreach ($expert_posts as $post) {
setup_postdata($post);
$permalink = get_permalink($post);
$title = get_the_title($post);
$excerpt = get_the_excerpt($post);
// Call "get_thread" API method.
$dsq_response = $dsq_api->get_thread($post, $permalink, $title, $excerpt);
if( $dsq_response < 0 ) {
return false;
}
// Sync comments with database.
dsq_sync_comments($post, $dsq_response['posts']);
}
}
}
}
This seems to work rather well so far, now it’s off to actually using the WordPress API again and checking for new messages to we can finally get to sending the new messages to the experts!