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

637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
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

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.