Commit 4b7c0718 authored by Thomas's avatar Thomas
Browse files

Add peertube comments

parent eecec33a
......@@ -138,7 +138,7 @@ class Mastodon_api {
*
* @return array $data
*/
protected function get_content_curl ($url,$parameters = array()) {
public function get_content_curl ($url,$parameters = array()) {
$data = array();
// set CURLOPT_USERAGENT
......
......@@ -41,6 +41,9 @@ if( isset($optionsPreferences['selector']) ){
);
}
}
if( isset($optionsPreferences['peertubecomments']) ){
register_setting( $this->plugin_name.'preferences', $this->plugin_name."preferences[peertubecomments]", $optionsPreferences['peertubecomments'] );
}
if( ! isset($_SESSION['mc_step']))
$_SESSION['mc_step'] = 1;
......@@ -200,6 +203,14 @@ if( $_SESSION['mc_step'] == 1 && isset($options['instance_name']) ){
name="<?php echo $this->plugin_name;?>preferences[selector]"
value="<?php echo $targeted_element;?>" />
<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>
<input type="checkbox" id="<?php echo $this->plugin_name;?>-peertubecomments"
name="<?php echo $this->plugin_name;?>preferences[peertubecomments]"
<?php if(
isset($optionsPreferences['peertubecomments']) )
{echo "checked";}?>
/>
</fieldset>
<?php submit_button(__('Save', $this->plugin_name), 'primary','submit', TRUE); ?>
</form>
......@@ -178,7 +178,10 @@ class Mastodon_comments {
$this->loader->add_action( 'wp_ajax_mastodon_comments_getcomment',$plugin_public, 'prefix_ajax_mastodon_comments_getcomment' );
$this->loader->add_action( 'wp_ajax_nopriv_mastodon_comments_getcomment',$plugin_public, 'prefix_ajax_mastodon_comments_getcomment' );
}
$this->loader->add_action( 'wp_ajax_mastodon_comments_getcomment_peertube',$plugin_public, 'prefix_ajax_mastodon_comments_getcomment_peertube' );
$this->loader->add_action( 'wp_ajax_nopriv_mastodon_comments_getcomment_peertube',$plugin_public, 'prefix_ajax_mastodon_comments_getcomment_peertube' );
}
/**
* Run the loader to execute all of the hooks with WordPress.
......
......@@ -97,17 +97,53 @@ class Mastodon_comments_Public {
*/
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/mastodon_comments-public.js', array( 'jquery' ), $this->version, false );
$params =
$optionsPreferences = get_option($this->plugin_name.'preferences');
$params =
array(
'ajaxurl' => admin_url('admin-ajax.php'),
'post_id' => get_the_id(),
'search' => "",
'peertube_comment' => isset($optionsPreferences['peertubecomments']),
'mastodon_comments_nonce' => wp_create_nonce('mastodon_comments_nonce')
);
wp_localize_script( $this->plugin_name, 'params', $params );
}
public function prefix_ajax_mastodon_comments_getcomment_peertube(){
$result = ['comments' => [], 'stats' => ['reblogs' => 0, 'favs' => 0, 'replies' => 0, 'url' => '', 'root' => 0]];
$search = isset($_GET['search']) ? $_GET['search'] : '';
@define('ROOTPATH', __DIR__);
require_once(ROOTPATH.'/../Class/Mastodon_api.php');
$MastodonAPI = new Mastodon_api();
$searchResult = $MastodonAPI->get_content_curl($search);
$resultArray = $searchResult['html']['data'];
$result['stats']['replies'] = $searchResult['html']['total'];
if (isset($resultArray)) {
foreach ($resultArray as $status) {
if( isset($status['account']['avatar']['path']))
$avatar = "https://".$status['account']['host'].$status['account']['avatar']['path'];
else
$avatar = strtolower(plugins_url($this->plugin_name))."/public/img/missing.png";
$result['comments'][$status['id']] = [
'author' => [
'display_name' => $status['account']['name'],
'avatar' => $avatar,
'url' => $status['account']['url']
],
'toot' => $status['text'],
'date' => $status['createdAt'],
'url' => $status['url'],
'reply_to' => null,
'root' => null,
];
}
}
wp_send_json($result);
}
public function prefix_ajax_mastodon_comments_getcomment(){
......
......@@ -40,12 +40,12 @@
'mastodon_comments_nonce': params.mastodon_comments_nonce
},
function(data){
if( typeof data.stats === "undefined")
return;
var targeted_element = "#comments";
if( typeof data.targeted_element !== "undefined")
targeted_element = data.targeted_element;
if( typeof data.stats === "undefined")
return;
var stats = data.stats;
var root = data.stats.root;
var element = jQuery('<div/>', {
......@@ -58,11 +58,6 @@
class: 'mastodon_container_counters',
title: 'Mastodon Comments Counters'
});
var elementComment = jQuery('<div/>', {
id: 'mastodon_comments_content',
class: 'mastodon_container_content',
title: 'Mastodon Comments Content'
});
elementCounter.append('<div id="mastodon-like-count"><a href="' + stats.url + '"><span title="Likes"><i class="fa fa-star"></i>' + stats.favs + '</span></a></div></div>');
elementCounter.append('<div id="mastodon-reblog-count"><a href="' + stats.url + '"><span title="Reblogs"><i class="fa fa-retweet"></i>' + stats.reblogs + '</span></a></div></div>');
elementCounter.append('<div id="mastodon-reply-count"><a href="' + stats.url + '"><span title="Comments"><i class="fa fa-comments"></i>' + stats.replies + '</span></a></div></div>');
......@@ -99,12 +94,65 @@
$(targeted_element).prepend(element);
});
if (parseInt(root) === 0)
element.empty();
$(targeted_element).prepend(element);
$(targeted_element).prepend(element);
}
);
if( params.peertube_comment) {
$(document).ready(function(){
var peertubeComment = jQuery('<div/>', {
id: 'mastodon_comments_peertube'
});
$("iframe").each(function() {
$(this).after($(peertubeComment));
var src= $(this).attr('src');
var regexPeertube = new RegExp('(https?:\\/\\/[\\da-z\\.-]+\\.[a-z\\.]{2,10})\\/videos\\/embed\\/(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12})$');
var result = src.split(regexPeertube);
if( result.length === 4){
var urlPeertube = result[1] + "/api/v1/videos/" + result[2] + "/comment-threads";
jQuery.get(
params.ajaxurl,
{
'action': 'mastodon_comments_getcomment_peertube',
'search': urlPeertube,
'mastodon_comments_nonce': params.mastodon_comments_nonce
},
function(data){
var targeted_element = $(peertubeComment);
if( typeof data.stats === "undefined")
return;
var stats = data.stats;
var element = jQuery('<div/>', {
class: 'comments-container',
title: 'Mastodon Comments'
});
var elementCounter = jQuery('<div/>', {
class: 'mastodon_container_counters',
title: 'Mastodon Comments Counters'
});
elementCounter.append('<div id="mastodon-reply-count"><a href="' + stats.url + '"><span title="Comments"><i class="fa fa-comments"></i>' + stats.replies + '</span></a></div></div>');
element.prepend(elementCounter);
var comments = data.comments;
$.each(comments, function(key, value) {
var timestamp = Date.parse(value.date);
var date = new Date(timestamp);
var comment;
comment = "<div class='comment' id='" + key + "'>";
comment += "<img class='avatar' src='" + value.author.avatar + "' />";
comment += "<div class='author'><a class='displayName' href='" + value.author.url + "'>" + value.author.display_name + "</a> wrote at ";
comment += "<a class='date' href='" + value.url + "'>" + date.toDateString() + ', ' + date.toLocaleTimeString() + "</a></div>";
comment += "<div class='toot'>" + value.toot + "</div>";
comment += "</div>";
element.append(comment);
$(targeted_element).prepend(element);
});
$(targeted_element).prepend(element);
}
);
}
});
});
}
})( jQuery );
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