Skip to:
Content
Pages
Categories
Search
Top
Bottom

Forum Replies Created

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

  • David Hunt
    Participant

    @dvd3141

    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’);`


    David Hunt
    Participant

    @dvd3141

    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!


    David Hunt
    Participant

    @dvd3141

    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?


    David Hunt
    Participant

    @dvd3141

    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.


    David Hunt
    Participant

    @dvd3141

    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…?


    David Hunt
    Participant

    @dvd3141

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


    David Hunt
    Participant

    @dvd3141

    @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.


    David Hunt
    Participant

    @dvd3141

    I think `bp_member_user_nicename` might be function you wanted? E.g.:

    `@`


    David Hunt
    Participant

    @dvd3141

    Thanks for confirming I wasn’t doing anything silly. :)

    Ticket posted: https://buddypress.trac.wordpress.org/ticket/4367

    (I set it as a bug rather than enhancement, because I genuinely believe this would be a big problem anytime anyone decides the change their permalink structure.)

Viewing 9 replies - 1 through 9 (of 9 total)
Skip to toolbar