Patch File for wpbooker to Fix Spam Comments Syncing with Facebook

So I am using wordbooker to sync the blog posts and comments with facebook.

The latest version of wordbooker seem to sync the comments, including spam, onto facebook.

Thanks to Stephen, after some back and forth on their support forum over the Chinese New Year, we’ve got a patch below which may end up in the wordbooker’s next update or release.

Get it here wordbooker.php.tar
As always, backup your wordbooker.php file.  Untar it.  Test it with spams and normal comments.  Let me know how it works.

Continue reading if you
want to know why the spam was synced to facebook and what this patch does.
You can also read the original forum thread over here

The commenting syncing is done in wordbooker.php with the

add_action('comment_post', 'wordbooker_post_comment');

The thing is that this call is triggered when a comment is posted.  This happens before a comment is auto marked as a spam by akismet or other spam filter.  It also precedes manual approval of a comment or manual marking of it as a spam.  So spam comments were happily being synced to facebook before being marked as spam and users of wordbooker would have to delete these spam comments.  I was one of them.

A check is added to make sure that only auto-approved comments are synced to facebook.  Further, the above hook’s priority is “lowered” to “20”.  Default priority is 10, 20 is a lower priority level than 20.  Don’t ask me why this is so.  This is a convention by wordpress.  It is a bit like the process ‘nice’ level in linux.

add_action('comment_post', 'wordbooker_post_comment', 20);

 

But this meant that comments manually approved would be missed.  A new problem.

After some digging, I found that there is a “wp_set_comment_status” function that is triggered when a comment is manually approved, spammed or unapproved etc. This would fit in nicely, but would mean that an identical syncing function would have to be written.  Initially, I thought that all comments that are approved would trigger this call.  So I renamed the “wordbooker_post_comment” function to “wordbooker_set_comment_status” and hook it as follows.

add_action('wp_set_comment_status', 'wordbooker_set_comment_status', 20, 2);

The ’20’ is priority level and ‘2’ indicates that the hook function receives two params.  It was not working without the ‘2’ param.  Some googling found an answer.

http://wordpress.org/support/topic/wp_set_comment_status-error-missing-argument-2

This worked, but I found that the auto approved comments were now skipped.  In the end, I wrote a wrapper function to process those auto approved comments and hooked it to “comment_post”.  So there are now two function hooks to process comment syncing.

add_action('comment_post', 'wordbooker_post_comment', 20);
add_action(‘wp_set_comment_status’, ‘wordbooker_set_comment_status’, 20, 2);

The wrapper function is as follows

 

// 20110207 CGZX - Need this wrapper to process autoapproved comments
function wordbooker_post_comment($commentid) {
$wordbooker_settings = wordbooker_options();
if ( !isset($wordbooker_settings[‘wordbook_comment_push’])) {
return;
}
global  $wpdb, $user_id,$table_prefix;
$comment_status = wp_get_comment_status($commentid);

if ($comment_status == "approved")
{
 wordbooker_debugger("CGZX:processing auto approved comment”,” “,$cpid,0) ;wordbooker_set_comment_status($commentid, $comment_status);

}
else
{
wordbooker_debugger(“CGZX:comment [$commentid] is currently $comment_status … skipped”,” “,$cpid,0) ;
}
}

 

The “wordbooker_set_comment_status” function is patched as follows:

 

function wordbooker_set_comment_status($commentid, $comment_status) {
$wordbooker_settings = wordbooker_options();
if ( !isset($wordbooker_settings['wordbook_comment_push'])) {
return;
}

global  $wpdb, $user_id,$table_prefix;
$comment= get_comment($commentid);

wordbooker_debugger("CGZX:wordbooker_set_comment_status:enter ($comment_status)"," ",$cpid,0) ;

if ($comment_status == "spam")
{
wordbooker_debugger("CGZX:Spam comment rejected "," ",$cpid,0) ;
return true;
}

if ($comment_status != "approve")
{
wordbooker_debugger("CGZX:comment is currently ($comment_status) "," ",$cpid,0) ;
return true;
}

wordbooker_debugger("CGZX:processing approved comment "," ",$cpid,0) ;

... ...
}
All the debugger statements can be removed if needed.  I included them for tracing.  Hope this helps.