/plugins/freshcomments/trunk/freshcomments.plugin.php

https://github.com/somefool/habari-extras · PHP · 200 lines · 151 code · 29 blank · 20 comment · 16 complexity · 2ca45ddc51b430426b564dd70a7a9248 MD5 · raw file

  1. <?php
  2. /**
  3. * Fresh Comments
  4. *
  5. * For those who really miss Brian’s Latest Comments. :)
  6. **/
  7. class FreshComments extends Plugin
  8. {
  9. private $cache_name = '';
  10. private $range_in_seconds = 0;
  11. private $color_diff = array( 'r' => 0, 'g' => 0, 'b' => 0 );
  12. private static function default_options( )
  13. {
  14. return array (
  15. 'post_count' => 5,
  16. 'comment_count' => 6,
  17. 'show_trackbacks' => FALSE,
  18. 'show_pingbacks' => FALSE,
  19. 'fade_out' => TRUE,
  20. 'range_in_days' => 10,
  21. 'newest_color' => '#444444',
  22. 'oldest_color' => '#cccccc'
  23. );
  24. }
  25. public function action_block_form_freshcomments( $form, $block )
  26. {
  27. // Load defaults
  28. foreach ( self::default_options( ) as $k => $v ) {
  29. if ( !isset( $block->$k ) )
  30. $block->$k = $v;
  31. }
  32. $form->append( 'fieldset', 'general', _t( 'General', 'freshcomments' ) );
  33. $form->general->append( 'text', 'post_count', $block, _t( '&#8470; of Posts', 'freshcomments' ) );
  34. $form->general->post_count->add_validator( array( $this, 'validate_uint' ) );
  35. $form->general->post_count->add_validator( 'validate_required' );
  36. $form->general->append( 'text', 'comment_count', $block, _t( '&#8470; of Comments', 'freshcomments' ) );
  37. $form->general->comment_count->add_validator( array( $this, 'validate_uint' ) );
  38. $form->general->comment_count->add_validator( 'validate_required' );
  39. $form->general->append( 'checkbox', 'show_trackbacks', $block, _t( 'Show Trackbacks', 'freshcomments' ) );
  40. $form->general->append( 'checkbox', 'show_pingbacks', $block, _t( 'Show Pingbacks', 'freshcomments' ) );
  41. $form->append( 'fieldset', 'fade', _t( 'Fade-out', 'freshcomments' ) );
  42. $form->fade->append( 'checkbox', 'fade_out', $block, _t( 'Fade-out Older Comment', 'freshcomments' ) );
  43. $form->fade->append( 'text', 'range_in_days', $block, _t( 'Fade-out Speed (bigger is slower )', 'freshcomments' ) );
  44. $form->fade->range_in_days->add_validator( array( $this, 'validate_uint' ) );
  45. $form->fade->range_in_days->add_validator( 'validate_required' );
  46. $form->fade->append( 'text', 'newest_color', $block, _t( 'Newest Color', 'freshcomments' ) );
  47. $form->fade->newest_color->add_validator( array( $this, 'validate_color' ) );
  48. $form->fade->newest_color->add_validator( 'validate_required' );
  49. $form->fade->append( 'text', 'oldest_color', $block, _t( 'Oldest Color', 'freshcomments' ) );
  50. $form->fade->oldest_color->add_validator( array( $this, 'validate_color' ) );
  51. $form->fade->oldest_color->add_validator( 'validate_required' );
  52. }
  53. public function validate_uint( $value )
  54. {
  55. if ( !ctype_digit( $value ) || strstr( $value, '.' ) || $value < 0 ) {
  56. return array( _t( 'This field must be positive integer.', 'freshcomments' ) );
  57. }
  58. return array( );
  59. }
  60. public function validate_color( $value )
  61. {
  62. if ( !preg_match( '/^#[0-9a-f]{6}$/i', $value ) ) {
  63. return array( _t( 'This field must be an HTML color hex code.', 'freshcomments' ) );
  64. }
  65. return array( );
  66. }
  67. public function action_block_content_freshcomments( $block, $theme )
  68. {
  69. // Load defaults
  70. foreach ( self::default_options( ) as $k => $v ) {
  71. if ( !isset( $block->$k ) )
  72. $block->$k = $v;
  73. }
  74. // Calculate colors
  75. if ( $block->fade_out ) {
  76. $this->range_in_seconds = $block->range_in_days * 24 * 60 * 60;
  77. $newest_color = ColorUtils::hex_rgb( $block->newest_color );
  78. $oldest_color = ColorUtils::hex_rgb( $block->oldest_color );
  79. $this->color_diff = array(
  80. 'r' => $oldest_color[ 'r' ] - $newest_color[ 'r' ],
  81. 'g' => $oldest_color[ 'g' ] - $newest_color[ 'g' ],
  82. 'b' => $oldest_color[ 'b' ] - $newest_color[ 'b' ]
  83. );
  84. }
  85. if ( Cache::has( $this->cache_name ) ) {
  86. $block->freshcomments = Cache::get( $this->cache_name );
  87. } else {
  88. $comment_types = array( Comment::COMMENT );
  89. if ( $block->show_trackbacks ) $comment_types[ ] = Comment::TRACKBACK;
  90. if ( $block->show_pingbacks ) $comment_types[ ] = Comment::PINGBACK;
  91. $query = 'SELECT {posts}.* FROM {posts} INNER JOIN {comments} ON ( {posts}.id = {comments}.post_id ) WHERE {posts}.status = ? AND {comments}.status = ? AND ( {comments}.type = ?' . str_repeat( ' OR {comments}.type = ?', count( $comment_types ) - 1 ) . ' ) GROUP BY {posts}.id ORDER BY {comments}.date DESC, {posts}.id DESC LIMIT ' . $block->post_count;
  92. $query_args = array_merge( array( Post::status( 'published' ), Comment::STATUS_APPROVED ), $comment_types );
  93. $commented_posts = DB::get_results( $query, $query_args, 'Post' );
  94. $freshcomments = array( );
  95. foreach ( $commented_posts as $i => $post ) {
  96. $query = 'SELECT * FROM {comments} WHERE post_id = ? AND status = ? AND ( type = ?' . str_repeat( ' OR type = ?', count( $comment_types ) - 1 ) . ' ) ORDER BY date DESC LIMIT ' . $block->comment_count;
  97. $query_args = array_merge( array( $post->id, Comment::STATUS_APPROVED ), $comment_types );
  98. $comments = DB::get_results( $query, $query_args, 'Comment' );
  99. $freshcomments[ $i ][ 'post' ] = $post;
  100. foreach ( $comments as $j => $comment ) {
  101. $freshcomments[ $i ][ 'comments' ][ $j ][ 'comment' ] = $comment;
  102. $freshcomments[ $i ][ 'comments' ][ $j ][ 'color' ] = $this->get_color( $comment->date->int, $block->newest_color, $block->fade_out );
  103. }
  104. }
  105. $block->freshcomments = $freshcomments;
  106. Cache::set( $this->cache_name, $block->freshcomments, 3600 );
  107. }
  108. }
  109. private static function sanitize_color( $new_color )
  110. {
  111. return round( max( 0, min( 255, $new_color ) ) );
  112. }
  113. private function get_color( $comment_date, $newest_color='#444444', $fade_out=TRUE )
  114. {
  115. if ( $fade_out ) {
  116. $time_span = ( $_SERVER[ 'REQUEST_TIME' ] - $comment_date ) / $this->range_in_seconds;
  117. $time_span = min( $time_span, 1 );
  118. $newest_color = ColorUtils::hex_rgb( $newest_color );
  119. $color = array(
  120. 'r' => self::sanitize_color( $newest_color[ 'r' ] + $this->color_diff[ 'r' ] * $time_span ),
  121. 'g' => self::sanitize_color( $newest_color[ 'g' ] + $this->color_diff[ 'g' ] * $time_span ),
  122. 'b' => self::sanitize_color( $newest_color[ 'b' ] + $this->color_diff[ 'b' ] * $time_span )
  123. );
  124. return '#' . ColorUtils::rgb_hex( $color );
  125. } else {
  126. return $newest_color;
  127. }
  128. }
  129. /**
  130. * After a new and approved comment is inserted, expire the cache
  131. */
  132. public function action_comment_insert_after( $comment )
  133. {
  134. if ( $comment->status === COMMENT::STATUS_APPROVED ) {
  135. Cache::expire( $this->cache_name );
  136. }
  137. }
  138. /**
  139. * After an approved comment is updated, expire the cache
  140. */
  141. public function action_comment_update_after( $comment )
  142. {
  143. if ( $comment->status === COMMENT::STATUS_APPROVED ) {
  144. Cache::expire( $this->cache_name );
  145. }
  146. }
  147. /**
  148. * After an approved comment is deleted, expire the cache
  149. */
  150. public function action_comment_delete_after( $comment )
  151. {
  152. if ( $comment->status === COMMENT::STATUS_APPROVED ) {
  153. Cache::expire( $this->cache_name );
  154. }
  155. }
  156. /**
  157. * On plugin init, add the template included with this plugin to the available templates in the theme
  158. */
  159. public function action_init( )
  160. {
  161. $this->cache_name = Site::get_url( 'habari', true ) . 'freshcomments';
  162. $this->load_text_domain( 'freshcomments' );
  163. $this->add_template( 'block.freshcomments', dirname( __FILE__ ) . '/block.freshcomments.php' );
  164. }
  165. public function filter_block_list( $block_list )
  166. {
  167. $block_list[ 'freshcomments' ] = _t( 'Fresh Comments', 'freshcomments' );
  168. return $block_list;
  169. }
  170. }
  171. ?>