bp_blogs_comments_open

Check if a blog post's activity item should be closed from commenting.

Description

bp_blogs_comments_open( (object) $activity ); 

This mirrors the and functions, but for use with the BuddyPress activity stream to be as lightweight as possible.

By lightweight, we actually mirror a few of the blog's commenting settings to blogmeta and checks the values in blogmeta instead. This is to prevent multiple calls in the activity stream.

Parameters (1)

0. $activity (object)
The BP_Activity_Activity object.

Usage

  1. if ( !function_exists( 'bp_blogs_comments_open' ) ) { 
  2. require_once '/bp-blogs/bp-blogs-activity.php'; 
  3.  
  4. // The BP_Activity_Activity object. 
  5. $activity = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bp_blogs_comments_open($activity); 
  9.  

Defined (1)

The function is defined in the following location(s).

/bp-blogs/bp-blogs-activity.php  
  1. function bp_blogs_comments_open( $activity ) { 
  2. $open = true; 
  3.  
  4. $blog_id = $activity->item_id; 
  5.  
  6. // See if we've mirrored the close comments option before. 
  7. $days_old = bp_blogs_get_blogmeta( $blog_id, 'close_comments_days_old' ); 
  8.  
  9. // We've never cached these items before, so do it now. 
  10. if ( '' === $days_old ) { 
  11. switch_to_blog( $blog_id ); 
  12.  
  13. // Use comments_open(). 
  14. remove_filter( 'comments_open', 'bp_comments_open', 10 ); 
  15. $open = comments_open( $activity->secondary_item_id ); 
  16. add_filter( 'comments_open', 'bp_comments_open', 10, 2 ); 
  17.  
  18. // Might as well mirror values to blogmeta since we're here! 
  19. $thread_depth = get_option( 'thread_comments' ); 
  20. if ( ! empty( $thread_depth ) ) { 
  21. $thread_depth = get_option( 'thread_comments_depth' ); 
  22. } else { 
  23. // Perhaps filter this? 
  24. $thread_depth = 1; 
  25.  
  26. bp_blogs_update_blogmeta( $blog_id, 'close_comments_for_old_posts', get_option( 'close_comments_for_old_posts' ) ); 
  27. bp_blogs_update_blogmeta( $blog_id, 'close_comments_days_old', get_option( 'close_comments_days_old' ) ); 
  28. bp_blogs_update_blogmeta( $blog_id, 'thread_comments_depth', $thread_depth ); 
  29.  
  30. restore_current_blog(); 
  31.  
  32. // Check blogmeta and manually check activity item. 
  33. // Basically a copy of _close_comments_for_old_post(). 
  34. } else { 
  35.  
  36. // Comments are closed. 
  37. if ( 'closed' == bp_activity_get_meta( $activity->id, 'post_comment_status' ) ) { 
  38. return false; 
  39.  
  40. if ( ! bp_blogs_get_blogmeta( $blog_id, 'close_comments_for_old_posts' ) ) { 
  41. return $open; 
  42.  
  43. $days_old = (int) $days_old; 
  44. if ( ! $days_old ) { 
  45. return $open; 
  46.  
  47. /** 
  48. Commenting out for now - needs some more thought... 
  49. should we add the post type to activity meta? 
  50.   
  51. $post = get_post($post_id); 
  52.   
  53. // This filter is documented in wp-includes/comment.php 
  54. $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); 
  55. if ( ! in_array( $post->post_type, $post_types ) ) 
  56. return $open; 
  57. */ 
  58.  
  59. if ( time() - strtotime( $activity->date_recorded ) > ( $days_old * DAY_IN_SECONDS ) ) { 
  60. return false; 
  61.  
  62. return $open; 
  63.  
  64. return $open;