/wp-content/themes/FinanceBlog/lib/widgets/comments.php

https://github.com/temperamente/temperamente · PHP · 166 lines · 142 code · 24 blank · 0 comment · 8 complexity · a2a67fcd9017d71783f6e95b2f61e5c3 MD5 · raw file

  1. <?php
  2. global $theme;
  3. $themater_comments_defaults = array(
  4. 'title' => 'Recent Comments',
  5. 'comments_number' => '5',
  6. 'display_author' => 'true',
  7. 'display_comment' => 'true',
  8. 'display_avatar' => 'true',
  9. 'read_more_text' => '&raquo;',
  10. 'comment_length' => '26',
  11. 'avatar_size' => '32',
  12. 'avatar_align' => 'alignleft'
  13. );
  14. $theme->options['widgets_options']['comments'] = is_array($theme->options['widgets_options']['comments'])
  15. ? array_merge($themater_comments_defaults, $theme->options['widgets_options']['comments'])
  16. : $themater_comments_defaults;
  17. add_action('widgets_init', create_function('', 'return register_widget("ThematerComments");'));
  18. class ThematerComments extends WP_Widget
  19. {
  20. function __construct()
  21. {
  22. $widget_options = array('description' => __('Advanced widget for displaying the recent posts with avatars', 'themater') );
  23. $control_options = array( 'width' => 400);
  24. $this->WP_Widget('themater_comments', '&raquo; Comments with Avatars', $widget_options, $control_options);
  25. }
  26. function widget($args, $instance)
  27. {
  28. global $wpdb, $theme;
  29. extract( $args );
  30. $title = apply_filters('widget_title', $instance['title']);
  31. $comments_number = $instance['comments_number'];
  32. $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_approved, comment_type,
  33. SUBSTRING(comment_content,1,50) AS com_excerpt
  34. FROM $wpdb->comments
  35. LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
  36. WHERE comment_approved = '1' AND comment_type = '' AND post_password = ''
  37. ORDER BY comment_date_gmt DESC
  38. LIMIT $comments_number";
  39. $comments = $wpdb->get_results($sql);
  40. ?>
  41. <ul class="widget-container"><li class="comments-widget">
  42. <?php if ( $title ) { ?> <h3 class="widgettitle"><?php echo $title; ?></h3> <?php } ?>
  43. <ul>
  44. <?php
  45. foreach ($comments as $comment) {
  46. ?>
  47. <li class="clearfix">
  48. <?php
  49. $get_the_peralink = get_permalink($comment->ID) . "#comment-" . $comment->comment_ID;
  50. if( $instance['display_avatar']) { ?>
  51. <a href="<?php echo $get_the_peralink; ?>" title="<?php echo $comment->post_title; ?>"><img class="comments-widget-avatar <?php echo $instance['avatar_align']; ?>" src="http://www.gravatar.com/avatar.php?gravatar_id=<?php echo md5($comment->comment_author_email); ?>&amp;size=<?php echo $instance['avatar_size']; ?>" /></a><?php
  52. }
  53. if($instance['display_comment'] || $instance['display_read_more'] || $instance['display_avatar']) { ?>
  54. <div class="comments-widget-entry">
  55. <?php
  56. if($instance['display_author']) { ?>
  57. <a href="<?php echo $get_the_peralink; ?>" class="comments-widget-author"><?php echo $comment->comment_author; ?></a>: <?php
  58. }
  59. if($instance['display_comment']) {
  60. $get_the_comment_length = $instance['comment_length'] ? $instance['comment_length'] : 16;
  61. echo $theme->shorten(strip_tags($comment->com_excerpt), $get_the_comment_length);
  62. }
  63. if($instance['read_more_text']) { ?>
  64. <a href="<?php echo $get_the_peralink; ?>" class="comments-widget-more"><?php echo $instance['read_more_text']; ?></a><?php
  65. }
  66. ?>
  67. </div><?php
  68. }
  69. ?>
  70. </li>
  71. <?php
  72. }
  73. ?>
  74. </ul>
  75. </li></ul>
  76. <?php
  77. }
  78. function update($new_instance, $old_instance)
  79. {
  80. $instance = $old_instance;
  81. $instance['title'] = strip_tags($new_instance['title']);
  82. $instance['comments_number'] = strip_tags($new_instance['comments_number']);
  83. $instance['display_author'] = strip_tags($new_instance['display_author']);
  84. $instance['display_comment'] = strip_tags($new_instance['display_comment']);
  85. $instance['display_avatar'] = strip_tags($new_instance['display_avatar']);
  86. $instance['read_more_text'] = strip_tags($new_instance['read_more_text']);
  87. $instance['comment_length'] = strip_tags($new_instance['comment_length']);
  88. $instance['avatar_size'] = strip_tags($new_instance['avatar_size']);
  89. $instance['avatar_align'] = strip_tags($new_instance['avatar_align']);
  90. return $instance;
  91. }
  92. function form($instance)
  93. {
  94. global $theme;
  95. $instance = wp_parse_args( (array) $instance, $theme->options['widgets_options']['comments'] );
  96. ?>
  97. <div class="tt-widget">
  98. <table width="100%">
  99. <tr>
  100. <td class="tt-widget-label" width="40%"><label for="<?php echo $this->get_field_id('title'); ?>">Title:</label></td>
  101. <td class="tt-widget-content" width="60%"><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($instance['title']); ?>" /></td>
  102. </tr>
  103. <tr>
  104. <td class="tt-widget-label"><label for="<?php echo $this->get_field_id('comments_number'); ?>">Number Of Comments:</label></td>
  105. <td class="tt-widget-content"><input class="widefat" id="<?php echo $this->get_field_id('comments_number'); ?>" name="<?php echo $this->get_field_name('comments_number'); ?>" type="text" value="<?php echo esc_attr($instance['comments_number']); ?>" /></td>
  106. </tr>
  107. <tr>
  108. <td class="tt-widget-label"><label for="<?php echo $this->get_field_id('comment_length'); ?>">The Comment Length:</label></td>
  109. <td class="tt-widget-content">
  110. <input class="widefat" id="<?php echo $this->get_field_id('comment_length'); ?>" name="<?php echo $this->get_field_name('comment_length'); ?>" type="text" value="<?php echo esc_attr($instance['comment_length']); ?>" />
  111. <br /><span class="tt-widget-help">Number of words</span>
  112. </td>
  113. </tr>
  114. <tr>
  115. <td class="tt-widget-label"><label for="<?php echo $this->get_field_id('read_more_text'); ?>">"Read More" Text:</label></td>
  116. <td class="tt-widget-content"><input class="widefat" id="<?php echo $this->get_field_id('read_more_text'); ?>" name="<?php echo $this->get_field_name('read_more_text'); ?>" type="text" value="<?php echo esc_attr($instance['read_more_text']); ?>" /></td>
  117. </tr>
  118. <tr>
  119. <td class="tt-widget-label">Display Elements:</td>
  120. <td class="tt-widget-content">
  121. <input type="checkbox" name="<?php echo $this->get_field_name('display_author'); ?>" <?php checked('true', $instance['display_author']); ?> value="true" /> <?php _e('Author', 'themater'); ?>
  122. <br /><input type="checkbox" name="<?php echo $this->get_field_name('display_comment'); ?>" <?php checked('true', $instance['display_comment']); ?> value="true" /> <?php _e('The Comment', 'themater'); ?>
  123. <br /><input type="checkbox" name="<?php echo $this->get_field_name('display_avatar'); ?>" <?php checked('true', $instance['display_avatar']); ?> value="true" /> <?php _e('Avatar', 'themater'); ?>
  124. </td>
  125. </tr>
  126. <tr>
  127. <td class="tt-widget-label">Avatar:</td>
  128. <td class="tt-widget-content">
  129. Size: <input type="text" style="width: 40px;" name="<?php echo $this->get_field_name('avatar_size'); ?>" value="<?php echo esc_attr($instance['avatar_size']); ?>" />
  130. Align: <select name="<?php echo $this->get_field_name('avatar_align'); ?>">
  131. <option value="alignleft" <?php selected('alignleft', $instance['avatar_align']); ?> >Left</option>
  132. <option value="alignright" <?php selected('alignright', $instance['avatar_align']); ?>>Right</option>
  133. <option value="aligncenter" <?php selected('aligncenter', $instance['avatar_align']); ?>>Center</option>
  134. </select>
  135. </td>
  136. </tr>
  137. </table>
  138. </div>
  139. <?php
  140. }
  141. }
  142. ?>