Skip to content

Instantly share code, notes, and snippets.

@readysetawesome
Created January 29, 2012 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save readysetawesome/1696394 to your computer and use it in GitHub Desktop.
Save readysetawesome/1696394 to your computer and use it in GitHub Desktop.
Livefyre's WordPress/PHP sync mechanism
<?php
//on page load the "is this a postback?" filter function runs:
function livefyre_comment_update() {
if (isset($_GET['lf_wp_comment_postback_request']) && $_GET['lf_wp_comment_postback_request']=='1') {
livefyre_do_sync();
// Instruct the backend to use the site sync postback mechanism for future updates.
echo "{\"status\":\"ok\",\"message\":\"sync-initiated\"}";
exit;
}
}
function livefyre_do_sync() {
/*fetch 200 comments from the livefyre server, providing last activity id we have
schedule the next sync if we get a full 200. if there are no more comments, no new sync
*/
$max_activity = livefyre_get_activity_id(); //the last activity_id from the last sync you did
if ($max_activity == null) {
$final_path_seg='';
} else {
$final_path_seg=$max_activity.'/';
}
$url=livefyre_site_rest_url().'/sync/'.$final_path_seg;
$sigcreated_param='sig_created='.time();
$key=get_option('livefyre_secret');
$url.='?'.$sigcreated_param.'&sig='.urlencode(getHmacsha1Signature(base64_decode($key), $sigcreated_param));
$str_comments=livefyre_request($url);
$json_array=json_decode($str_comments);
if (!is_array($json_array)) {
// error reporting confirmed
return;
}
foreach ($json_array as $json) {
if ($json->message_type=='more-data') {
livefyre_schedule_sync(true); //schedule a new job almost immediately using WordPress's cron mechanism
return;
} else if ($json->message_type=='lf-activity') {
$comment_date =(int) $json->created;
$comment_date = get_date_from_gmt(date('Y-m-d H:i:s', $comment_date));
$data = array(
'lf_activity_id' => $json->activity_id,
'lf_action_type' => $json->activity_type,
'comment_post_ID' => $json->article_identifier,
'comment_author' => $json->author,
'comment_author_email' => $json->author_email,
'comment_author_url' => $json->author_url,
'comment_type' => '',
'lf_comment_parent' => $json->lf_parent_id,
'lf_comment_id' => $json->lf_comment_id,
'user_id' => null,
'comment_author_IP' => $json->author_ip,
'comment_agent' => 'Livefyre, Inc. Comments Agent',
'comment_date' => $comment_date,
'lf_state' => $json->state
);
if (isset($json->wp_comment_id)) {
$data['wp_comment_id']=$json->wp_comment_id;
}
if (isset($json->body_text)) {
$data['comment_content'] = $json->body_text;
}
livefyre_insert_activity($data);
}
}
livefyre_schedule_sync();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment