bbp_forum_subscriptions_handler( string $action = '' )

Handles the front end subscribing and unsubscribing forums

Description

Parameters

$action

(Optional) The requested action to compare this function to

Default value: ''

Source

File: bp-forums/users/functions.php

1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
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
function bbp_forum_subscriptions_handler( $action = '' ) {
 
    if ( ! bbp_is_subscriptions_active() ) {
        return false;
    }
 
    // Bail if no forum ID is passed
    if ( empty( $_GET['forum_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 );
    $forum_id = intval( $_GET['forum_id'] );
 
    // Check for empty forum
    if ( empty( $forum_id ) ) {
        bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found! Which forum are you subscribing/unsubscribing to?', 'buddyboss' ) );
 
    // Check nonce
    } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) {
        bbp_add_error( 'bbp_subscription_forum_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, $forum_id );
    $success         = false;
 
    if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
        $success = bbp_remove_user_subscription( $user_id, $forum_id );
    } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
        $success = bbp_add_user_subscription( $user_id, $forum_id );
    }
 
    // Do additional subscriptions actions
    do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_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_forum_post_type() ) ) {
            $redirect = bbp_get_forum_permalink( $forum_id );
        } elseif ( is_single() || is_page() ) {
            $redirect = get_permalink();
        } else {
            $redirect = get_permalink( $forum_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 forum!', 'buddyboss' ) );
    } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
        bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that forum!', 'buddyboss' ) );
    }
}

Changelog

Changelog
Version Description
bbPress (r5156) 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.