BBP_Default::ajax_subscription()

AJAX handler to Subscribe/Unsubscribe a user from a topic

Description

Source

File: bp-forums/templates/default/bbpress-functions.php

773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
public function ajax_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 discussion.', 'buddyboss' ), 301 );
    }
 
    // Get user and topic 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 topic
    $topic = bbp_get_topic( $id );
 
    // Bail if topic cannot be found
    if ( empty( $topic ) ) {
        bbp_ajax_response( false, __( 'The discussion 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_' . $topic->ID ) ) {
        bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'buddyboss' ), 304 );
    }
 
    // Take action
    $status = bbp_is_user_subscribed( $user_id, $topic->ID ) ? bbp_remove_user_subscription( $user_id, $topic->ID ) : bbp_add_user_subscription( $user_id, $topic->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(
        'topic_id' => $topic->ID,
        'user_id'  => $user_id
    );
 
    // Action succeeded
    bbp_ajax_response( true, bbp_get_user_subscribe_link( $attrs, $user_id, false ), 200 );
}

Changelog

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