| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * This sunrise.php file is meant to separate the user tables to each |
|---|
| 4 | * blog inside a Multisite Network install. |
|---|
| 5 | * |
|---|
| 6 | * |
|---|
| 7 | * |
|---|
| 8 | * NOTE: Don't use this file in a production environment. |
|---|
| 9 | * Not stable. Use only for test purposes. |
|---|
| 10 | * |
|---|
| 11 | * |
|---|
| 12 | * |
|---|
| 13 | * Instalation Procedure: |
|---|
| 14 | * ---------------------- |
|---|
| 15 | * 1. Put inside wp-contents/sunrise.php |
|---|
| 16 | * 2. Write <code>define('SUNRISE', true);</code> in wp-contents.php |
|---|
| 17 | * 3. Put blog-user-tables.php inside wp-contents/mu-plugins/ |
|---|
| 18 | * 4. Test and have fun! :) |
|---|
| 19 | * |
|---|
| 20 | * |
|---|
| 21 | * @package WordPress |
|---|
| 22 | * @since 3.4 |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Define an important constant |
|---|
| 28 | */ |
|---|
| 29 | if ( !defined( 'SUNRISE_LOADED' ) ) |
|---|
| 30 | define( 'SUNRISE_LOADED', 1 ); |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Let's check for the presence of |
|---|
| 35 | * wp-content/mu-plugins/blog-user-tables.php plugin which is necessary |
|---|
| 36 | * to create the required custom user tables for each blog. |
|---|
| 37 | */ |
|---|
| 38 | if ( defined( 'WPMU_PLUGIN_DIR' ) ) : |
|---|
| 39 | $mu_plugins_dir = WPMU_PLUGIN_DIR; |
|---|
| 40 | |
|---|
| 41 | else : |
|---|
| 42 | $mu_plugins_dir = WP_CONTENT_DIR . '/mu-plugins'; |
|---|
| 43 | |
|---|
| 44 | endif; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | if ( !file_exists( $mu_plugins_dir . '/blog-user-tables.php' ) ) |
|---|
| 48 | die( 'The file blog-user-tables.php must be present on Mu-plugins dir or you should remove the code from wp-contents/sunrise.php' ); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * Load $wpdb object if not already loaded |
|---|
| 54 | */ |
|---|
| 55 | if ( function_exists( 'require_wp_db' ) ) { |
|---|
| 56 | require_wp_db(); |
|---|
| 57 | |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Now its time to fill $blog_id and $site_id variables with the correct |
|---|
| 63 | * ID because, as multisite is not yet fully loaded, we can't know what |
|---|
| 64 | * blog was requested., so we have to fetch it from the DB. |
|---|
| 65 | * |
|---|
| 66 | * Why do we need this? |
|---|
| 67 | * The goal is to dynamically set CUSTOM_USER_TABLE and |
|---|
| 68 | * CUSTOM_USER_META_TABLE constants with values like: |
|---|
| 69 | * wp_2_users and wp_2_usermeta for blog #2 |
|---|
| 70 | * wp_3_users and wp_3_usermeta for blog #3 |
|---|
| 71 | * ... and so on! |
|---|
| 72 | * |
|---|
| 73 | * We need to know the current blog ID to achieve this. |
|---|
| 74 | * Meanwhile, we will set global variables related to multisite |
|---|
| 75 | * $blog_id, $site_id and $current_blog too. |
|---|
| 76 | * |
|---|
| 77 | */ |
|---|
| 78 | |
|---|
| 79 | global $blog_id, $site_id, $current_blog, $table_prefix; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Lets take the Domain value from the server. We test HTTP_HOST and |
|---|
| 85 | * SERVER_NAME elements, depending on the server configuration either |
|---|
| 86 | * the first or the second (or even both) are set. |
|---|
| 87 | */ |
|---|
| 88 | if ( isset( $_SERVER['HTTP_HOST'] ) ) : |
|---|
| 89 | $current_domain = $_SERVER['HTTP_HOST']; |
|---|
| 90 | |
|---|
| 91 | elseif ( isset( $_SERVER['SERVER_NAME'] ) ) : |
|---|
| 92 | $current_domain = $_SERVER['SERVER_NAME']; |
|---|
| 93 | |
|---|
| 94 | endif; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Lets check if requested domain has www. if so there is a possibility |
|---|
| 100 | * that the registered domain in thd DB starts with www. |
|---|
| 101 | * Let's check both... |
|---|
| 102 | */ |
|---|
| 103 | if ( ( $nowww = preg_replace( '|^www\.|', '', $current_domain ) ) != $current_domain ) : |
|---|
| 104 | $where = $wpdb->prepare( "domain IN ( %s, %s )", $wpdb->escape( $current_domain ), $wpdb->escape( $nowww ) ); |
|---|
| 105 | |
|---|
| 106 | else : |
|---|
| 107 | $where = $wpdb->prepare( "domain = %s", $wpdb->escape( $current_domain ) ); |
|---|
| 108 | |
|---|
| 109 | endif; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * If the domain is set and the $table_prefix variable set in |
|---|
| 115 | * wp-config.php then |
|---|
| 116 | * Everything is prepared to get the blog ID. Let's Rock! :) |
|---|
| 117 | */ |
|---|
| 118 | if ( $current_domain && $table_prefix ) { |
|---|
| 119 | |
|---|
| 120 | /** Get the ID from the blogs table */ |
|---|
| 121 | $current_blog = $wpdb->get_row( "SELECT * FROM {$table_prefix}blogs WHERE {$where} LIMIT 1" ); |
|---|
| 122 | |
|---|
| 123 | /** If the domain was found in the table, let's set the variables and proceed */ |
|---|
| 124 | if ( !empty( $current_blog ) ) { |
|---|
| 125 | |
|---|
| 126 | /** Fill the global variables! :) */ |
|---|
| 127 | $blog_id = $current_blog->blog_id; |
|---|
| 128 | $site_id = $current_blog->site_id; |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * Let's change the cookie domain for this domain |
|---|
| 133 | * TODO: Test if this can lead into issues |
|---|
| 134 | */ |
|---|
| 135 | if ( !defined( 'COOKIE_DOMAIN' ) ) |
|---|
| 136 | define( 'COOKIE_DOMAIN', $current_domain ); |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| 139 | * Last but not least: |
|---|
| 140 | * Set the custom user tables but only if it is not the primary blog |
|---|
| 141 | * We will use the main wp_users and wp_usermeta tables just for |
|---|
| 142 | * the primary blog. |
|---|
| 143 | */ |
|---|
| 144 | if ( $blog_id != 1 ) { |
|---|
| 145 | if ( !defined( 'CUSTOM_USER_TABLE' ) ) |
|---|
| 146 | define( 'CUSTOM_USER_TABLE', $table_prefix.$blog_id.'_users' ); |
|---|
| 147 | |
|---|
| 148 | if ( !defined( 'CUSTOM_USER_META_TABLE' ) ) |
|---|
| 149 | define( 'CUSTOM_USER_META_TABLE', $table_prefix.$blog_id.'_usermeta' ); |
|---|
| 150 | |
|---|
| 151 | /** |
|---|
| 152 | * We have to hack and change the global variables in $wpdb |
|---|
| 153 | * object because at this time they are already set. |
|---|
| 154 | */ |
|---|
| 155 | $wpdb->users = CUSTOM_USER_TABLE; |
|---|
| 156 | $wpdb->usermeta = CUSTOM_USER_META_TABLE; |
|---|
| 157 | |
|---|
| 158 | $wpdb->global_tables[0] = $blog_id.'_users'; |
|---|
| 159 | $wpdb->global_tables[1] = $blog_id.'_usermeta'; |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * For compatibility with plugins, I think event |
|---|
| 163 | * if they are not necessary, we set this just |
|---|
| 164 | * to organize this! |
|---|
| 165 | */ |
|---|
| 166 | $wpdb->tables[] = 'users'; |
|---|
| 167 | $wpdb->tables[] = 'usermeta'; |
|---|
| 168 | |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | } |
|---|