BBP_Default::ajax_forum_subscription()
AJAX handler to Subscribe/Unsubscribe a user from a forum
Description
Source
File: bp-forums/templates/default/bbpress-functions.php
public function ajax_forum_subscription() {
// Bail if subscriptions are not active
if ( ! bbp_is_subscriptions_active() ) {
bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'buddyboss' ), 300 );
}
// Bail if user is not logged in
if ( ! is_user_logged_in() ) {
bbp_ajax_response( false, __( 'Please login to subscribe to this forum.', 'buddyboss' ), 301 );
}
// Get user and forum data
$user_id = bbp_get_current_user_id();
$id = intval( $_POST['id'] );
// Bail if user cannot add favorites for this user
if ( ! current_user_can( 'edit_user', $user_id ) ) {
bbp_ajax_response( false, __( 'You do not have permission to do this.', 'buddyboss' ), 302 );
}
// Get the forum
$forum = bbp_get_forum( $id );
// Bail if forum cannot be found
if ( empty( $forum ) ) {
bbp_ajax_response( false, __( 'The forum could not be found.', 'buddyboss' ), 303 );
}
// Bail if user did not take this action
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $forum->ID ) ) {
bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'buddyboss' ), 304 );
}
// Take action
$status = bbp_is_user_subscribed( $user_id, $forum->ID ) ? bbp_remove_user_subscription( $user_id, $forum->ID ) : bbp_add_user_subscription( $user_id, $forum->ID );
// Bail if action failed
if ( empty( $status ) ) {
bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'buddyboss' ), 305 );
}
// Put subscription attributes in convenient array
$attrs = array(
'forum_id' => $forum->ID,
'user_id' => $user_id
);
// Action succeeded
bbp_ajax_response( true, bbp_get_forum_subscription_link( $attrs, $user_id, false ), 200 );
}
Changelog
| Version | Description |
|---|---|
| bbPress (r5155) | 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.