bp_blogs_sync_add_from_activity_comment

Syncs activity comments and posts them back as blog comments.

Description

bp_blogs_sync_add_from_activity_comment( (int) $comment_id, (array) $params, (object) $parent_activity ); 

Note: This is only a one-way sync - activity comments -> blog comment.

For blog post -> activity comment, see .

Parameters (3)

0. $comment_id (int)
The activity ID for the posted activity comment.
1. $params (array)
Parameters for the activity comment.
2. $parent_activity (object)
Parameters of the parent activity item (in this case, the blog post).

Usage

  1. if ( !function_exists( 'bp_blogs_sync_add_from_activity_comment' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-blogs/bp-blogs-activity.php'; 
  3.  
  4. // The activity ID for the posted activity comment. 
  5. $comment_id = -1; 
  6.  
  7. // Parameters for the activity comment. 
  8. $params = array(); 
  9.  
  10. // Parameters of the parent activity item (in this case, the blog post). 
  11. $parent_activity = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bp_blogs_sync_add_from_activity_comment($comment_id, $params, $parent_activity); 
  15.  

Defined (1)

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

/bp-blogs/bp-blogs-activity.php  
  1. function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_activity ) { 
  2. // if parent activity isn't a post type having the buddypress-activity support, stop now! 
  3. if ( ! bp_activity_type_supports( $parent_activity->type, 'post-type-comment-tracking' ) ) { 
  4. return; 
  5.  
  6. // If activity comments are disabled for blog posts, stop now! 
  7. return; 
  8.  
  9. // Do not sync if the activity comment was marked as spam. 
  10. $activity = new BP_Activity_Activity( $comment_id ); 
  11. if ( $activity->is_spam ) { 
  12. return; 
  13.  
  14. // Get userdata. 
  15. if ( $params['user_id'] == bp_loggedin_user_id() ) { 
  16. $user =buddypress)->loggedin_user->userdata; 
  17. } else { 
  18. $user = bp_core_get_core_userdata( $params['user_id'] ); 
  19.  
  20. // Get associated post type and set default comment parent 
  21. $post_type = bp_activity_post_type_get_tracking_arg( $parent_activity->type, 'post_type' ); 
  22. $comment_parent = 0; 
  23.  
  24. // See if a parent WP comment ID exists. 
  25. if ( ! empty( $params['parent_id'] ) && ! empty( $post_type ) ) { 
  26. $comment_parent = bp_activity_get_meta( $params['parent_id'], "bp_blogs_{$post_type}_comment_id" ); 
  27.  
  28. // Comment args. 
  29. $args = array( 
  30. 'comment_post_ID' => $parent_activity->secondary_item_id,  
  31. 'comment_author' => bp_core_get_user_displayname( $params['user_id'] ),  
  32. 'comment_author_email' => $user->user_email,  
  33. 'comment_author_url' => bp_core_get_user_domain( $params['user_id'], $user->user_nicename, $user->user_login ),  
  34. 'comment_content' => $params['content'],  
  35. 'comment_type' => '', // Could be interesting to add buddypress here... 
  36. 'comment_parent' => (int) $comment_parent,  
  37. 'user_id' => $params['user_id'],  
  38. 'comment_approved' => 1 
  39. ); 
  40.  
  41. // Prevent separate activity entry being made. 
  42.  
  43. // Handle multisite. 
  44. switch_to_blog( $parent_activity->item_id ); 
  45.  
  46. // Handle timestamps for the WP comment after we've switched to the blog. 
  47. $args['comment_date'] = current_time( 'mysql' ); 
  48. $args['comment_date_gmt'] = current_time( 'mysql', 1 ); 
  49.  
  50. // Post the comment. 
  51. $post_comment_id = wp_insert_comment( $args ); 
  52.  
  53. // Add meta to comment. 
  54. add_comment_meta( $post_comment_id, 'bp_activity_comment_id', $comment_id ); 
  55.  
  56. // Add meta to activity comment. 
  57. if ( ! empty( $post_type ) ) { 
  58. bp_activity_update_meta( $comment_id, "bp_blogs_{$post_type}_comment_id", $post_comment_id ); 
  59.  
  60. // Resave activity comment with WP comment permalink. 
  61. // 
  62. // in bp_blogs_activity_comment_permalink(), we change activity comment 
  63. // permalinks to use the post comment link 
  64. // 
  65. // @todo since this is done after AJAX posting, the activity comment permalink 
  66. // doesn't change on the front end until the next page refresh. 
  67. $resave_activity = new BP_Activity_Activity( $comment_id ); 
  68. $resave_activity->primary_link = get_comment_link( $post_comment_id ); 
  69.  
  70. /** 
  71. * Now that the activity id exists and the post comment was created, we don't need to update 
  72. * the content of the comment as there are no chances it has evolved. 
  73. */ 
  74.  
  75. $resave_activity->save(); 
  76.  
  77. // Add the edit activity comment hook back. 
  78.  
  79. // Multisite again! 
  80.  
  81. // Add the comment hook back. 
  82.  
  83. /** 
  84. * Fires after activity comments have been synced and posted as blog comments. 
  85. * @since 2.0.0 
  86. * @param int $comment_id The activity ID for the posted activity comment. 
  87. * @param array $args Array of args used for the comment syncing. 
  88. * @param object $parent_activity Parameters of the blog post parent activity item. 
  89. * @param object $user User data object for the blog comment. 
  90. */ 
  91. do_action( 'bp_blogs_sync_add_from_activity_comment', $comment_id, $args, $parent_activity, $user );