Example #1
0
/**
 * Check if a topic is in user's subscription list or not
 *
 * @since 2.5.0 bbPress (r5156)
 *
 * @param int $user_id Optional. User id
 * @param int $topic_id Optional. Topic id
 * @param array $subscribed_ids Optional. Array of topic ID's to check
 * @uses bbp_get_user_id() To get the user id
 * @uses bbp_get_topic() To get the topic
 * @uses bbp_get_topic_id() To get the topic id
 * @uses bbp_is_object_of_user() To check if the user is subscribed
 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
 *                        topic id and subsriptions
 * @return bool True if the topic is in user's subscriptions, otherwise false
 */
function bbp_is_user_subscribed_to_topic($user_id = 0, $topic_id = 0, $subscribed_ids = array())
{
    // Assume user is not subscribed
    $retval = false;
    // Validate user
    $user_id = bbp_get_user_id($user_id, true, true);
    if (!empty($user_id)) {
        // Get subscription ID's if none passed
        if (empty($subscribed_ids)) {
            $subscribed_ids = bbp_get_user_subscribed_topic_ids($user_id);
        }
        // User has topic subscriptions
        if (!empty($subscribed_ids)) {
            // Checking a specific topic id
            if (!empty($topic_id)) {
                $topic = bbp_get_topic($topic_id);
                $topic_id = !empty($topic) ? $topic->ID : 0;
                // Using the global topic id
            } elseif (bbp_get_topic_id()) {
                $topic_id = bbp_get_topic_id();
                // Use the current post id
            } elseif (!bbp_get_topic_id()) {
                $topic_id = get_the_ID();
            }
            // Is topic_id in the user's subscriptions
            if (!empty($topic_id)) {
                $retval = bbp_is_object_of_user($topic_id, $user_id, '_bbp_subscription');
            }
        }
    }
    return (bool) apply_filters('bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscribed_ids);
}
Example #2
0
 /**
  * @covers ::bbp_is_object_of_user
  */
 public function test_bbp_is_object_of_user()
 {
     $u = $this->factory->user->create();
     $t = $this->factory->topic->create();
     $r = bbp_is_object_of_user($t, $u, '_bbp_moderator');
     $this->assertFalse($r);
     // Add user id.
     add_metadata('post', $t, '_bbp_moderator', $u, false);
     $r = bbp_is_object_of_user($t, $u, '_bbp_moderator');
     $this->assertTrue($r);
 }