bp_nouveau_ajax_delete_thread()

AJAX delete entire thread.

Description

Source

File: bp-templates/bp-nouveau/includes/messages/ajax.php

1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
function bp_nouveau_ajax_delete_thread() {
 
    global $wpdb, $bp;
 
    $response = array(
        'feedback' => __( 'There was a problem deleting your messages. Please try again.', 'buddyboss' ),
        'type'     => 'error',
    );
 
    if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
        wp_send_json_error( $response );
    }
 
    if ( empty( $_POST['id'] ) ) {
        wp_send_json_error( $response );
    }
 
    $thread_ids = wp_parse_id_list( $_POST['id'] );
 
    foreach ( $thread_ids as $thread_id ) {
        if ( ! messages_check_thread_access( $thread_id ) && ! bp_current_user_can( 'bp_moderate' ) ) {
            wp_send_json_error( $response );
        }
 
        // Removed the thread id from the group meta.
        if ( bp_is_active( 'groups' ) && function_exists( 'bp_disable_group_messages' ) && true === bp_disable_group_messages() ) {
            // Get the group id from the first message
            $first_message    = BP_Messages_Thread::get_first_message( (int) $thread_id );
            $message_group_id = (int) bp_messages_get_meta( $first_message->id, 'group_id', true ); // group id
            if ( $message_group_id > 0 ) {
                $group_thread = (int) groups_get_groupmeta( $message_group_id, 'group_message_thread' );
                if ( $group_thread > 0 && $group_thread === (int) $thread_id ) {
                    groups_update_groupmeta( $message_group_id, 'group_message_thread', '' );
                }
            }
        }
 
        // Get the message ids in order to pass to the action.
        $message_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) ); // WPCS: db call ok. // WPCS: cache ok.
 
        if ( bp_is_active( 'notifications' ) ) {
            // Delete Message Notifications.
            bp_messages_message_delete_notifications( $thread_id, $message_ids );
        }
 
        // Delete thread messages.
        $query = $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id );
        $wpdb->query( $query ); // db call ok; no-cache ok;
 
        // Delete messages meta.
        $query = $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_meta} WHERE message_id IN(%s)", implode( ',', $message_ids ) );
        $wpdb->query( $query ); // db call ok; no-cache ok;
 
        // Delete thread.
        $query = $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id );
        $wpdb->query( $query ); // db call ok; no-cache ok;
    }
 
    wp_send_json_success(
        array(
            'type'     => 'success',
            'messages' => 'Thread successfully deleted.',
        )
    );
 
}

Changelog

Changelog
Version Description
BuddyPress 3.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.