Skip to content

Instantly share code, notes, and snippets.

@johnpbloch
Created December 21, 2012 20:54
Show Gist options
  • Save johnpbloch/4355748 to your computer and use it in GitHub Desktop.
Save johnpbloch/4355748 to your computer and use it in GitHub Desktop.
<?php
if (WP_STAGE == 'production') {
return;
}
if(!class_exists('WP_Stack_Plugin')){class WP_Stack_Plugin{function hook($h){$p=10;$m=$this->sanitize_method($h);$b=func_get_args();unset($b[0]);foreach((array)$b as $a){if(is_int($a))$p=$a;else $m=$a;}return add_action($h,array($this,$m),$p,999);}private function sanitize_method($m){return str_replace(array('.','-'),array('_DOT_','_DASH_'),$m);}}}
class AVM_Staging_Url_Rewrite extends WP_Stack_Plugin {
public static $instance;
public $from = array();
public $to;
public function __construct() {
self::$instance = $this;
if (!is_admin()) {
$this->hook('init');
}
}
public function init() {
$this->hook('template_redirect');
$this->hook('avm_staging_rewrite', 'rewrite');
$this->from[] = WP_PRODUCTION_HOST;
$this->to = WP_STAGING_HOST;
}
public function rewrite($content) {
$toReplace = (array)$this->from;
$from = $to = array();
foreach ($toReplace as $url) {
$from[] = "='$url";
$to[] = "='$this->to";
$from[] = "=\"$url";
$to[] = "=\"$this->to";
if (strpos($url, '://www.')) {
$url = str_replace('://www.', '://', $url);
$from[] = "='$url";
$to[] = "='$this->to";
$from[] = "=\"$url";
$to[] = "=\"$this->to";
}
}
return str_replace($from, $to, $content);
}
public function template_redirect() {
ob_start(array($this, 'ob'));
}
public function ob($contents) {
return apply_filters('avm_staging_rewrite', $contents, $this);
}
}
new AVM_Staging_Url_Rewrite;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment