bp_nouveau_ajax_star_thread_messages()

AJAX mark message with star.

Description

Source

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

1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
function bp_nouveau_ajax_star_thread_messages() {
    if ( empty( $_POST['action'] ) ) {
        wp_send_json_error();
    }
 
    $action = str_replace( 'messages_', '', $_POST['action'] );
 
    if ( 'star' === $action ) {
        $error_message = __( 'There was a problem starring your messages. Please try again.', 'buddyboss' );
    } else {
        $error_message = __( 'There was a problem unstarring your messages. Please try again.', 'buddyboss' );
    }
 
    $response = array(
        'feedback' => esc_html( $error_message ),
        'type'     => 'error',
    );
 
    if ( false === bp_is_active( 'messages', 'star' ) || empty( $_POST['id'] ) ) {
        wp_send_json_error( $response );
    }
 
    // Check capability.
    if ( ! is_user_logged_in() || ! bp_core_can_edit_settings() ) {
        wp_send_json_error( $response );
    }
 
    $ids      = wp_parse_id_list( $_POST['id'] );
    $messages = array();
 
    // Use global nonce for bulk actions involving more than one id
    if ( 1 !== count( $ids ) ) {
        if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_messages' ) ) {
            wp_send_json_error( $response );
        }
 
        foreach ( $ids as $mid ) {
            if ( 'star' === $action ) {
                bp_messages_star_set_action( array(
                    'action'     => 'star',
                    'message_id' => $mid,
                ) );
            } else {
                $thread_id = messages_get_message_thread_id( $mid );
 
                bp_messages_star_set_action( array(
                    'action'    => 'unstar',
                    'thread_id' => $thread_id,
                    'bulk'      => true
                ) );
            }
 
            $messages[ $mid ] = array(
                'star_link' => bp_get_the_message_star_action_link( array(
                    'message_id' => $mid,
                    'url_only'  => true,
                ) ),
                'is_starred' => 'star' === $action,
            );
        }
 
    // Use global star nonce for bulk actions involving one id or regular action
    } else {
        $id = reset( $ids );
 
        if ( empty( $_POST['star_nonce'] ) || ! wp_verify_nonce( $_POST['star_nonce'], 'bp-messages-star-' . $id ) ) {
            wp_send_json_error( $response );
        }
 
        bp_messages_star_set_action( array(
            'action'     => $action,
            'message_id' => $id,
        ) );
 
        $messages[ $id ] = array(
            'star_link' => bp_get_the_message_star_action_link( array(
                'message_id' => $id,
                'url_only'  => true,
            ) ),
            'is_starred' => 'star' === $action,
        );
    }
 
    if ( 'star' === $action ) {
        $success_message = __( 'Messages successfully starred.', 'buddyboss' );
    } else {
        $success_message = __( 'Messages successfully unstarred.', 'buddyboss' );
    }
 
    wp_send_json_success( array(
        'feedback' => esc_html( $success_message ),
        'type'     => 'success',
        'messages' => $messages,
    ) );
}

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.