Skip to content

Instantly share code, notes, and snippets.

@gioxx
Last active October 25, 2020 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gioxx/8764c0d66a7257866e51a03c833b4399 to your computer and use it in GitHub Desktop.
Save gioxx/8764c0d66a7257866e51a03c833b4399 to your computer and use it in GitHub Desktop.
Disqus Recents Widget basato su JSON per WordPress. Per saperne di più e capire come utilizzare il plugin dai un'occhiata all'articolo ufficiale su https://wp.me/pdQ5q-dC2 (a partire dalle 9:30 del 26/10/2020).
<?php
/*
Plugin Name: Disqus Recent Comments
Plugin URI: https://gioxx.org/
Description: Show the latest comments from Disqus (using API 3.0 and JSON).
Author: Gioxx
Version: 0.1
Author URI: https://gioxx.org
License: GPL2
*/
/*
Credits: https://help.disqus.com/en/articles/1717320-widgets
https://disqus.com/api/docs/
https://disqus.com/api/docs/forums/listPosts/
https://github.com/TaltonFiggins/disqus-recent-comments/blob/master/disqus-recent-comments.php
https://wordpress.stackexchange.com/a/112578
https://wordpress.stackexchange.com/a/20034
https://stackoverflow.com/a/19164186
https://stackoverflow.com/a/10142695
*/
class wdg_DsqRcnCmmJSON extends WP_Widget {
public function __construct() {
parent::__construct(
'DisqusRecentComments_Widget', // Base ID
'(GX) Disqus Recent Comments (JSON)', // Name
array( 'description' => __( 'Show the latest comments from Disqus', 'text_domain' ), ) // Args
);
}
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$jsonsource = $instance[ 'jsonsource' ];
$cmnts_load = $instance[ 'cmnts_load' ];
$avatar = $instance[ 'avatar' ] ? 'true' : 'false';
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
$request = wp_safe_remote_get($jsonsource);
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
if( ! empty( $data ) ) {
$limit = 0;
foreach( $data->response as $response ) {
$limit++;
$post_author = $response->author->name;
$post_author_profileUrl = $response->author->profileUrl;
$post_author_avatar = $response->author->avatar->small->cache;
$post_thread_link = $response->thread->link;
$post_thread_commentlink = $response->url;
$post_thread_commentdate_Y = substr($response->createdAt,0,4);
$post_thread_commentdate_M = substr($response->createdAt,5,2);
$post_thread_commentdate_D = substr($response->createdAt,8,2);
$post_thread_commentdate_H = substr($response->createdAt,11,5);
//Checking the length of the comment and trimming it if it's too long
$post_message = strip_tags($response->message);
if(strlen($post_message)>120){
$post_message = substr($post_message, 0 , 120);
$post_message = substr($post_message, 0 , strripos($post_message, ' ')).' ...';
}
//Same for the title
$post_thread_title = $response->thread->clean_title;
if(strlen($post_thread_title)>60){$post_thread_title = substr($post_thread_title, 0 , 60);
$post_thread_title = substr($post_thread_title, 0 , strripos($post_thread_title, ' ')).' ...';}
echo '<p>';
if( 'on' == $instance[ 'avatar' ] ) : ?>
<div class="about-us-avatar">
<?php echo '<img src="'.$post_author_avatar.'" style="float: right; padding-left: 5px;" />'; ?>
</div>
<?php endif;
echo ' <strong><a href="'.$post_author_profileUrl.'">'.$post_author.'</a></strong> '.$post_message.'<br />
<small><a href="'.$post_thread_link.'">'.$post_thread_title.'</a> · <a href="'.$post_thread_commentlink.'">'.$post_thread_commentdate_D.'/'.$post_thread_commentdate_M.'/'.$post_thread_commentdate_Y.' '.$post_thread_commentdate_H.'</a></small>
</p>';
if ( $limit == $cmnts_load ) break;
}
}
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['jsonsource'] = $new_instance['jsonsource'];
$instance['cmnts_load'] = $new_instance['cmnts_load'];
$instance['avatar'] = $new_instance['avatar'];
return $instance;
}
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; }
else { $title = __( '', 'text_domain' ); }
if ( isset( $instance[ 'jsonsource' ] ) ) { $jsonsource = $instance[ 'jsonsource' ]; }
else { $jsonsource = __( '', 'text_domain' ); }
if ( isset( $instance[ 'cmnts_load' ] ) ) { $cmnts_load = $instance[ 'cmnts_load' ]; }
else { $cmnts_load = __( '', 'text_domain' ); }
?>
<p>
<!-- Widget title -->
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<!-- JSON Source -->
<label for="<?php echo $this->get_field_id( 'jsonsource' ); ?>"><?php _e( 'JSON URL<br />(example: https://disqus.com/api/3.0/forums/listPosts.json?api_key=PUBLICAPIKEY&forum=gioxx&related=thread). Get your API Key from <a href="https://disqus.com/api/applications/" target="_blank">disqus.com/api/applications</a>:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'jsonsource' ); ?>" name="<?php echo $this->get_field_name( 'jsonsource' ); ?>" type="text" value="<?php echo esc_attr( $jsonsource ); ?>" />
</p>
<p>
<!-- How many comments? -->
<label for="<?php echo $this->get_field_id('cmnts_load'); ?>"><?php _e( 'Comments to load:' ); ?></label>
<select class='widefat' id="<?php echo $this->get_field_id('cmnts_load'); ?>" name="<?php echo $this->get_field_name('cmnts_load'); ?>">
<?php
for ($maxcomments=1; $maxcomments<=24; $maxcomments++) { ?>
<option <?php selected( $instance['cmnts_load'], $maxcomments); ?> value="<?php echo $maxcomments; ?>"><?php echo $maxcomments; ?></option>
<?php } ?>
</select>
</p>
<p>
<!--Avatar-->
<input class="checkbox" type="checkbox" <?php checked( $instance[ 'avatar' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'avatar' ); ?>" name="<?php echo $this->get_field_name( 'avatar' ); ?>" />
<label for="<?php echo $this->get_field_id( 'avatar' ); ?>"><?php _e( 'Show avatars' ); ?></label>
</p>
<?php
}
} // Disqus Recent Comments JSON - The End
// Register widget
function src_load_DsqsRecentsWidget() { register_widget( 'wdg_DsqRcnCmmJSON' ); }
add_action( 'widgets_init', 'src_load_DsqsRecentsWidget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment