-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvolution.php
More file actions
99 lines (76 loc) · 3.14 KB
/
Evolution.php
File metadata and controls
99 lines (76 loc) · 3.14 KB
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
<?php
class Evolution
{
const DOMAIN = 'example.com';
public static $hostname;
public static $env;
public static function getDbName($name)
{
return sprintf('%s_%s', $name, static::getEnv());
}
public static function getHostname()
{
if (isset(self::$hostname)) {
return self::$hostname;
}
$env = self::getEnv();
if (in_array($env, array('local', 'staging'))) {
self::$hostname = $env.'.'.self::DOMAIN;
}
else {
$content = file_get_contents(dirname(__FILE__).'/lib/ansible/group_vars/all');
if (preg_match('/www[ ]*:[ ]*(true)/', $content)) {
self::$hostname = 'www.'.self::DOMAIN;
}
else {
self::$hostname = self::DOMAIN;
}
}
return self::$hostname;
}
public static function getEnv()
{
if (isset(self::$env)) {
return self::$env;
}
preg_match('/(?:(local|staging|production)\.)?'.preg_quote(self::DOMAIN).'$/', $_SERVER['SERVER_NAME'], $matches);
$match = count($matches) > 1 ? array_pop($matches) : 'production';
$known = array('local', 'staging', 'production');
self::$env = in_array($match, $known) ? $match : 'production';
return self::$env;
}
public static function initEnv()
{
// Set environment to the last sub-domain (e.g. foo.staging.site.com => 'staging')
if (!defined('WP_ENV')) {
define('WP_ENV', Evolution::getEnv());
}
return WP_ENV;
}
public static function rewriteUrls()
{
if (!function_exists('is_blog_installed') || !is_blog_installed()) {
return false;
}
$old_url = '://' . self::getHostname();
$new_url = '://' . htmlspecialchars($_SERVER['HTTP_HOST']);
if ($old_url === $new_url) {
return false;
}
// Remove domain from uploads
update_option('upload_path', null);
// Ensure internal WordPress functions map correctly to new url (but don't want to persist in the DB)
add_filter('option_home', function($value) use ($old_url, $new_url) { return str_replace($old_url, $new_url, $value); });
add_filter('option_siteurl', function($value) use ($old_url, $new_url) { return str_replace($old_url, $new_url, $value); });
add_filter('option_upload_path', function($value) use ($old_url, $new_url) { return str_replace($old_url, $new_url, $value); });
add_filter('option_upload_url_path', function($value) use ($old_url, $new_url) { return str_replace($old_url, $new_url, $value); });
add_filter('wp_get_attachment_url', function($value) use ($old_url, $new_url) { return str_replace($old_url, $new_url, $value); });
// Override URLs in output with local environment URL
ob_start( function( $output ) use ( $old_url, $new_url ) {
return str_replace( $old_url, $new_url, $output );
} );
register_shutdown_function( function() use ( $old_url, $new_url ) {
@ob_end_flush();
} );
}
}