/** * Return checked value of topic subscription * * @since 2.0.0 bbPress (r2976) * * @uses bbp_is_topic_edit() To check if it's the topic edit page * @uses bbp_is_user_subscribed_to_topic() To check if the user is * subscribed to the topic * @uses apply_filters() Calls 'bbp_get_form_topic_subscribed' with the * option * @return string Checked value of topic subscription */ function bbp_get_form_topic_subscribed() { // Get _POST data if (bbp_is_topic_form_post_request() && isset($_POST['bbp_topic_subscription'])) { $topic_subscribed = (bool) $_POST['bbp_topic_subscription']; // Get edit data } elseif (bbp_is_topic_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) { $topic_subscribed = bbp_is_user_subscribed_to_topic($post_author); // Post author is the current user } else { $topic_subscribed = bbp_is_user_subscribed_to_topic(bbp_get_current_user_id()); } // Get current status } elseif (bbp_is_single_topic()) { $topic_subscribed = bbp_is_user_subscribed_to_topic(bbp_get_current_user_id()); // No data } else { $topic_subscribed = false; } // Get checked output $checked = checked($topic_subscribed, true, false); return apply_filters('bbp_get_form_topic_subscribed', $checked, $topic_subscribed); }
/** * 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' => ' | ', '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); }
/** * Add a topic to user's subscriptions * * @since 2.0.0 bbPress (r2668) * * @param int $user_id Optional. User id * @param int $topic_id Optional. Topic id * @uses bbp_get_topic() To get the topic * @uses bbp_add_user_subscription() To add the subscription * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id * @return bool Always true */ function bbp_add_user_topic_subscription($user_id = 0, $topic_id = 0) { // Bail if not enough info if (empty($user_id) || empty($topic_id)) { return false; } // Bail if no topic $topic = bbp_get_topic($topic_id); if (empty($topic)) { return false; } // Bail if already subscribed if (bbp_is_user_subscribed_to_topic($user_id, $topic_id)) { return false; } // Bail if add fails if (!bbp_add_user_subscription($user_id, $topic_id)) { return false; } do_action('bbp_add_user_topic_subscription', $user_id, $topic_id); return true; }
/** * 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); }
/** * @covers ::bbp_remove_user_topic_subscription */ public function test_bbp_remove_user_topic_subscription() { $u = $this->factory->user->create(); $t = $this->factory->topic->create(); // Add forum subscription. bbp_add_user_topic_subscription($u, $t); $this->assertTrue(bbp_is_user_subscribed_to_topic($u, $t)); // Remove topic subscription. bbp_remove_user_topic_subscription($u, $t); $this->assertFalse(bbp_is_user_subscribed_to_topic($u, $t)); }