├── core ├── index.php ├── bp-multi-network-install.php ├── bp-multi-network-user-functions.php ├── class-bp-multi-network-actions.php ├── class-bp-multi-network-users.php └── class-bp-multi-network-filters.php ├── bp-multinetwork.php └── class-bp-multi-network-component.php /core/index.php: -------------------------------------------------------------------------------- 1 | charset ) ) { 16 | $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset"; 17 | } 18 | 19 | $sql[] = "CREATE TABLE IF NOT EXISTS {$wpdb->base_prefix}bp_mnetwork_users ( 20 | user_id bigint(20) NOT NULL, 21 | network_id bigint(20) NOT NULL, 22 | is_active tinyint(2) NOT NULL, 23 | UNIQUE KEY user_id (user_id,network_id) 24 | ) {$charset_collate};"; 25 | 26 | dbDelta( $sql ); 27 | update_site_option( 'bpmnetwork_db_version', 22 ); 28 | } 29 | 30 | /** 31 | * Check if already installed. If not, Create table(s). 32 | */ 33 | function mnetwork_check_installed() { 34 | if ( is_network_admin() && is_super_admin() ) { 35 | // if a super admin is logged in the network admin, 36 | // let us check for the installation. 37 | $version = get_site_option( 'bpmnetwork_db_version' ); 38 | // if not installed, let us do it now. 39 | if ( $version < 22 ) { 40 | mnetwork_install(); 41 | } 42 | } 43 | 44 | } 45 | 46 | add_action( 'admin_init', 'mnetwork_check_installed' ); 47 | -------------------------------------------------------------------------------- /bp-multinetwork.php: -------------------------------------------------------------------------------- 1 | mnetwork = BP_Multi_Network_Component::get_instance(); 68 | } 69 | 70 | /** 71 | * Get the user table name. 72 | * 73 | * @return string 74 | */ 75 | public function get_table_name() { 76 | global $wpdb; 77 | return $wpdb->base_prefix . 'bp_mnetwork_users'; 78 | } 79 | } 80 | 81 | BP_Multi_Network_Helper::get_instance(); 82 | 83 | // backward compatibility. 84 | class_alias( 'BP_Multi_Network_Helper', 'BPMultiNetworkHelper' ); 85 | 86 | /** 87 | * Get the user->network mapping table. 88 | * 89 | * @return string 90 | */ 91 | function mnetwork_get_table_name() { 92 | return BP_Multi_Network_Helper::get_instance()->get_table_name(); 93 | } 94 | -------------------------------------------------------------------------------- /core/bp-multi-network-user-functions.php: -------------------------------------------------------------------------------- 1 | mnetwork. 8 | */ 9 | class BP_Multi_Network_Component extends BP_Component { 10 | 11 | /** 12 | * Component. 13 | * 14 | * @var BP_Multi_Network_Component 15 | */ 16 | private static $instance; 17 | 18 | /** 19 | * Get the singleton instance 20 | * 21 | * @return BP_Multi_Network_Component 22 | */ 23 | public static function get_instance() { 24 | if ( ! isset( self::$instance ) ) { 25 | self::$instance = new self(); 26 | } 27 | 28 | return self::$instance; 29 | } 30 | 31 | /** 32 | * BPMultiNetworkComponent constructor. 33 | */ 34 | private function __construct() { 35 | 36 | parent::start( 37 | 'mnetwork', // unique id. 38 | __( 'Network', 'mnetwork' ), 39 | untrailingslashit( BP_MNETWORK_DIR )// base path. 40 | ); 41 | } 42 | 43 | /** 44 | * Setup globals. 45 | * 46 | * @param array $globals globals. 47 | */ 48 | public function setup_globals( $globals = array() ) { 49 | 50 | global $bp; 51 | 52 | // Define a slug, if necessary. 53 | if ( ! defined( 'BP_MNETWORK_SLUG' ) ) { 54 | define( 'BP_MNETWORK_SLUG', $this->id ); 55 | } 56 | 57 | 58 | $global_tables = array( 59 | 'table_network_users' => mnetwork_get_table_name(), 60 | // these tables can be accessed from $bp->mnetwork->table_name. 61 | ); 62 | 63 | // all other globals. 64 | // Note that global_tables is included in this array. 65 | $globals = array( 66 | 'slug' => 'network', 67 | 'root_slug' => isset( $bp->pages->mnetwork->slug ) ? $bp->pages->mnetwork->slug : BP_MNETWORK_SLUG, 68 | //'notification_callback' => 'mnetwork_format_notifications', 69 | 'search_string' => __( 'Search Networks...', 'mnetwork' ), 70 | 'global_tables' => $global_tables, 71 | 'has_directory' => false, 72 | ); 73 | 74 | // it will call do_action("bp_mnetwork_setup_global") after setting up the constants properly. 75 | parent::setup_globals( $globals ); 76 | } 77 | 78 | /** 79 | * Include files 80 | * 81 | * @param array $includes files. 82 | */ 83 | public function includes( $includes = array() ) { 84 | $includes = array(); 85 | 86 | } 87 | 88 | /** 89 | * Do we really need it ? No, if we don't want to list the networks on user profile 90 | * 91 | * @param array $main_nav main nav. 92 | * @param array $sub_nav sub nav. 93 | * 94 | * @return bool|void 95 | */ 96 | public function setup_nav( $main_nav = array(), $sub_nav = array() ) { 97 | global $bp; 98 | 99 | // sorry I am not putting it in the initial version, 100 | // if the community suggests I will be happy to add a My Network Tab. 101 | return false; 102 | 103 | // Add 'Networks' to the user's main navigation 104 | $main_nav = array( 105 | 'name' => sprintf( __( 'Networks %d', 'mnetwork' ), 2 ), 106 | // bp_get_total_networks_for_user() 107 | 'slug' => $this->slug, 108 | 'position' => 86, 109 | 'screen_function' => 'mnetwork_screen_my_networks', 110 | 'default_subnav_slug' => 'my-galleries', 111 | 'item_css_id' => $this->id, 112 | ); 113 | 114 | $network_link = trailingslashit( $bp->loggedin_user->domain . $this->slug );//with a trailing slash 115 | 116 | // Add the My Groups nav item. 117 | $sub_nav[] = array( 118 | 'name' => __( 'My Networks', 'mnetwork' ), 119 | 'slug' => 'my-networks', 120 | 'parent_url' => $network_link, 121 | 'parent_slug' => $this->slug, 122 | 'screen_function' => 'mnetwork_screen_my_networks', 123 | 'position' => 10, 124 | 'item_css_id' => 'mnetwork-my-networks' 125 | ); 126 | 127 | 128 | // if this is single gallery, add edit gallery link too! 129 | parent::setup_nav( $main_nav, $sub_nav ); 130 | 131 | 132 | do_action( 'mnetwork_setup_nav' ); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /core/class-bp-multi-network-users.php: -------------------------------------------------------------------------------- 1 | get_var( $wpdb->prepare( $query, $user_id, $network_id ) ); 23 | } 24 | 25 | 26 | /** 27 | * Retrieves the list of users who are member of this network 28 | * 29 | * @param int $network_id network id. 30 | * 31 | * @return array 32 | */ 33 | public static function get_users( $network_id ) { 34 | global $wpdb; 35 | $table_users = mnetwork_get_table_name(); 36 | $query = "SELECT user_id FROM {$table_users} WHERE network_id = %d"; 37 | $users = $wpdb->get_col( $wpdb->prepare( $query, $network_id ) ); 38 | 39 | return $users; 40 | } 41 | 42 | /** 43 | * Retrieves networks for the user. 44 | * 45 | * @param int $user_id user id. 46 | * 47 | * @return array 48 | */ 49 | public static function get_networks( $user_id ) { 50 | global $wpdb; 51 | 52 | $table_users = mnetwork_get_table_name(); 53 | 54 | $query = "SELECT DISTINCT(network_id) FROM {$table_users} WHERE user_id = %d"; 55 | $networks = $wpdb->get_col( $wpdb->prepare( $query, $user_id ) ); 56 | 57 | return $networks; 58 | } 59 | 60 | /** 61 | * Retrieves the total number of users in the given network. 62 | * 63 | * @param int $network_id network id. 64 | * 65 | * @return int 66 | */ 67 | public static function get_network_users_count( $network_id ) { 68 | global $wpdb; 69 | $table_users = mnetwork_get_table_name(); 70 | 71 | $query = "SELECT COUNT(DISTINCT(user_id)) FROM {$table_users} WHERE network_id = %d"; 72 | 73 | $networks = $wpdb->get_var( $wpdb->prepare( $query, $network_id ) ); 74 | 75 | return $networks; 76 | } 77 | 78 | /** 79 | * Adds user to a network 80 | * 81 | * @param int $user_id user id. 82 | * @param int $network_id network id. 83 | * 84 | * @return bool 85 | */ 86 | public static function add_user( $user_id, $network_id ) { 87 | if ( empty( $user_id ) || empty( $network_id ) ) { 88 | return false; 89 | } 90 | 91 | global $wpdb; 92 | 93 | $table_users = mnetwork_get_table_name(); 94 | 95 | $query = "INSERT INTO {$table_users} SET user_id=%d, network_id = %d"; 96 | 97 | $wpdb->query( $wpdb->prepare( $query, $user_id, $network_id ) ); 98 | 99 | return true; 100 | } 101 | 102 | /** 103 | * Removes a user from network. 104 | * 105 | * @param int $user_id user id. 106 | * @param int $network_id network id. 107 | * 108 | * @return bool 109 | */ 110 | public static function remove_user( $user_id, $network_id = 0 ) { 111 | if ( empty( $user_id ) && empty( $network_id ) ) { 112 | return false; 113 | } 114 | global $wpdb; 115 | 116 | $table_users = mnetwork_get_table_name(); 117 | 118 | $where_conditions = array(); 119 | $where_sql = ''; 120 | if ( ! empty( $user_id ) ) { 121 | $where_conditions[] = $wpdb->prepare( 'user_id = %d', $user_id ); 122 | } 123 | 124 | if ( ! empty( $network_id ) ) { 125 | $where_conditions[] = $wpdb->prepare( 'network_id = %d', $network_id ); 126 | } 127 | 128 | $where_sql = join( ' AND ', $where_conditions ); 129 | 130 | $query = "DELETE FROM {$table_users} WHERE {$where_sql}"; 131 | 132 | $wpdb->query( $query ); 133 | 134 | return true; 135 | } 136 | 137 | /** 138 | * Removes users from a network. 139 | * 140 | * @param int $network_id network id. 141 | * 142 | * @return bool 143 | */ 144 | public static function remove_network( $network_id ) { 145 | 146 | if ( empty( $network_id ) ) { 147 | return false; 148 | } 149 | 150 | global $wpdb; 151 | 152 | $table_users = mnetwork_get_table_name(); 153 | 154 | $query = "DELETE FROM {$table_users} WHERE network_id = %d"; 155 | 156 | $wpdb->query( $wpdb->prepare( $query, $network_id ) ); 157 | 158 | return true; 159 | } 160 | } 161 | 162 | // for backward compatibility, alias. 163 | class_alias( 'BP_Multi_Network_Users', 'BPNetworkUsers' ); 164 | -------------------------------------------------------------------------------- /core/class-bp-multi-network-filters.php: -------------------------------------------------------------------------------- 1 | prefix; 125 | } 126 | 127 | /** 128 | * Filter total users sql 129 | * An extra IN {user list} will not cause any har in case the $include/$friends is specified 130 | * 131 | * @param string $sql query. 132 | * @param array $sql_array sql array. 133 | * 134 | * @return string 135 | */ 136 | public function filter_total_users_sql( $sql, $sql_array ) { 137 | 138 | // if you want to filter on the main site too, please comment the next two line. 139 | if ( is_main_site() ) { 140 | return $sql; 141 | } 142 | 143 | 144 | $blog_id = get_current_blog_id(); 145 | $users = mnetwork_get_users( $blog_id ); 146 | 147 | $list = '(' . join( ',', $users ) . ')'; 148 | 149 | // since $type will be always passed to get users, we can safely assume this. 150 | $order_by = array_pop( $sql_array ); 151 | 152 | 153 | $sql_array['where_network'] = " AND u.ID IN {$list}"; 154 | array_push( $sql_array, $order_by ); 155 | $sql = join( ' ', $sql_array ); 156 | 157 | return $sql; 158 | } 159 | 160 | /** 161 | * Filter total user count. 162 | * 163 | * @param int $count count. 164 | * 165 | * @return int 166 | */ 167 | public function filter_total_user_count( $count ) { 168 | if ( is_main_site() ) { 169 | return $count; 170 | }//on main site, we don't need to worry about the count change 171 | 172 | $blog_id = get_current_blog_id(); 173 | 174 | $count = mnetwork_get_total_users( $blog_id ); 175 | 176 | // get the total users count for current buddypress network. 177 | return $count; 178 | 179 | } 180 | 181 | /** 182 | * User list filters. 183 | * 184 | * @param \BP_User_Query $query_obj query object. 185 | */ 186 | public function users_filter( $query_obj ) { 187 | 188 | if ( is_main_site() ) { 189 | return; 190 | } 191 | 192 | $uid_where = $query_obj->uid_clauses['where']; 193 | 194 | $blog_id = get_current_blog_id(); 195 | 196 | $users = mnetwork_get_users( $blog_id ); 197 | 198 | if ( empty( $users ) ) { 199 | // if no users found, let us fake it. 200 | $users = array( 0 => 0 ); 201 | } 202 | 203 | 204 | $list = '(' . join( ',', $users ) . ')'; 205 | 206 | if ( $uid_where ) { 207 | $uid_where .= " AND u.{$query_obj->uid_name} IN {$list}"; 208 | } else { 209 | $uid_where = "WHERE u.{$query_obj->uid_name} IN {$list}"; 210 | }//we are treading a hard line here 211 | 212 | $query_obj->uid_clauses['where'] = $uid_where; 213 | } 214 | 215 | /** 216 | * Pre BP 1.7 filter. 217 | * 218 | * @param string $sql clause. 219 | * @param array $sql_array clause array. 220 | * 221 | * @return string 222 | */ 223 | public function filter_paged_users_sql( $sql, $sql_array ) { 224 | // if u want to scope on main site, please comment the next 2 lines. 225 | if ( is_main_site() ) { 226 | return $sql; 227 | } 228 | // do not filter user list on amin site 229 | // if on sub network site, let us filter users. 230 | $blog_id = get_current_blog_id(); 231 | $users = mnetwork_get_users( $blog_id ); 232 | 233 | if ( empty( $users ) ) { 234 | // if no users found, let us fake it. 235 | $users = array( 0 => 0 ); 236 | } 237 | 238 | $list = '(' . join( ',', $users ) . ')'; 239 | 240 | if ( ! empty( $sql_array['pagination'] ) ) { 241 | $pagination = array_pop( $sql_array ); 242 | } 243 | 244 | // since $type will be always passed to get users, we can safely assume this. 245 | $order_by = array_pop( $sql_array ); 246 | 247 | 248 | $sql_array['where_network'] = " AND u.ID IN {$list}"; 249 | 250 | array_push( $sql_array, $order_by ); 251 | 252 | if ( ! empty( $pagination ) ) { 253 | array_push( $sql_array, $pagination ); 254 | } 255 | 256 | $sql = join( ' ', $sql_array ); 257 | 258 | return $sql; 259 | } 260 | 261 | /** 262 | * Pre BP 1.7 total number filter. 263 | * 264 | * @param int $count count. 265 | * 266 | * @return int 267 | */ 268 | public function total_member_count( $count ) { 269 | if ( is_main_site() ) { 270 | return $count; 271 | } 272 | global $wpdb; 273 | $blog_id = get_current_blog_id(); 274 | 275 | if ( ! $count = wp_cache_get( 'bp_total_member_count_' . $blog_id, 'bp' ) ) { 276 | $status_sql = bp_core_get_status_sql(); 277 | $list_users = mnetwork_get_users( $blog_id ); 278 | $list = '(' . join( ',', $list_users ) . ')'; 279 | 280 | $count = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->users WHERE {$status_sql} and ID IN {$list}" ); 281 | wp_cache_set( 'bp_total_member_count_' . $blog_id, $count, 'bp' ); 282 | } 283 | 284 | return $count; 285 | } 286 | 287 | } 288 | // backward compatibility. 289 | class_alias( 'BP_Multi_Network_Filters', 'BPMultiNetworkFilter' ); 290 | 291 | // initialize filters. 292 | BP_Multi_Network_Filters::get_instance(); 293 | --------------------------------------------------------------------------------