/**
  * Test two follow relationships with the same leader_id and follower_id.
  *
  * But, set the follow_type for the second relationship to 'blogs'. This is to
  * determine if there are any conflicts with setting the same leader and
  * follower IDs.
  *
  * @group blogs
  */
 public function test_follow_start_following_user_blog_with_same_leader_follower_id()
 {
     // add a user relationship
     $u1 = $this->factory->user->create();
     $u2 = $this->factory->user->create();
     $f1 = bp_follow_start_following(array('leader_id' => $u1, 'follower_id' => $u2));
     // now add a blog relationship
     // use the exact same leader_id and follower_id, but set different type
     $f2 = bp_follow_start_following(array('leader_id' => $u1, 'follower_id' => $u2, 'follow_type' => 'blogs'));
     $this->assertTrue($f2);
 }
 /**
  * @group groupblog
  */
 public function test_follow_blog_and_groupblog()
 {
     if (!is_multisite()) {
         return;
     }
     // save the current user and override logged-in user
     $old_user = get_current_user_id();
     $u = $this->factory->user->create();
     $this->set_current_user($u);
     // create some blogs
     $b = $this->factory->blog->create(array('title' => 'Groupblog', 'user_id' => $u));
     $b2 = $this->factory->blog->create(array('title' => 'Test blog 1', 'user_id' => $u));
     $b3 = $this->factory->blog->create(array('title' => 'Test blog 2', 'user_id' => $u));
     // create a group and connect a blog
     $g = $this->factory->group->create(array('creator_id' => $u));
     groups_update_groupmeta($g, 'groupblog_blog_id', $b);
     // follow the groupblog
     $f = bp_follow_start_following(array('leader_id' => $b, 'follower_id' => $u, 'follow_type' => 'blogs'));
     // follow a regular blog
     $f2 = bp_follow_start_following(array('leader_id' => $b2, 'follower_id' => $u, 'follow_type' => 'blogs'));
     // add some activity items
     $a = $this->factory->activity->create(array('component' => buddypress()->groups->id, 'type' => 'new_groupblog_post', 'user_id' => $u, 'item_id' => $g, 'secondary_item_id' => 1));
     $a2 = $this->factory->activity->create(array('component' => buddypress()->blogs->id, 'type' => 'new_blog_post', 'user_id' => $u, 'item_id' => $b3, 'secondary_item_id' => 1));
     $a3 = $this->factory->activity->create(array('component' => buddypress()->blogs->id, 'type' => 'new_blog_post', 'user_id' => $u, 'item_id' => $b2, 'secondary_item_id' => 1));
     // fake that we're on a user's "Activity > Followed Sites" page
     add_filter('bp_ajax_querystring', array($this, 'add_activity_scope_filter'));
     // fake that BP groupblog is installed so groupblog filter will kick in
     if (!function_exists('bp_groupblog_init')) {
         function bp_groupblog_init()
         {
         }
     }
     // run the activity loop
     global $activities_template;
     bp_has_activities(bp_ajax_querystring('activity'));
     // grab the activity IDs from the loop
     $ids = wp_list_pluck($activities_template->activities, 'id');
     // assert!
     $this->assertEquals(array($a, $a3), $ids);
     // reset everything
     $activities_template = null;
     $this->set_current_user($old_user);
     remove_filter('bp_ajax_querystring', array($this, 'add_activity_scope_filter'));
 }
 /**
  * @group date_query
  */
 public function test_date_query()
 {
     $u1 = $this->factory->user->create();
     $u2 = $this->factory->user->create();
     $u3 = $this->factory->user->create();
     $u4 = $this->factory->user->create();
     // follow all users at different dates
     bp_follow_start_following(array('leader_id' => $u2, 'follower_id' => $u1));
     bp_follow_start_following(array('leader_id' => $u3, 'follower_id' => $u1, 'date_recorded' => '2001-01-01 12:00'));
     bp_follow_start_following(array('leader_id' => $u4, 'follower_id' => $u1, 'date_recorded' => '2005-01-01 12:00'));
     // 'date_query' before test
     $query = BP_Follow::get_following($u1, '', array('date_query' => array(array('before' => array('year' => 2004, 'month' => 1, 'day' => 1)))));
     $this->assertEquals(array($u3), $query);
     // 'date_query' range test
     $query = BP_Follow::get_following($u1, '', array('date_query' => array(array('after' => 'January 2nd, 2001', 'before' => array('year' => 2013, 'month' => 1, 'day' => 1), 'inclusive' => true))));
     $this->assertEquals(array($u4), $query);
     // 'date_query' after and relative test
     $query = BP_Follow::get_following($u1, '', array('date_query' => array(array('after' => '1 day ago'))));
     $this->assertEquals(array($u2), $query);
 }
/**
 * AJAX callback when clicking on the "Follow" button to follow a user.
 *
 * @uses check_admin_referer() Checks to make sure the WP security nonce matches.
 * @uses bp_follow_start_following() Starts a user following another user.
 * @uses bp_follow_is_following() Checks to see if a user is following another user already.
 */
