Skip to:
Content
Pages
Categories
Search
Top
Bottom

Help me fix a function that adds blog comments to activity

  • Avatar of Andrew Tibbetts
    Andrew Tibbetts
    Participant

    @andrewgtibbetts

    I have this function that I pieced together a few months back that worked until a recent WP or BP upgrade (not sure which or when). The function adds blog comments as activity items so that I can have them show up in notifications without actually using the activity feed. It no longer works. Does anyone know why this wouldn’t work anymore?

    function rt_bp_blogs_record_comment( $comment_id, $is_approved = true ) {
    global $bp;

    // Get the users comment
    $recorded_comment = get_comment( $comment_id );

    // Don't record activity if the comment hasn't been approved
    if ( empty( $is_approved ) )
    return false;

    // Don't record activity if no email address has been included
    if ( empty( $recorded_comment->comment_author_email ) )
    return false;

    // Get the user_id from the comment author email.
    $user = get_user_by( 'email', $recorded_comment->comment_author_email );
    $user_id = (int)$user->ID;

    // If there's no registered user id, don't record activity
    if ( empty( $user_id ) )
    return false;

    // Get blog and post data
    $blog_id = get_current_blog_id();
    $recorded_comment->post = get_post( $recorded_comment->comment_post_ID );

    if ( empty( $recorded_comment->post ) || is_wp_error( $recorded_comment->post ) )
    return false;

    // If this is a password protected post, don't record the comment
    if ( !empty( $recorded_comment->post->post_password ) )
    return false;

    // Don't record activity if the comment's associated post isn't a WordPress Post
    if ( !in_array( $recorded_comment->post->post_type, apply_filters( 'bp_blogs_record_comment_post_types', array( 'post' ) ) ) )
    return false;

    $is_blog_public = apply_filters( 'bp_is_blog_public', (int)get_blog_option( $blog_id, 'blog_public' ) );

    // If blog is public allow activity to be posted
    if ( $is_blog_public ) {

    // Get activity related links
    $post_permalink = get_permalink( $recorded_comment->comment_post_ID );
    $comment_link = htmlspecialchars( get_comment_link( $recorded_comment->comment_ID ) );

    // Prepare to record in activity streams
    if ( is_multisite() )
    $activity_action = sprintf( __( '%1$s commented on the post, %2$s, on the site %3$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '' . apply_filters( 'the_title', $recorded_comment->post->post_title ) . '', '' . get_blog_option( $blog_id, 'blogname' ) . '' );
    else
    $activity_action = sprintf( __( '%1$s commented on the post, %2$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '' . apply_filters( 'the_title', $recorded_comment->post->post_title ) . '' );

    $activity_content = $recorded_comment->comment_content;

    // Record in activity streams
    $activity_id = bp_blogs_record_activity( array(
    'user_id' => $user_id,
    'action' => apply_filters_ref_array( 'bp_blogs_activity_new_comment_action', array( $activity_action, &$recorded_comment, $comment_link ) ),
    'content' => apply_filters_ref_array( 'bp_blogs_activity_new_comment_content', array( $activity_content, &$recorded_comment, $comment_link ) ),
    'primary_link' => apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link, &$recorded_comment ) ),
    'type' => 'new_blog_comment',
    'item_id' => $blog_id,
    'secondary_item_id' => $comment_id,
    'recorded_time' => $recorded_comment->comment_date_gmt
    ) );

    // Update the blogs last active date
    bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
    }

    do_action('bp_blogs_comment_recorded',$activity_id);

    return $recorded_comment;
    }
    remove_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
    remove_action( 'edit_comment', 'bp_blogs_record_comment', 10 );
    add_action( 'comment_post', 'rt_bp_blogs_record_comment', 10, 2 );
    add_action( 'edit_comment', 'rt_bp_blogs_record_comment', 10 );

Viewing 8 replies - 1 through 8 (of 8 total)
  • Avatar of Andrew Tibbetts
    Andrew Tibbetts
    Participant

    @andrewgtibbetts

    Anyone in the know with the new BP guts wanna take a crack at this? It may be a useful plugin…

    Avatar of Andrew Tibbetts
    Andrew Tibbetts
    Participant

    @andrewgtibbetts

    Actually, this function is working—it is creating activity items based on blog posts and comments. I found out that it’s my custom plugin that is supposed to hook into this custom post or comment action to create a notification. Here is the plugin. I know no one will even try to tackle this but I have to post this because it’s my only option and I one for due diligence…

    /**
    * Plugin Name:BuddyPress Post Comment Notifier
    * Description: Mod of Brajesh Singh's BuddyPress Activity Comment Notifier to work for Blog Post Comments
    *
    */

    // we are not much concerned with the slug, it is not visible
    define("BP_COMMENT_NOTIFIER_SLUG","pc_notification");

    //register a dummy notifier component, I don't want to do it, but bp has no other mechanism for passing the notification data to function, so we need the format_notification_function
    function pc_notifier_setup_globals() {
    global $bp, $current_blog;
    $bp->pc_notifier=new stdClass();
    $bp->pc_notifier->id = 'pc_notifier';//I asume others are not going to use this is
    $bp->pc_notifier->slug = BP_COMMENT_NOTIFIER_SLUG;
    $bp->pc_notifier->notification_callback = 'pc_notifier_format_notifications';//show the notification
    /* Register this in the active components array */
    $bp->active_components[$bp->pc_notifier->slug] = $bp->pc_notifier->id;

    do_action( 'pc_notifier_setup_globals' );
    }
    add_action( 'bp_setup_globals', 'pc_notifier_setup_globals' );

    /**
    * storing notification for users
    * notify all the users who have commented, or who was the original poster of the update, when someone comments
    * hook to bp_blogs_comment_recorded action
    */
    function pc_notifier_notify($activity_id) {
    global $bp;

    $activity = new BP_Activity_Activity($activity_id);
    $comment = get_comment($activity->secondary_item_id);
    $users = pc_notifier_find_involved_persons($comment->comment_post_ID);
    $link = get_permalink($comment->comment_post_ID);

    if($activity->hide_sitewide) return;

    if(!in_array($activity->user_id, $users)&&($bp->loggedin_user->id!=$activity->user_id)) array_push ($users, $activity->user_id);

    foreach((array)$users as $user_id){
    bp_core_add_notification( $activity_id, $user_id, $bp->pc_notifier->id, 'new_post_comment_'.$activity_id );
    }

    }
    add_action("bp_blogs_comment_recorded","pc_notifier_notify",10,1); // hook to bp_blogs_comment_recorded for adding notification

    /** our notification format function which shows notification to user
    *
    * @global $bp
    * @param $action
    * @param $activity_id
    * @param $secondary_item_id
    * @param $total_items
    * @return
    * @since 1.0.2
    * @desc format and show the notification to the user
    */
    function pc_notifier_format_notifications( $action, $activity_id, $secondary_item_id, $total_items, $format='string' ) {

    global $bp;
    $glue = '';
    $user_names = array();
    $activity = new BP_Activity_Activity( $activity_id );
    $comment = get_comment($activity->secondary_item_id);
    $link = get_permalink($comment->comment_post_ID);
    $comment_post = get_post($comment->comment_post_ID);

    if ( $activity->user_id == $bp->loggedin_user->id ) $text = __("your post");
    else $text = sprintf(__("%s"), $comment_post->post_title);

    $pc_action = 'new_post_comment_'.$activity_id;

    if ( $action == $pc_action ) {

    $users = pc_notifier_find_involved_persons($comment->comment_post_ID);

    $total_user = $count = count($users);//how many unique users have commented

    if ( $count > 2 ) {
    $users = array_slice($users, $count-2);//just show name of two poster, rest should be as and 'n' other also commeted
    $count = $count-2;
    $glue=", ";
    }
    else if ( $total_user == 2 ) $glue = " and ";//if there are 2 unique users , say x and y commented

    foreach ( (array)$users as $user_id )
    $user_names[] = bp_core_get_user_displayname($user_id);

    if ( !empty($user_names) )
    $commenting_users = join($glue,$user_names);

    if ( $total_user > 2 )
    $text = $commenting_users." and ".$count." others commented on ".$comment_post->post_title;
    else
    $text = $commenting_users." commented on ".$comment_post->post_title;

    if ( $format == 'string' )
    return apply_filters('bp_activity_multiple_new_comment_notification','comment_ID.'">'.$text.'');
    else {
    $link .= '#comment-'.$comment->comment_ID;
    return array('link'=>$link,'text'=>$text);
    }

    }

    return false;

    }

    /*
    * Remove activity for the comments on new_blog_post & new_blog_comment activity item.
    * Since these items do not have a single activity view and are linked to the single post screen, we will do the needed on single post view
    */

    function pc_notifier_remove_notification_for_blog_posts(){
    if( !( is_user_logged_in() && is_singular() ) )
    return;

    global $bp,$wpdb;
    $blog_id = (int)$wpdb->blogid;
    $post = wp_get_single_post();
    $activity_id = bp_activity_get_activity_id(
    array(
    'user_id' => $post->post_author,
    'component' => $bp->blogs->id,
    'type' => "new_blog_post",
    'item_id' => $blog_id,
    'secondary_item_id' => $post->ID
    )
    );
    //delete the notification for activity comment on new_blog_post
    if( !empty($activity_id) )
    bp_core_delete_notifications_by_item_id( $bp->loggedin_user->id, $activity_id, $bp->pc_notifier->id, 'new_post_comment_'.$activity_id );

    //for replies on blog comments in activity stream
    $comments = pc_notifier_get_all_blog_post_comment_ids($post->ID);//get all the comment ids as array

    //added in v 1.0.3 for better database performance, no more looping to get individual activity ids
    $activities = pc_notifier_get_activity_ids(
    array(
    "type"=>"new_blog_comment",
    "component" => $bp->blogs->id,
    "item_id"=>$blog_id,
    "secondary_ids"=>$comments
    )
    );

    foreach( (array)$activities as $pc_id )
    bp_core_delete_notifications_by_item_id( $bp->loggedin_user->id, $pc_id, $bp->pc_notifier->id, 'new_post_comment_'.$pc_id );

    }
    add_action("wp_head","pc_notifier_remove_notification_for_blog_posts");

    /**
    * @since v 1.0.2
    * @desc delete notification when an activity is deleted, thanks to @kat_uk for pointing the issue
    * @param pc_ids:we get an arry of activity ids
    */
    function bp_pc_clear_notification_on_activity_delete($pc_ids){
    global $bp;

    foreach ( (array)$pc_ids as $activity_id )
    bp_core_delete_all_notifications_by_type( $activity_id, $bp->pc_notifier->id, 'new_post_comment_'.$activity_id, $secondary_item_id = false );
    }
    add_action("bp_activity_deleted_activities","bp_pc_clear_notification_on_activity_delete");

    /************************************ HELPER FUNCTIONS ********************************************************/

    // find all users who commented on the post
    function pc_notifier_find_involved_persons($comment_post_ID){
    global $bp,$wpdb;

    return $wpdb->get_col($wpdb->prepare("SELECT DISTINCT(user_id) from {$wpdb->comments} where comment_post_ID=%d and user_id!=%d",$comment_post_ID,$bp->loggedin_user->id));
    }

    // return an array of comment ids for the post
    function pc_notifier_get_all_blog_post_comment_ids($post_id) {
    global $wpdb;

    return $wpdb->get_col($wpdb->prepare("SELECT comment_ID as id FROM {$wpdb->comments} WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
    }

    // get activity ids when type, component, secondary_ids, item_id is specified
    function pc_notifier_get_activity_ids($params){
    global $bp,$wpdb;
    extract($params);
    $list="(".join(",", $secondary_ids).")";//create a set to use in the query;

    return $wpdb->get_col($wpdb->prepare("SELECT id from {$bp->activity->table_name} where type=%s and component=%s and item_id=%d and secondary_item_id in {$list}",$type,$component,$item_id));
    }

    Avatar of @ubernaut
    @ubernaut
    Participant

    @ubernaut

    thats a neat idea would be cool to add that as a standard option imo

    Avatar of Andrew Tibbetts
    Andrew Tibbetts
    Participant

    @andrewgtibbetts

    Ok, I think I’ve narrowed it down to this function in the plugin. I know that the function that it is hooking into works. Anyone know why this would fail?

    function pc_notifier_notify($activity_id) {
    global $bp;

    $activity = new BP_Activity_Activity($activity_id);
    $comment = get_comment($activity->secondary_item_id);
    $users = pc_notifier_find_involved_persons($comment->comment_post_ID);
    $link = get_permalink($comment->comment_post_ID);

    if($activity->hide_sitewide) return;

    if(!in_array($activity->user_id, $users)&&($bp->loggedin_user->id!=$activity->user_id)) array_push ($users, $activity->user_id);

    foreach((array)$users as $user_id){
    bp_core_add_notification( $activity_id, $user_id, $bp->pc_notifier->id, 'new_post_comment_'.$activity_id );
    }

    }
    add_action("bp_blogs_comment_recorded","pc_notifier_notify",10,1); // hook to bp_blogs_comment_recorded for adding notification

    Avatar of Andrew Tibbetts
    Andrew Tibbetts
    Participant

    @andrewgtibbetts

    Okaaay, the plugin IS working. Does anyone know why an entry in wp_bp_notifications would not be showing up in notifications?

    entry in wp_bp_notifications:

    id user_id item_id secondary_item_id component_name component_action date_notified is_new

    196 4 209 0 pc_notifier new_post_comment_209 2012-12-07 17:08:16 1

    User id#3 is creating the comment and user id#4 is not receiving the notification in the top bar. Anyone know why?

    Avatar of yzqiang
    yzqiang
    Participant

    @yzqiang

    Thanks a lot for your shareing. Hoping you to finish a downloadable plugin. Thanks.

    Avatar of ggsalas
    ggsalas
    Participant

    @ggsalas

    @andrewgtibbetts do you publish he plugin?

    Thanks

    Avatar of guitfu
    Justin
    Participant

    @justineterniawebcom

    @andrewgtibbetts where di you end up with this? Would love blog comments to show up as notifications.

Viewing 8 replies - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.