static function sync($threadID, $returnmessage = false) { $message = "no comments on disqus server"; $comments = false; $config = SiteConfig::current_site_config(); $config->disqus_secretkey; $disqus = new DisqusAPI($config->disqus_secretkey); try { $comments = $disqus->threads->listPosts(array("forum" => $config->disqus_shortname, "thread" => "ident:" . $threadID)); } catch (Exception $e) { //user_error ( 'Caught exception (probably cant get thread by ID, does it exists?): ' . $e->getMessage()); } if ($comments) { $message = "There are some comments on disqus server"; // Debug if ($returnmessage) { //print_r($comments); } DB::query("UPDATE DisqusComment SET `isSynced` = 0 WHERE `threadIdentifier` = '{$threadID}'"); foreach ($comments as $comment) { //if ($c = DataObject::get_one('DisqusComment',"disqusId = '$comment->id'")) { if ($c = DisqusComment::get()->filter('disqusId', $comment->id)->First()) { // Comment is already here, fine ;) $message .= " | updating comment id " . $comment->id; } else { // Comment is new, create it $c = new DisqusComment(); $message .= " | adding comment id " . $comment->id; } $c->isSynced = 1; $c->threadIdentifier = $threadID; $c->disqusId = $comment->id; $c->author_name = $comment->author->name; $c->forum = $comment->forum; $c->parent = $comment->parent; $c->thread = $comment->thread; $c->isApproved = $comment->isApproved; $c->isDeleted = $comment->isDeleted; $c->isHighlighted = $comment->isHighlighted; $c->isSpam = $comment->isSpam; $c->createdAt = $comment->createdAt; //$c->ipAddress = $comment->ipAddress; $c->message = $comment->message; // finaly, save it to DB $c->write(); } } if ($returnmessage) { return $message; } }
function DisqusPageComments() { // if the owner DataObject is Versioned, don't display DISQUS until the post is published // to avoid identifier / URL conflicts. if ($this->owner->hasExtension('Versioned') && Versioned::current_stage() == 'Stage') { return '<p class="alert">' . _t("Disqus.NOTLIVEALERT", "Disqus comments are temporary OFF in Stage mode. Logout or turn in Live mode!") . '</p>'; } $config = SiteConfig::current_site_config(); $ti = $this->disqusIdentifier(); if ($config->disqus_shortname && $this->owner->disqusEnabled()) { $script = ' var disqus_shortname = \'' . $config->disqus_shortname . '\'; ' . $this->owner->disqusDeveloperJsVar() . ' var disqus_identifier = \'' . $ti . '\'; var disqus_url = \'' . $this->owner->absoluteLink() . '\'; ' . $this->owner->disqusLocaleJsVar() . ' (function() { var dsq = document.createElement(\'script\'); dsq.type = \'text/javascript\'; dsq.async = true; dsq.src = \'https://\' + disqus_shortname + \'.disqus.com/embed.js\'; (document.getElementsByTagName(\'head\')[0] || document.getElementsByTagName(\'body\')[0]).appendChild(dsq); })(); '; Requirements::customScript($script); $templateData = array('SyncDisqus' => true); // Hide Local Comments -> we will use Disqus service $hideLocal = "\n\t\t\t\tfunction hideLocalComments() {\n\t\t\t\t\tdocument.getElementById('disqus_local').style.display = 'none';\n\t\t\t\t}\n\t\t\t\twindow.onload = hideLocalComments;\n\t\t\t"; Requirements::customScript($hideLocal); // Get comments //$results = DataObject::get('DisqusComment',"isSynced = 1 AND isApproved = 1 AND threadIdentifier = '$ti'"); $results = DisqusComment::get()->filter(array('isSynced' => '1', 'isApproved' => '1', 'threadIdentifier' => $ti)); // Prepare data for template $templateData['LocalComments'] = $results; // Sync comments $now = time(); $synced = strtotime($this->owner->LastEdited); if ($now - $synced > $config->disqus_synctime) { if ($config->disqus_syncinbg) { // background process // from here: https://stackoverflow.com/questions/1993036/run-function-in-background // TODO: Windows check is not fully correct // Debug // echo "trying to sync in BG"; $cmd = "php " . Director::baseFolder() . DIRECTORY_SEPARATOR . "framework" . DIRECTORY_SEPARATOR . "cli-script.php /disqussync/sync_by_ident/" . $ti . "/"; // Debug //echo $cmd; if (substr(php_uname(), 0, 7) == "Windows") { pclose(popen("start /B " . $cmd, "r")); } else { exec($cmd . " > /dev/null &"); } } else { $returnmessage = Director::isDev() ? 1 : 0; DisqusSync::sync($ti, $returnmessage); } // updates LastEdited data $this->owner->write(); // saves the record } else { // Debug // echo "not needed to sync"; } return $this->owner->customise($templateData)->renderWith(array('DisqusComments')); } }