function bp_follow_ajax_action_start()
{
    check_admin_referer('start_following');
    $link_class = !empty($_POST['link_class']) ? str_replace('follow ', '', $_POST['link_class']) : false;
    // successful follow
    if (bp_follow_start_following(array('leader_id' => $_POST['uid'], 'follower_id' => bp_loggedin_user_id()))) {
        // output unfollow button
        $output = bp_follow_get_add_follow_button(array('leader_id' => $_POST['uid'], 'follower_id' => bp_loggedin_user_id(), 'wrapper' => false, 'link_class' => $link_class));
        // failed follow
    } else {
        // output fallback invalid button
        $args = array('id' => 'invalid', 'link_href' => 'javascript:;', 'component' => 'follow', 'wrapper' => false, 'link_class' => $link_class);
        if (bp_follow_is_following(array('leader_id' => $_POST['uid'], 'follower_id' => bp_loggedin_user_id()))) {
            $output = bp_get_button(array_merge(array('link_text' => __('Already following', 'bp-follow')), $args));
        } else {
            $output = bp_get_button(array_merge(array('link_text' => __('Error following user', 'bp-follow')), $args));
        }
    }
    echo $output;
    exit;
}
Example #5
0
function invite_anyone_activate_user($user_id, $key, $user)
{
    global $bp;
    $email = bp_core_get_user_email($user_id);
    $inviters = array();
    // Fire the query
    $invites = invite_anyone_get_invitations_by_invited_email($email);
    if ($invites->have_posts()) {
        // From the posts returned by the query, get a list of unique inviters
        $groups = array();
        while ($invites->have_posts()) {
            $invites->the_post();
            $inviter_id = get_the_author_meta('ID');
            $inviters[] = $inviter_id;
            $groups_data = wp_get_post_terms(get_the_ID(), invite_anyone_get_invited_groups_tax_name());
            foreach ($groups_data as $group_data) {
                if (!isset($groups[$group_data->name])) {
                    // Keyed by inviter, which means they'll only get one invite per group
                    $groups[$group_data->name] = $inviter_id;
                }
            }
            // Mark as accepted
            update_post_meta(get_the_ID(), 'bp_ia_accepted', date('Y-m-d H:i:s'));
        }
        $inviters = array_unique($inviters);
        // Friendship requests
        if (bp_is_active('friends') && apply_filters('invite_anyone_send_friend_requests_on_acceptance', true)) {
            if (function_exists('friends_add_friend')) {
                foreach ($inviters as $inviter) {
                    friends_add_friend($inviter, $user_id);
                }
            }
        }
        // BuddyPress Followers support
        if (function_exists('bp_follow_start_following') && apply_filters('invite_anyone_send_follow_requests_on_acceptance', true)) {
            foreach ($inviters as $inviter) {
                bp_follow_start_following(array('leader_id' => $user_id, 'follower_id' => $inviter));
                bp_follow_start_following(array('leader_id' => $inviter, 'follower_id' => $user_id));
            }
        }
        // Group invitations
        if (bp_is_active('groups')) {
            foreach ($groups as $group_id => $inviter_id) {
                $args = array('user_id' => $user_id, 'group_id' => $group_id, 'inviter_id' => $inviter_id);
                groups_invite_user($args);
                groups_send_invites($inviter_id, $group_id);
            }
        }
    }
    do_action('accepted_email_invite', $user_id, $inviters);
}
 function rtmedia_api_process_follow_request()
 {
     $this->rtmediajsonapifunction->rtmedia_api_verfiy_token();
     $ec_empty_follow_id = 400003;
     $msg_empty_follow_id = __('follow user id missing', 'rtmedia');
     $ec_started_following = 400004;
     $msg_started_following = __('started following', 'rtmedia');
     $ec_already_following = 400005;
     $msg_already_following = __('already following', 'rtmedia');
     extract($_POST);
     if (empty($follow_id)) {
         echo $this->rtmedia_api_response_object('FALSE', $ec_empty_follow_id, $msg_empty_follow_id);
         exit;
     }
     $args = array('leader_id' => $follow_id, 'follower_id' => $this->user_id);
     $already_following = bp_follow_is_following($args);
     if (!$already_following) {
         $follow_user = bp_follow_start_following($args);
         if ($follow_user) {
             echo $this->rtmedia_api_response_object('TRUE', $ec_started_following, $msg_started_following);
             exit;
         } else {
             echo $this->rtmedia_api_response_object('TRUE', $this->ec_server_error, $this->msg_server_error);
             exit;
         }
     } else {
         echo $this->rtmedia_api_response_object('TRUE', $ec_already_following, $msg_already_following);
         exit;
     }
 }
