Beispiel #1
0
/**
 * Return checked value of forum subscription
 *
 * @since bbPress (r5156)
 *
 * @uses bbp_is_forum_edit() To check if it's the forum edit page
 * @uses bbp_get_global_post_field() To get current post author
 * @uses bbp_get_current_user_id() To get the current user id
 * @uses bbp_is_user_subscribed_to_forum() To check if the user is
 *                                          subscribed to the forum
 * @uses apply_filters() Calls 'bbp_get_form_forum_subscribed' with the
 *                option
 * @return string Checked value of forum subscription
 */
function bbp_get_form_forum_subscribed()
{
    // Get _POST data
    if (bbp_is_post_request() && isset($_POST['bbp_forum_subscription'])) {
        $forum_subscribed = (bool) $_POST['bbp_forum_subscription'];
        // Get edit data
    } elseif (bbp_is_forum_edit() || bbp_is_reply_edit()) {
        // Get current posts author
        $post_author = bbp_get_global_post_field('post_author', 'raw');
        // Post author is not the current user
        if (bbp_get_current_user_id() !== $post_author) {
            $forum_subscribed = bbp_is_user_subscribed_to_forum($post_author);
            // Post author is the current user
        } else {
            $forum_subscribed = bbp_is_user_subscribed_to_forum(bbp_get_current_user_id());
        }
        // Get current status
    } elseif (bbp_is_single_forum()) {
        $forum_subscribed = bbp_is_user_subscribed_to_forum(bbp_get_current_user_id());
        // No data
    } else {
        $forum_subscribed = false;
    }
    // Get checked output
    $checked = checked($forum_subscribed, true, false);
    return apply_filters('bbp_get_form_forum_subscribed', $checked, $forum_subscribed);
}
Beispiel #2
0
/**
 * Check if a topic or forum is in user's subscription list or not
 *
 * @since bbPress (r5156)
 *
 * @param int $user_id Optional. User id
 * @param int $forum_id Optional. Topic id
 * @uses get_post() To get the post object
 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
 * @uses bbp_get_forum_post_type() To get the forum post type
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
 *                        forum/topic id and subsriptions
 * @return bool True if the forum or topic is in user's subscriptions, otherwise false
 */
