/**
 * Delete a notification in case a comment is trashed, deleted or spammed
 *
 * @package WP Idea Stream
 * @subpackage buddypress/notifications
 *
 * @since  2.0.0
 *
 * @param  int $comment_id the ID of the comment
 * @uses   wp_idea_stream_comments_get_comment() to get the "ideafied" comment object
 * @uses   bp_notifications_delete_notifications_by_item_id() to delete the notification
 * @uses   buddypress() to get BuddyPress instance
 * @uses   wp_idea_stream_get_post_type() to get the ideas post type identifier
 */
function wp_idea_stream_buddypress_comments_delete_notifications_by_comment_id($comment_id = 0)
{
    // Bail, if no comment
    if (empty($comment_id)) {
        return;
    }
    $comment = wp_idea_stream_comments_get_comment($comment_id);
    if (empty($comment->comment_post_author)) {
        return;
    }
    // Remove the notification.
    bp_notifications_delete_notifications_by_item_id($comment->comment_post_author, $comment->comment_post_ID, buddypress()->ideastream->id, 'new_' . wp_idea_stream_get_post_type() . '_comment', $comment_id);
}
/**
 * Make sure the comment edit link about an ideas post type will
 * open the Ideastream Comments Submenu once cliked on.
 *
 * @package WP Idea Stream
 * @subpackage comments/functions
 *
 * @since 2.0.0
 *
 * @param  string $location the comment edit link
 * @uses   wp_idea_stream_comments_get_comment() to get the Idea comment object
 * @uses   wp_idea_stream_get_post_type() to get the ideas post type identifier
 * @uses   add_query_arg() to add the post type query arg to the comment edit link
 * @uses   apply_filters() call 'wp_idea_stream_edit_comment_link' to override the url
 * @return string           the new comment edit link if about an idea, unchanged otherwise
 */
function wp_idea_stream_edit_comment_link($location = '')
{
    if (empty($location)) {
        return $location;
    }
    // Too bad WordPres is not sending the comment object or ID in the filter :(
    if (!preg_match('/[&|&]c=(\\d+)/', $location, $matches)) {
        return $location;
    }
    if (empty($matches[1])) {
        return $location;
    }
    $comment_id = absint($matches[1]);
    $comment = wp_idea_stream_comments_get_comment($comment_id);
    if (empty($comment->comment_post_type) || wp_idea_stream_get_post_type() != $comment->comment_post_type) {
        return $location;
    }
    $new_location = add_query_arg('post_type', wp_idea_stream_get_post_type(), $location);
    /**
     * @param  string $new_location the new comment edit link
     * @param  string $location     the original comment edit link
     * @param  object $comment      the idea's comment object
     */
    return apply_filters('wp_idea_stream_edit_comment_link', $new_location, $location, $comment);
}
 /**
  * Edit the new comment notification message
  *
  * @package WP Idea Stream
  * @subpackage comments/classes
  *
  * @since 2.0.0
  *
  * @param  string  $message    the content of the notification
  * @param  integer $comment_id the comment ID
  * @uses   wp_idea_stream_comments_get_comment() to get the comment object
  * @uses   wp_idea_stream_get_post_type() to get the idea post type identifier
  * @uses   do_action() call 'wp_idea_stream_ideas_get_pagination_count' to perform custom actions
  * @return string              the content, edited if needed
  */
 public function comment_notification($message = '', $comment_id = 0)
 {
     // Return if no comment ID
     if (empty($comment_id)) {
         return $message;
     }
     // Check caught value
     if (!empty($this->{'comment_post_' . $comment_id})) {
         $comment = $this->{'comment_post_' . $comment_id};
         // Get the comment to check if it relates to an idea
     } else {
         $comment = wp_idea_stream_comments_get_comment($comment_id);
     }
     // Return if no user_id or the comment does not relate to an idea
     if (empty($comment->comment_post_author) || empty($comment->comment_post_type) || wp_idea_stream_get_post_type() != $comment->comment_post_type) {
         return $message;
     }
     // First add a post type var at the end of the links
     preg_match_all('/(comment|comments).php\\?(.*)\\r\\n/', $message, $matches);
     if (!empty($matches[2])) {
         foreach ($matches[2] as $action) {
             $message = str_replace($action, $action . '&post_type=' . wp_idea_stream_get_post_type(), $message);
         }
     }
     // It's not a notification to author return the message
     if (empty($comment->comment_approved)) {
         return $message;
     }
     /**
      * If we arrive here, then WordPress is notifying the author of the idea
      * that a new comment has been posted and approuved on his idea. So if the
      * idea's author does not have the capability to moderate comments, we need
      * to make sure he won't receive the links to delete|trash|spam the comment
      * The easiest way is to completely replace the content of the message sent.
      *
      * @todo in a future release, we could create a specific capability to let
      * for instance BuddyPress group moderators/administrator moderate the comments
      * about the ideas posted in their group.
      */
     if (!user_can($comment->comment_post_author, 'moderate_comments')) {
         // reset the message
         $message = sprintf(__('New comment on your idea "%s"', 'wp-idea-stream'), $comment->comment_post_title) . "\r\n";
         $message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
         $message .= sprintf(__('Permalink to the comment: %s', 'wp-idea-stream'), wp_idea_stream_comments_get_comment_link($comment_id)) . "\r\n";
     }
     /**
      * Used internally to generate a BuddyPress screen notification
      *
      * @param  object $comment the comment object
      */
     do_action('wp_idea_stream_comments_notify_author', $comment);
     return $message;
 }