/** * AJAX callback when clicking on the "Unfollow" button to unfollow a user. * * @uses check_admin_referer() Checks to make sure the WP security nonce matches. * @uses bp_follow_stop_following() Stops 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_stop() { check_admin_referer('stop_following'); $link_class = !empty($_POST['link_class']) ? str_replace('unfollow ', '', $_POST['link_class']) : false; // successful unfollow if (bp_follow_stop_following(array('leader_id' => $_POST['uid'], 'follower_id' => bp_loggedin_user_id()))) { // output follow 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 unfollow } 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' => __('Not following', 'bp-follow')), $args)); } else { $output = bp_get_button(array_merge(array('link_text' => __('Error unfollowing user', 'bp-follow')), $args)); } } echo $output; exit; }
function rtmedia_api_process_unfollow_request() { $this->rtmediajsonapifunction->rtmedia_api_verfiy_token(); $ec_empty_unfollow_id = 400006; $msg_empty_unfollow_id = __('unfollow id missing', 'rtmedia'); $ec_stopped_following = 400007; $msg_stopped_following = __('stopped following', 'rtmedia'); $ec_not_following = 400008; $msg_not_following = __('not following', 'rtmedia'); extract($_POST); if (empty($unfollow_id)) { echo $this->rtmedia_api_response_object('FALSE', $ec_empty_unfollow_id, $msg_empty_unfollow_id); exit; } $args = array('leader_id' => $unfollow_id, 'follower_id' => $this->user_id); $following = bp_follow_is_following($args); if ($following) { $unfollow_user = bp_follow_stop_following($args); if ($unfollow_user) { echo $this->rtmedia_api_response_object('TRUE', $ec_stopped_following, $msg_stopped_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_not_following, $msg_not_following); exit; } }
function rtmedia_api_process_unfollow_request() { $this->rtmediajsonapifunction->rtmedia_api_verfiy_token(); $ec_empty_unfollow_id = 400006; $msg_empty_unfollow_id = esc_html__('unfollow id missing', 'buddypress-media'); $ec_stopped_following = 400007; $msg_stopped_following = esc_html__('stopped following', 'buddypress-media'); $ec_not_following = 400008; $msg_not_following = esc_html__('not following', 'buddypress-media'); $unfollow_id = filter_input(INPUT_POST, 'unfollow_id', FILTER_SANITIZE_NUMBER_INT); if (empty($unfollow_id)) { wp_send_json($this->rtmedia_api_response_object('FALSE', $ec_empty_unfollow_id, $msg_empty_unfollow_id)); } $args = array('leader_id' => $unfollow_id, 'follower_id' => $this->user_id); $following = bp_follow_is_following($args); if ($following) { $unfollow_user = bp_follow_stop_following($args); if ($unfollow_user) { wp_send_json($this->rtmedia_api_response_object('TRUE', $ec_stopped_following, $msg_stopped_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_not_following, $msg_not_following)); } }
/** * Clear cache when activity item is deleted. */ public function on_activity_delete($activities) { // Pluck the activity IDs out of the $activities array. $activity_ids = wp_parse_id_list(wp_list_pluck($activities, 'id')); // See if any of the deleted activity IDs were being followed $sql = 'SELECT leader_id, follower_id FROM ' . esc_sql(buddypress()->follow->table_name) . ' '; $sql .= 'WHERE leader_id IN (' . implode(',', wp_parse_id_list($activity_ids)) . ') '; $sql .= "AND follow_type = 'activity'"; $followed_ids = $GLOBALS['wpdb']->get_results($sql); foreach ($followed_ids as $activity) { // clear followers count for activity item wp_cache_delete($activity->leader_id, 'bp_follow_activity_followers_count'); // clear queried followers for activity item wp_cache_delete($activity->leader_id, 'bp_follow_activity_followers_query'); // delete user's activity follow count wp_cache_delete($activity->follower_id, 'bp_follow_user_activity_following_count'); // delete queried activity that user was following wp_cache_delete($activity->follower_id, 'bp_follow_user_activity_following_query'); // Delete the follow entry // @todo Need a mass bulk-delete method bp_follow_stop_following(array('leader_id' => $activity->leader_id, 'follower_id' => $activity->follower_id, 'follow_type' => 'activity')); } }
/** * Check stop following function when follow ID doesn't exist. * * @group bp_follow_stop_following */ public function test_stop_following_when_follow_id_does_not_exist() { $f1 = bp_follow_stop_following(array('leader_id' => get_current_user_id(), 'follower_id' => 9999)); $this->assertFalse($f1); }
/** * @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')); }