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 ABSPATH . PLUGINDIR . 'buddypress/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. $open = comments_open( $activity->secondary_item_id ); 
  15.  
  16. // Might as well mirror values to blogmeta since we're here! 
  17. $thread_depth = get_option( 'thread_comments' ); 
  18. if ( ! empty( $thread_depth ) ) { 
  19. $thread_depth = get_option( 'thread_comments_depth' ); 
  20. } else { 
  21. // Perhaps filter this? 
  22. $thread_depth = 1; 
  23.  
  24. bp_blogs_update_blogmeta( $blog_id, 'close_comments_for_old_posts', get_option( 'close_comments_for_old_posts' ) ); 
  25. bp_blogs_update_blogmeta( $blog_id, 'close_comments_days_old', get_option( 'close_comments_days_old' ) ); 
  26. bp_blogs_update_blogmeta( $blog_id, 'thread_comments_depth', $thread_depth ); 
  27.  
  28.  
  29. // Check blogmeta and manually check activity item. 
  30. // Basically a copy of _close_comments_for_old_post(). 
  31. } else { 
  32.  
  33. // Comments are closed. 
  34. if ( 'closed' == bp_activity_get_meta( $activity->id, 'post_comment_status' ) ) { 
  35. return false; 
  36.  
  37. if ( ! bp_blogs_get_blogmeta( $blog_id, 'close_comments_for_old_posts' ) ) { 
  38. return $open; 
  39.  
  40. $days_old = (int) $days_old; 
  41. if ( ! $days_old ) { 
  42. return $open; 
  43.  
  44. /** 
  45. Commenting out for now - needs some more thought... 
  46. should we add the post type to activity meta? 
  47.   
  48. $post = get_post($post_id); 
  49.   
  50. // This filter is documented in wp-includes/comment.php 
  51. $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); 
  52. if ( ! in_array( $post->post_type, $post_types ) ) 
  53. return $open; 
  54. */ 
  55.  
  56. if ( time() - strtotime( $activity->date_recorded ) > ( $days_old * DAY_IN_SECONDS ) ) { 
  57. return false; 
  58.  
  59. return $open; 
  60.  
  61. return $open;