/** * AJAX handler to Subscribe/Unsubscribe a user from a topic * * @since bbPress (r3732) * * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_is_user_logged_in() To check if user is logged in * @uses bbp_get_current_user_id() To get the current user id * @uses current_user_can() To check if the current user can edit the user * @uses bbp_get_topic() To get the topic * @uses wp_verify_nonce() To verify the nonce * @uses bbp_is_user_subscribed() To check if the topic is in user's subscriptions * @uses bbp_remove_user_subscriptions() To remove the topic from user's subscriptions * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions * @uses bbp_ajax_response() To return JSON */ public function ajax_subscription() { // Bail if subscriptions are not active if (!bbp_is_subscriptions_active()) { bbp_ajax_response(false, __('Subscriptions are no longer active.', 'bbpress'), 300); } // Bail if user is not logged in if (!is_user_logged_in()) { bbp_ajax_response(false, __('Please login to subscribe to this topic.', 'bbpress'), 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.', 'bbpress'), 302); } // Get the topic $topic = bbp_get_topic($id); // Bail if topic cannot be found if (empty($topic)) { bbp_ajax_response(false, __('The topic could not be found.', 'bbpress'), 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?', 'bbpress'), 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.', 'bbpress'), 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); }
/** * AJAX handler to gettting the Subscribe/Unsubscribe state for a user from a forum * * @since bbPress (r5155) * * @uses bbp_is_subscriptions_active() To check if the subscriptions are active * @uses bbp_is_user_logged_in() To check if user is logged in * @uses bbp_get_current_user_id() To get the current user id * @uses current_user_can() To check if the current user can edit the user * @uses bbp_get_forum() To get the forum * @uses wp_verify_nonce() To verify the nonce * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions * @uses bbp_ajax_response() To return JSON */ public function ajax_get_forum_subscription() { // Bail if subscriptions are not active if (!bbp_is_subscriptions_active()) { bbp_ajax_response(false, __('Subscriptions are no longer active.', 'bbpress'), 300); } // Bail if user is not logged in if (!is_user_logged_in()) { bbp_ajax_response(false, __('Please login to subscribe to this forum.', 'bbpress'), 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.', 'bbpress'), 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.', 'bbpress'), 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?', 'bbpress' ), 304 ); //} // 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); }