Skip to:
Content

BuddyPress.org

Changeset 13114


Ignore:
Timestamp:
09/24/2021 01:54:39 AM (4 weeks ago)
Author:
imath
Message:

Make sure deleting an activity comment also deletes its children

This commit also takes care of redirecting the user to the activity single view when clicking on the comment button and there is no comment form available for the corresponding activity. This case can happen on a single member's activity stream when the main activity is an activity comment.

Props yesbutmaybeno

Fixes #8415

Location:
trunk/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-activity/bp-activity-template.php

    r13108 r13114  
    26782678        if ( bp_is_activity_component() && is_numeric( bp_current_action() ) ) {
    26792679            $class = 'delete-activity-single';
     2680        } elseif ( 'activity_comment' === bp_get_activity_type() ) {
     2681            $class = 'acomment-delete';
    26802682        }
    26812683
     
    27142716        global $activities_template;
    27152717
    2716         $url = trailingslashit( bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/delete/' . $activities_template->activity->id );
     2718        $activity_id = 0;
     2719        if ( isset( $activities_template->activity->id ) ) {
     2720            $activity_id = (int) $activities_template->activity->id;
     2721        }
     2722
     2723        $url = trailingslashit( bp_get_root_domain() . '/' . bp_get_activity_root_slug() . '/delete/' . $activity_id );
    27172724
    27182725        // Determine if we're on a single activity page, and customize accordingly.
    27192726        if ( bp_is_activity_component() && is_numeric( bp_current_action() ) ) {
    27202727            $url = add_query_arg( array( 'redirect_to' => wp_get_referer() ), $url );
     2728        } elseif ( 'activity_comment' === bp_get_activity_type() ) {
     2729            $url = add_query_arg( 'cid', $activity_id, $url );
    27212730        }
    27222731
  • trunk/src/bp-templates/bp-legacy/js/buddypress.js

    r12935 r13114  
    558558            form = jq( '#ac-form-' + a_id );
    559559
     560            if ( ! form.length ) {
     561                var viewDiscussionLink = target.closest( 'li.activity' ).find( '.activity-meta a.view' ).prop( 'href' );
     562
     563                if ( viewDiscussionLink ) {
     564                    window.location.href = viewDiscussionLink;
     565                }
     566
     567                return false;
     568            }
     569
    560570            form.css( 'display', 'none' );
    561571            form.removeClass('root');
     
    672682        if ( target.hasClass('acomment-delete') ) {
    673683            link_href = target.attr('href');
    674             comment_li = target.parent().parent();
    675             form = comment_li.parents('div.activity-comments').children('form');
     684            comment_li = target.closest( 'li' );
     685
     686            form = comment_li.find( 'form.ac-form' );
    676687
    677688            nonce = link_href.split('_wpnonce=');
     
    688699
    689700            /* Reset the form position */
    690             comment_li.parents('.activity-comments').append(form);
     701            if ( form && form.length ) {
     702                comment_li.closest( '.activity-comments' ).append( form );
     703            }
    691704
    692705            jq.post( ajaxurl, {
  • trunk/src/bp-templates/bp-nouveau/buddypress/activity/comment.php

    r12755 r13114  
    77 *
    88 * @since 3.0.0
    9  * @version 7.0.0
     9 * @version 10.0.0
    1010 */
    1111
    1212bp_nouveau_activity_hook( 'before', 'comment' ); ?>
    1313
    14 <li id="acomment-<?php bp_activity_comment_id(); ?>" class="comment-item" data-bp-activity-comment-id="<?php bp_activity_comment_id(); ?>">
     14<li id="acomment-<?php bp_activity_comment_id(); ?>" class="comment-item" <?php bp_nouveau_activity_data_attribute_id(); ?>>
    1515    <div class="acomment-avatar item-avatar">
    1616        <a href="<?php bp_activity_comment_user_link(); ?>">
  • trunk/src/bp-templates/bp-nouveau/buddypress/activity/entry.php

    r12082 r13114  
    77 *
    88 * @since 3.0.0
    9  * @version 3.0.0
     9 * @version 10.0.0
    1010 */
    1111
    1212bp_nouveau_activity_hook( 'before', 'entry' ); ?>
    1313
    14 <li class="<?php bp_activity_css_class(); ?>" id="activity-<?php bp_activity_id(); ?>" data-bp-activity-id="<?php bp_activity_id(); ?>" data-bp-timestamp="<?php bp_nouveau_activity_timestamp(); ?>">
     14<li class="<?php bp_activity_css_class(); ?>" id="activity-<?php bp_activity_id(); ?>" <?php bp_nouveau_activity_data_attribute_id(); ?> data-bp-timestamp="<?php bp_nouveau_activity_timestamp(); ?>">
    1515
    1616    <div class="activity-avatar item-avatar">
  • trunk/src/bp-templates/bp-nouveau/includes/activity/ajax.php

    r12972 r13114  
    44 *
    55 * @since 3.0.0
    6  * @version 8.1.0
     6 * @version 10.0.0
    77 */
    88
     
    233233    // Deleting an activity comment.
    234234    if ( ! empty( $_POST['is_comment'] ) ) {
     235        // Get replies before they are deleted.
     236        $replies   = (array) BP_Activity_Activity::get_child_comments( $activity->id );
     237        $reply_ids = wp_list_pluck( $replies, 'id' );
     238
    235239        if ( ! bp_activity_delete_comment( $activity->item_id, $activity->id ) ) {
    236240            wp_send_json_error( $response );
     241
     242            // The comment and its replies has been deleted successfully.
     243        } else {
     244            $response = array(
     245                'deleted' => array_merge(
     246                    array( $activity->id ),
     247                    $reply_ids
     248                ),
     249            );
    237250        }
    238251
     
    241254        if ( ! bp_activity_delete( array( 'id' => $activity->id, 'user_id' => $activity->user_id ) ) ) {
    242255            wp_send_json_error( $response );
     256
     257            // The activity has been deleted successfully.
     258        } else {
     259            $response = array(
     260                'deleted' => array( $activity->id ),
     261            );
    243262        }
    244263    }
     
    246265    /** This action is documented in bp-activity/bp-activity-actions.php */
    247266    do_action( 'bp_activity_action_delete_activity', $activity->id, $activity->user_id );
    248 
    249     // The activity has been deleted successfully
    250     $response = array( 'deleted' => $activity->id );
    251267
    252268    // If on a single activity redirect to user's home.
  • trunk/src/bp-templates/bp-nouveau/includes/activity/template-tags.php

    r12970 r13114  
    44 *
    55 * @since 3.0.0
    6  * @version 8.1.0
     6 * @version 10.0.0
    77 */
    88
     
    145145
    146146    bp_nouveau_hook( $hook );
     147}
     148
     149/**
     150 * Output the `data-bp-activity-id` or `data-bp-activity-comment-id` attribute
     151 * according to the activity type.
     152 *
     153 * @since 10.0.0
     154 */
     155function bp_nouveau_activity_data_attribute_id() {
     156    $attribute  = 'data-bp-%1$s-id="%2$s"';
     157    $type       = 'activity';
     158    $id         = (int) bp_get_activity_id();
     159    $comment_id = (int) bp_get_activity_comment_id();
     160
     161    if ( 'activity_comment' === bp_get_activity_type() || $comment_id ) {
     162        $type = 'activity-comment';
     163
     164
     165        if ( $comment_id ) {
     166            $id = $comment_id;
     167        }
     168    }
     169
     170    printf( $attribute, $type, $id );
    147171}
    148172
  • trunk/src/bp-templates/bp-nouveau/js/buddypress-activity.js

    r12909 r13114  
    22/* global BP_Nouveau */
    33/* @since 3.0.0 */
    4 /* @version 8.0.0 */
     4/* @version 10.0.0 */
    55window.bp = window.bp || {};
    66
     
    557557                }
    558558
     559                // Move the form if needed
     560                if ( activity_comment_li.find( 'form' ).length ) {
     561                    activity_item.find( '.activity-comments' ).append( activity_comment_li.find( 'form' ) );
     562                }
     563
    559564                parent.ajax( ajaxData, 'activity' ).done( function( response ) {
    560565                    target.removeClass( 'loading' );
     
    571576                        if ( activity_comment_id ) {
    572577                            deleted_comments_count = 1;
    573 
    574                             // Move the form if needed
    575                             activity_item.append( activity_comment_li.find( 'form' ) );
    576 
    577                             // Count child comments if there are some
    578                             $.each( activity_comment_li.find( 'li' ), function() {
    579                                 deleted_comments_count += 1;
    580                             } );
     578                            if ( response.data.deleted ) {
     579                                deleted_comments_count = response.data.deleted.length;
     580
     581                                response.data.deleted.forEach( function( cid ) {
     582                                    $( '[data-bp-activity-comment-id="' + cid + '"]' ).remove();
     583                                } );
     584                            } else {
     585                                // Count child comments if there are some
     586                                $.each( activity_comment_li.find( 'li' ), function() {
     587                                    deleted_comments_count += 1;
     588                                } );
     589                            }
    581590
    582591                            // Update the button count
     
    660669                // Stop event propagation
    661670                event.preventDefault();
     671
     672                if ( ! form.length ) {
     673                    var viewDiscussionLink = target.closest( 'li.activity' ).find( '.activity-meta a.view' ).prop( 'href' );
     674
     675                    if ( viewDiscussionLink ) {
     676                        window.location.href = viewDiscussionLink;
     677                    }
     678
     679                    return false;
     680                }
    662681
    663682                // If the comment count span inside the link is clicked
Note: See TracChangeset for help on using the changeset viewer.