📜 ⬆️ ⬇️

Cackle - indexing comments

This topic will be useful and interesting to all those who use the Cackle widget and would like to implement indexing of comments in search engines. Everything that will be described below is already implemented in the plugin for WordPress version 2.0 and higher. If you do not use this CMS, then under the cat a detailed description of the process of synchronizing comments with examples in php.

For indexing, comments should be located in your local database and drawn on html pages using your site, without using the Cackle widget. In general, the synchronization process looks like this:


Creating a table


Everything is simple, you need a data structure that stores comments and we need parameters (example mysql):
CREATE TABLE `comment` ( `comment_id` BIGINT(20) NOT NULL AUTO_INCREMENT, `post_id` BIGINT(20) NULL DEFAULT NULL, `url` VARCHAR(2000) NULL DEFAULT NULL, `parent` VARCHAR(20) NULL DEFAULT NULL, `message` TEXT NULL, `media` VARCHAR(1000) NULL DEFAULT NULL, `status` VARCHAR(11) NULL DEFAULT NULL, `rating` INT(11) NULL DEFAULT NULL, `user_agent` VARCHAR(1000) NULL DEFAULT NULL, `ip` VARCHAR(39) NULL DEFAULT NULL, `author_name` VARCHAR(60) NULL DEFAULT NULL, `author_email` VARCHAR(100) NULL DEFAULT NULL, `author_www` VARCHAR(200) NULL DEFAULT NULL, `author_avatar` VARCHAR(200) NULL DEFAULT NULL, `author_provider` VARCHAR(32) NULL DEFAULT NULL, `anonym_name` VARCHAR(60) NULL DEFAULT NULL, `anonym_email` VARCHAR(100) NULL DEFAULT NULL, `created` DATETIME NULL DEFAULT NULL, PRIMARY KEY (`comment_id`) ); CREATE TABLE `common` ( `common_name` VARCHAR(50) NULL DEFAULT NULL, `common_value` VARCHAR(50) NULL DEFAULT NULL ) 

Small remarks:

PS: For simplicity, I did not divide the table into entities - post, author, anonym, although for speed and normalization this would not be a bad thing to do.

Synchronize comments


You can get Cackle comments via the following RESTful API service:
 http://cackle.me/api/comment/list?siteApiKey={siteApiKey}&accountApiKey={accountApiKey}&id={id} 

Where siteApiKey (Site API Key) and accountApiKey (Account API Key) can be found in the administration panel, on the “Widget” tab, selecting WordPress from the bottom, as shown in the figures below:
image
image
The optional id parameter serves as a cursor, that is, this query will return all id comments that are more than id in the query. If the id parameter is missing, the first 100 comments will be transmitted.
')
Synchronization should work according to the scheme:


The interval between API calls is needed because of the established limit (1000 times per hour, to call any API request).
Php code:
First, we need to define all the constants for the operation of the scripts: connection to the database, crown time, keys to the api Cackle
 //Define db connection settings define('CACKLE_DB_LOCALHOST', "localhost"); define('CACKLE_DB_USER', "root"); define('CACKLE_DB_PASSWORD', ""); define('CACKLE_DB_NAME', "cackle-php" ); //Define timer define('CACKLE_TIMER', 60); //Define Cackle API define('ACCOUNT_API_KEY', ""); define('SITE_API_KEY', ""); 

We need a function to implement the custom crown:
 function time_is_over($cron_time){ $sql="select common_value from common where `common_name` = 'last_time'"; $get_last_time = $this->db_connect($sql, "common_value"); $now=time(); $establish_time_sql="insert into `common` (`common_name`,`common_value`) values ('last_time',$now)"; $delete_time_sql="delete from `common` where `common_name` = 'last_time' and `common_value` > 0;"; if ($get_last_time==null){ $this->db_connect($establish_time_sql); return time(); } else{ if($get_last_time + $cron_time > $now){ return false; } if($get_last_time + $cron_time < $now){ $this->db_connect($delete_time_sql); $this->db_connect($establish_time_sql); return $cron_time; } } } 

