bp_activity_transition_post_type_comment_status

When a post type comment status transition occurs, update the relevant activity's status.

Description

bp_activity_transition_post_type_comment_status( (string) $new_status, (string) $old_status, (WP_Comment) $comment ); 

Parameters (3)

0. $new_status (string)
New comment status.
1. $old_status (string)
Previous comment status.
2. $comment (WP_Comment)
Comment data.

Usage

  1. if ( !function_exists( 'bp_activity_transition_post_type_comment_status' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-actions.php'; 
  3.  
  4. // New comment status. 
  5. $new_status = ''; 
  6.  
  7. // Previous comment status. 
  8. $old_status = ''; 
  9.  
  10. // Comment data. 
  11. $comment = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bp_activity_transition_post_type_comment_status($new_status, $old_status, $comment); 
  15.  

Defined (1)

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

/bp-activity/bp-activity-actions.php  
  1. function bp_activity_transition_post_type_comment_status( $new_status, $old_status, $comment ) { 
  2. $post_type = get_post_type( $comment->comment_post_ID ); 
  3. if ( ! $post_type ) { 
  4. return; 
  5.  
  6. // Get the post type tracking args. 
  7. $activity_post_object = bp_activity_get_post_type_tracking_args( $post_type ); 
  8.  
  9. // Bail if the activity type does not exist 
  10. if ( empty( $activity_post_object->comments_tracking->action_id ) ) { 
  11. return false; 
  12.  
  13. // Set the $activity_comment_object 
  14. } else { 
  15. $activity_comment_object = $activity_post_object->comments_tracking; 
  16.  
  17. // Init an empty activity ID 
  18. $activity_id = 0; 
  19.  
  20. /** 
  21. * Activity currently doesn't have any concept of a trash, or an unapproved/approved state. 
  22. * If a blog comment transitions to a "delete" or "hold" status, delete the activity item. 
  23. * If a blog comment transitions to trashed, or spammed, mark the activity as spam. 
  24. * If a blog comment transitions to approved (and the activity exists), mark the activity as ham. 
  25. * If a blog comment transitions to unapproved (and the activity exists), mark the activity as spam. 
  26. * Otherwise, record the comment into the activity stream. 
  27. */ 
  28.  
  29. // This clause handles delete/hold. 
  30. if ( in_array( $new_status, array( 'delete', 'hold' ) ) ) { 
  31. return bp_activity_post_type_remove_comment( $comment->comment_ID, $activity_post_object ); 
  32.  
  33. // These clauses handle trash, spam, and un-spams. 
  34. } elseif ( in_array( $new_status, array( 'trash', 'spam', 'unapproved' ) ) ) { 
  35. $action = 'spam_activity'; 
  36. } elseif ( 'approved' == $new_status ) { 
  37. $action = 'ham_activity'; 
  38.  
  39. // Get the activity 
  40. $activity_id = bp_activity_get_activity_id( array( 
  41. 'component' => $activity_comment_object->component_id,  
  42. 'item_id' => get_current_blog_id(),  
  43. 'secondary_item_id' => $comment->comment_ID,  
  44. 'type' => $activity_comment_object->action_id,  
  45. ) ); 
  46. } else { 
  47. $activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ); 
  48.  
  49. /** 
  50. * Leave a chance to plugins to manage activity comments differently. 
  51. * @since 2.5.0 
  52. * @param bool $value True to override BuddyPress management. 
  53. * @param string $post_type The post type name. 
  54. * @param int $activity_id The post type activity (0 if not found). 
  55. * @param string $new_status The new status of the post type comment. 
  56. * @param string $old_status The old status of the post type comment. 
  57. * @param WP_Comment $comment Comment data. 
  58. */ 
  59. if ( true === apply_filters( 'bp_activity_pre_transition_post_type_comment_status', false, $post_type, $activity_id, $new_status, $old_status, $comment ) ) { 
  60. return false; 
  61.  
  62. // Check activity item exists 
  63. if ( empty( $activity_id ) ) { 
  64. // If no activity exists, but the comment has been approved, record it into the activity table. 
  65. if ( 'approved' == $new_status ) { 
  66. return bp_activity_post_type_comment( $comment->comment_ID, true, $activity_post_object ); 
  67.  
  68. return; 
  69.  
  70. // Create an activity object 
  71. $activity = new BP_Activity_Activity( $activity_id ); 
  72. if ( empty( $activity->component ) ) { 
  73. return; 
  74.  
  75. // Spam/ham the activity if it's not already in that state 
  76. if ( 'spam_activity' === $action && ! $activity->is_spam ) { 
  77. bp_activity_mark_as_spam( $activity ); 
  78. } elseif ( 'ham_activity' == $action) { 
  79. bp_activity_mark_as_ham( $activity ); 
  80.  
  81. // Add "new_post_type_comment" to the whitelisted activity types, so that the activity's Akismet history is generated 
  82. $post_type_comment_action = $activity_comment_object->action_id; 
  83. $comment_akismet_history = function ( $activity_types ) use ( $post_type_comment_action ) { 
  84. $activity_types[] = $post_type_comment_action; 
  85.  
  86. return $activity_types; 
  87. }; 
  88. add_filter( 'bp_akismet_get_activity_types', $comment_akismet_history ); 
  89.  
  90. // Make sure the activity change won't edit the comment if sync is on 
  91.  
  92. // Save the updated activity 
  93. $activity->save(); 
  94.  
  95. // Restore the action 
  96.  
  97. // Remove the "new_blog_comment" activity type whitelist so we don't break anything 
  98. remove_filter( 'bp_akismet_get_activity_types', $comment_akismet_history );