bp_activity_new_comment

Add an activity comment.

Description

(int|bool) bp_activity_new_comment( (string) $args = '' ); 

Returns (int|bool)

The ID of the comment on success, otherwise false.

Parameters (1)

0. $args — Optional. (string) => ''
An array of arguments.

Options

  • id (int) => 0

    Pass an ID to update an existing comment.

  • content (string) => ''

    The content of the comment.

  • user_id (int) => to the ID of the logged-in user

    The ID of the user making the comment.

  • activity_id (int) => 0

    The ID of the root activity item, ie the oldest ancestor of the comment.

  • parent_id (int) => 0

    The ID of the parent activity item, ie the item to which the comment is an immediate reply. If not provided, this value defaults to the $activity_id.

  • primary_link (string) => to an string

    the primary link for the comment.

  • skip_notification (bool) => to false

    false to send a comment notification, false otherwise.

array(

    /**
     * Optional. Pass an ID to update an existing comment.
     *
     * @type int
     * @optional
     */
    'id' => 0,

    /**
     * The content of the comment.
     *
     * @type string
     * @default ''
     */
    'content' => '',

    /**
     * Optional. The ID of the user making the comment.
     *
     * @type int
     * @default to the ID of the logged-in user
     * @optional
     */
    'user_id' => to the ID of the logged-in user,

    /**
     * The ID of the "root" activity item, ie the oldest ancestor of the comment.
     *
     * @type int
     */
    'activity_id' => 0,

    /**
     * Optional. The ID of the parent activity item, ie the item to which the comment is an immediate
     * reply. If not provided, this value defaults to the $activity_id.
     *
     * @type int
     * @optional
     */
    'parent_id' => 0,

    /**
     * Optional. the primary link for the comment.
     *
     * @type string
     * @default to an  string
     * @optional
     */
    'primary_link' => to an  string,

    /**
     * Optional. false to send a comment notification, false otherwise.
     *
     * @type bool
     * @default to false
     * @optional
     */
    'skip_notification' => to false
);        


