Skip to content

Commit 215d590

Browse files
committed
sunrise.php for early-load ms-settings
1 parent d58688f commit 215d590

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ DB_HOST='mariadb'
3030
WP_DEBUG_LOG='/var/www/log/wp_debug.log'
3131

3232
# Set MULTISITE=true after completing the multisite setup in WordPress
33+
SUNRISE=true
3334
MULTISITE=false
3435
WP_ALLOW_MULTISITE=true
3536

config/application.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
Config::define('BLOG_ID_CURRENT_SITE', env('BLOG_ID_CURRENT_SITE') ?: 1);
166166
Config::define('ADMIN_COOKIE_PATH', '/');
167167
Config::define('COOKIEPATH', '');
168+
Config::define('SUNRISE', env('SUNRISE') ?: false);
168169

169170
Config::apply();
170171

web/app/sunrise.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
use Roots\WPConfig\Config;
4+
5+
// Access environment and configuration values
6+
$environment = defined('WP_ENV') ? WP_ENV : '';
7+
$wp_home = Config::get('WP_HOME') ?: 'http://localhost';
8+
$nginx_port = Config::get('NGINX_PORT') ? ':' . Config::get('NGINX_PORT') : '';
9+
10+
// Only proceed for development or staging environments
11+
if ($environment === 'development' || $environment === 'staging') {
12+
global $current_site, $current_blog, $wpdb;
13+
14+
// Parse the domain and subdomain from the request
15+
$domain = strtolower($_SERVER['HTTP_HOST'] ?? '');
16+
$base_domain = parse_url($wp_home, PHP_URL_HOST) . $nginx_port;
17+
$subdomain = null;
18+
19+
// Extract the subdomain if it differs from the base domain
20+
if (strpos(explode('.', $domain)[0], $base_domain) === false) {
21+
$subdomain = explode('.', $domain)[0];
22+
}
23+
24+
// If subdomain exists, fetch corresponding blog_id and site_id from the database
25+
$site_id = $blog_id = 1; // Default site ID for the main network
26+
if ($subdomain) {
27+
// Fetch blog ID for the subdomain
28+
$querystring = "SELECT blog_id, site_id FROM {$wpdb->blogs} WHERE domain LIKE %s AND path = '/'";
29+
$blog = $wpdb->get_row($wpdb->prepare($querystring, $subdomain . '%'));
30+
31+
// Update blog_id and site_id if found
32+
if ($blog) {
33+
$blog_id = (int) $blog->blog_id;
34+
$site_id = (int) $blog->site_id;
35+
}
36+
}
37+
38+
// Set $current_site and $current_blog based on environment variables and dynamic values
39+
$current_site = (object) [
40+
'id' => $site_id,
41+
'domain' => $base_domain,
42+
'path' => Config::get('PATH_CURRENT_SITE') ?: '/',
43+
'blog_id' => $blog_id,
44+
'public' => 1,
45+
'archived' => 0,
46+
'mature' => 0,
47+
'spam' => 0,
48+
'deleted' => 0,
49+
'site_id' => $site_id,
50+
];
51+
52+
$current_blog = (object) [
53+
'blog_id' => $blog_id,
54+
'site_id' => $site_id,
55+
'domain' => $subdomain ? $subdomain . '.' . $base_domain : $base_domain,
56+
'path' => Config::get('PATH_CURRENT_SITE') ?: '/',
57+
'public' => 1,
58+
'archived' => 0,
59+
'mature' => 0,
60+
'spam' => 0,
61+
'deleted' => 0,
62+
'lang_id' => 0,
63+
];
64+
65+
// Debugging log to confirm settings in development
66+
if ($environment === 'development') {
67+
error_log("SUNRISE: Detected subdomain '{$subdomain}', setting current_blog->domain to {$current_blog->domain}, blog_id to {$blog_id}, site_id to {$site_id}");
68+
}
69+
}

0 commit comments

Comments
 (0)