function bp_blogs_add_user_to_blog($user_id, $role = false, $blog_id = 0) { global $wpdb; if (empty($blog_id)) { $blog_id = isset($wpdb->blogid) ? $wpdb->blogid : bp_get_root_blog_id(); } if (empty($role)) { $key = $wpdb->get_blog_prefix($blog_id) . 'capabilities'; $roles = bp_get_user_meta($user_id, $key, true); if (is_array($roles)) { $role = array_search(1, $roles); } else { return false; } } if ($role != 'subscriber') { bp_blogs_record_blog($blog_id, $user_id, true); } }
/** * Action handler when a follow blogs button is clicked. * * Handles both following and unfollowing a blog. */ public static function action_handler() { if (empty($_GET['blog_id']) || !is_user_logged_in()) { return; } $action = false; if (!empty($_GET['bpfb-follow']) || !empty($_GET['bpfb-unfollow'])) { $nonce = !empty($_GET['bpfb-follow']) ? $_GET['bpfb-follow'] : $_GET['bpfb-unfollow']; $action = !empty($_GET['bpfb-follow']) ? 'follow' : 'unfollow'; $save = !empty($_GET['bpfb-follow']) ? 'bp_follow_start_following' : 'bp_follow_stop_following'; } if (!$action) { return; } if (!wp_verify_nonce($nonce, "bp_follow_blog_{$action}")) { return; } if (!$save(array('leader_id' => (int) $_GET['blog_id'], 'follower_id' => bp_loggedin_user_id(), 'follow_type' => 'blogs'))) { if ('follow' == $action) { $message = __('You are already following that blog.', 'bp-follow'); } else { $message = __('You are not following that blog.', 'bp-follow'); } bp_core_add_message($message, 'error'); // success on follow action } else { $blog_name = bp_blogs_get_blogmeta((int) $_GET['blog_id'], 'name'); // blog has never been recorded into BP; record it now if ('' === $blog_name && apply_filters('bp_follow_blogs_record_blog', true, (int) $_GET['blog_id'])) { // get the admin of the blog $admin = get_users(array('blog_id' => get_current_blog_id(), 'role' => 'administrator', 'orderby' => 'ID', 'number' => 1, 'fields' => array('ID'))); // record the blog $record_site = bp_blogs_record_blog((int) $_GET['blog_id'], $admin[0]->ID, true); // now refetch the blog name from blogmeta if (false !== $record_site) { $blog_name = bp_blogs_get_blogmeta((int) $_GET['blog_id'], 'name'); } } if ('follow' == $action) { if (!empty($blog_name)) { $message = sprintf(__('You are now following the site, %s.', 'bp-follow'), $blog_name); } else { $message = __('You are now following that site.', 'bp-follow'); } } else { if (!empty($blog_name)) { $message = sprintf(__('You are no longer following the site, %s.', 'bp-follow'), $blog_name); } else { $message = __('You are no longer following that site.', 'bp-follow'); } } bp_core_add_message($message); } // it's possible that wp_get_referer() returns false, so let's fallback to the displayed user's page $redirect = wp_get_referer() ? wp_get_referer() : bp_displayed_user_domain() . bp_get_blogs_slug() . '/' . constant('BP_FOLLOW_BLOGS_USER_FOLLOWING_SLUG') . '/'; bp_core_redirect($redirect); }
/** * Record a user's association with a blog. * * This function is hooked to several WordPress actions where blog roles are * set/changed ('add_user_to_blog', 'profile_update', 'user_register'). It * parses the changes, and records them as necessary in the BP blog tracker. * * BuddyPress does not track blogs for users with the 'subscriber' role by * default, though as of 2.1.0 you can filter 'bp_blogs_get_allowed_roles' to * modify this behavior. * * @param int $user_id The ID of the user * @param string|bool $role User's WordPress role for this blog ID * @param int $blog_id Blog ID user is being added to * * @return bool|null False on failure. */ function bp_blogs_add_user_to_blog($user_id, $role = false, $blog_id = 0) { global $wpdb; // If no blog ID was passed, use the root blog ID if (empty($blog_id)) { $blog_id = isset($wpdb->blogid) ? $wpdb->blogid : bp_get_root_blog_id(); } // If no role was passed, try to find the blog role if (empty($role)) { // Get user capabilities $key = $wpdb->get_blog_prefix($blog_id) . 'capabilities'; $user_roles = array_keys((array) bp_get_user_meta($user_id, $key, true)); // User has roles so lets if (!empty($user_roles)) { // Get blog roles $blog_roles = array_keys(bp_get_current_blog_roles()); // Look for blog only roles of the user $intersect_roles = array_intersect($user_roles, $blog_roles); // If there's a role in the array, use the first one. This isn't // very smart, but since roles aren't exactly hierarchical, and // WordPress does not yet have a UI for multiple user roles, it's // fine for now. if (!empty($intersect_roles)) { $role = array_shift($intersect_roles); } } } // Bail if no role was found or role is not in the allowed roles array if (empty($role) || !in_array($role, bp_blogs_get_allowed_roles())) { return false; } // Record the blog activity for this user being added to this blog bp_blogs_record_blog($blog_id, $user_id, true); }
function bp_blogs_add_user_to_blog( $user_id, $role, $blog_id = false ) { global $current_blog; if ( empty( $blog_id ) ) $blog_id = $current_blog->blog_id; if ( $role != 'subscriber' ) bp_blogs_record_blog( $blog_id, $user_id, true ); }
/** * Upgrade blog. Basically add removed blog entry to BP cache * * @param int $blog_id * @param int $level * @param int $old_level * * @return null **/ function upgrade_blog($blog_id = null, $level = null, $old_level = null) { if (empty($blog_id)) { return; } // Get user ID switch_to_blog($blog_id); $user = get_admin_users_for_domain(); restore_current_blog(); if (function_exists('bp_blogs_record_blog')) { bp_blogs_record_blog($blog_id, $user['ID']); } }
function bp_blogs_add_user_to_blog($user_id, $role, $blog_id) { if ($role != 'subscriber') { bp_blogs_record_blog($blog_id, $user_id); } }
/** * @group update_blog_details */ public function test_update_blog_details_should_purge_blogmeta_cache() { if (!is_multisite()) { return; } $u = $this->factory->user->create(); $b1 = $this->factory->blog->create(); bp_blogs_record_blog($b1, $u, true); // prime cache bp_blogs_get_blogmeta($b1, 'url'); $this->assertNotEmpty(wp_cache_get($b1, 'blog_meta')); // updating blog details should purge cache update_blog_details($b1, array('domain' => 'awesome.com')); // assert cache is purged $this->assertEmpty(wp_cache_get($b1, 'blog_meta')); }