Usage

  1. if ( !function_exists( 'bp_activity_new_comment' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // An array of arguments. 
  5. $args = array( 
  6. 'id' => 0, 
  7. 'content' => '', 
  8. 'user_id' => to the ID of the logged-in user, 
  9. 'activity_id' => 0, 
  10. 'parent_id' => 0, 
  11. 'primary_link' => to an string, 
  12. 'skip_notification' => to false 
  13. ); 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = bp_activity_new_comment($args); 
  17.  

Defined (1)

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

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_new_comment( $args = '' ) { 
  2. $bp =buddypress); 
  3.  
  4. $r = wp_parse_args( $args, array( 
  5. 'id' => false,  
  6. 'content' => false,  
  7. 'user_id' => bp_loggedin_user_id(),  
  8. 'activity_id' => false, // ID of the root activity item. 
  9. 'parent_id' => false, // ID of a parent comment (optional). 
  10. 'primary_link' => '',  
  11. 'skip_notification' => false,  
  12. 'error_type' => 'bool' 
  13. ) ); 
  14.  
  15. // Error type is boolean; need to initialize some variables for backpat. 
  16. if ( 'bool' === $r['error_type'] ) { 
  17. if ( empty( $bp->activity->errors ) ) { 
  18. $bp->activity->errors = array(); 
  19.  
  20. // Defaulterrormessage. 
  21. $feedback = __( 'There was anerrorposting your reply. Please try again.', buddypress ); 
  22.  
  23. // Bail if missing necessary data. 
  24. if ( empty( $r['content'] ) || empty( $r['user_id'] ) || empty( $r['activity_id'] ) ) { 
  25. $error = new WP_Error( 'missing_data', $feedback ); 
  26.  
  27. if ( wp_error === $r['error_type'] ) { 
  28. return $error; 
  29.  
  30. // Backpat. 
  31. } else { 
  32. $bp->activity->errors['new_comment'] = $error; 
  33. return false; 
  34.  
  35. // Maybe set current activity ID as the parent. 
  36. if ( empty( $r['parent_id'] ) ) { 
  37. $r['parent_id'] = $r['activity_id']; 
  38.  
  39. $activity_id = $r['activity_id']; 
  40.  
  41. // Get the parent activity. 
  42. $activity = new BP_Activity_Activity( $activity_id ); 
  43.  
  44. // Bail if the parent activity does not exist. 
  45. if ( empty( $activity->date_recorded ) ) { 
  46. $error = new WP_Error( 'missing_activity', __( 'The item you were replying to no longer exists.', buddypress ) ); 
  47.  
  48. if ( wp_error === $r['error_type'] ) { 
  49. return $error; 
  50.  
  51. // Backpat. 
  52. } else { 
  53. $bp->activity->errors['new_comment'] = $error; 
  54. return false; 
  55.  
  56.  
  57. // Check to see if the parent activity is hidden, and if so, hide this comment publicly. 
  58. $is_hidden = $activity->hide_sitewide ? 1 : 0; 
  59.  
  60. /** 
  61. * Filters the content of a new comment. 
  62. * @since 1.2.0 
  63. * @param string $r Content for the newly posted comment. 
  64. */ 
  65. $comment_content = apply_filters( 'bp_activity_comment_content', $r['content'] ); 
  66.  
  67. // Insert the activity comment. 
  68. $comment_id = bp_activity_add( array( 
  69. 'id' => $r['id'],  
  70. 'content' => $comment_content,  
  71. 'component' =>buddypress)->activity->id,  
  72. 'type' => 'activity_comment',  
  73. 'primary_link' => $r['primary_link'],  
  74. 'user_id' => $r['user_id'],  
  75. 'item_id' => $activity_id,  
  76. 'secondary_item_id' => $r['parent_id'],  
  77. 'hide_sitewide' => $is_hidden,  
  78. 'error_type' => $r['error_type'] 
  79. ) ); 
  80.  
  81. // Bail on failure. 
  82. if ( false === $comment_id || is_wp_error( $comment_id ) ) { 
  83. return $comment_id; 
  84.  
  85. // Comment caches are stored only with the top-level item. 
  86. wp_cache_delete( $activity_id, 'bp_activity_comments' ); 
  87.  
  88. // Walk the tree to clear caches for all parent items. 
  89. $clear_id = $r['parent_id']; 
  90. while ( $clear_id != $activity_id ) { 
  91. $clear_object = new BP_Activity_Activity( $clear_id ); 
  92. wp_cache_delete( $clear_id, 'bp_activity' ); 
  93. $clear_id = intval( $clear_object->secondary_item_id ); 
  94. wp_cache_delete( $activity_id, 'bp_activity' ); 
  95.  
  96. if ( empty( $r[ 'skip_notification' ] ) ) { 
  97. /** 
  98. * Fires near the end of an activity comment posting, before the returning of the comment ID. 
  99. * Sends a notification to the user @see bp_activity_new_comment_notification_helper(). 
  100. * @since 1.2.0 
  101. * @param int $comment_id ID of the newly posted activity comment. 
  102. * @param array $r Array of parsed comment arguments. 
  103. * @param BP_Activity_Activity $activity Activity item being commented on. 
  104. */ 
  105. do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity ); 
  106. } else { 
  107. /** 
  108. * Fires near the end of an activity comment posting, before the returning of the comment ID. 
  109. * without sending a notification to the user 
  110. * @since 2.5.0 
  111. * @param int $comment_id ID of the newly posted activity comment. 
  112. * @param array $r Array of parsed comment arguments. 
  113. * @param BP_Activity_Activity $activity Activity item being commented on. 
  114. */ 
  115.  
  116. if ( empty( $comment_id ) ) { 
  117. $error = new WP_Error( 'comment_failed', $feedback ); 
  118.  
  119. if ( wp_error === $r['error_type'] ) { 
  120. return $error; 
  121.  
  122. // Backpat. 
  123. } else { 
  124. $bp->activity->errors['new_comment'] = $error; 
  125.  
  126. return $comment_id;