forked from humanmade/Mercator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mercator.php
339 lines (289 loc) · 9.46 KB
/
mercator.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Mercator
*
* WordPress multisite domain mapping for the modern era.
*
* @package Mercator
*/
namespace Mercator;
use WP_CLI;
/**
* Current version of Mercator.
*/
const VERSION = '1.0.3';
require __DIR__ . '/class-mapping.php';
require __DIR__ . '/class-network-mapping.php';
require __DIR__ . '/multinetwork.php';
require __DIR__ . '/sso.php';
require __DIR__ . '/sso-multinetwork.php';
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once __DIR__ . '/inc/cli/class-mapping-command.php';
require_once __DIR__ . '/inc/cli/class-network-mapping-command.php';
}
// Allow skipping bootstrap checks if you *really* know what you're doing.
// This lets Mercator run after muplugins_loaded, which you might need if you're
// doing unit tests.
if ( defined( 'MERCATOR_SKIP_CHECKS' ) && MERCATOR_SKIP_CHECKS ) {
startup();
} else {
run_preflight();
}
/**
* Perform preflight checks for Mercator
*
* Checks that we can actually run Mercator, then attaches the relevant actions
* and filters to make it useful.
*/
function run_preflight() {
// Are we installing? Bail if so.
if ( defined( 'WP_INSTALLING' ) ) {
return;
}
// Are we still in sunrise stage?
if ( did_action( 'muplugins_loaded' ) ) {
warn_with_message( 'Mercator must be loaded in your <code>sunrise.php</code>. Check out the <a href="https://github.com/humanmade/Mercator/wiki/Installation">installation instructions</a>.' );
return;
}
// Are we actually on multisite?
if ( ! is_multisite() ) {
warn_with_message( 'Mercator requires WordPress to be in <a href="http://codex.wordpress.org/Create_A_Network">multisite mode</a>.' );
return;
}
// Are we running a good version of WP?
if ( ! function_exists( 'get_site_by_path' ) ) {
warn_with_message( 'Mercator requires <a href="https://wordpress.org/download/">WordPress 3.9</a> or newer. Update now.' );
return;
}
// M: We have clearance, Clarence.
// O: Roger, Roger. What's our Vector Victor?
startup();
}
/**
* Attach Mercator into WordPress
*
* Imagine this as attaching the strings to the puppet.
*/
function startup() {
// Define the table variables
if ( empty( $GLOBALS['wpdb']->dmtable ) ) {
$GLOBALS['wpdb']->dmtable = $GLOBALS['wpdb']->base_prefix . 'domain_mapping';
$GLOBALS['wpdb']->ms_global_tables[] = 'domain_mapping';
}
// Ensure cache is shared
wp_cache_add_global_groups( array( 'domain_mapping', 'network_mapping' ) );
// Actually hook in!
add_filter( 'pre_get_site_by_path', __NAMESPACE__ . '\\check_domain_mapping', 10, 2 );
add_action( 'admin_init', __NAMESPACE__ . '\\load_admin', -100 );
add_action( 'wp_uninitialize_site', __NAMESPACE__ . '\\clear_mappings_on_delete' );
add_action( 'muplugins_loaded', __NAMESPACE__ . '\\register_mapped_filters', -10 );
// Add CLI commands
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'mercator mapping', __NAMESPACE__ . '\\CLI\\Mapping_Command' );
WP_CLI::add_command( 'mercator network-mapping', __NAMESPACE__ . '\\CLI\\Network_Mapping_Command' );
}
/**
* Check the table exists and that we're up to date.
*/
add_action( 'admin_init', __NAMESPACE__ . '\\check_table', -101 );
/**
* Fired after Mercator core has been loaded
*
* Hook into this to handle any add-on functionality.
*/
do_action( 'mercator_load' );
}
/**
* Warn the user via the admin panels.
*
* @param string $message Message to use in the warning.
*/
function warn_with_message( $message ) {
add_action( 'all_admin_notices', function () use ( $message ) {
echo '<div class="error"><p>' . esc_html( $message ) . '</p></div>';
} );
}
/**
* Check if a domain has a mapping available
*
* @param stdClass|null $site Site object if already found, null otherwise
* @param string $domain Domain we're looking for
* @return stdClass|null Site object if already found, null otherwise
*/
function check_domain_mapping( $site, $domain ) {
// Have we already matched? (Allows other plugins to match first)
if ( ! empty( $site ) ) {
return $site;
}
// Grab both WWW and no-WWW
if ( strpos( $domain, 'www.' ) === 0 ) {
$www = $domain;
$nowww = substr( $domain, 4 );
} else {
$nowww = $domain;
$www = 'www.' . $domain;
}
$mapping = Mapping::get_by_domain( array( $www, $nowww ) );
if ( empty( $mapping ) || is_wp_error( $mapping ) ) {
return $site;
}
/**
* Determine whether a mapping should be used
*
* Typically, you'll want to only allow active mappings to be used. However,
* if you want to use more advanced logic, or allow non-active domains to
* be mapped too, simply filter here.
*
* @param boolean $is_active Should the mapping be treated as active?
* @param Mapping $mapping Mapping that we're inspecting
* @param string $domain
*/
$is_active = apply_filters( 'mercator.use_mapping', $mapping->is_active(), $mapping, $domain );
// Ignore non-active mappings
if ( ! $is_active ) {
return $site;
}
// Fetch the actual data for the site
$mapped_site = $mapping->get_site();
if ( empty( $mapped_site ) ) {
return $site;
}
// Note: This is only for backwards compatibility with WPMU Domain Mapping,
// do not rely on this constant in new code.
defined( 'DOMAIN_MAPPING' ) or define( 'DOMAIN_MAPPING', 1 );
return $mapped_site;
}
/**
* Check the Mercator mapping table
*
* @return string|boolean One of 'exists' (table already existed), 'created' (table was created), or false if could not be created
*/
function check_table() {
global $wpdb;
if ( get_site_option( 'mercator.db.version' ) === VERSION ) {
return 'exists';
}
$schema = "CREATE TABLE {$wpdb->dmtable} (
id bigint(20) NOT NULL auto_increment,
blog_id bigint(20) NOT NULL,
domain varchar(191) NOT NULL,
active tinyint(4) default 1,
PRIMARY KEY (id),
KEY blog_id (blog_id,domain,active),
KEY domain (domain)
);";
if ( ! function_exists( 'dbDelta' ) ) {
if ( ! is_admin() ) {
return false;
}
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
}
// Ensure we always try to create this, regardless of whether we're on the
// main site or not. dbDelta will skip creation of global tables on
// non-main sites.
$offset = array_search( 'domain_mapping', $wpdb->ms_global_tables );
if ( ! empty( $offset ) ) {
unset( $wpdb->ms_global_tables[ $offset ] );
}
$result = dbDelta( $schema );
$wpdb->ms_global_tables[] = 'domain_mapping';
// Update db version option.
update_site_option( 'mercator.db.version', VERSION );
if ( empty( $result ) ) {
// No changes, database already exists and is up-to-date
return 'exists';
}
// utf8mb4 conversion.
maybe_convert_table_to_utf8mb4( $wpdb->dmtable );
return 'created';
}
/**
* Load administration functions
*
* We do this here rather than just including it to avoid extra load on
* non-admin pages.
*/
function load_admin() {
require_once __DIR__ . '/admin.php';
require_once __DIR__ . '/inc/admin/class-alias-list-table.php';
}
/**
* Clear mappings for a site when it's deleted
*
* @param WP_Site $site Deleted site object.
*/
function clear_mappings_on_delete( $site ) {
$mappings = Mapping::get_by_site( $site->id );
if ( empty( $mappings ) ) {
return;
}
foreach ( $mappings as $mapping ) {
$error = $mapping->delete();
if ( is_wp_error( $error ) ) {
$message = sprintf(
__( 'Unable to delete mapping %1$d for site %2$d', 'mercator' ),
$mapping->get_id(),
$site_id
);
trigger_error( $message, E_USER_WARNING );
}
}
}
/**
* Register filters for URLs, if we've mapped
*/
function register_mapped_filters() {
$current_site = $GLOBALS['current_blog'];
$real_domain = $current_site->domain;
$domain = $_SERVER['HTTP_HOST'];
if ( $domain === $real_domain ) {
// Domain hasn't been mapped
return;
}
// Grab both WWW and no-WWW
if ( strpos( $domain, 'www.' ) === 0 ) {
$www = $domain;
$nowww = substr( $domain, 4 );
} else {
$nowww = $domain;
$www = 'www.' . $domain;
}
$mapping = Mapping::get_by_domain( array( $www, $nowww ) );
if ( empty( $mapping ) || is_wp_error( $mapping ) ) {
return;
}
$GLOBALS['mercator_current_mapping'] = $mapping;
add_filter( 'site_url', __NAMESPACE__ . '\\mangle_url', -10, 4 );
add_filter( 'home_url', __NAMESPACE__ . '\\mangle_url', -10, 4 );
add_filter( 'plugins_url', __NAMESPACE__ . '\\mangle_url', -10, 2 );
add_filter( 'content_url', __NAMESPACE__ . '\\mangle_url', -10, 2 );
// If on network site, also filter network urls
if ( is_main_site() ) {
add_filter( 'network_site_url', __NAMESPACE__ . '\\mangle_url', -10, 3 );
add_filter( 'network_home_url', __NAMESPACE__ . '\\mangle_url', -10, 3 );
}
}
/**
* Mangle the home URL to give our primary domain
*
* @param string $url The complete home URL including scheme and path.
* @param string $path Path relative to the home URL. Blank string if no path is specified.
* @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative' or null.
* @param int|null $site_id Blog ID, or null for the current blog.
* @return string Mangled URL
*/
function mangle_url( $url, $path, $orig_scheme = '', $site_id = 0 ) {
if ( empty( $site_id ) ) {
$site_id = get_current_blog_id();
}
$current_mapping = $GLOBALS['mercator_current_mapping'];
if ( empty( $current_mapping ) || $site_id !== $current_mapping->get_site_id() ) {
return $url;
}
// Replace the domain
$domain = parse_url( $url, PHP_URL_HOST );
$domain = rtrim( $domain . $current_mapping->get_site()->path, '/' );
$regex = '#^(\w+://)' . preg_quote( $domain, '#' ) . '#i';
$mangled = preg_replace( $regex, '${1}' . $current_mapping->get_domain(), $url );
return $mangled;
}