/** * Follow button */ function get_follow_button() { if (bp_follow_is_following(array('leader_id' => get_the_author_meta('ID'), 'follower_id' => bp_loggedin_user_id()))) { $link_text = __('Unfollow', 'artgorae'); } else { $link_text = __('Follow', 'artgorae'); } $args = array('leader_id' => get_the_author_meta('ID'), 'follower_id' => bp_loggedin_user_id(), 'link_text' => $link_text, 'link_class' => 'button alt', 'wrapper' => ''); echo bp_follow_get_add_follow_button($args); }
/** * @group bp_follow_data */ public function test_bp_follow_data() { $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); $args = array('leader_id' => $u1, 'follower_id' => $u2); // create a follow relationship bp_follow_start_following($args); // check if user is following - this should generate cache bp_follow_is_following($args); // assert that cache is there $key = "{$u1}:{$u2}:"; $cache = wp_cache_get($key, 'bp_follow_data'); $this->assertTrue(!empty($cache->id), (bool) $cache->id); // delete the follow relationship bp_follow_stop_following($args); // assert $this->assertEmpty(wp_cache_get($key, 'bp_follow_data')); }
/** * @group delete */ public function test_follow_and_delete_blog() { if (!is_multisite()) { return; } // create user and blog $u = $this->factory->user->create(); $b = $this->factory->blog->create(array('title' => 'The Foo Bar Blog', 'user_id' => $u)); // make blog creator follow own blog $f = bp_follow_start_following(array('leader_id' => $b, 'follower_id' => $u, 'follow_type' => 'blogs')); // assert that follow relationship worked $this->assertTrue($f); // now delete blog wpmu_delete_blog($b); // check if cache was deleted $this->assertEmpty(wp_cache_get($u, 'bp_follow_following_blogs_count')); $this->assertEmpty(wp_cache_get($b, 'bp_follow_followers_blogs_count')); // check if follow relationship was deleted $is_following = bp_follow_is_following(array('leader_id' => $b, 'follower_id' => $u, 'follow_type' => 'blogs')); $this->assertSame(0, $is_following); }
/** * 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; } }
/** * Checks if the owner is a follower of current user * Used in status callback * * @see mpp_init * @see mpp_register_status * * @param type $component_type * @param type $component_id * @param type $user_id * @return type */ function mpp_check_following_access($component_type, $component_id, $user_id = null) { $allow = false; if (is_super_admin() || $component_id == $user_id || function_exists('bp_follow_is_following') && bp_follow_is_following(array('leader_id' => get_current_user_id(), 'follower_id' => $component_id))) { $allow = true; } return apply_filters('mpp_check_friends_access', $allow, $component_type, $component_id, $user_id); }
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)); } }
/** * Returns a follow / unfollow button for a given user depending on the follower status. * * Checks to see if the follower is already following the leader. If is following, returns * "Stop following" button; if not following, returns "Follow" button. * * @param array $args { * Array of arguments. * @type int $leader_id The user ID of the person we want to follow. * @type int $follower_id The user ID initiating the follow request. * @type string $link_text The anchor text for the link. * @type string $link_title The title attribute for the link. * @type string $wrapper_class CSS class for the wrapper container. * @type string $link_class CSS class for the link. * @type string $wrapper The element for the wrapper container. Defaults to 'div'. * } * @return mixed String of the button on success. Boolean false on failure. * @uses bp_get_button() Renders a button using the BP Button API * @author r-a-y * @since 1.1 */ function bp_follow_get_add_follow_button($args = '') { global $bp, $members_template; $r = wp_parse_args($args, array('leader_id' => bp_displayed_user_id(), 'follower_id' => bp_loggedin_user_id(), 'link_text' => '', 'link_title' => '', 'wrapper_class' => '', 'link_class' => '', 'wrapper' => 'div')); if (!$r['leader_id'] || !$r['follower_id']) { return false; } // if we're checking during a members loop, then follow status is already // queried via bp_follow_inject_member_follow_status() if (!empty($members_template->in_the_loop) && $r['follower_id'] == bp_loggedin_user_id() && $r['leader_id'] == bp_get_member_user_id()) { $is_following = $members_template->member->is_following; // else we manually query the follow status } else { $is_following = bp_follow_is_following(array('leader_id' => $r['leader_id'], 'follower_id' => $r['follower_id'])); } // if the logged-in user is the leader, use already-queried variables if (bp_loggedin_user_id() && $r['leader_id'] == bp_loggedin_user_id()) { $leader_domain = bp_loggedin_user_domain(); $leader_fullname = bp_get_loggedin_user_fullname(); // else we do a lookup for the user domain and display name of the leader } else { $leader_domain = bp_core_get_user_domain($r['leader_id']); $leader_fullname = bp_core_get_user_displayname($r['leader_id']); } // setup some variables if ($is_following) { $id = 'following'; $action = 'stop'; $class = 'unfollow'; $link_text = sprintf(_x('Unfollow', 'Button', 'bp-follow'), apply_filters('bp_follow_leader_name', bp_get_user_firstname($leader_fullname), $r['leader_id'])); if (empty($r['link_text'])) { $r['link_text'] = $link_text; } } else { $id = 'not-following'; $action = 'start'; $class = 'follow'; $link_text = sprintf(_x('Follow', 'Button', 'bp-follow'), apply_filters('bp_follow_leader_name', bp_get_user_firstname($leader_fullname), $r['leader_id'])); if (empty($r['link_text'])) { $r['link_text'] = $link_text; } } $wrapper_class = 'follow-button ' . $id; if (!empty($r['wrapper_class'])) { $wrapper_class .= ' ' . esc_attr($r['wrapper_class']); } $link_class = $class; if (!empty($r['link_class'])) { $link_class .= ' ' . esc_attr($r['link_class']); } // make sure we can view the button if a user is on their own page $block_self = empty($members_template->member) ? true : false; // if we're using AJAX and a user is on their own profile, we need to set // block_self to false so the button shows up if (bp_follow_is_doing_ajax() && bp_is_my_profile()) { $block_self = false; } // setup the button arguments $button = array('id' => $id, 'component' => 'follow', 'must_be_logged_in' => true, 'block_self' => $block_self, 'wrapper_class' => $wrapper_class, 'wrapper_id' => 'follow-button-' . (int) $r['leader_id'], 'link_href' => wp_nonce_url($leader_domain . $bp->follow->followers->slug . '/' . $action . '/', $action . '_following'), 'link_text' => esc_attr($r['link_text']), 'link_title' => esc_attr($r['link_title']), 'link_id' => $class . '-' . (int) $r['leader_id'], 'link_class' => $link_class, 'wrapper' => !empty($r['wrapper']) ? esc_attr($r['wrapper']) : false); // Filter and return the HTML button return bp_get_button(apply_filters('bp_follow_get_add_follow_button', $button, $r['leader_id'], $r['follower_id'])); }
/** * Output a 'Follow' activity button. * * @param $args { * Array of arguments. Also see other args via {@link BP_Button} class. * @type int $leader_id Activity ID to follow. * @type int $follower_id User ID initiating the follow request. * @type bool $show_follower_count Should we show the follower count for this item? Default: false. * } */ function bp_follow_activity_button($args = array()) { global $activities_template; $r = bp_parse_args($args, array('leader_id' => !empty($activities_template->in_the_loop) ? bp_get_activity_id() : 0, 'follower_id' => bp_loggedin_user_id(), 'link_text' => '', 'link_title' => '', 'wrapper_class' => '', 'link_class' => 'button bp-primary-action', 'wrapper' => false, 'show_follower_count' => false), 'follow_activity_button'); if (!$r['leader_id'] || !$r['follower_id']) { return; } $follow_type = bp_follow_activity_get_type($r['leader_id']); // if we're checking during an activity loop, then follow status is already // queried via bulk_inject_follow_activity_status() if (!empty($activities_template->in_the_loop) && $r['follower_id'] == bp_loggedin_user_id() && $r['leader_id'] == bp_get_activity_id() && 'activity' === $follow_type) { $is_following = $activities_template->activity->is_following; // else we manually query the follow status } else { $is_following = bp_follow_is_following(array('leader_id' => $r['leader_id'], 'follower_id' => $r['follower_id'], 'follow_type' => $follow_type)); } // setup some variables if ($is_following) { $id = 'following'; $action = 'unfollow'; /* @todo Maybe bring back the count for the 'unfollow' button? $count = bp_follow_get_the_followers_count( array( 'object_id' => $r['leader_id'], 'follow_type' => $follow_type ) ); */ $count = 0; if (empty($count)) { $link_text = _x('Unfollow', 'Follow activity button', 'bp-follow'); } else { $link_text = sprintf(_x('Unfollow %s', 'Follow activity button', 'bp-follow'), '<span>' . $count . '</span>'); } if (empty($r['link_text'])) { $r['link_text'] = $link_text; } } else { $id = 'not-following'; $action = 'follow'; $count = 0; if (true === $r['show_follower_count']) { $count = bp_follow_get_the_followers_count(array('object_id' => $r['leader_id'], 'follow_type' => $follow_type)); } if (empty($count)) { $link_text = _x('Follow', 'Follow activity button', 'bp-follow'); } else { $link_text = sprintf(_x('Follow %s', 'Follow activity button', 'bp-follow'), '<span>' . $count . '</span>'); } if (empty($r['link_text'])) { $r['link_text'] = $link_text; } } $wrapper_class = 'follow-button ' . $id; if (!empty($r['wrapper_class'])) { $wrapper_class .= ' ' . esc_attr($r['wrapper_class']); } $link_class = $action; if (!empty($r['link_class'])) { $link_class .= ' ' . esc_attr($r['link_class']); } // setup the button arguments $button = array('id' => $id, 'component' => 'follow', 'must_be_logged_in' => true, 'block_self' => false, 'wrapper_class' => $wrapper_class, 'wrapper_id' => 'follow-button-' . (int) $r['leader_id'], 'link_href' => wp_nonce_url(trailingslashit(bp_get_activity_directory_permalink() . $action . '/' . esc_attr($r['leader_id'])), "bp_follow_activity_{$action}"), 'link_text' => $r['link_text'], 'link_title' => esc_attr($r['link_title']), 'link_id' => $action . '-' . (int) $r['leader_id'], 'link_class' => $link_class, 'wrapper' => !empty($r['wrapper']) ? esc_attr($r['wrapper']) : false); // Filter and output the HTML button bp_button(apply_filters('bp_follow_activity_get_follow_button', $button, $r, $is_following)); }
/** * Static method to generate a follow blogs button. */ public static function get_button($args = '') { global $blogs_template; $r = wp_parse_args($args, array('leader_id' => !empty($blogs_template->in_the_loop) ? bp_get_blog_id() : get_current_blog_id(), 'follower_id' => bp_loggedin_user_id(), 'link_text' => '', 'link_title' => '', 'wrapper_class' => '', 'link_class' => '', 'wrapper' => 'div')); if (!$r['leader_id'] || !$r['follower_id']) { return false; } // if we're checking during a blog loop, then follow status is already // queried via bulk_inject_follow_blog_status() if (!empty($blogs_template->in_the_loop) && $r['follower_id'] == bp_loggedin_user_id() && $r['leader_id'] == bp_get_blog_id()) { $is_following = $blogs_template->blog->is_following; // else we manually query the follow status } else { $is_following = bp_follow_is_following(array('leader_id' => $r['leader_id'], 'follower_id' => $r['follower_id'], 'follow_type' => 'blogs')); } // setup some variables if ($is_following) { $id = 'following'; $action = 'unfollow'; $link_text = _x('Unfollow', 'Button', 'bp-follow'); if (empty($blogs_template->in_the_loop)) { $link_text = _x('Unfollow Site', 'Button', 'bp-follow'); } if (empty($r['link_text'])) { $r['link_text'] = $link_text; } } else { $id = 'not-following'; $action = 'follow'; $link_text = _x('Follow', 'Button', 'bp-follow'); if (empty($blogs_template->in_the_loop)) { $link_text = _x('Follow Site', 'Button', 'bp-follow'); } if (empty($r['link_text'])) { $r['link_text'] = $link_text; } } $wrapper_class = 'follow-button ' . $id; if (!empty($r['wrapper_class'])) { $wrapper_class .= ' ' . esc_attr($r['wrapper_class']); } $link_class = $action; if (!empty($r['link_class'])) { $link_class .= ' ' . esc_attr($r['link_class']); } // setup the button arguments $button = array('id' => $id, 'component' => 'follow', 'must_be_logged_in' => true, 'block_self' => false, 'wrapper_class' => $wrapper_class, 'wrapper_id' => 'follow-button-' . (int) $r['leader_id'], 'link_href' => wp_nonce_url(add_query_arg('blog_id', $r['leader_id'], home_url('/')), "bp_follow_blog_{$action}", "bpfb-{$action}"), 'link_text' => esc_attr($r['link_text']), 'link_title' => esc_attr($r['link_title']), 'link_id' => $action . '-' . (int) $r['leader_id'], 'link_class' => $link_class, 'wrapper' => !empty($r['wrapper']) ? esc_attr($r['wrapper']) : false); // Filter and return the HTML button return bp_get_button(apply_filters('bp_follow_blogs_get_follow_button', $button, $r, $is_following)); }