Example #1
0
 /**
  * @covers ::bbp_is_user_favorite
  */
 public function test_bbp_is_user_favorite()
 {
     $u = $this->factory->user->create();
     $t = $this->factory->topic->create();
     $favorite = bbp_is_user_favorite($u, $t);
     $this->assertFalse($favorite);
     // Add topic favorite.
     bbp_add_user_favorite($u, $t);
     $favorite = bbp_is_user_favorite($u, $t);
     $this->assertTrue($favorite);
 }
 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 #3
0
/**
 * User favorites link
 *
 * Return the link to make a topic favorite/remove a topic from
 * favorites
 *
 * @since bbPress (r2652)
 *
 * @param mixed $args This function supports these arguments:
 *  - subscribe: Favorite text
 *  - unsubscribe: Unfavorite text
 *  - user_id: User id
 *  - topic_id: Topic id
 *  - before: Before the link
 *  - after: After the link
 * @param int $user_id Optional. User id
 * @param int $topic_id Optional. Topic id
 * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">. See ajax_favorite()
 * @uses bbp_get_user_id() To get the user id
 * @uses current_user_can() If the current user can edit the user
 * @uses bbp_get_topic_id() To get the topic id
 * @uses bbp_is_user_favorite() To check if the topic is user's favorite
 * @uses bbp_get_favorites_permalink() To get the favorites permalink
 * @uses bbp_get_topic_permalink() To get the topic permalink
 * @uses bbp_is_favorites() Is it the favorites page?
 * @uses apply_filters() Calls 'bbp_get_user_favorites_link' with the
 *                        html, add args, remove args, user & topic id
 * @return string User favorites link
 */
function stachestack_bbp_get_user_favorites_link($args = '', $user_id = 0, $wrap = true)
{
    if (!bbp_is_favorites_active()) {
        return false;
    }
    // Parse arguments against default values
    $r = bbp_parse_args($args, array('favorite' => __('Favorite', 'bbpress'), 'favorited' => __('Favorited', 'bbpress'), 'user_id' => 0, 'topic_id' => 0, 'before' => '', 'after' => ''), 'get_user_favorites_link');
    // Validate user and topic ID's
    $user_id = bbp_get_user_id($r['user_id'], true, true);
    $topic_id = bbp_get_topic_id($r['topic_id']);
    if (empty($user_id) || empty($topic_id)) {
        return false;
    }
    // No link if you can't edit yourself
    if (!current_user_can('edit_user', (int) $user_id)) {
        return false;
    }
    // Decide which link to show
    $is_fav = bbp_is_user_favorite($user_id, $topic_id);
    if (!empty($is_fav)) {
        $text = $r['favorited'];
        $query_args = array('action' => 'bbp_favorite_remove', 'topic_id' => $topic_id);
    } else {
        $text = $r['favorite'];
        $query_args = array('action' => 'bbp_favorite_add', 'topic_id' => $topic_id);
    }
    // Create the link based where the user is and if the topic is
    // already the user's favorite
    if (bbp_is_favorites()) {
        $permalink = bbp_get_favorites_permalink($user_id);
    } elseif (bbp_is_single_topic() || bbp_is_single_reply()) {
        $permalink = bbp_get_topic_permalink($topic_id);
    } else {
        $permalink = get_permalink();
    }
    $url = esc_url(wp_nonce_url(add_query_arg($query_args, $permalink), 'toggle-favorite_' . $topic_id));
    $sub = $is_fav ? ' class="is-favorite"' : '';
    $html = sprintf('%s<span id="favorite-%d"  %s><a href="%s" class="btn btn-success btn-xs favorite-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after']);
    // Initial output is wrapped in a span, ajax output is hooked to this
    if (!empty($wrap)) {
        $html = '<span id="favorite-toggle">' . $html . '</span>';
    }
    // Return the link
    return apply_filters('bbp_get_user_favorites_link', $html, $r, $user_id, $topic_id);
}
Example #4
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 #6
0
/**
 * User favorites link
 *
 * Return the link to make a topic favorite/remove a topic from
 * favorites
 *
 * @since bbPress (r2652)
 *
 * @param array $add Optional. Add to favorites args
 * @param array $rem Optional. Remove from favorites args
 * @param int $user_id Optional. User id
 * @param int $topic_id Optional. Topic id
 * @uses bbp_get_user_id() To get the user id
 * @uses current_user_can() If the current user can edit the user
 * @uses bbp_get_topic_id() To get the topic id
 * @uses bbp_is_user_favorite() To check if the topic is user's favorite
 * @uses bbp_get_favorites_permalink() To get the favorites permalink
 * @uses bbp_get_topic_permalink() To get the topic permalink
 * @uses bbp_is_favorites() Is it the favorites page?
 * @uses apply_filters() Calls 'bbp_get_user_favorites_link' with the
 *                        html, add args, remove args, user & topic id
 * @return string User favorites link
 */
