-
Notifications
You must be signed in to change notification settings - Fork 0
/
sunrise.php
76 lines (58 loc) · 2.14 KB
/
sunrise.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
// namespace BlogAlias;
if ( ! defined( 'ABSPATH' ) ) {
die( 'FU!' );
}
if ( ! defined( 'WPMS_BLOG_ALIAS_REDIRECT_BY' ) ) {
define( 'WPMS_BLOG_ALIAS_REDIRECT_BY', 'WPMS-Blog-Alias@' . get_network( get_site()->site_id )->domain );
}
/**
* Redirect if $domain is an alias
*
* @param String $domain
* @param String $path
*/
function blog_alias_network_not_found( $domain, $path ) {
blog_alias_site_not_found( null, $domain, $path );
}
/**
* Redirect if $domain is an alias
*
* @param Object $current_site
* @param String $domain
* @param String $path
*/
function blog_alias_site_not_found( $current_site, $domain, $path ) {
if ( '' === get_site_option( 'multisite_blog_alias_sunrise_active' ) ) {
return;
}
require_once ABSPATH . WPINC . '/kses.php'; // dep of pluggable.php
require_once ABSPATH . WPINC . '/pluggable.php'; // wp_sanitize_redirect()
require_once ABSPATH . WPINC . '/formatting.php'; // untrailingslashit()
require_once __DIR__ . DIRECTORY_SEPARATOR . 'include/autoload.php';
$model = BlogAlias\Model\AliasDomains::instance();
if ( $result = $model->fetch_one_by( 'domain_alias', $domain ) ) {
global $wpdb;
// get site url
// #11 $wpdb->options is not set during sunrise. Need to trigger it manually here.
$wpdb->set_blog_id( $result->blog_id );
$site_url = get_option( 'siteurl' );
$add_path = defined( 'WPMU_BLOG_ALIAS_REDIRECT_WITH_PATH' )
? WPMU_BLOG_ALIAS_REDIRECT_WITH_PATH
: get_site_option( 'blog_alias_redirect_with_path' );
$add_admin_path = get_site_option( 'blog_alias_redirect_wp_admin' );
if ( $add_admin_path && preg_match( '/^\/wp-(admin\/|login\.php$)/', $_SERVER['REQUEST_URI'] ) ) {
// REQUEST_URI, SCRIPT_NAME, SCRIPT_URI, PHP_SELF
$path = $_SERVER['REQUEST_URI'];
} else if ( ! $add_path ) {
$path = '/';
}
$redirect = untrailingslashit( $site_url ) . $path;
http_response_code( 301 );
header( "X-Redirect-By: ".WPMS_BLOG_ALIAS_REDIRECT_BY );
header( 'Location: ' . wp_sanitize_redirect( $redirect ) );
exit();
}
}
add_action( 'ms_site_not_found', 'blog_alias_site_not_found', 10, 3 );
add_action( 'ms_network_not_found', 'blog_alias_network_not_found', 10, 2 );