diff --git a/lib/yeoman/templates/Evolution.php b/lib/yeoman/templates/Evolution.php index 9d88d691..6cae73c8 100644 --- a/lib/yeoman/templates/Evolution.php +++ b/lib/yeoman/templates/Evolution.php @@ -63,4 +63,37 @@ public static function initEnv() 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(); + } ); + } + } diff --git a/lib/yeoman/templates/web/wp-config.php b/lib/yeoman/templates/web/wp-config.php index d2d7a9d1..9820f233 100644 --- a/lib/yeoman/templates/web/wp-config.php +++ b/lib/yeoman/templates/web/wp-config.php @@ -47,3 +47,5 @@ // Replace ABSPATH .replace(/define\('ABSPATH'.+\);/, "define('ABSPATH', dirname(__FILE__) . '/wp/');") %> +/** Output rewriting for production.DOMAIN or *.staging.DOMAIN **/ +Evolution::rewriteUrls();