From 3f70f5d683c3e5550a3ab02203a6e65e5d353d53 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Wed, 24 Jun 2015 10:48:07 -0500 Subject: [PATCH 1/2] Implemented filtering of content via output buffering Worth noting that this only occurs when Evolution::getHostname() amd $_SERVER[HTTP_HOST] are *not* equal --- lib/yeoman/templates/Evolution.php | 33 ++++++++++++++++++++++++++ lib/yeoman/templates/web/wp-config.php | 2 ++ 2 files changed, 35 insertions(+) diff --git a/lib/yeoman/templates/Evolution.php b/lib/yeoman/templates/Evolution.php index 9d88d691..7aa74c25 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(); From 94e99f9f35c1db54a84cfebbc7a42006268c4aef Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Thu, 25 Jun 2015 11:08:06 -0500 Subject: [PATCH 2/2] Updated old/new urls for output filtering --- lib/yeoman/templates/Evolution.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/yeoman/templates/Evolution.php b/lib/yeoman/templates/Evolution.php index 7aa74c25..6cae73c8 100644 --- a/lib/yeoman/templates/Evolution.php +++ b/lib/yeoman/templates/Evolution.php @@ -69,8 +69,8 @@ public static function rewriteUrls() return false; } - $old_url = self::getHostname(); - $new_url = htmlspecialchars($_SERVER['HTTP_HOST']); + $old_url = '://' . self::getHostname(); + $new_url = '://' . htmlspecialchars($_SERVER['HTTP_HOST']); if ($old_url === $new_url) { return false;