bbp_subscriptions_handler( string $action = '' )

Handles the front end subscribing and unsubscribing topics

Description

Parameters

$action

(Optional) The requested action to compare this function to

Default value: ''

Source

File: bp-forums/users/functions.php

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
function bbp_subscriptions_handler( $action = '' ) {
 
    if ( !bbp_is_subscriptions_active() ) {
        return false;
    }
 
    // Bail if no topic ID is passed
    if ( empty( $_GET['topic_id'] ) ) {
        return;
    }
 
    // Setup possible get actions
    $possible_actions = array(
        'bbp_subscribe',
        'bbp_unsubscribe',
    );
 
    // Bail if actions aren't meant for this function
    if ( !in_array( $action, $possible_actions ) ) {
        return;
    }
 
    // Get required data
    $user_id  = bbp_get_user_id( 0, true, true );
    $topic_id = intval( $_GET['topic_id'] );
 
    // Check for empty topic
    if ( empty( $topic_id ) ) {
        bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No discussion was found! Which discussion are you subscribing/unsubscribing to?', 'buddyboss' ) );
 
    // Check nonce
    } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $topic_id ) ) {
        bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'buddyboss' ) );
 
    // Check current user's ability to edit the user
    } elseif ( !current_user_can( 'edit_user', $user_id ) ) {
        bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'buddyboss' ) );
    }
 
    // Bail if we have errors
    if ( bbp_has_errors() ) {
        return;
    }
 
    /** No errors *************************************************************/
 
    $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id );
    $success         = false;
 
    if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
        $success = bbp_remove_user_subscription( $user_id, $topic_id );
    } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
        $success = bbp_add_user_subscription( $user_id, $topic_id );
    }
 
    // Do additional subscriptions actions
    do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );
 
    // Success!
    if ( true === $success ) {
 
        // Redirect back from whence we came
        if ( bbp_is_subscriptions() ) {
            $redirect = bbp_get_subscriptions_permalink( $user_id );
        } elseif ( bbp_is_single_user() ) {
            $redirect = bbp_get_user_profile_url();
        } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
            $redirect = bbp_get_topic_permalink( $topic_id );
        } elseif ( is_single() || is_page() ) {
            $redirect = get_permalink();
        } else {
            $redirect = get_permalink( $topic_id );
        }
 
        wp_safe_redirect( $redirect );
 
        // For good measure
        exit();
 
    // Fail! Handle errors
    } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
        bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that discussion!', 'buddyboss' ) );
    } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
        bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that discussion!', 'buddyboss' ) );
    }
}

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.