Example #7
0
 function rtmedia_api_process_follow_request()
 {
     $this->rtmediajsonapifunction->rtmedia_api_verfiy_token();
     $ec_empty_follow_id = 400003;
     $msg_empty_follow_id = esc_html__('follow user id missing', 'buddypress-media');
     $ec_started_following = 400004;
     $msg_started_following = esc_html__('started following', 'buddypress-media');
     $ec_already_following = 400005;
     $msg_already_following = esc_html__('already following', 'buddypress-media');
     $follow_id = filter_input(INPUT_POST, 'follow_id', FILTER_SANITIZE_NUMBER_INT);
     if (empty($follow_id)) {
         wp_send_json($this->rtmedia_api_response_object('FALSE', $ec_empty_follow_id, $msg_empty_follow_id));
     }
     $args = array('leader_id' => $follow_id, 'follower_id' => $this->user_id);
     $already_following = bp_follow_is_following($args);
     if (!$already_following) {
         $follow_user = bp_follow_start_following($args);
         if ($follow_user) {
             wp_send_json($this->rtmedia_api_response_object('TRUE', $ec_started_following, $msg_started_following));
         } else {
             wp_send_json($this->rtmedia_api_response_object('TRUE', $this->ec_server_error, $this->msg_server_error));
         }
     } else {
         wp_send_json($this->rtmedia_api_response_object('TRUE', $ec_already_following, $msg_already_following));
     }
 }
Example #8
0
function invite_anyone_activate_user($user_id, $key, $user)
{
    global $bp;
    $email = bp_core_get_user_email($user_id);
    if ($invites = invite_anyone_get_invitations_by_invited_email($email)) {
        // Mark as "is_joined"
        invite_anyone_mark_as_joined($email);
        // Friendship requests
        if (bp_is_active('friends')) {
            $inviters = array();
            foreach ($invites as $invite) {
                if (!in_array($invite->inviter_id, $inviters)) {
                    $inviters[] = $invite->inviter_id;
                }
            }
            if (function_exists('friends_add_friend')) {
                foreach ($inviters as $inviter) {
                    friends_add_friend($inviter, $user_id);
                }
            }
        }
        // BuddyPress Followers support
        if (function_exists('bp_follow_start_following')) {
            $inviters = array();
            foreach ($invites as $invite) {
                if (!in_array($invite->inviter_id, $inviters)) {
                    $inviters[] = $invite->inviter_id;
                }
            }
            foreach ($inviters as $inviter) {
                bp_follow_start_following(array('leader_id' => $user_id, 'follower_id' => $inviter));
                bp_follow_start_following(array('leader_id' => $inviter, 'follower_id' => $user_id));
            }
        }
        // BuddyPress Followers support
        if (function_exists('bp_follow_start_following')) {
            $inviters = array();
            foreach ($invites as $invite) {
                if (!in_array($invite->inviter_id, $inviters)) {
                    $inviters[] = $invite->inviter_id;
                }
            }
            foreach ($inviters as $inviter) {
                bp_follow_start_following(array('leader_id' => $user_id, 'follower_id' => $inviter));
                bp_follow_start_following(array('leader_id' => $inviter, 'follower_id' => $user_id));
            }
        }
        // Group invitations
        if (bp_is_active('groups')) {
            $groups = array();
            foreach ($invites as $invite) {
                if (!$invite->group_invitations[0]) {
                    continue;
                } else {
                    $group_invitations = unserialize($invite->group_invitations);
                }
                foreach ($group_invitations as $group) {
                    if (!in_array($group, array_keys($groups))) {
                        $groups[$group] = $invite->inviter_id;
                    }
                }
            }
            foreach ($groups as $group_id => $inviter_id) {
                $args = array('user_id' => $user_id, 'group_id' => $group_id, 'inviter_id' => $inviter_id);
                groups_invite_user($args);
                groups_send_invites($inviter_id, $group_id);
            }
        }
    }
    do_action('accepted_email_invite', $user_id, $inviters);
}
 /**
  * @group bp_follow_get_followers
  */
 public function test_bp_follow_get_followers()
 {
     $u1 = $this->factory->user->create();
     $u2 = $this->factory->user->create();
     $u3 = $this->factory->user->create();
     $u4 = $this->factory->user->create();
     // let user 1 be followed by everyone
     bp_follow_start_following(array('leader_id' => $u1, 'follower_id' => $u2));
     bp_follow_start_following(array('leader_id' => $u1, 'follower_id' => $u3));
     bp_follow_start_following(array('leader_id' => $u1, 'follower_id' => $u4));
     // get followers for user 1
     bp_follow_get_followers(array('user_id' => $u1));
     // assert
     $this->assertEqualSets(array($u2, $u3, $u4), wp_cache_get($u1, 'bp_follow_user_followers_query'));
     // one user stops following user 1
     bp_follow_stop_following(array('leader_id' => $u1, 'follower_id' => $u4));
     // make sure cache is invalidated
     $this->assertEmpty(wp_cache_get($u1, 'bp_follow_user_followers_query'));
 }