|
| 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