function bbp_is_user_subscribed($user_id = 0, $object_id = 0)
{
    // Assume user is not subscribed
    $retval = false;
    // Setup ID's array
    $subscribed_ids = array();
    // User and object ID's are passed
    if (!empty($user_id) && !empty($object_id)) {
        // Get the post type
        $post_type = get_post_type($object_id);
        // Post exists, so check the types
        if (!empty($post_type)) {
            switch ($post_type) {
                // Forum
                case bbp_get_forum_post_type():
                    $subscribed_ids = bbp_get_user_subscribed_forum_ids($user_id);
                    $retval = bbp_is_user_subscribed_to_forum($user_id, $object_id, $subscribed_ids);
                    break;
                    // Topic (default)
                // Topic (default)
                case bbp_get_topic_post_type():
                default:
                    $subscribed_ids = bbp_get_user_subscribed_topic_ids($user_id);
                    $retval = bbp_is_user_subscribed_to_topic($user_id, $object_id, $subscribed_ids);
                    break;
            }
        }
    }
    return (bool) apply_filters('bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscribed_ids);
}
Beispiel #3
0
/**
 * Return the link to subscribe/unsubscribe from a forum or topic
 *
 * @since 2.0.0 bbPress (r2668)
 *
 * @param array $args This function supports these arguments:
 *  - subscribe: Subscribe text
 *  - unsubscribe: Unsubscribe text
 *  - user_id: User id
 *  - topic_id: Topic id
 *  - forum_id: Forum id
 *  - before: Before the link
 *  - after: After the link
 * @param int $user_id Optional. User id
 * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">.
 * @uses bbp_is_subscriptions_active() to check if subscriptions are active
 * @uses bbp_get_user_id() To get the user id
 * @uses bbp_get_user_id() To get the user id
 * @uses bbp_get_topic_id() To get the topic id
 * @uses bbp_get_forum_id() To get the forum id
 * @uses current_user_can() To check if the current user can edit user
 * @uses bbp_is_user_subscribed_to_forum() To check if the user is subscribed to the forum
 * @uses bbp_is_user_subscribed_to_topic() To check if the user is subscribed to the topic
 * @uses bbp_is_subscriptions() To check if it's the subscriptions page
 * @uses bbp_get_subscriptions_permalink() To get subscriptions link
 * @uses bbp_get_topic_permalink() To get topic link
 * @uses apply_filters() Calls 'bbp_get_user_subscribe_link' with the
 *                        link, args, user id & topic id
 * @return string Permanent link to topic
 */
function bbp_get_user_subscribe_link($args = array(), $user_id = 0, $wrap = true)
{
    if (!bbp_is_subscriptions_active()) {
        return;
    }
    // Parse arguments against default values
    $r = bbp_parse_args($args, array('subscribe' => __('Subscribe', 'bbpress'), 'unsubscribe' => __('Unsubscribe', 'bbpress'), 'user_id' => 0, 'topic_id' => 0, 'forum_id' => 0, 'before' => '&nbsp;|&nbsp;', 'after' => ''), 'get_user_subscribe_link');
    // Validate user and object ID's
    $user_id = bbp_get_user_id($r['user_id'], true, true);
    $topic_id = bbp_get_topic_id($r['topic_id']);
    $forum_id = bbp_get_forum_id($r['forum_id']);
    if (empty($user_id) || empty($topic_id) && empty($forum_id)) {
        return false;
    }
    // No link if you can't edit yourself
    if (!current_user_can('edit_user', $user_id)) {
        return false;
    }
    // Check if viewing a single forum
    if (empty($topic_id) && !empty($forum_id)) {
        // Decide which link to show
        $is_subscribed = bbp_is_user_subscribed_to_forum($user_id, $forum_id);
        if (!empty($is_subscribed)) {
            $text = $r['unsubscribe'];
            $query_args = array('action' => 'bbp_unsubscribe', 'forum_id' => $forum_id);
        } else {
            $text = $r['subscribe'];
            $query_args = array('action' => 'bbp_subscribe', 'forum_id' => $forum_id);
        }
        // Create the link based where the user is and if the user is
        // subscribed already
        if (bbp_is_subscriptions()) {
            $permalink = bbp_get_subscriptions_permalink($user_id);
        } elseif (bbp_is_single_forum() || bbp_is_single_reply()) {
            $permalink = bbp_get_forum_permalink($forum_id);
        } else {
            $permalink = get_permalink();
        }
        $url = esc_url(wp_nonce_url(add_query_arg($query_args, $permalink), 'toggle-subscription_' . $forum_id));
        $sub = $is_subscribed ? ' class="is-subscribed"' : '';
        $html = sprintf('%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-forum="%d">%s</a></span>%s', $r['before'], $forum_id, $sub, $url, $forum_id, $text, $r['after']);
        // Initial output is wrapped in a span, ajax output is hooked to this
        if (!empty($wrap)) {
            $html = '<span id="subscription-toggle">' . $html . '</span>';
        }
    } else {
        // Decide which link to show
        $is_subscribed = bbp_is_user_subscribed_to_topic($user_id, $topic_id);
        if (!empty($is_subscribed)) {
            $text = $r['unsubscribe'];
            $query_args = array('action' => 'bbp_unsubscribe', 'topic_id' => $topic_id);
        } else {
            $text = $r['subscribe'];
            $query_args = array('action' => 'bbp_subscribe', 'topic_id' => $topic_id);
        }
        // Create the link based where the user is and if the user is
        // subscribed already
        if (bbp_is_subscriptions()) {
            $permalink = bbp_get_subscriptions_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-subscription_' . $topic_id));
        $sub = $is_subscribed ? ' class="is-subscribed"' : '';
        $html = sprintf('%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-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="subscription-toggle">' . $html . '</span>';
        }
    }
    // Return the link
    return apply_filters('bbp_get_user_subscribe_link', $html, $r, $user_id, $topic_id);
}
Beispiel #4
0
 /**
  * @covers ::bbp_remove_user_forum_subscription
  */
 public function test_bbp_remove_user_forum_subscription()
 {
     $u = $this->factory->user->create();
     $f = $this->factory->forum->create();
     // Add forum subscription.
     bbp_add_user_forum_subscription($u, $f);
     $this->assertTrue(bbp_is_user_subscribed_to_forum($u, $f));
     // Remove forum subscription.
     bbp_remove_user_forum_subscription($u, $f);
     $this->assertFalse(bbp_is_user_subscribed_to_forum($u, $f));
 }