Commit f5bb8cd3 authored by Thomas's avatar Thomas
Browse files

Prepares cache feature

parent 716e4720
......@@ -26,19 +26,7 @@ if( isset($optionsAccount['disconnect']) && $optionsAccount['disconnect'] == 1){
delete_option($this->plugin_name.'account');
}
if( isset($optionsPreferences['selector']) ){
$currentUser = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix . "mastodon_comment_users ORDER BY date DESC LIMIT 1", OBJECT );
if( !empty($currentUser) ){
$wpdb->query('UPDATE '. $wpdb->prefix . 'mastodon_comment_users SET targeted_element = "'.$optionsPreferences['selector'].'" WHERE id="' . $currentUser[0]->id. '"');
}else{
$wpdb->insert(
$wpdb->prefix . 'mastodon_comment_users',
array(
'targeted_element' => $optionsPreferences['selector']
)
);
}
}
if( isset($optionsPreferences['peertubecomments']) ){
register_setting( $this->plugin_name.'preferences', $this->plugin_name."preferences[peertubecomments]", $optionsPreferences['peertubecomments'] );
}
......@@ -60,18 +48,18 @@ if( isset($optionsPreferences['peertube_text']) ){
if( isset($optionsPreferences['peertube_link']) ){
register_setting( $this->plugin_name.'preferences', $this->plugin_name."preferences[peertube_link]", $optionsPreferences['peertube_link'] );
}
if( isset($optionsPreferences['cache']) ){
register_setting( $this->plugin_name.'preferences', $this->plugin_name."preferences[cache]", $optionsPreferences['cache'] );
}
if( isset($optionsPreferences['selector']) ){
register_setting( $this->plugin_name.'preferences', $this->plugin_name."preferences[selector]", $optionsPreferences['selector'] );
}
if( ! isset($_SESSION['mc_step']))
$_SESSION['mc_step'] = 1;
$currentUser = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix . "mastodon_comment_users ORDER BY date DESC LIMIT 1", OBJECT );
$targeted_element = "#comments";
if( !empty($currentUser) && !empty($currentUser[0]->targeted_element)){
$targeted_element = $currentUser[0]->targeted_element;
}
//Check if there is a valid account in DB
if( !empty($currentUser) && !empty($currentUser[0]->mastodon_instance) && strlen(trim($currentUser[0]->mastodon_instance)) > 0 )
......@@ -228,7 +216,7 @@ if ( isset($options['oauth']) ){
<input type="text" id="<?php echo $this->plugin_name;?>-selector"
placeholder="<?php _e('Use a jquery selector', $this->plugin_name);?>"
name="<?php echo $this->plugin_name;?>preferences[selector]"
value="<?php echo $targeted_element;?>" />
value="<?php echo isset($optionsPreferences['selector'])?$optionsPreferences['selector']:"#comments";?>" />
<a href="https://api.jquery.com/category/selectors/" target="_blank" title="<?php _e('JQuery selectors', $this->plugin_name);?>" ><?php _e('More about JQuery selectors', $this->plugin_name);?></a>
<br/><br/>
<label for="<?php echo $this->plugin_name;?>-peertubecomments"><?php _e('Display peertube comments', $this->plugin_name);?></label>
......@@ -238,6 +226,12 @@ if ( isset($options['oauth']) ){
isset($optionsPreferences['peertubecomments']) )
{echo "checked";}?>
/>
<br/><br/>
<label for="<?php echo $this->plugin_name;?>-cache"><?php _e('Cache in minutes', $this->plugin_name);?></label>
<input type="number" id="<?php echo $this->plugin_name;?>-cache"
name="<?php echo $this->plugin_name;?>preferences[cache]"
value="<?php echo isset($optionsPreferences['cache'])?$optionsPreferences['cache']:5;?>"
/>
</fieldset>
<h2 class="nav-tab-wrapper"><?php _e('Custom colors', $this->plugin_name);?></h2>
<fieldset style="margin-top: 10px;">
......
......@@ -110,6 +110,7 @@ class Mastodon_comments_Public {
'peertube_bg' => $optionsPreferences['peertube_bg'],
'peertube_text' => $optionsPreferences['peertube_text'],
'peertube_link' => $optionsPreferences['peertube_link'],
'selector' => $optionsPreferences['selector'],
'mastodon_comments_nonce' => wp_create_nonce('mastodon_comments_nonce')
);
wp_localize_script( $this->plugin_name, 'params', $params );
......@@ -117,6 +118,7 @@ class Mastodon_comments_Public {
public function prefix_ajax_mastodon_comments_getcomment_peertube(){
global $wpdb;
$result = ['comments' => [], 'stats' => ['reblogs' => 0, 'favs' => 0, 'replies' => 0, 'url' => '', 'root' => 0]];
$search = isset($_GET['search']) ? $_GET['search'] : '';
@define('ROOTPATH', __DIR__);
......@@ -124,7 +126,48 @@ class Mastodon_comments_Public {
$MastodonAPI = new Mastodon_api();
$source = isset($_GET['source']) ? $_GET['source'] : '#';
$source = str_replace("embed","watch",$source);
$searchResult = $MastodonAPI->get_content_curl($search);
$cachedURL = $wpdb->get_results(
"SELECT * FROM ".$wpdb->prefix . "mastodon_comment_cache
WHERE URL='".urlencode($search)."' ORDER BY date DESC LIMIT 1", OBJECT );
$needRefresh = false;
//Something in cache
if( !empty($cachedURL)){
$date = $cachedURL[0]->date;
$optionsPreferences = get_option($this->plugin_name.'preferences');
$cache_time = $optionsPreferences['cache_time'];
$cache_time = isset($cache_time)?$cache_time:5;
if((time()-($cache_time*60)) > strtotime($date)){
$needRefresh = true;
}
}else{
$needRefresh = true;
}
if ($needRefresh) {
$searchResult = $MastodonAPI->get_content_curl( $search );
$dateInsert = new DateTime();
//Cache the status
if( empty($cachedURL)) {
$wpdb->insert(
$wpdb->prefix . 'mastodon_comment_cache',
array(
'date' => $dateInsert->format( 'Y-m-d H:i:s' ),
'URL' => urlencode( $search ),
'cache' => json_encode( $searchResult )
)
);
} else{ //Update cache
$wpdb->query(
"UPDATE ".$wpdb->prefix . "mastodon_comment_cache
SET cache='".json_encode( $searchResult )."', date = '".$dateInsert->format( 'Y-m-d H:i:s' )."'
WHERE URL='".urlencode($search)."'");
}
}else{
$searchResult = json_decode($cachedURL[0]->cache, true);
}
$resultArray = $searchResult['html']['data'];
$result['stats']['replies'] = $searchResult['html']['total'];
$domain = explode("/",str_replace("https://","",$source))[0];
......@@ -181,10 +224,24 @@ class Mastodon_comments_Public {
$collector = new CollectMastodonData($config);
$ids = [];
if (!empty($search)) {
$oldCollection = $collector->getCachedCollection($search);
//if (empty($oldCollection)) {
$cachedURL = $wpdb->get_results(
"SELECT * FROM ".$wpdb->prefix . "mastodon_comment_cache
WHERE URL='".urlencode($search)."' ORDER BY date DESC LIMIT 1", OBJECT );
$needRefresh = false;
//Something in cache
if( !empty($cachedURL)){
$date = $cachedURL[0]->date;
$optionsPreferences = get_option($this->plugin_name.'preferences');
$cache_time = $optionsPreferences['cache_time'];
$cache_time = isset($cache_time)?$cache_time:5;
if((time()-($cache_time*60)) > strtotime($date)){
$needRefresh = true;
}
}else{
$needRefresh = true;
}
if ($needRefresh) {
$ids = $collector->findToots($search);
$result['stats']['root'] = isset($ids[0]) ? $ids[0] : 0;
foreach ($ids as $id) {
......@@ -192,16 +249,32 @@ class Mastodon_comments_Public {
$collector->getComments($id, $result);
// get statistics (likes, replies, boosts,...)
$collector->getStatistics($id, $result);
// FIXME: At the moment the API doesn't return the correct replies count so I count it manually
$result['stats']['replies'] = count($result['comments']);
}
$collector->storeCollection($search, $result);
// } else {
// $result = $oldCollection;
//}
$dateInsert = new DateTime();
//Cache the status
if( empty($cachedURL)) {
$wpdb->insert(
$wpdb->prefix . 'mastodon_comment_cache',
array(
'date' => $dateInsert->format( 'Y-m-d H:i:s' ),
'URL' => urlencode( $search ),
'cache' => json_encode( $result )
)
);
} else{ //Update cache
$wpdb->query(
"UPDATE ".$wpdb->prefix . "mastodon_comment_cache
SET cache='".json_encode( $result )."', date = '".$dateInsert->format( 'Y-m-d H:i:s' )."'
WHERE URL='".urlencode($search)."'");
}
}else{
$result = json_decode($cachedURL[0]->cache, true);
}
}
if( !empty($currentUser[0]->targeted_element) && strlen(trim($currentUser[0]->targeted_element)) > 1)
$result['targeted_element'] = $currentUser[0]->targeted_element;
// send the result now
wp_send_json($result);
}
......
......@@ -36,13 +36,13 @@
{
'action': 'mastodon_comments_getcomment',
'post_id': params.post_id,
'search': "https://mastalab.app/2018/09/02/how-to-login/",
'search': window.location.href,
'mastodon_comments_nonce': params.mastodon_comments_nonce
},
function(data){
var targeted_element = "#comments";
if( typeof data.targeted_element !== "undefined")
targeted_element = data.targeted_element;
if( typeof params.selector !== "undefined")
targeted_element = params.selector;
if( typeof data.stats === "undefined")
return;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment