wp-domain-mapping/sunrise.php

104 lines
3 KiB
PHP
Raw Normal View History

2025-03-16 01:03:10 +08:00
<?php
/**
2025-05-26 22:47:52 +08:00
* Sunrise.php for WP Domain Mapping
2025-03-16 01:03:10 +08:00
*
2025-05-20 22:52:24 +08:00
* This file must be copied to wp-content/sunrise.php
* Also, you must add "define('SUNRISE', 'on');" to wp-config.php
* Make sure the SUNRISE definition appears before the last require_once in wp-config.php
2025-03-16 01:03:10 +08:00
*/
2025-05-20 22:52:24 +08:00
// Mark as loaded
define('SUNRISE_LOADED', true);
// Check if we're in WP multi-site mode
if (!defined('MULTISITE') || !MULTISITE) {
return;
2025-03-16 01:03:10 +08:00
}
2025-05-20 22:52:24 +08:00
// Enable domain mapping
define('DOMAIN_MAPPING', 1);
2025-05-26 22:47:52 +08:00
// Don't process if we're in admin and on the original domain
if (is_admin() && isset($_SERVER['HTTP_HOST'])) {
// Allow admin access from any domain - we'll handle this in the plugin itself
2025-05-20 22:52:24 +08:00
}
global $wpdb, $current_blog, $current_site;
2025-05-26 22:47:52 +08:00
// Check if tables exist before proceeding
if (!$wpdb) {
return;
}
2025-05-20 22:52:24 +08:00
// Get domains table name
$domain_mapping_table = $wpdb->base_prefix . 'domain_mapping';
2025-05-26 22:47:52 +08:00
// Check if the domain mapping table exists
$table_exists = $wpdb->get_var("SHOW TABLES LIKE '{$domain_mapping_table}'") == $domain_mapping_table;
if (!$table_exists) {
return;
}
2025-05-20 22:52:24 +08:00
// Check for the current domain in the domain mapping table
2025-05-25 00:32:34 +08:00
$domain = sanitize_text_field($_SERVER['HTTP_HOST']);
$blog_id = $wpdb->get_var($wpdb->prepare(
"SELECT blog_id FROM {$domain_mapping_table} WHERE domain = %s LIMIT 1",
$domain
));
2025-05-20 22:52:24 +08:00
// If we found a mapped domain, override current_blog
if (!empty($blog_id)) {
// Get the mapped blog details
2025-05-26 22:47:52 +08:00
$mapped_blog = $wpdb->get_row($wpdb->prepare(
"SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1",
$blog_id
));
2025-05-20 22:52:24 +08:00
if ($mapped_blog) {
// Override current_blog
$current_blog = $mapped_blog;
2025-05-26 22:47:52 +08:00
// Set cookie domain for the mapped domain (only if not defined in wp-config.php)
if (!defined('COOKIE_DOMAIN')) {
define('COOKIE_DOMAIN', $domain);
}
2025-05-20 22:52:24 +08:00
// Define the mapped domain constant
define('MAPPED_DOMAIN', true);
// Allow other plugins to know this is a mapped domain
$GLOBALS['dm_domain'] = array(
'original' => $current_blog->domain,
2025-05-26 22:47:52 +08:00
'mapped' => $domain,
'blog_id' => $blog_id
2025-05-20 22:52:24 +08:00
);
// Fix request URI for path sites
if ($current_blog->path != '/' && ($current_blog->path != '/wp/' || strpos($_SERVER['REQUEST_URI'], '/wp/') === false)) {
$current_blog->path = '/';
}
2025-05-26 22:47:52 +08:00
// Store original values for reference
if (!defined('DM_ORIGINAL_DOMAIN')) {
define('DM_ORIGINAL_DOMAIN', $mapped_blog->domain);
}
if (!defined('DM_ORIGINAL_PATH')) {
define('DM_ORIGINAL_PATH', $mapped_blog->path);
}
2025-05-20 22:52:24 +08:00
}
2025-03-16 01:03:10 +08:00
}
2025-05-26 22:47:52 +08:00
// Function to check if current request is for admin area
function dm_is_admin_request() {
if (!isset($_SERVER['REQUEST_URI'])) {
return false;
}
$request_uri = $_SERVER['REQUEST_URI'];
return (
strpos($request_uri, '/wp-admin/') !== false ||
strpos($request_uri, '/wp-login.php') !== false ||
(defined('WP_ADMIN') && WP_ADMIN)
);
}