Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/yeoman/templates/Evolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
} );
}

}
2 changes: 2 additions & 0 deletions lib/yeoman/templates/web/wp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@
// Replace ABSPATH
.replace(/define\('ABSPATH'.+\);/, "define('ABSPATH', dirname(__FILE__) . '/wp/');")
%>
/** Output rewriting for production.DOMAIN or *.staging.DOMAIN **/
Evolution::rewriteUrls();