function bbPress_ajax_favorite()
 {
     $user_id = bbp_get_current_user_id();
     $id = intval($_POST['id']);
     if (!current_user_can('edit_user', $user_id)) {
         die('-1');
     }
     if (!($topic = bbp_get_topic($id))) {
         die('0');
     }
     check_ajax_referer('toggle-favorite_' . $topic->ID);
     if (bbp_is_user_favorite($user_id, $topic->ID)) {
         if (bbp_remove_user_favorite($user_id, $topic->ID)) {
             die('1');
         }
     } else {
         if (bbp_add_user_favorite($user_id, $topic->ID)) {
             die('1');
         }
     }
     die('0');
 }
Example #2
0
/**
 * Remove a deleted topic from all users' favorites
 *
 * @since bbPress (r2652)
 *
 * @param int $topic_id Get the topic id to remove
 * @uses bbp_get_topic_id To get the topic id
 * @uses bbp_get_topic_favoriters() To get the topic's favoriters
 * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
 */
function bbp_remove_topic_from_all_favorites($topic_id = 0)
{
    $topic_id = bbp_get_topic_id($topic_id);
    // Bail if no topic
    if (empty($topic_id)) {
        return;
    }
    // Get users
    $users = (array) bbp_get_topic_favoriters($topic_id);
    // Users exist
    if (!empty($users)) {
        // Loop through users
        foreach ($users as $user) {
            // Remove each user
            bbp_remove_user_favorite($user, $topic_id);
        }
    }
}
Example #3
0
/**
 * Handles the front end adding and removing of favorite topics
 *
 * @uses bbp_get_user_id() To get the user id
 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
 * @uses current_user_can() To check if the current user can edit the user
 * @uses bbPress:errors:add() To log the error messages
 * @uses bbp_is_user_favorite() To check if the topic is in user's favorites
 * @uses bbp_remove_user_favorite() To remove the user favorite
 * @uses bbp_add_user_favorite() To add the user favorite
 * @uses do_action() Calls 'bbp_favorites_handler' with success, user id, topic
 *                    id and action
 * @uses bbp_is_favorites() To check if it's the favorites page
 * @uses bbp_get_favorites_link() To get the favorites page link
 * @uses bbp_get_topic_permalink() To get the topic permalink
 * @uses wp_safe_redirect() To redirect to the url
 */
