wid-comment.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. add_action( 'widgets_init', 'd_comments' );
  3. function d_comments() {
  4. register_widget( 'd_comment' );
  5. }
  6. class d_comment extends WP_Widget {
  7. function d_comment() {
  8. $widget_ops = array( 'classname' => 'd_comment', 'description' => '显示网友最新评论(头像+名称+评论)' );
  9. $this->WP_Widget( 'd_comment', 'Yusi-最新评论', $widget_ops, $control_ops );
  10. }
  11. function widget( $args, $instance ) {
  12. extract( $args );
  13. $title = apply_filters('widget_name', $instance['title']);
  14. $limit = $instance['limit'];
  15. $outer = $instance['outer'];
  16. $outpost = $instance['outpost'];
  17. $more = $instance['more'];
  18. $link = $instance['link'];
  19. $mo='';
  20. if( $more!='' && $link!='' ) $mo='<a class="btn" href="'.$link.'">'.$more.'</a>';
  21. echo $before_widget;
  22. echo $before_title.$mo.$title.$after_title;
  23. echo '<ul>';
  24. echo mod_newcomments( $limit,$outpost,$outer );
  25. echo '</ul>';
  26. echo $after_widget;
  27. }
  28. function form($instance) {
  29. ?>
  30. <p>
  31. <label>
  32. 标题:
  33. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" />
  34. </label>
  35. </p>
  36. <p>
  37. <label>
  38. 显示数目:
  39. <input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="number" value="<?php echo $instance['limit']; ?>" />
  40. </label>
  41. </p>
  42. <p>
  43. <label>
  44. 排除某用户ID:
  45. <input class="widefat" id="<?php echo $this->get_field_id('outer'); ?>" name="<?php echo $this->get_field_name('outer'); ?>" type="number" value="<?php echo $instance['outer']; ?>" />
  46. </label>
  47. </p>
  48. <p>
  49. <label>
  50. 排除某文章ID:
  51. <input class="widefat" id="<?php echo $this->get_field_id('outpost'); ?>" name="<?php echo $this->get_field_name('outpost'); ?>" type="text" value="<?php echo $instance['outpost']; ?>" />
  52. </label>
  53. </p>
  54. <p>
  55. <label>
  56. More 显示文字:
  57. <input style="width:100%;" id="<?php echo $this->get_field_id('more'); ?>" name="<?php echo $this->get_field_name('more'); ?>" type="text" value="<?php echo $instance['more']; ?>" size="24" />
  58. </label>
  59. </p>
  60. <p>
  61. <label>
  62. More 链接:
  63. <input style="width:100%;" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="url" value="<?php echo $instance['link']; ?>" size="24" />
  64. </label>
  65. </p>
  66. <?php
  67. }
  68. }
  69. function mod_newcomments( $limit,$outpost,$outer ){
  70. global $wpdb;
  71. $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,40) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_post_ID!='".$outpost."' AND user_id!='".$outer."' AND comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT $limit";
  72. $comments = $wpdb->get_results($sql);
  73. foreach ( $comments as $comment ) {
  74. $output .= '<li><a href="'.get_permalink($comment->ID).'#comment-'.$comment->comment_ID.'" title="'.$comment->post_title.'上的评论">'.str_replace(' src=', ' data-original=', get_avatar( $comment->comment_author_email, $size = '36' , deel_avatar_default())).' <div class="muted"><i>'.strip_tags($comment->comment_author).'</i>'.timeago( $comment->comment_date_gmt ).'说:'.str_replace(' src=', ' data-original=', convert_smilies(strip_tags($comment->com_excerpt))).'</div></a></li>';
  75. }
  76. echo $output;
  77. };
  78. ?>