bp_activity_post_type_comment

Create an activity item for a newly posted post type comment.

Description

(int|bool) bp_activity_post_type_comment( (int) $comment_id = 0, (constant) $is_approved = true, (null) $activity_post_object = null ); 

Returns (int|bool)

The ID of the activity on success. False on error.

Parameters (3)

0. $comment_id — Optional. (int)
ID of the comment.
1. $is_approved — Optional. (constant) => true
Whether the comment is approved or not.
2. $activity_post_object — Optional. (null) => null
The post type tracking args object.

Usage

  1. if ( !function_exists( 'bp_activity_post_type_comment' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // ID of the comment. 
  5. $comment_id = -1; 
  6.  
  7. // Whether the comment is approved or not. 
  8. $is_approved = true; 
  9.  
  10. // The post type tracking args object. 
  11. $activity_post_object = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bp_activity_post_type_comment($comment_id, $is_approved, $activity_post_object); 
  15.  

Defined (1)

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

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_post_type_comment( $comment_id = 0, $is_approved = true, $activity_post_object = null ) { 
  2. // Get the users comment 
  3. $post_type_comment = get_comment( $comment_id ); 
  4.  
  5. // Don't record activity if the comment hasn't been approved 
  6. if ( empty( $is_approved ) ) { 
  7. return false; 
  8.  
  9. // Don't record activity if no email address has been included 
  10. if ( empty( $post_type_comment->comment_author_email ) ) { 
  11. return false; 
  12.  
  13. // Don't record activity if the comment has already been marked as spam 
  14. if ( 'spam' === $is_approved ) { 
  15. return false; 
  16.  
  17. // Get the user by the comment author email. 
  18. $user = get_user_by( 'email', $post_type_comment->comment_author_email ); 
  19.  
  20. // If user isn't registered, don't record activity 
  21. if ( empty( $user ) ) { 
  22. return false; 
  23.  
  24. // Get the user_id 
  25. $user_id = (int) $user->ID; 
  26.  
  27. // Get blog and post data 
  28. $blog_id = get_current_blog_id(); 
  29.  
  30. // Get the post 
  31. $post_type_comment->post = get_post( $post_type_comment->comment_post_ID ); 
  32.  
  33. if ( ! is_a( $post_type_comment->post, 'WP_Post' ) ) { 
  34. return false; 
  35.  
  36. /** 
  37. * Filters whether to publish activities about the comment regarding the post status 
  38. * @since 2.5.0 
  39. * @param bool true to bail, false otherwise. 
  40. */ 
  41. $is_post_status_not_allowed = (bool) apply_filters( 'bp_activity_post_type_is_post_status_allowed', 'publish' !== $post_type_comment->post->post_status || ! empty( $post_type_comment->post->post_password ) ); 
  42.  
  43. // If this is a password protected post, or not a public post don't record the comment 
  44. if ( $is_post_status_not_allowed ) { 
  45. return false; 
  46.  
  47. // Set post type 
  48. $post_type = $post_type_comment->post->post_type; 
  49.  
  50. if ( empty( $activity_post_object ) ) { 
  51. // Get the post type tracking args. 
  52. $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type ); 
  53.  
  54. // Bail if the activity type does not exist 
  55. if ( empty( $activity_post_object->comments_tracking->action_id ) ) { 
  56. return false; 
  57.  
  58. // Set the $activity_comment_object 
  59. $activity_comment_object = $activity_post_object->comments_tracking; 
  60.  
  61. /** 
  62. * Filters whether or not to post the activity about the comment. 
  63. * This is a variable filter, dependent on the post type,  
  64. * that lets components or plugins bail early if needed. 
  65. * @since 2.5.0 
  66. * @param bool $value Whether or not to continue. 
  67. * @param int $blog_id ID of the current site. 
  68. * @param int $post_id ID of the current post being commented. 
  69. * @param int $user_id ID of the current user. 
  70. * @param int $comment_id ID of the current comment being posted. 
  71. */ 
  72. if ( false === apply_filters( "bp_activity_{$post_type}_pre_comment", true, $blog_id, $post_type_comment->post->ID, $user_id, $comment_id ) ) { 
  73. return false; 
  74.  
  75. // Is this an update ? 
  76. $activity_id = bp_activity_get_activity_id( array( 
  77. 'user_id' => $user_id,  
  78. 'component' => $activity_comment_object->component_id,  
  79. 'type' => $activity_comment_object->action_id,  
  80. 'item_id' => $blog_id,  
  81. 'secondary_item_id' => $comment_id,  
  82. ) ); 
  83.  
  84. // Record this in activity streams. 
  85. $comment_link = get_comment_link( $post_type_comment->comment_ID ); 
  86.  
  87. // Backward compatibility filters for the 'blogs' component. 
  88. if ( 'blogs' == $activity_comment_object->component_id ) { 
  89. $activity_content = apply_filters_ref_array( 'bp_blogs_activity_new_comment_content', array( $post_type_comment->comment_content, &$post_type_comment, $comment_link ) ); 
  90. $activity_primary_link = apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link, &$post_type_comment ) ); 
  91. } else { 
  92. $activity_content = $post_type_comment->comment_content; 
  93. $activity_primary_link = $comment_link; 
  94.  
  95. $activity_args = array( 
  96. 'id' => $activity_id,  
  97. 'user_id' => $user_id,  
  98. 'content' => $activity_content,  
  99. 'primary_link' => $activity_primary_link,  
  100. 'component' => $activity_comment_object->component_id,  
  101. 'recorded_time' => $post_type_comment->comment_date_gmt,  
  102. ); 
  103.  
  104. $blog_url = get_home_url( $blog_id ); 
  105. $post_url = add_query_arg
  106. 'p',  
  107. $post_type_comment->post->ID,  
  108. trailingslashit( $blog_url ) 
  109. ); 
  110.  
  111. $activity_args['type'] = $activity_comment_object->action_id; 
  112. $activity_args['item_id'] = $blog_id; 
  113. $activity_args['secondary_item_id'] = $post_type_comment->comment_ID; 
  114.  
  115. if ( ! empty( $activity_args['content'] ) ) { 
  116. // Create the excerpt. 
  117. $activity_summary = bp_activity_create_summary( $activity_args['content'], $activity_args ); 
  118.  
  119. // Backward compatibility filter for blog comments. 
  120. if ( 'blogs' == $activity_post_object->component_id ) { 
  121. $activity_args['content'] = apply_filters( 'bp_blogs_record_activity_content', $activity_summary, $activity_args['content'], $activity_args, $post_type ); 
  122. } else { 
  123. $activity_args['content'] = $activity_summary; 
  124.  
  125. // Set up the action by using the format functions. 
  126. $action_args = array_merge( $activity_args, array( 
  127. 'post_title' => $post_type_comment->post->post_title,  
  128. 'post_url' => $post_url,  
  129. 'blog_url' => $blog_url,  
  130. 'blog_name' => get_blog_option( $blog_id, 'blogname' ),  
  131. ) ); 
  132.  
  133. $activity_args['action'] = call_user_func_array( $activity_comment_object->format_callback, array( '', (object) $action_args ) ); 
  134.  
  135. // Make sure the action is set. 
  136. if ( empty( $activity_args['action'] ) ) { 
  137. return; 
  138. } else { 
  139. // Backward compatibility filter for the blogs component. 
  140. if ( 'blogs' === $activity_post_object->component_id ) { 
  141. $activity_args['action'] = apply_filters( 'bp_blogs_record_activity_action', $activity_args['action'] ); 
  142.  
  143. $activity_id = bp_activity_add( $activity_args ); 
  144.  
  145. /** 
  146. * Fires after the publishing of an activity item for a newly published post type post. 
  147. * @since 2.5.0 
  148. * @param int $activity_id ID of the newly published activity item. 
  149. * @param WP_Comment $post_type_comment Comment object. 
  150. * @param array $activity_args Array of activity arguments. 
  151. * @param object $activity_post_object the post type tracking args object. 
  152. */ 
  153. do_action_ref_array( 'bp_activity_post_type_comment', array( &$activity_id, $post_type_comment, $activity_args, $activity_post_object ) ); 
  154.  
  155. return $activity_id;