function bbp_favorites_handler()
{
    if (!bbp_is_favorites_active()) {
        return false;
    }
    // Bail if not a GET action
    if ('GET' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    // Bail if required GET actions aren't passed
    if (empty($_GET['topic_id']) || empty($_GET['action'])) {
        return;
    }
    // Setup possible get actions
    $possible_actions = array('bbp_favorite_add', 'bbp_favorite_remove');
    // Bail if actions aren't meant for this function
    if (!in_array($_GET['action'], $possible_actions)) {
        return;
    }
    // What action is taking place?
    $action = $_GET['action'];
    $topic_id = intval($_GET['topic_id']);
    $user_id = bbp_get_user_id(0, true, true);
    // Check for empty topic
    if (empty($topic_id)) {
        bbp_add_error('bbp_favorite_topic_id', __('<strong>ERROR</strong>: No topic was found! Which topic are you marking/unmarking as favorite?', 'bbpress'));
        // Check nonce
    } elseif (!bbp_verify_nonce_request('toggle-favorite_' . $topic_id)) {
        bbp_add_error('bbp_favorite_nonce', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress'));
        // Check current user's ability to edit the user
    } elseif (!current_user_can('edit_user', $user_id)) {
        bbp_add_error('bbp_favorite_permissions', __('<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress'));
    }
    // Bail if errors
    if (bbp_has_errors()) {
        return;
    }
    /** No errors *************************************************************/
    $is_favorite = bbp_is_user_favorite($user_id, $topic_id);
    $success = false;
    if (true == $is_favorite && 'bbp_favorite_remove' == $action) {
        $success = bbp_remove_user_favorite($user_id, $topic_id);
    } elseif (false == $is_favorite && 'bbp_favorite_add' == $action) {
        $success = bbp_add_user_favorite($user_id, $topic_id);
    }
    // Do additional favorites actions
    do_action('bbp_favorites_handler', $success, $user_id, $topic_id, $action);
    // Success!
    if (true == $success) {
        // Redirect back from whence we came
        if (bbp_is_favorites()) {
            $redirect = bbp_get_favorites_permalink($user_id);
        } elseif (bbp_is_single_user()) {
            $redirect = bbp_get_user_profile_url();
        } elseif (is_singular(bbp_get_topic_post_type())) {
            $redirect = bbp_get_topic_permalink($topic_id);
        } elseif (is_single() || is_page()) {
            $redirect = get_permalink();
        }
        wp_safe_redirect($redirect);
        // For good measure
        exit;
        // Fail! Handle errors
    } elseif (true == $is_favorite && 'bbp_favorite_remove' == $action) {
        bbp_add_error('bbp_favorite_remove', __('<strong>ERROR</strong>: There was a problem removing that topic from favorites!', 'bbpress'));
    } elseif (false == $is_favorite && 'bbp_favorite_add' == $action) {
        bbp_add_error('bbp_favorite_add', __('<strong>ERROR</strong>: There was a problem favoriting that topic!', 'bbpress'));
    }
}
 /**
  * AJAX handler to add or remove a topic from a user's favorites
  *
  * @since bbPress (r3732)
  *
  * @uses bbp_is_favorites_active() To check if favorites 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 & check the referer
  * @uses bbp_is_user_favorite() To check if the topic is user's favorite
  * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
  * @uses bbp_add_user_favorite() To add the topic from user's favorites
  * @uses bbp_ajax_response() To return JSON
  */
 public function ajax_favorite()
 {
     // Bail if favorites are not active
     if (!bbp_is_favorites_active()) {
         bbp_ajax_response(false, __('Favorites 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 make this topic a favorite.', 'bbpress'), 301);
     }
     // Get user and topic data
     $user_id = bbp_get_current_user_id();
     $id = !empty($_POST['id']) ? intval($_POST['id']) : 0;
     // 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-favorite_' . $topic->ID)) {
         bbp_ajax_response(false, __('Are you sure you meant to do that?', 'bbpress'), 304);
     }
     // Take action
     $status = bbp_is_user_favorite($user_id, $topic->ID) ? bbp_remove_user_favorite($user_id, $topic->ID) : bbp_add_user_favorite($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_favorites_link($attrs, $user_id, false), 200);
 }
Example #5
0
 /**
  * @covers ::bbp_remove_user_favorite
  */
 public function test_bbp_remove_user_favorite()
 {
     $u = $this->factory->user->create();
     $t = $this->factory->topic->create_many(3);
     // Add topic favorites.
     update_user_option($u, '_bbp_favorites', implode(',', $t));
     // Remove user favorite.
     bbp_remove_user_favorite($u, $t[2]);
     $favorites = bbp_get_user_favorites_topic_ids($u);
     $this->assertEquals(array($t[0], $t[1]), $favorites);
     // Remove user favorite.
     bbp_remove_user_favorite($u, $t[1]);
     $favorites = bbp_get_user_favorites_topic_ids($u);
     $this->assertEquals(array($t[0]), $favorites);
 }
Example #6
0
 /**
  * @covers ::bbp_remove_user_favorite
  */
 public function test_bbp_remove_user_favorite()
 {
     $u = $this->factory->user->create();
     $t = $this->factory->topic->create_many(3);
     // Add topic favorites.
     foreach ($t as $topic) {
         add_post_meta($topic, '_bbp_favorite', $u);
     }
     // Remove user favorite.
     bbp_remove_user_favorite($u, $t[2]);
     $favorites = bbp_get_user_favorites_topic_ids($u);
     $this->assertEqualSets(array($t[0], $t[1]), $favorites);
     // Remove user favorite.
     bbp_remove_user_favorite($u, $t[1]);
     $favorites = bbp_get_user_favorites_topic_ids($u);
     $this->assertEqualSets(array($t[0]), $favorites);
 }