function run_backfill($site_id)
 {
     $backend_upgrade = $this->ext->get_option('livefyre_backend_upgrade', 'not_started');
     if ($backend_upgrade == 'not_started') {
         # Need to upgrade the backend for this plugin. It's never been done for this site.
         # Since this only happens once, notify the user and then run it.
         $url = LFAPPS_Comments_Core::$quill_url . '/import/wordpress/' . $site_id . '/upgrade';
         $http = new LFAPPS_Http_Extension();
         $resp = $http->request($url, array('timeout' => 10));
         if (is_wp_error($resp)) {
             update_option('livefyre_backend_upgrade', 'error');
             update_option('livefyre_backend_msg', $resp->get_error_message());
             return;
         }
         $resp_code = $resp['response']['code'];
         $resp_message = $resp['response']['message'];
         if ($resp_code != '200') {
             update_option('livefyre_backend_upgrade', 'error');
             $this->lf_core->Raven->captureMessage("Backfill error for site " . $site_id . ": " . $resp->get_error_message());
             return;
         }
         $json_data = json_decode($resp['body']);
         $backfill_status = $json_data->status;
         $backfill_msg = $json_data->msg;
         if ($backfill_status == 'success') {
             $backfill_msg = 'Request for Comments 2 upgrade has been sent';
         }
         update_option('livefyre_backend_upgrade', $backfill_status);
         update_option('livefyre_backend_msg', $backfill_msg);
     }
 }
 function begin()
 {
     if (!isset($_GET['page']) || $_GET['page'] != 'livefyre_apps_comments' || !isset($_GET['livefyre_import_begin'])) {
         return;
     }
     $siteId = get_option('livefyre_apps-livefyre_site_id', '');
     if ($siteId == '') {
         return;
     }
     $url = LFAPPS_Comments_Core::$quill_url . '/import/wordpress/' . $siteId . '/start';
     $http = new LFAPPS_Http_Extension();
     $resp = $http->request($url, array('method' => 'POST'));
     if (is_wp_error($resp)) {
         $status = 'error';
         $message = $resp->get_error_message();
     } else {
         $json = json_decode($resp['body']);
         $status = $json->status;
         $message = $json->message;
     }
     if ($status == 'error') {
         update_option('livefyre_apps-livefyre_import_status', 'error');
         update_option('livefyre_apps-livefyre_import_message', $message);
         $this->ext->delete_option('livefyre_v3_notify_installed');
     } else {
         update_option('livefyre_apps-livefyre_import_status', 'pending');
         $this->ext->delete_option('livefyre_v3_notify_installed');
     }
 }
 function report_error($message)
 {
     $args = array('data' => array('message' => $message, 'method' => 'POST'));
     $url = $this->lf_core->http_url . '/site/' . get_option('livefyre_apps-livefyre_site_id');
     $http = new LFAPPS_Http_Extension();
     $http->request($url . '/error', $args);
 }
<?php

if ($display_template) {
    global $wp_query, $post;
    if ($parent_id = wp_is_post_revision($wp_query->post->ID)) {
        $post_id = $parent_id;
    } else {
        $post_id = $post->ID;
    }
    $url = LFAPPS_Comments_Core::$bootstrap_url_v3 . '/' . base64_encode($post_id) . '/bootstrap.html';
    $lfHttp = new LFAPPS_Http_Extension();
    $result = $lfHttp->request($url);
    $cached_html = '';
    if ($result['response']['code'] == 200) {
        $cached_html = $result['body'];
        $cached_html = preg_replace('(<script>[\\w\\W]*<\\/script>)', '', $cached_html);
    }
    echo '<div id="' . esc_attr($livefyre_element) . '">' . wp_kses_post($cached_html) . '</div>';
}
?>

