| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Plugin Name: BP Multi Network |
|---|
| 5 | * Plugin URI: https://wordpress.org/plugins/bp-multi-network/ |
|---|
| 6 | * Description: Unique BuddyPress networks in your WordPress multi-network installation |
|---|
| 7 | * Version: 0.2.0 |
|---|
| 8 | * Author: The BuddyPress Community |
|---|
| 9 | * Author URI: http://buddypress.org |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * The main BuddyPress multi-network class |
|---|
| 14 | * |
|---|
| 15 | * This class adds filters to three places in BuddyPress to intercept and modify |
|---|
| 16 | * database table prefixes based on the current network. |
|---|
| 17 | */ |
|---|
| 18 | class BP_Multi_Network { |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * Array of BuddyPress user-meta keys |
|---|
| 22 | * |
|---|
| 23 | * @var array |
|---|
| 24 | */ |
|---|
| 25 | private $user_meta_keys = array( |
|---|
| 26 | 'last_activity' => false, |
|---|
| 27 | 'bp_new_mention_count' => false, |
|---|
| 28 | 'bp_favorite_activities' => false, |
|---|
| 29 | 'bp_latest_update' => false, |
|---|
| 30 | 'total_friend_count' => false, |
|---|
| 31 | 'total_group_count' => false, |
|---|
| 32 | 'notification_activity_new_mention' => false, |
|---|
| 33 | 'notification_activity_new_reply' => false, |
|---|
| 34 | 'notification_groups_group_updated' => false, |
|---|
| 35 | 'notification_groups_membership_request' => false, |
|---|
| 36 | 'notification_membership_request_completed' => false, |
|---|
| 37 | 'notification_groups_admin_promotion' => false, |
|---|
| 38 | 'notification_groups_invite' => false, |
|---|
| 39 | 'notification_messages_new_message' => false, |
|---|
| 40 | 'notification_messages_new_notice' => false, |
|---|
| 41 | 'closed_notices' => false, |
|---|
| 42 | 'profile_last_updated' => false |
|---|
| 43 | ); |
|---|
| 44 | |
|---|
| 45 | /** Filters ***************************************************************/ |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Attach filters to what are usually considered "global" values, and modify |
|---|
| 49 | * them based on the currently loaded WordPress network of sites. |
|---|
| 50 | * |
|---|
| 51 | * - Global cache key |
|---|
| 52 | * - Table prefix |
|---|
| 53 | * - User meta key |
|---|
| 54 | */ |
|---|
| 55 | public function __construct() { |
|---|
| 56 | add_filter( 'bp_core_get_global_cache_key', array( $this, 'filter_global_cache_key' ) ); |
|---|
| 57 | add_filter( 'bp_core_get_table_prefix', array( $this, 'filter_table_prefix' ) ); |
|---|
| 58 | add_filter( 'bp_get_user_meta_key', array( $this, 'filter_user_meta_key' ) ); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Filter the "global" cache key and maybe add a network ID |
|---|
| 63 | * |
|---|
| 64 | * @param string $key |
|---|
| 65 | * @return string |
|---|
| 66 | */ |
|---|
| 67 | public function filter_global_cache_key( $key = 'bp' ) { |
|---|
| 68 | |
|---|
| 69 | // Override prefix if not main network and there is a prefix match |
|---|
| 70 | if ( ! $this->is_main_network() && ( true === $this->is_base_cache_key( $key ) ) ) { |
|---|
| 71 | $key = $this->get_network_cache_key() . $key; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | // Use this network's prefix |
|---|
| 75 | return $key; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Filter the DB table prefix and maybe add a network ID |
|---|
| 80 | * |
|---|
| 81 | * @param string $prefix |
|---|
| 82 | * @return string |
|---|
| 83 | */ |
|---|
| 84 | public function filter_table_prefix( $prefix = '' ) { |
|---|
| 85 | |
|---|
| 86 | // Override prefix if not main network and there is a prefix match |
|---|
| 87 | if ( ! $this->is_main_network() && ( true === $this->is_base_db_prefix( $prefix ) ) ) { |
|---|
| 88 | $prefix = $this->get_network_db_prefix(); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | // Use this network's prefix |
|---|
| 92 | return $prefix; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Filter the appropriate BuddyPress user-meta keys and prefix them with the |
|---|
| 97 | * ID of the network they belong to. |
|---|
| 98 | * |
|---|
| 99 | * @param string $key |
|---|
| 100 | * @return string |
|---|
| 101 | */ |
|---|
| 102 | public function filter_user_meta_key( $key = '' ) { |
|---|
| 103 | |
|---|
| 104 | // Bail if key is not BuddyPress user meta |
|---|
| 105 | if ( ! isset( $this->user_meta_keys[ $key ] ) ) { |
|---|
| 106 | return $key; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | // Bail if on the main network |
|---|
| 110 | if ( $this->is_main_network() ) { |
|---|
| 111 | return $key; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | // Set the user meta key to the new prefix |
|---|
| 115 | if ( false === $this->user_meta_keys[ $key ] ) { |
|---|
| 116 | $this->user_meta_keys[ $key ] = $this->get_network_db_prefix() . $key; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | // Return the modified user meta key |
|---|
| 120 | return $this->user_meta_keys[ $key ]; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** Helpers ***************************************************************/ |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Whether or not the current DB query is from the main network. |
|---|
| 127 | * |
|---|
| 128 | * The main network is typically ID 1 (and does not have a modified prefix) |
|---|
| 129 | * but we also need to check for the PRIMARY_NETWORK_ID constant introduced |
|---|
| 130 | * in WordPress 3.7 for more sophisticated installations. |
|---|
| 131 | * |
|---|
| 132 | * @global object $wpdb |
|---|
| 133 | * @return boolean |
|---|
| 134 | */ |
|---|
| 135 | private function is_main_network() { |
|---|
| 136 | global $wpdb; |
|---|
| 137 | |
|---|
| 138 | // Use primary network ID if defined |
|---|
| 139 | $primary_network_id = defined( 'PRIMARY_NETWORK_ID' ) |
|---|
| 140 | ? (int) PRIMARY_NETWORK_ID |
|---|
| 141 | : 1; |
|---|
| 142 | |
|---|
| 143 | return (bool) ( $wpdb->siteid === $primary_network_id ); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** Cache Keys ************************************************************/ |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | * Compare a given cache-key with BuddyPress's "global" cache-key. |
|---|
| 150 | * |
|---|
| 151 | * @param string $key |
|---|
| 152 | * @return string |
|---|
| 153 | */ |
|---|
| 154 | private function is_base_cache_key( $key = '' ) { |
|---|
| 155 | return (bool) ( 'bp' === $key ); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /** |
|---|
| 159 | * Return the cache-key for the current network |
|---|
| 160 | * |
|---|
| 161 | * Note that we're using the prefix for the root-blog, and not the network |
|---|
| 162 | * ID itself. This is because BuddyPress stores much of its data in the |
|---|
| 163 | * root-blog options table VS the sitemeta table. |
|---|
| 164 | * |
|---|
| 165 | * @global object $wpdb |
|---|
| 166 | * @return string |
|---|
| 167 | */ |
|---|
| 168 | private function get_network_cache_key() { |
|---|
| 169 | global $wpdb; |
|---|
| 170 | return $wpdb->get_blog_prefix( get_current_site()->blog_id ); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /** DB ********************************************************************/ |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | * Compare a given DB table prefix with the base DB prefix. |
|---|
| 177 | * |
|---|
| 178 | * @global object $wpdb |
|---|
| 179 | * @param string $prefix |
|---|
| 180 | * @return string |
|---|
| 181 | */ |
|---|
| 182 | private function is_base_db_prefix( $prefix = '' ) { |
|---|
| 183 | global $wpdb; |
|---|
| 184 | return (bool) ( $prefix === $wpdb->base_prefix ); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | /** |
|---|
| 188 | * Return the DB table prefix for the current network |
|---|
| 189 | * |
|---|
| 190 | * Note that we're using the prefix for the root-blog, and not the network |
|---|
| 191 | * ID itself. This is because BuddyPress stores much of its data in the |
|---|
| 192 | * root-blog options table VS the sitemeta table. |
|---|
| 193 | * |
|---|
| 194 | * @global object $wpdb |
|---|
| 195 | * @return string |
|---|
| 196 | */ |
|---|
| 197 | private function get_network_db_prefix() { |
|---|
| 198 | global $wpdb; |
|---|
| 199 | return $wpdb->get_blog_prefix( get_current_site()->blog_id ); |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | new BP_Multi_Network(); |
|---|