function bbp_get_user_favorites_link($add = array(), $rem = array(), $user_id = 0, $topic_id = 0)
{
    if (!bbp_is_favorites_active()) {
        return false;
    }
    // Validate user and topic ID's
    $user_id = bbp_get_user_id($user_id, true, true);
    $topic_id = bbp_get_topic_id($topic_id);
    if (empty($user_id) || empty($topic_id)) {
        return false;
    }
    if (!current_user_can('edit_user', (int) $user_id)) {
        return false;
    }
    if (empty($add) || !is_array($add)) {
        $add = array('mid' => __('Add this topic to your favorites', 'bbpress'), 'post' => __(' (%?%)', 'bbpress'));
    }
    if (empty($rem) || !is_array($rem)) {
        $rem = array('pre' => __('This topic is one of your %favorites% [', 'bbpress'), 'mid' => __('&times;', 'bbpress'), 'post' => __(']', 'bbpress'));
    }
    $is_fav = bbp_is_user_favorite($user_id, $topic_id);
    if (!empty($is_fav)) {
        $url = esc_url(bbp_get_favorites_permalink($user_id));
        $rem = preg_replace('|%(.+)%|', "<a href='{$url}'>\$1</a>", $rem);
        $favs = array('action' => 'bbp_favorite_remove', 'topic_id' => $topic_id);
        $pre = is_array($rem) && isset($rem['pre']) ? $rem['pre'] : '';
        $mid = is_array($rem) && isset($rem['mid']) ? $rem['mid'] : (is_string($rem) ? $rem : '');
        $_post = is_array($rem) && isset($rem['post']) ? $rem['post'] : '';
    } else {
        $url = esc_url(bbp_get_favorites_permalink($user_id));
        $add = preg_replace('|%(.+)%|', "<a href='{$url}'>\$1</a>", $add);
        $favs = array('action' => 'bbp_favorite_add', 'topic_id' => $topic_id);
        $pre = is_array($add) && isset($add['pre']) ? $add['pre'] : '';
        $mid = is_array($add) && isset($add['mid']) ? $add['mid'] : (is_string($add) ? $add : '');
        $_post = is_array($add) && isset($add['post']) ? $add['post'] : '';
    }
    // Create the link based where the user is and if the topic is
    // already the user's favorite
    if (bbp_is_favorites()) {
        $permalink = bbp_get_favorites_permalink($user_id);
    } elseif (is_singular(bbp_get_topic_post_type())) {
        $permalink = bbp_get_topic_permalink($topic_id);
    } elseif (is_singular(bbp_get_reply_post_type())) {
        $permalink = bbp_get_topic_permalink($topic_id);
    } elseif (bbp_is_query_name('bbp_single_topic')) {
        $permalink = get_permalink();
    }
    $url = esc_url(wp_nonce_url(add_query_arg($favs, $permalink), 'toggle-favorite_' . $topic_id));
    $is_fav = $is_fav ? 'is-favorite' : '';
    $html = '<span id="favorite-toggle"><span id="favorite-' . $topic_id . '" class="' . $is_fav . '">' . $pre . '<a href="' . $url . '" class="dim:favorite-toggle:favorite-' . $topic_id . ':is-favorite">' . $mid . '</a>' . $_post . '</span></span>';
    // Return the link
    return apply_filters('bbp_get_user_favorites_link', $html, $add, $rem, $user_id, $topic_id);
}
 /**
  * Perform the export
  *
  * @access public
  * @since 1.0
  * @uses bbp_Export::csv_rows_out()
  * @return void
  */
 public function import()
 {
     if (!$this->can_import()) {
         wp_die(__('You do not have permission to import data.', 'bbp-export-import'), __('Error', 'bbp-export-import'));
     }
     $csv_array = $this->csv_to_array($_FILES['bbp_csv_file']['tmp_name'], ';');
     foreach ($csv_array as $key => $line) {
         $post_args = $line;
         $meta_args = array();
         // Setup author date
         if ($post_args['anonymous'] == '1') {
             $meta_args['anonymous_email'] = $line['post_author'];
         } else {
             $user = get_user_by('email', $post_args['post_author']);
             if (!$user) {
                 // The user doesn't exist, so create them
                 $user = wp_insert_user(array('user_email' => $post_args['post_author'], 'user_login' => $post_args['user_login']));
             }
             $post_args['post_author'] = $user->ID;
         }
         // Decode content
         $post_args['post_content'] = html_entity_decode($post_args['post_content']);
         $topic_type = bbp_get_topic_post_type();
         $reply_type = bbp_get_reply_post_type();
         // Remove the post args we don't want sent to wp_insert_post
         unset($post_args['anonymous']);
         unset($post_args['user_login']);
         switch ($line['post_type']) {
             case $topic_type:
                 // Set the forum parent for topics
                 $post_args['post_parent'] = $this->forum_id;
                 $meta_args['voice_count'] = $line['voices'];
                 $meta_args['reply_count'] = $post_args['reply_count'];
                 $topic_id = bbp_insert_topic($post_args, $meta_args);
                 // Subscribe the original poster to the topic
                 bbp_add_user_subscription($post_args['post_author'], $topic_id);
                 // Add the topic to the user's favorites
                 if (bbp_is_user_favorite($post_args['post_author'], $topic_id)) {
                     bbp_add_user_favorite($post_args['post_author'], $topic_id);
                 }
                 // Set topic as resolved if GetShopped Support Forum is active
                 if ($post_args['resolved'] == '1') {
                     add_post_meta($topic_id, '_bbps_topic_status', '2');
                 }
                 break;
             case $reply_type:
                 // Set the forum parent for replies. The topic ID is created above when the replie's topic is first created
                 $post_args['post_parent'] = $topic_id;
                 $reply_id = bbp_insert_reply($post_args, $meta_args);
                 // Subscribe reply author, if not already
                 if (!bbp_is_user_subscribed($post_args['post_author'], $topic_id)) {
                     bbp_add_user_subscription($post_args['post_author'], $topic_id);
                 }
                 // Mark as favorite
                 if (bbp_is_user_favorite($post_args['post_author'], $topic_id)) {
                     bbp_add_user_favorite($post_args['post_author'], $topic_id);
                 }
                 // Check if the next row is a topic, meaning we have reached the last reply and need to update the last active time
                 if ($csv_array[$key + 1]['post_type'] == bbp_get_topic_post_type()) {
                     bbp_update_forum_last_active_time($this->forum_id, $post_args['post_date']);
                 }
                 break;
         }
     }
     // Recount forum topic / reply counts
     bbp_admin_repair_forum_topic_count();
     bbp_admin_repair_forum_reply_count();
     wp_redirect(admin_url('post.php?post=' . $this->forum_id . '&action=edit'));
     exit;
 }