<script type="text/javascript">
    var networkConfigComments = {
<?php 
echo isset($strings) ? 'strings: ' . json_encode($strings) . ',' : '';
?>
        network: "<?php 
echo esc_js($network->getName());
?>
"
    };
 /**
  * Get the ActivityStream for current LF credentials
  * 
  */
 public static function get_activity_stream()
 {
     $api_urn = 'urn:livefyre:api:core=GetActivityStream';
     $expiration = time() + 1000 * 60 * 5;
     //5 mins
     $network_urn = 'urn:livefyre:' . get_option('livefyre_apps-livefyre_domain_name');
     $data = array('iss' => $network_urn, 'aud' => $network_urn, 'sub' => $network_urn, 'scope' => $api_urn, 'exp' => $expiration);
     $token = LFAPPS_JWT::encode($data, get_option('livefyre_apps-livefyre_domain_key'));
     $header = array('Authorization' => 'Bearer ' . $token);
     $domain = get_option('livefyre_apps-livefyre_domain_name');
     $domain_part = substr($domain, 0, strpos($domain, '.'));
     $url = 'https://' . $domain_part . '.bootstrap.fyre.co/api/v3.1/activity/?resource=urn:livefyre:umg.fyre.co&since=0';
     $http = new LFAPPS_Http_Extension();
     $resp = $http->request($url, array('timeout' => 5, 'headers' => $header));
     if (isset($resp['body'])) {
         $body = json_decode($resp['body'], true);
         return $body;
     }
     return null;
 }
 function do_sync()
 {
     /*
         Fetch comments from the livefyre server, providing last activity id we have.
         Schedule the next sync if we got >50 or the server says "more-data".
         If there are no more comments, schedule a sync for several hrs out.
     */
     $result = array('status' => 'ok', 'message' => 'The sync process completed successfully.', 'last-message-type' => null, 'activities-handled' => 0);
     $inserts_remaining = LFAPPS_SYNC_MAX_INSERTS;
     $max_activity = $this->ext->get_option('livefyre_activity_id', '0');
     if ($max_activity == '0') {
         $final_path_seg = '';
     } else {
         $final_path_seg = $max_activity . '/';
     }
     $url = $this->site_rest_url() . '/sync/' . $final_path_seg;
     $qstring = 'page_size=' . $inserts_remaining . '&plugin_version=' . LFAPPS_COMMENTS_PLUGIN_VERSION . '&sig_created=' . time();
     $key = get_option('livefyre_apps-livefyre_site_key');
     $url .= '?' . $qstring . '&sig=' . urlencode(getHmacsha1Signature(base64_decode($key), $qstring));
     $http = new LFAPPS_Http_Extension();
     $http_result = $http->request($url, array('timeout' => 120));
     if (is_array($http_result) && isset($http_result['response']) && $http_result['response']['code'] == 200) {
         $str_comments = $http_result['body'];
     } else {
         $str_comments = '';
     }
     $json_array = json_decode($str_comments);
     if (!is_array($json_array)) {
         $this->schedule_sync(LFAPPS_SYNC_LONG_TIMEOUT);
         $error_message = 'Error during do_sync: Invalid response ( not a valid json array ) from sync request to url: ' . $url . ' it responded with: ' . $str_comments;
         $this->livefyre_report_error($error_message);
         return array_merge($result, array('status' => 'error', 'message' => $error_message));
     }
     $data = array();
     // What to record for the "latest" id we know about, when done inserting
     $last_activity_id = 0;
     // By default, we don't queue an other near-term sync unless we discover the need to
     $timeout = LFAPPS_SYNC_LONG_TIMEOUT;
     $first = true;
     foreach ($json_array as $json) {
         $mtype = $json->message_type;
         if ($mtype == LFAPPS_SYNC_ERROR) {
             // An error was encountered, don't schedule next sync for near-term
             $timeout = LFAPPS_SYNC_LONG_TIMEOUT;
             break;
         }
         if ($mtype == LFAPPS_SYNC_MORE) {
             // There is more data we need to sync, schedule next sync soon
             $timeout = LFAPPS_SYNC_SHORT_TIMEOUT;
             break;
         }
         if ($mtype == LFAPPS_SYNC_ACTIVITY) {
             $last_activity_id = $json->activity_id;
             $inserts_remaining--;
             $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_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 ($first) {
                 $first_id_msg = 'Livefyre: Processing activity page starting with ' . $data['lf_activity_id'];
                 $first = false;
             }
             if (isset($json->lf_parent_comment_id)) {
                 $data['lf_comments_parent'] = $json->lf_parent_comment_id;
             }
             if (isset($json->body_text)) {
                 $data['comment_content'] = $json->body_text;
             }
             $this->livefyre_insert_activity($data);
             if (!$inserts_remaining) {
                 $timeout = LFAPPS_SYNC_SHORT_TIMEOUT;
                 break;
             }
         }
     }
     $result['last-message-type'] = $mtype;
     $result['activities-handled'] = LFAPPS_SYNC_MAX_INSERTS - $inserts_remaining;
     $result['last-activity-id'] = $last_activity_id;
     if ($last_activity_id) {
         $activity_update = $this->ext->update_option('livefyre_activity_id', $last_activity_id);
         if (!$activity_update) {
         }
         $last_id_msg = 'Livefyre: Set last activity ID processed to ' . $last_activity_id;
     }
     $this->schedule_sync($timeout);
     return $result;
 }