bp_blogs_comment_sync_activity_comment

Update Activity and blogs meta and eventually sync comment with activity comment.

Description

(int|bool) bp_blogs_comment_sync_activity_comment( (int|bool) &$activity_id, (constant) $comment = null, (array) $activity_args = array(), (null) $activity_post_object = null ); 

Returns (int|bool)

Returns false if no activity, the activity id otherwise.

Parameters (4)

0. $activity_id (int|bool) => &$activity_id
ID of recorded activity, or false if sync is active.
1. $comment — Optional. (constant) => null
The comment object.
2. $activity_args — Optional. (array) => array()
Array of activity arguments.
3. $activity_post_object — Optional. (null) => null
The post type tracking args object.

Usage

  1. if ( !function_exists( 'bp_blogs_comment_sync_activity_comment' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-blogs/bp-blogs-functions.php'; 
  3.  
  4. // ID of recorded activity, or false if sync is active. 
  5. $activity_id = &$activity_id; 
  6.  
  7. // The comment object. 
  8. $comment = null; 
  9.  
  10. // Array of activity arguments. 
  11. $activity_args = array(); 
  12.  
  13. // The post type tracking args object. 
  14. $activity_post_object = null; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = bp_blogs_comment_sync_activity_comment($activity_id, $comment, $activity_args, $activity_post_object); 
  18.  

Defined (1)

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

/bp-blogs/bp-blogs-functions.php  
  1. function bp_blogs_comment_sync_activity_comment( &$activity_id, $comment = null, $activity_args = array(), $activity_post_object = null ) { 
  2. if ( empty( $activity_args ) || empty( $comment->post->ID ) || empty( $activity_post_object->comment_action_id ) ) { 
  3. return false; 
  4.  
  5. // Set the current blog id. 
  6. $blog_id = get_current_blog_id(); 
  7.  
  8. // These activity metadatas are used to build the new_blog_comment action string 
  9. if ( ! empty( $activity_id ) && ! empty( $activity_args['item_id'] ) && 'new_blog_comment' === $activity_post_object->comment_action_id ) { 
  10. // add some post info in activity meta 
  11. bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title ); 
  12. bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) ); 
  13.  
  14. // Sync comment - activity comment 
  15.  
  16. if ( ! empty( $_REQUEST['action'] ) ) { 
  17. $existing_activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ); 
  18.  
  19. if ( ! empty( $existing_activity_id ) ) { 
  20. $activity_args['id'] = $existing_activity_id; 
  21.  
  22. if ( empty( $activity_post_object ) ) { 
  23. $activity_post_object = bp_activity_get_post_type_tracking_args( $comment->post->post_type ); 
  24.  
  25. if ( isset( $activity_post_object->action_id ) && isset( $activity_post_object->component_id ) ) { 
  26. // find the parent 'new_post_type' activity entry 
  27. $parent_activity_id = bp_activity_get_activity_id( array( 
  28. 'component' => $activity_post_object->component_id,  
  29. 'type' => $activity_post_object->action_id,  
  30. 'item_id' => $blog_id,  
  31. 'secondary_item_id' => $comment->comment_post_ID 
  32. ) ); 
  33.  
  34. // Try to create a new activity item for the parent blog post. 
  35. if ( empty( $parent_activity_id ) ) { 
  36. $parent_activity_id = bp_activity_post_type_publish( $comment->post->ID, $comment->post ); 
  37.  
  38. // we found the parent activity entry 
  39. // so let's go ahead and reconfigure some activity args 
  40. if ( ! empty( $parent_activity_id ) ) { 
  41. // set the parent activity entry ID 
  42. $activity_args['activity_id'] = $parent_activity_id; 
  43.  
  44. // now see if the WP parent comment has a BP activity ID 
  45. $comment_parent = 0; 
  46. if ( ! empty( $comment->comment_parent ) ) { 
  47. $comment_parent = get_comment_meta( $comment->comment_parent, 'bp_activity_comment_id', true ); 
  48.  
  49. // WP parent comment does not have a BP activity ID 
  50. // so set to 'new_' . post_type activity ID 
  51. if ( empty( $comment_parent ) ) { 
  52. $comment_parent = $parent_activity_id; 
  53.  
  54. $activity_args['parent_id'] = $comment_parent; 
  55. $activity_args['skip_notification'] = true; 
  56.  
  57. // could not find corresponding parent activity entry 
  58. // so wipe out $args array 
  59. } else { 
  60. $activity_args = array(); 
  61.  
  62. // Record in activity streams 
  63. if ( ! empty( $activity_args ) ) { 
  64. $activity_id = bp_activity_new_comment( $activity_args ); 
  65.  
  66. if ( empty( $activity_args['id'] ) ) { 
  67. // The activity metadata to inform about the corresponding comment ID 
  68. bp_activity_update_meta( $activity_id, "bp_blogs_{$comment->post->post_type}_comment_id", $comment->comment_ID ); 
  69.  
  70. // The comment metadata to inform about the corresponding activity ID 
  71. add_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', $activity_id ); 
  72.  
  73. // These activity metadatas are used to build the new_blog_comment action string 
  74. if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) { 
  75. bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title ); 
  76. bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) ); 
  77.  
  78. /** 
  79. * Fires after an activity comment is added from a WP post comment. 
  80. * @since 2.6.0 
  81. * @param int $activity_id The activity comment ID. 
  82. * @param WP_Comment $post_type_comment WP Comment object. 
  83. * @param array $activity_args Activity comment arguments. 
  84. * @param object $activity_post_object The post type tracking args object. 
  85. */ 
  86. do_action( 'bp_blogs_comment_sync_activity_comment', $activity_id, $comment, $activity_args, $activity_post_object ); 
  87.  
  88. // Update the blogs last active date 
  89. bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() ); 
  90.  
  91. if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) { 
  92. /** 
  93. * Fires after BuddyPress has recorded metadata about a published blog post comment. 
  94. * @since 2.5.0 
  95. * @param int $value Comment ID of the blog post comment being recorded. 
  96. * @param WP_Post $post WP_Comment object for the current blog post. 
  97. * @param string $value ID of the user associated with the current blog post comment. 
  98. */ 
  99. do_action( 'bp_blogs_new_blog_comment', $comment->comment_ID, $comment, bp_loggedin_user_id() ); 
  100.  
  101. return $activity_id;