Skip to:
Content
Pages
Categories
Search
Top
Bottom

Send user notification when activity is deleted

  • @dvd3141

    Participant

    Hi,

    I’m trying to create a new action which sends an email to the relevant user when his/her activity is deleted by a moderator. So far I have:

    add_action( 'bp_before_activity_delete', 'pa_send_notification_email');
    function pa_send_notification_email( $args ) {
    // If current user deletes his/her own activity, do nothing
    if ($args['user_id'] != bp_loggedin_user_id()) {
    $ud = get_userdata($args['user_id']);
    $to = $ud->user_email;
    if (($to) && ($to != '')) {
    $subject = 'Your activity has been removed';
    $message = 'A moderator has removed your activity posting. If you want to know why, please contact the moderation team.';
    $message .= 'Original text:';
    $message .= bp_get_activity_content_body();

    //sends email
    wp_mail($to, $subject, $message, 'content-type: text/html' );
    }
    }
    }

    This works fine except for the line $message .= bp_get_activity_content_body();, which is attempting to include the original content of the activity (I want to include it so that the user knows exactly what was deleted).

    I’ve tried $message .= bp_get_activity_content_body($args['id']); but that didn’t work either.

    Does anyone know how I ought to be pulling in the activity content body text?

    David

Viewing 11 replies - 1 through 11 (of 11 total)
  • @shanebp

    Moderator

    Try

    $message .= $args['content'];

    @dvd3141

    Participant

    @shanebpx: Thanks for that suggestion — it’s beautifully simple. Unfortunately, it appear as though $args['content'] is empty? (But there’s definitely content in the test activity I am deleting!)

    For debugging, I tried $message .= print_r($args, true);, which gave:

    Array
    (
    [id] => 31
    [action] =>
    [content] =>
    [component] =>
    [type] =>
    [primary_link] =>
    [user_id] => 52
    [item_id] =>
    [secondary_item_id] =>
    [date_recorded] =>
    [hide_sitewide] =>
    )

    I admit I’m a little bewildered as to why all those values appear empty.

    @dvd3141

    Participant

    I guess the delete button only passes user_id and id to $args…

    @shanebp

    Moderator

    Yeah, those fields will be empty unless the values are passed by whatever is calling the function that has the do_action ‘bp_before_activity_delete’

    Iffy, but try adding this to the top of your function:
    global $activities_template;

    and then revert to:
    $message .= bp_get_activity_content_body();

    There is definitely some BP way to get the activity content… sometimes it takes some real searching.

    And since the activity id is being passed, there is always the custom query approach.
    If you don’t know how to do that, take a look at $wpdb and get_var
    https://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Variable

    The query will something like this:
    $content_id = $args[‘id’];
    $content = $wpdb->get_var( “SELECT content FROM wp_bp_activity WHERE id = $content_id” );

    @dvd3141

    Participant

    Well, after being driven almost bonkers, I have solved this — and found what I think is a bug in BuddyPress 1.6.1.

    I went with @shanebpx’s great suggestion of using a get_var query:


    $content_id = $args['id'];
    global $wpdb;
    $content = $wpdb->get_var( $wpdb->prepare("SELECT content FROM ".$wpdb->prefix."bp_activity WHERE id = %d;", $content_id ) );
    $message .= wpautop($content);

    However, I was baffled by the fact that $content kept coming up empty. Until I finally took a look at how BuddyPress was calling do_action( 'bp_before_activity_delete', $args ); in /bp-activity/bp-activity-functions.php:


    if ( !$activity_ids_deleted = BP_Activity_Activity::delete( $args ) )
    return false;

    // Check if the user's latest update has been deleted
    if ( empty( $args['user_id'] ) )
    $user_id = $bp->loggedin_user->id;
    else
    $user_id = $args['user_id'];

    do_action( 'bp_before_activity_delete', $args );

    Yup, it’s apparently doing the bp_before_activity_delete action after BP_Activity_Activity::delete!

    I have edited my copy of /bp-activity/bp-activity-functions.php to put the do_action before the deletion.


    do_action( 'bp_before_activity_delete', $args );
    if ( !$activity_ids_deleted = BP_Activity_Activity::delete( $args ) )
    return false;

    // Check if the user's latest update has been deleted
    if ( empty( $args['user_id'] ) )
    $user_id = bp_loggedin_user_id();
    else
    $user_id = $args['user_id'];

    and at last, my email function works!

    Now, how do I go about submitting this as a bug…?

    @dvd3141

    Participant

    Ah, it’s already been logged and fixed for 1.6.2. I do wish 1.6.2 had been released already! Would have saved me a few frustrating hours.

    @shanebp

    Moderator

    Too bad about the release timing, but good work David.

    @dvd3141

    Participant

    Revisiting this thread with a new issue!

    My function has been working well, except I’ve recently discovered a small flaw: if an administrator deletes a user, then the user receives an email informing him or her that his activity has been removed for each and every thing he or she has ever posted! For a prolific user, that could be hundreds or thousands of emails. Not good!

    This is by virtue of the user-delete process — it appears as though BuddyPress deletes each activity individually, and then WordPress deletes the user account.

    For reference, my function is:

    `function pa_send_notification_email( $args ) {
    // If current user deletes his/her own activity, do nothing
    // otherwise construct and send email
    if ($args[‘user_id’] != bp_loggedin_user_id()) {
    $ud = get_userdata($args[‘user_id’]);
    $to = $ud->user_email;
    // Build the message
    if (($to) && ($to != ”)) {
    $subject = ‘Your post has been removed’;
    $message = ‘A moderator has removed your post. If you want to know why, please forward this message to the moderation team.
    ‘;
    $message .= ‘Removed content:
    ‘;
    global $wpdb;
    $message .= wpautop( $wpdb->get_var( $wpdb->prepare( “SELECT content FROM “.$wpdb->prefix.”bp_activity WHERE id = %d;”, $args[‘id’] ) ) );
    $message .= ”;

    //Send email
    wp_mail($to, $subject, $message, ‘content-type: text/html’ );
    }
    }
    }
    add_action( ‘bp_before_activity_delete’, ‘pa_send_notification_email’);`

    So what I think I need is an extra check in my function, to make sure we are not about to delete the user. I’ve thought about a secondary function to hook into the `delete_user` action to set a global variable, and then checking for this variable in my `pa_send_notification_email()` function.

    But I am not sure how to go about this. Does anyone have any thoughts?

    @dvd3141

    Participant

    Hmm, the situation is not as dire as I thought. I did more testing and it appears that, when I delete a user who as posted multiple times, my function only sends one notification email to the user — but the SQL query is empty (as we might expect). So I think all I need to do is put a check in to see whether the deleted “content” is empty or not, and if it is, don’t send the email.

    I’ll keep updating this thread with my findings in case it’s useful to anyone!

    @dvd3141

    Participant

    Okay, phew, it wasn’t as serious as I first thought. This now won’t send an email if the content is empty (which is, apparently, the case when a user is being deleted):

    `function pa_send_notification_email( $args ) {
    // If current user deletes his/her own activity, do nothing
    if (($args[‘user_id’] != bp_loggedin_user_id())) {
    $ud = get_userdata($args[‘user_id’]);
    $to = $ud->user_email;
    // Build the message
    if (($to) && ($to != ”)) {
    global $wpdb;
    $content = $wpdb->get_var( $wpdb->prepare( “SELECT content FROM “.$wpdb->prefix.”bp_activity WHERE id = %d;”, $args[‘id’] ) );
    if ($content != ”) {
    $subject = ‘Your post has been removed’;
    $message = ‘A moderator has removed your post. If you want to know why, please forward this message to the moderation team.
    ‘;
    $message .= ‘Removed content:
    ‘;

    $message .= wpautop( $content );
    $message .= ”;

    //Send email
    wp_mail($to, $subject, $message, ‘content-type: text/html’ );
    }
    }
    }
    }
    add_action( ‘bp_before_activity_delete’, ‘pa_send_notification_email’);`

    @shanebp

    Moderator

    Thanks for all the updates.
    This will be useful to somebody.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Send user notification when activity is deleted’ is closed to new replies.
Skip to toolbar