The comment_sync function implements the request to api, and saves comments in the database.
 function comment_sync($accountApiKey,$siteApiKey,$cackle_last_comment=0){ $params = "accountApiKey=$accountApiKey&siteApiKey=$siteApiKey&id=$cackle_last_comment"; $host="cackle.me/api/comment/list?$params"; $response = curl($host); $response = $this->cackle_json_decodes($response); $this->push_comments($response); // for       insert_comment() } 

The insert_comment () function itself that stores comments.
 function insert_comm($comment){ ... if ($comment['parentId']) { $comment_parent_id = $comment['parentId']; $sql = "select comment_id from comment where user_agent='Cackle:$comment_parent_id';"; $get_parent_local_id = $this->db_connect($sql, "comment_id"); //    ,      comment_id    user_agent } $commentdata = array( 'url' => $url, 'author_name' => $author_name, 'author_email' => $author_email, 'author_www' => $author_www, 'author_avatar' => $author_avatar, 'author_provider' => $author_provider, 'anonym_name' => $author_anonym_name, 'anonym_email' => $anonym_email, 'rating' => $comment['rating'], 'created' => strftime("%Y-%m-%d %H:%M:%S", $comment['created']/1000), 'ip' => $comment['ip'], 'message' =>$comment['message'], 'status' => $status, 'user_agent' => 'Cackle:' . $comment['id'], 'parent' => $get_parent_local_id ); //   $commentdata   'post_id' = > $post_id,        url    $this->db_connect($this->insCreate("comment",$commentdata)); //    } 

A full working comment sync code is available here .

Drawing comments on html


The final point - draw comments on your page, in the following form:
 <div id="mc-container"> [...         ...] </div> <script> [...   Cackle ...] </script> 

Php code:
Get all comments for this page from the local database:
 function get_local_comments(){ //getting all comments for special post_id from database. //$post_id = 1; $get_all_comments = $this->db_connect("select * from `comment` where `post_id` = $post_id;"); return $get_all_comments; } 

Create a template function to render a separate comment.
 <?php function cackle_comment( $comment) {?> <li id="cackle-comment-<?php echo $comment['comment_id']; ?>"> <div id="cackle-comment-header-<?php echo $comment['comment_id']; ?>" class="cackle-comment-header"> <cite id="cackle-cite-<?php echo $comment['comment_id']; ?>"> <?php if($comment['author_name']) : ?> <a id="cackle-author-user-<?php echo $comment['comment_id']; ?>" href="<?php echo $comment['author_www']; ?>" target="_blank" rel="nofollow"><?php echo $comment['author_name']; ?></a> <?php else : ?> <span id="cackle-author-user-<?php echo $comment['comment_id']; ?>"><?php echo $comment['anonym_name']; ?></span> <?php endif; ?> </cite> </div> <div id="cackle-comment-body-<?php echo $comment['comment_id']; ?>" class="cackle-comment-body"> <div id="cackle-comment-message-<?php echo $comment['comment_id']; ?>" class="cackle-comment-message"> <?php echo $comment['message']; ?> </div> </div> </li> <?php } 

Run through the list of comments
 function list_comments(){ $obj = $this->get_local_comments(); foreach ($obj as $comment) { $this->cackle_comment($comment); } } 

Display html comments with the widget
 function cackle_display_comments(){ ?> <div id="mc-container"> <div id="mc-content"> <ul id="cackle-comments"> <?php $this->list_comments(); ?> </ul> </div> </div> <script type="text/javascript"> var mcSite = '<?php echo $api_id?>'; var mcChannel = '<?php echo $post->ID?>'; document.getElementById('mc-container').innerHTML = ''; (function() { var mc = document.createElement('script'); mc.type = 'text/javascript'; mc.async = true; mc.src = 'http://cackle.me/mc.widget-min.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(mc); })(); </script> <?php } 

As can be seen from js, we clear mc-container so that the list of comments generated by us does not appear on the page.
As a result, only the Cackle widget with comments will be visible on the page, and in the html code there will be a list for indexing by search engines.

Source: https://habr.com/ru/post/154293/


All Articles